fix: corrected github recent commits

This commit is contained in:
Michael Freno
2026-01-13 09:48:27 -05:00
parent 53dc7c1aab
commit 9d6c32d8b0
2 changed files with 55 additions and 36 deletions

View File

@@ -68,18 +68,28 @@ export function RightBarContent() {
onMount(() => { onMount(() => {
const fetchData = async () => { const fetchData = async () => {
try { try {
// Fetch more commits to account for deduplication
const [ghCommits, gtCommits, ghActivity, gtActivity] = const [ghCommits, gtCommits, ghActivity, gtActivity] =
await Promise.all([ await Promise.all([
api.gitActivity.getGitHubCommits api.gitActivity.getGitHubCommits
.query({ limit: 3 }) .query({ limit: 6 })
.catch(() => []), .catch(() => []),
api.gitActivity.getGiteaCommits.query({ limit: 3 }).catch(() => []), api.gitActivity.getGiteaCommits.query({ limit: 6 }).catch(() => []),
api.gitActivity.getGitHubActivity.query().catch(() => []), api.gitActivity.getGitHubActivity.query().catch(() => []),
api.gitActivity.getGiteaActivity.query().catch(() => []) api.gitActivity.getGiteaActivity.query().catch(() => [])
]); ]);
setGithubCommits(ghCommits); // Take first 3 from GitHub
setGiteaCommits(gtCommits); const displayedGithubCommits = ghCommits.slice(0, 3);
// Deduplicate Gitea commits - only against the 3 shown in GitHub section
const githubShas = new Set(displayedGithubCommits.map((c) => c.sha));
const uniqueGiteaCommits = gtCommits.filter(
(commit) => !githubShas.has(commit.sha)
);
setGithubCommits(displayedGithubCommits);
setGiteaCommits(uniqueGiteaCommits.slice(0, 3));
setGithubActivity(ghActivity); setGithubActivity(ghActivity);
setGiteaActivity(gtActivity); setGiteaActivity(gtActivity);
} catch (error) { } catch (error) {

View File

@@ -33,8 +33,9 @@ export const gitActivityRouter = createTRPCRouter({
`github-commits-${input.limit}`, `github-commits-${input.limit}`,
CACHE_CONFIG.GIT_ACTIVITY_CACHE_TTL_MS, CACHE_CONFIG.GIT_ACTIVITY_CACHE_TTL_MS,
async () => { async () => {
const reposResponse = await fetchWithTimeout( // Use Events API to get recent push events - much more efficient
`https://api.github.com/users/MikeFreno/repos?sort=pushed&per_page=10`, const eventsResponse = await fetchWithTimeout(
`https://api.github.com/users/MikeFreno/events/public?per_page=100`,
{ {
headers: { headers: {
Authorization: `Bearer ${env.GITHUB_API_TOKEN}`, Authorization: `Bearer ${env.GITHUB_API_TOKEN}`,
@@ -44,46 +45,53 @@ export const gitActivityRouter = createTRPCRouter({
} }
); );
await checkResponse(reposResponse); await checkResponse(eventsResponse);
const repos = await reposResponse.json(); const events = await eventsResponse.json();
const allCommits: GitCommit[] = []; const allCommits: GitCommit[] = [];
for (const repo of repos) { // Extract push events and fetch commit details
if (allCommits.length >= input.limit * 3) break; // Get extra to sort later for (const event of events) {
if (event.type !== "PushEvent") continue;
if (allCommits.length >= input.limit * 5) break; // Get extra to ensure we have enough
const repoName = event.repo.name;
const commitSha = event.payload.head;
try { try {
const commitsResponse = await fetchWithTimeout( // Fetch the actual commit details to get the message
`https://api.github.com/repos/${repo.full_name}/commits?per_page=5`, const commitResponse = await fetchWithTimeout(
`https://api.github.com/repos/${repoName}/commits/${commitSha}`,
{ {
headers: { headers: {
Authorization: `Bearer ${env.GITHUB_API_TOKEN}`, Authorization: `Bearer ${env.GITHUB_API_TOKEN}`,
Accept: "application/vnd.github.v3+json" Accept: "application/vnd.github.v3+json"
}, },
timeout: 10000 timeout: 5000
} }
); );
if (commitsResponse.ok) { if (commitResponse.ok) {
const commits = await commitsResponse.json(); const commit = await commitResponse.json();
for (const commit of commits) {
if ( // Filter for your commits
commit.author?.login === "MikeFreno" || if (
commit.commit?.author?.email?.includes("mike") commit.author?.login === "MikeFreno" ||
) { commit.author?.login === "mikefreno" ||
allCommits.push({ commit.commit?.author?.email?.includes("mike")
sha: commit.sha?.substring(0, 7) || "unknown", ) {
message: allCommits.push({
commit.commit?.message?.split("\n")[0] || "No message", sha: commit.sha?.substring(0, 7) || "unknown",
author: message:
commit.commit?.author?.name || commit.commit?.message?.split("\n")[0] || "No message",
commit.author?.login || author:
"Unknown", commit.commit?.author?.name ||
date: commit.author?.login ||
commit.commit?.author?.date || new Date().toISOString(), "Unknown",
repo: repo.full_name, date:
url: `https://github.com/${repo.full_name}/commit/${commit.sha}` commit.commit?.author?.date || new Date().toISOString(),
}); repo: repoName,
} url: `https://github.com/${repoName}/commit/${commit.sha}`
});
} }
} }
} catch (error) { } catch (error) {
@@ -92,17 +100,18 @@ export const gitActivityRouter = createTRPCRouter({
error instanceof TimeoutError error instanceof TimeoutError
) { ) {
console.warn( console.warn(
`Network error fetching commits for ${repo.full_name}, skipping` `Network error fetching commit ${commitSha} for ${repoName}, skipping`
); );
} else { } else {
console.error( console.error(
`Error fetching commits for ${repo.full_name}:`, `Error fetching commit ${commitSha} for ${repoName}:`,
error error
); );
} }
} }
} }
// Already sorted by event date, but sort again by commit date to be precise
allCommits.sort( allCommits.sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime() (a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
); );