fix github activity

This commit is contained in:
2026-04-06 14:41:30 -04:00
parent 4dbd0ac965
commit 3635133994
2 changed files with 95 additions and 67 deletions

View File

@@ -46,43 +46,32 @@ interface ContributionDay {
count: number; count: number;
} }
interface GitActivityData { // Four independent cached promises — first RightBarContent instance to mount
githubCommits: GitCommit[]; // starts each fetch; the second gets the already-in-flight promise.
giteaCommits: GitCommit[]; let ghCommitsPromise: Promise<GitCommit[]> | null = null;
githubActivity: ContributionDay[]; let gtCommitsPromise: Promise<GitCommit[]> | null = null;
giteaActivity: ContributionDay[]; let ghActivityPromise: Promise<ContributionDay[]> | null = null;
let gtActivityPromise: Promise<ContributionDay[]> | null = null;
function getGhCommitsPromise(): Promise<GitCommit[]> {
return (ghCommitsPromise ??= api.gitActivity.getGitHubCommits
.query({ limit: 6 })
.catch(() => []));
} }
function getGtCommitsPromise(): Promise<GitCommit[]> {
// Shared fetch promise — whichever instance mounts first starts the fetch; return (gtCommitsPromise ??= api.gitActivity.getGiteaCommits
// the second instance awaits the same Promise instead of firing its own requests. .query({ limit: 6 })
let gitActivityPromise: Promise<GitActivityData> | null = null; .catch(() => []));
}
function fetchGitActivity(): Promise<GitActivityData> { function getGhActivityPromise(): Promise<ContributionDay[]> {
if (gitActivityPromise) return gitActivityPromise; return (ghActivityPromise ??= api.gitActivity.getGitHubActivity
.query()
gitActivityPromise = (async () => { .catch(() => []));
const [ghCommits, gtCommits, ghActivity, gtActivity] = await Promise.all([ }
api.gitActivity.getGitHubCommits.query({ limit: 6 }).catch(() => []), function getGtActivityPromise(): Promise<ContributionDay[]> {
api.gitActivity.getGiteaCommits.query({ limit: 6 }).catch(() => []), return (gtActivityPromise ??= api.gitActivity.getGiteaActivity
api.gitActivity.getGitHubActivity.query().catch(() => []), .query()
api.gitActivity.getGiteaActivity.query().catch(() => []) .catch(() => []));
]);
const displayedGithubCommits = ghCommits.slice(0, 3);
const githubShas = new Set(displayedGithubCommits.map((c) => c.sha));
const uniqueGiteaCommits = gtCommits
.filter((commit) => !githubShas.has(commit.sha))
.slice(0, 3);
return {
githubCommits: displayedGithubCommits,
giteaCommits: uniqueGiteaCommits,
githubActivity: ghActivity,
giteaActivity: gtActivity
};
})();
return gitActivityPromise;
} }
export function RightBarContent() { export function RightBarContent() {
@@ -93,7 +82,8 @@ export function RightBarContent() {
[] []
); );
const [giteaActivity, setGiteaActivity] = createSignal<ContributionDay[]>([]); const [giteaActivity, setGiteaActivity] = createSignal<ContributionDay[]>([]);
const [loading, setLoading] = createSignal(true); const [githubCommitsLoading, setGithubCommitsLoading] = createSignal(true);
const [giteaCommitsLoading, setGiteaCommitsLoading] = createSignal(true);
const handleLinkClick = () => { const handleLinkClick = () => {
if ( if (
@@ -106,19 +96,22 @@ export function RightBarContent() {
onMount(() => { onMount(() => {
setTimeout(() => { setTimeout(() => {
fetchGitActivity() getGhCommitsPromise().then((commits) => {
.then((data) => { setGithubCommits(commits.slice(0, 3));
setGithubCommits(data.githubCommits); setGithubCommitsLoading(false);
setGiteaCommits(data.giteaCommits); });
setGithubActivity(data.githubActivity);
setGiteaActivity(data.giteaActivity); // Deduplicate Gitea against whatever GitHub has resolved by the time this lands
}) getGtCommitsPromise().then((gtCommits) => {
.catch((error) => { const ghShas = new Set(githubCommits().map((c) => c.sha));
console.error("Failed to fetch git activity:", error); setGiteaCommits(
}) gtCommits.filter((c) => !ghShas.has(c.sha)).slice(0, 3)
.finally(() => { );
setLoading(false); setGiteaCommitsLoading(false);
}); });
getGhActivityPromise().then((activity) => setGithubActivity(activity));
getGtActivityPromise().then((activity) => setGiteaActivity(activity));
}, 0); }, 0);
}); });
@@ -208,7 +201,7 @@ export function RightBarContent() {
<RecentCommits <RecentCommits
commits={giteaCommits()} commits={giteaCommits()}
title="Recent Gitea Commits" title="Recent Gitea Commits"
loading={loading()} loading={giteaCommitsLoading()}
/> />
<ActivityHeatmap <ActivityHeatmap
contributions={giteaActivity()} contributions={giteaActivity()}
@@ -217,7 +210,7 @@ export function RightBarContent() {
<RecentCommits <RecentCommits
commits={githubCommits()} commits={githubCommits()}
title="Recent GitHub Commits" title="Recent GitHub Commits"
loading={loading()} loading={githubCommitsLoading()}
/> />
<ActivityHeatmap <ActivityHeatmap
contributions={githubActivity()} contributions={githubActivity()}

View File

@@ -33,7 +33,7 @@ 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 () => {
// Use Events API to get recent push events - much more efficient // Use Events API to get recent push events
const eventsResponse = await fetchWithTimeout( const eventsResponse = await fetchWithTimeout(
`https://api.github.com/users/MikeFreno/events/public?per_page=100`, `https://api.github.com/users/MikeFreno/events/public?per_page=100`,
{ {
@@ -47,31 +47,66 @@ export const gitActivityRouter = createTRPCRouter({
await checkResponse(eventsResponse); await checkResponse(eventsResponse);
const events = await eventsResponse.json(); const events = await eventsResponse.json();
const allCommits: GitCommit[] = [];
// Extract commits directly from PushEvent payload — no per-commit API calls needed // Collect (repo, sha) pairs from push events up front
const toFetch: { repoName: string; sha: string }[] = [];
for (const event of events) { for (const event of events) {
if (event.type !== "PushEvent") continue; if (event.type !== "PushEvent") continue;
if (allCommits.length >= input.limit) break; if (toFetch.length >= input.limit * 5) break;
toFetch.push({
repoName: event.repo.name,
sha: event.payload.head
});
}
const repoName = event.repo.name; // Fetch all commits in parallel instead of serially
const payloadCommits: any[] = event.payload.commits || []; const results = await Promise.allSettled(
toFetch.map(({ repoName, sha }) =>
fetchWithTimeout(
`https://api.github.com/repos/${repoName}/commits/${sha}`,
{
headers: {
Authorization: `Bearer ${env.GITHUB_API_TOKEN}`,
Accept: "application/vnd.github.v3+json"
},
timeout: 5000
}
)
.then((res) => (res.ok ? res.json() : null))
.catch(() => null)
)
);
for (const payloadCommit of payloadCommits) { const allCommits: GitCommit[] = [];
if (allCommits.length >= input.limit) break; for (let i = 0; i < results.length; i++) {
const result = results[i];
if (result.status === "rejected" || !result.value) continue;
const commit = result.value;
const { repoName } = toFetch[i];
if (
commit.author?.login === "MikeFreno" ||
commit.author?.login === "mikefreno" ||
commit.commit?.author?.email?.includes("mike")
) {
allCommits.push({ allCommits.push({
sha: payloadCommit.sha?.substring(0, 7) || "unknown", sha: commit.sha?.substring(0, 7) || "unknown",
message: payloadCommit.message?.split("\n")[0] || "No message", message: commit.commit?.message?.split("\n")[0] || "No message",
author: payloadCommit.author?.name || "Unknown", author:
// event.created_at is the push timestamp — close enough to commit date commit.commit?.author?.name ||
date: event.created_at || new Date().toISOString(), commit.author?.login ||
"Unknown",
date: commit.commit?.author?.date || new Date().toISOString(),
repo: repoName, repo: repoName,
url: `https://github.com/${repoName}/commit/${payloadCommit.sha}` url: `https://github.com/${repoName}/commit/${commit.sha}`
}); });
} }
} }
// Events are already in reverse-chronological order allCommits.sort(
(a, b) => new Date(b.date).getTime() - new Date(a.date).getTime()
);
return allCommits.slice(0, input.limit); return allCommits.slice(0, input.limit);
}, },
{ maxStaleMs: CACHE_CONFIG.GIT_ACTIVITY_MAX_STALE_MS } { maxStaleMs: CACHE_CONFIG.GIT_ACTIVITY_MAX_STALE_MS }