Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions badgers-web/src/app/github/activity/[owner]/[repo]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import type { NextRequest } from 'next/server'

import Badge from '@/utils/Badge'
import GitHub from '@/utils/GitHub'

interface Params {
params: Promise<{
owner: string
repo: string
}>
}

function getRelativeTimeText(commitDate: Date): string {
const now = new Date()
const diffMs = now.getTime() - commitDate.getTime()
const diffMinutes = Math.floor(diffMs / (1000 * 60))
const diffHours = Math.floor(diffMs / (1000 * 60 * 60))
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24))
const diffWeeks = Math.floor(diffDays / 7)
const diffMonths = Math.floor(diffDays / 30)
const diffYears = Math.floor(diffDays / 365)

Comment on lines +20 to +22
Copy link
Preview

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fixed values (30 days per month, 365 days per year) for date calculations can lead to inaccuracies. Consider using more precise date arithmetic or documenting that these are approximations for display purposes.

Suggested change
const diffMonths = Math.floor(diffDays / 30)
const diffYears = Math.floor(diffDays / 365)
// Calculate months and years using date components for accuracy
let diffMonths = (now.getFullYear() - commitDate.getFullYear()) * 12 + (now.getMonth() - commitDate.getMonth());
// If the current day is less than the commit day, subtract one month
if (now.getDate() < commitDate.getDate()) {
diffMonths -= 1;
}
const diffYears = now.getFullYear() - commitDate.getFullYear();
// If the current month/day is before the commit month/day, subtract one year
if (
now.getMonth() < commitDate.getMonth() ||
(now.getMonth() === commitDate.getMonth() && now.getDate() < commitDate.getDate())
) {
diffYears -= 1;
}

Copilot uses AI. Check for mistakes.

if (diffMinutes < 60) {
return 'today'
} else if (diffHours < 24) {
return 'today'
} else if (diffDays === 1) {
return 'yesterday'
} else if (diffDays < 7) {
return `${diffDays} days ago`
} else if (diffWeeks < 4) {
return diffWeeks === 1 ? '1 week ago' : `${diffWeeks} weeks ago`
} else if (diffMonths < 12) {
return diffMonths === 1 ? '1 month ago' : `${diffMonths} months ago`
} else {
return diffYears === 1 ? '1 year ago' : `${diffYears} years ago`
}
}

function getActivityColor(commitDate: Date): string {
const now = new Date()
const diffDays = Math.floor((now.getTime() - commitDate.getTime()) / (1000 * 60 * 60 * 24))

if (diffDays <= 1) return 'green'
if (diffDays <= 7) return 'yellow'
if (diffDays <= 30) return 'orange'
return 'red'
}

export async function GET(request: NextRequest, props: Params) {
const params = await props.params;

const {
owner,
repo
} = params;

// Get repository info to find default branch
const repoResp = await GitHub.wrapRequest(octokit =>
octokit.repos.get({ owner, repo }),
)

if (!repoResp.data) {
return await Badge.error(request, 'github')
}

const defaultBranch = repoResp.data.default_branch

// Get latest commit on the default branch
const commitsResp = await GitHub.wrapRequest(octokit =>
octokit.repos.listCommits({
owner,
repo,
sha: defaultBranch,
per_page: 1
}),
)

if (!commitsResp.data || commitsResp.data.length === 0) {
return await Badge.error(request, 'github')
}

const latestCommit = commitsResp.data[0]
const commitDate = new Date(latestCommit.commit.author?.date || latestCommit.commit.committer?.date || '')

Comment on lines +84 to +85
Copy link
Preview

Copilot AI Sep 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback to an empty string will create an invalid Date object. Consider using a more explicit fallback or throwing a specific error when no valid date is found.

Suggested change
const commitDate = new Date(latestCommit.commit.author?.date || latestCommit.commit.committer?.date || '')
const commitAuthorDate = latestCommit.commit.author?.date;
const commitCommitterDate = latestCommit.commit.committer?.date;
const commitDateString = commitAuthorDate || commitCommitterDate;
if (!commitDateString) {
return await Badge.error(request, 'github')
}
const commitDate = new Date(commitDateString);

Copilot uses AI. Check for mistakes.

if (isNaN(commitDate.getTime())) {
return await Badge.error(request, 'github')
}

const relativeTime = getRelativeTimeText(commitDate)
const color = getActivityColor(commitDate)

return await Badge.generate(
request,
'last commit',
relativeTime,
{ color }
)
}
5 changes: 5 additions & 0 deletions badgers-web/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ export default function Home() {
path="/github/license/:owner/:repo"
inject={['quintschaf', 'schafkit']}
/>
<Row
name="Last commit"
path="/github/activity/:owner/:repo"
inject={['quintschaf', 'schafkit']}
/>
</div>
</Section>
<Section name="Codeberg">
Expand Down
Loading