From c0880d42ac1eda9eb6201103a017c32d96d9b883 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:41:05 +0300 Subject: [PATCH 01/10] fix: anchor contribution history to a fixed start date --- src/contributions.ts | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/contributions.ts b/src/contributions.ts index 04f8d00..ab30e92 100644 --- a/src/contributions.ts +++ b/src/contributions.ts @@ -51,6 +51,13 @@ const CONTRIBUTION_QUERY = ` const MAX_REQUEST_ATTEMPTS = 3; const RETRY_DELAYS_MS = [250, 500] as const; +/** + * The first Sunday at or before the start of the originally displayed window. + * Keep this fixed so existing contribution cells never fall off the left side. + * The garden will grow horizontally as newer dates are added. + */ +export const CONTRIBUTION_HISTORY_START_DATE = "2025-10-12"; + export function flattenContributionWeeks(weeks: ContributionWeek[]): ContributionDay[] { return weeks.flatMap((week) => week.contributionDays).sort((a, b) => a.date.localeCompare(b.date)); } @@ -64,8 +71,11 @@ export async function fetchGitHubContributions( if (!token.trim()) throw new Error("A GitHub token is required."); const to = new Date(now); - const from = new Date(to); - from.setUTCDate(from.getUTCDate() - 364); + const from = new Date(`${CONTRIBUTION_HISTORY_START_DATE}T00:00:00.000Z`); + + if (to.getTime() < from.getTime()) { + throw new Error("The requested end date is earlier than the contribution history start date."); + } const response = await requestContributionCalendar(username, token, { query: CONTRIBUTION_QUERY, From f691abdd69e74a082008a010e79c6a33bafac41b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:41:17 +0300 Subject: [PATCH 02/10] test: prevent contribution history from rolling forward --- tests/contributions.test.ts | 38 +++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/contributions.test.ts b/tests/contributions.test.ts index 500cb22..24d7843 100644 --- a/tests/contributions.test.ts +++ b/tests/contributions.test.ts @@ -1,6 +1,9 @@ import assert from "node:assert/strict"; import test from "node:test"; -import { fetchGitHubContributions } from "../src/contributions.js"; +import { + CONTRIBUTION_HISTORY_START_DATE, + fetchGitHubContributions, +} from "../src/contributions.js"; const payload = { data: { @@ -24,6 +27,37 @@ const payload = { }, }; +test("uses a fixed history start date and extends through the requested current date", async () => { + const originalFetch = globalThis.fetch; + let requestedVariables: { from: string; to: string } | undefined; + + globalThis.fetch = (async (_input, init) => { + const body = JSON.parse(String(init?.body)) as { + variables: { from: string; to: string }; + }; + requestedVariables = body.variables; + + return new Response(JSON.stringify(payload), { + status: 200, + headers: { "Content-Type": "application/json" }, + }); + }) as typeof fetch; + + const now = new Date("2026-10-15T12:34:56.000Z"); + + try { + await fetchGitHubContributions("codelovingcat", "test-token", now); + + assert.equal( + requestedVariables?.from, + `${CONTRIBUTION_HISTORY_START_DATE}T00:00:00.000Z`, + ); + assert.equal(requestedVariables?.to, now.toISOString()); + } finally { + globalThis.fetch = originalFetch; + } +}); + test("retries a temporary 502 response and then returns contribution data", async () => { const originalFetch = globalThis.fetch; let calls = 0; @@ -60,7 +94,7 @@ test("does not retry permanent client errors such as unauthorized", async () => try { await assert.rejects( fetchGitHubContributions("codelovingcat", "invalid-token", new Date("2026-10-12T12:00:00Z")), - /\(401\) after 1 attempt/, + /\\(401\\) after 1 attempt/, ); assert.equal(calls, 1); } finally { From e0a65de43226efa2e17822fdb736bd7fce04b662 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:41:29 +0300 Subject: [PATCH 03/10] docs: explain anchored contribution garden history --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f7aed93..70c8ab3 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,7 @@ GIF_OUTPUT_PATH="output/commit-man.gif" \ npm run build && npm run generate:gif ``` -Never commit or paste access tokens into issues, commits, or README files. The generator requests the most recent 365 days of contribution data; the available information depends on the token and contribution visibility. +Never commit or paste access tokens into issues, commits, or README files. The garden history starts on the fixed date `2025-10-12` and extends through the current date. Previously displayed days are retained and new dates extend the garden to the right, so the SVG and GIF canvas grow over time rather than dropping the oldest cells. Available contribution information still depends on the token and contribution visibility. ### Publish and embed the GIF on your profile From c0d922e71e0dc3e6c9e6da86465b7fe32a5a5e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:42:33 +0300 Subject: [PATCH 04/10] fix: fetch anchored contribution history in safe date chunks --- src/contributions.ts | 67 ++++++++++++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/src/contributions.ts b/src/contributions.ts index ab30e92..99b31c4 100644 --- a/src/contributions.ts +++ b/src/contributions.ts @@ -50,11 +50,13 @@ const CONTRIBUTION_QUERY = ` const MAX_REQUEST_ATTEMPTS = 3; const RETRY_DELAYS_MS = [250, 500] as const; +// Keep each request comfortably under GitHub's one-year contribution-range limit. +const MAX_RANGE_DAYS_PER_REQUEST = 364; /** * The first Sunday at or before the start of the originally displayed window. * Keep this fixed so existing contribution cells never fall off the left side. - * The garden will grow horizontally as newer dates are added. + * The garden grows horizontally as newer dates are added. */ export const CONTRIBUTION_HISTORY_START_DATE = "2025-10-12"; @@ -71,32 +73,55 @@ export async function fetchGitHubContributions( if (!token.trim()) throw new Error("A GitHub token is required."); const to = new Date(now); - const from = new Date(`${CONTRIBUTION_HISTORY_START_DATE}T00:00:00.000Z`); - - if (to.getTime() < from.getTime()) { + const historyStart = new Date(`${CONTRIBUTION_HISTORY_START_DATE}T00:00:00.000Z`); + if (to.getTime() < historyStart.getTime()) { throw new Error("The requested end date is earlier than the contribution history start date."); } - const response = await requestContributionCalendar(username, token, { - query: CONTRIBUTION_QUERY, - variables: { - username, - from: from.toISOString(), - to: to.toISOString(), - }, - }); - - const payload = (await response.json()) as ContributionCalendarResponse; - if (payload.errors?.length) { - throw new Error(`GitHub GraphQL error: ${payload.errors.map((error) => error.message).join("; ")}`); - } + const contributionDays = new Map(); + const endDate = to.toISOString().slice(0, 10); + let chunkFrom = historyStart; + + while (chunkFrom.getTime() <= to.getTime()) { + const maxChunkTo = new Date(chunkFrom); + maxChunkTo.setUTCDate(maxChunkTo.getUTCDate() + MAX_RANGE_DAYS_PER_REQUEST); + const chunkTo = maxChunkTo.getTime() < to.getTime() ? maxChunkTo : to; + + const response = await requestContributionCalendar(username, token, { + query: CONTRIBUTION_QUERY, + variables: { + username, + from: chunkFrom.toISOString(), + to: chunkTo.toISOString(), + }, + }); + + const payload = (await response.json()) as ContributionCalendarResponse; + if (payload.errors?.length) { + throw new Error(`GitHub GraphQL error: ${payload.errors.map((error) => error.message).join("; ")}`); + } + + const weeks = payload.data?.user?.contributionsCollection?.contributionCalendar?.weeks; + if (!weeks) { + throw new Error(`Could not read a contribution calendar for GitHub user "${username}". Check that the username exists and is accessible.`); + } + + for (const day of flattenContributionWeeks(weeks)) { + // GraphQL may return complete calendar weeks around a requested range. + // Keep only the fixed history window, then de-duplicate overlapping weeks. + if (day.date >= CONTRIBUTION_HISTORY_START_DATE && day.date <= endDate) { + contributionDays.set(day.date, day); + } + } + + if (chunkTo.getTime() >= to.getTime()) break; - const weeks = payload.data?.user?.contributionsCollection?.contributionCalendar?.weeks; - if (!weeks) { - throw new Error(`Could not read a contribution calendar for GitHub user "${username}". Check that the username exists and is accessible.`); + chunkFrom = new Date(chunkTo); + chunkFrom.setUTCDate(chunkFrom.getUTCDate() + 1); + chunkFrom.setUTCHours(0, 0, 0, 0); } - return flattenContributionWeeks(weeks); + return [...contributionDays.values()].sort((a, b) => a.date.localeCompare(b.date)); } async function requestContributionCalendar( From 8bfe0cf70729b61846e089bf2771e84120767099 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:42:51 +0300 Subject: [PATCH 05/10] test: verify anchored history is fetched in safe chunks --- tests/contributions.test.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/tests/contributions.test.ts b/tests/contributions.test.ts index 24d7843..576681c 100644 --- a/tests/contributions.test.ts +++ b/tests/contributions.test.ts @@ -14,7 +14,7 @@ const payload = { { contributionDays: [ { - date: "2026-10-12", + date: "2026-06-12", contributionCount: 2, contributionLevel: "FIRST_QUARTILE", }, @@ -27,15 +27,15 @@ const payload = { }, }; -test("uses a fixed history start date and extends through the requested current date", async () => { +test("keeps a fixed history start and requests long histories in one-year-safe chunks", async () => { const originalFetch = globalThis.fetch; - let requestedVariables: { from: string; to: string } | undefined; + const requestedVariables: Array<{ from: string; to: string }> = []; globalThis.fetch = (async (_input, init) => { const body = JSON.parse(String(init?.body)) as { variables: { from: string; to: string }; }; - requestedVariables = body.variables; + requestedVariables.push(body.variables); return new Response(JSON.stringify(payload), { status: 200, @@ -48,11 +48,17 @@ test("uses a fixed history start date and extends through the requested current try { await fetchGitHubContributions("codelovingcat", "test-token", now); - assert.equal( - requestedVariables?.from, - `${CONTRIBUTION_HISTORY_START_DATE}T00:00:00.000Z`, - ); - assert.equal(requestedVariables?.to, now.toISOString()); + assert.equal(requestedVariables.length, 2); + assert.deepEqual(requestedVariables, [ + { + from: `${CONTRIBUTION_HISTORY_START_DATE}T00:00:00.000Z`, + to: "2026-10-11T00:00:00.000Z", + }, + { + from: "2026-10-12T00:00:00.000Z", + to: now.toISOString(), + }, + ]); } finally { globalThis.fetch = originalFetch; } @@ -72,10 +78,10 @@ test("retries a temporary 502 response and then returns contribution data", asyn }) as typeof fetch; try { - const days = await fetchGitHubContributions("codelovingcat", "test-token", new Date("2026-10-12T12:00:00Z")); + const days = await fetchGitHubContributions("codelovingcat", "test-token", new Date("2026-06-12T12:00:00Z")); assert.equal(calls, 3); assert.equal(days.length, 1); - assert.equal(days[0]?.date, "2026-10-12"); + assert.equal(days[0]?.date, "2026-06-12"); assert.equal(days[0]?.contributionCount, 2); } finally { globalThis.fetch = originalFetch; @@ -93,7 +99,7 @@ test("does not retry permanent client errors such as unauthorized", async () => try { await assert.rejects( - fetchGitHubContributions("codelovingcat", "invalid-token", new Date("2026-10-12T12:00:00Z")), + fetchGitHubContributions("codelovingcat", "invalid-token", new Date("2026-06-12T12:00:00Z")), /\\(401\\) after 1 attempt/, ); assert.equal(calls, 1); From 8e13a42b2b8baf86a45d6e9da201b4ad20edfeea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:43:13 +0300 Subject: [PATCH 06/10] fix: include each contribution chunk's final day --- src/contributions.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/contributions.ts b/src/contributions.ts index 99b31c4..7b8b013 100644 --- a/src/contributions.ts +++ b/src/contributions.ts @@ -85,6 +85,7 @@ export async function fetchGitHubContributions( while (chunkFrom.getTime() <= to.getTime()) { const maxChunkTo = new Date(chunkFrom); maxChunkTo.setUTCDate(maxChunkTo.getUTCDate() + MAX_RANGE_DAYS_PER_REQUEST); + maxChunkTo.setUTCHours(23, 59, 59, 999); const chunkTo = maxChunkTo.getTime() < to.getTime() ? maxChunkTo : to; const response = await requestContributionCalendar(username, token, { From 6d1b78dd6fe4df1b1cb91dcfe3c47dddfff2a9c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:43:29 +0300 Subject: [PATCH 07/10] test: cover the inclusive end of each history chunk --- tests/contributions.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/contributions.test.ts b/tests/contributions.test.ts index 576681c..e82ae34 100644 --- a/tests/contributions.test.ts +++ b/tests/contributions.test.ts @@ -52,7 +52,7 @@ test("keeps a fixed history start and requests long histories in one-year-safe c assert.deepEqual(requestedVariables, [ { from: `${CONTRIBUTION_HISTORY_START_DATE}T00:00:00.000Z`, - to: "2026-10-11T00:00:00.000Z", + to: "2026-10-11T23:59:59.999Z", }, { from: "2026-10-12T00:00:00.000Z", From f971aa2ffae4a469a5a268526bc72529c16d68b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:46:09 +0300 Subject: [PATCH 08/10] test: fix contribution request assertions --- tests/contributions.test.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/contributions.test.ts b/tests/contributions.test.ts index e82ae34..6f66396 100644 --- a/tests/contributions.test.ts +++ b/tests/contributions.test.ts @@ -29,11 +29,11 @@ const payload = { test("keeps a fixed history start and requests long histories in one-year-safe chunks", async () => { const originalFetch = globalThis.fetch; - const requestedVariables: Array<{ from: string; to: string }> = []; + const requestedVariables: Array<{ username: string; from: string; to: string }> = []; globalThis.fetch = (async (_input, init) => { const body = JSON.parse(String(init?.body)) as { - variables: { from: string; to: string }; + variables: { username: string; from: string; to: string }; }; requestedVariables.push(body.variables); @@ -51,10 +51,12 @@ test("keeps a fixed history start and requests long histories in one-year-safe c assert.equal(requestedVariables.length, 2); assert.deepEqual(requestedVariables, [ { + username: "codelovingcat", from: `${CONTRIBUTION_HISTORY_START_DATE}T00:00:00.000Z`, to: "2026-10-11T23:59:59.999Z", }, { + username: "codelovingcat", from: "2026-10-12T00:00:00.000Z", to: now.toISOString(), }, From 71b43b89aed0512a493073442468470b40d50936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 20:46:41 +0300 Subject: [PATCH 09/10] test: correct unauthorized response assertion --- tests/contributions.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/contributions.test.ts b/tests/contributions.test.ts index 6f66396..16f501a 100644 --- a/tests/contributions.test.ts +++ b/tests/contributions.test.ts @@ -102,7 +102,7 @@ test("does not retry permanent client errors such as unauthorized", async () => try { await assert.rejects( fetchGitHubContributions("codelovingcat", "invalid-token", new Date("2026-06-12T12:00:00Z")), - /\\(401\\) after 1 attempt/, + /\(401\) after 1 attempt/, ); assert.equal(calls, 1); } finally { From 4ffa16723e28545cf253957437a50837c8aca819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Crem=20Sancaktutan?= <36550960+codelovingcat@users.noreply.github.com> Date: Thu, 15 Oct 2026 21:00:13 +0300 Subject: [PATCH 10/10] test: verify anchored dates across request chunks --- tests/contributions.test.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/contributions.test.ts b/tests/contributions.test.ts index 16f501a..8dd4ba9 100644 --- a/tests/contributions.test.ts +++ b/tests/contributions.test.ts @@ -35,9 +35,26 @@ test("keeps a fixed history start and requests long histories in one-year-safe c const body = JSON.parse(String(init?.body)) as { variables: { username: string; from: string; to: string }; }; + const responseDate = requestedVariables.length === 0 ? "2025-10-12" : "2026-10-15"; requestedVariables.push(body.variables); - return new Response(JSON.stringify(payload), { + return new Response(JSON.stringify({ + data: { + user: { + contributionsCollection: { + contributionCalendar: { + weeks: [{ + contributionDays: [{ + date: responseDate, + contributionCount: 2, + contributionLevel: "FIRST_QUARTILE", + }], + }], + }, + }, + }, + }, + }), { status: 200, headers: { "Content-Type": "application/json" }, }); @@ -46,8 +63,9 @@ test("keeps a fixed history start and requests long histories in one-year-safe c const now = new Date("2026-10-15T12:34:56.000Z"); try { - await fetchGitHubContributions("codelovingcat", "test-token", now); + const days = await fetchGitHubContributions("codelovingcat", "test-token", now); + assert.deepEqual(days.map((day) => day.date), ["2025-10-12", "2026-10-15"]); assert.equal(requestedVariables.length, 2); assert.deepEqual(requestedVariables, [ {