From dd2909ae1e1b7bba4d3d9c095b89c90b5fc4ddc9 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 6 Jun 2026 03:20:44 +0000 Subject: [PATCH] refactor: extract graphQL fragment and deep nested loop logic Co-authored-by: is0692vs <135803462+is0692vs@users.noreply.github.com> --- src/lib/githubYearInReview.ts | 67 ++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/src/lib/githubYearInReview.ts b/src/lib/githubYearInReview.ts index 23c4538d..f66d7057 100644 --- a/src/lib/githubYearInReview.ts +++ b/src/lib/githubYearInReview.ts @@ -139,6 +139,42 @@ function mergeTopRepository(data: NonNullable["con return top; } + +function buildRepoFragment(index: number): string { + return ` + repo${index}: repository(owner: $owner${index}, name: $name${index}) { + defaultBranchRef { + target { + ... on Commit { + history(author: { id: $authorId }, since: $since, until: $until, first: 100) { + nodes { + author { + date + } + } + } + } + } + } + } + `; +} + +function extractCommitDatesFromRepo(repoData: Record | undefined): string[] { + const dates: string[] = []; + const defaultBranchRef = repoData?.defaultBranchRef as Record | undefined; + const target = defaultBranchRef?.target as Record | undefined; + const history = target?.history as Record | undefined; + const historyNodes = (history?.nodes as Array> | undefined) || []; + for (const node of historyNodes) { + const author = node?.author as Record | undefined; + if (typeof author?.date === 'string') { + dates.push(author.date); + } + } + return dates; +} + async function fetchCommitDatesForTopRepos( authorId: string, token: string, @@ -163,23 +199,7 @@ async function fetchCommitDatesForTopRepos( }; candidates.forEach((repo, index) => { - fragments.push(` - repo${index}: repository(owner: $owner${index}, name: $name${index}) { - defaultBranchRef { - target { - ... on Commit { - history(author: { id: $authorId }, since: $since, until: $until, first: 100) { - nodes { - author { - date - } - } - } - } - } - } - } - `); + fragments.push(buildRepoFragment(index)); variables[`owner${index}`] = repo.repository.owner.login; variables[`name${index}`] = repo.repository.name; }); @@ -198,18 +218,9 @@ async function fetchCommitDatesForTopRepos( for (let i = 0; i < candidates.length; i++) { const repoData = response[`repo${i}`] as Record | undefined; - const defaultBranchRef = repoData?.defaultBranchRef as Record | undefined; - const target = defaultBranchRef?.target as Record | undefined; - const history = target?.history as Record | undefined; - const historyNodes = (history?.nodes as Array> | undefined) || []; - for (const node of historyNodes) { - const author = node?.author as Record | undefined; - if (typeof author?.date === 'string') { - dates.push(author.date); - } - } + const repoDates = extractCommitDatesFromRepo(repoData); + dates.push(...repoDates); } - return dates; } catch (error) { // Fallback to empty array on failure, matching original behavior somewhat