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
116 changes: 116 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,116 @@
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)

// 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;
Comment on lines +27 to +33
}
if (diffMinutes < 60) {
return 'today'
} else if (diffHours < 24) {
return 'today'
} else if (diffDays === 1) {
return 'yesterday'
Comment on lines +37 to +40
} 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`
Comment on lines +43 to +46
} 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 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);
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