-
-
Notifications
You must be signed in to change notification settings - Fork 3
Implement GitHub activity badge showing last commit time #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
4
commits into
main
Choose a base branch
from
copilot/fix-66
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
badgers-web/src/app/github/activity/[owner]/[repo]/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||||||||||||||||||||
|
||||||||||||||||||||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||||||
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 } | ||||||||||||||||||||
) | ||||||||||||||||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
Copilot uses AI. Check for mistakes.