-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 [refactor: extract graphQL fragment and deep nested loop logic] #352
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -139,6 +139,42 @@ function mergeTopRepository(data: NonNullable<YearInReviewResponse["user"]>["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<string, unknown> | undefined): string[] { | ||
| const dates: string[] = []; | ||
| const defaultBranchRef = repoData?.defaultBranchRef as Record<string, unknown> | undefined; | ||
| const target = defaultBranchRef?.target as Record<string, unknown> | undefined; | ||
| const history = target?.history as Record<string, unknown> | undefined; | ||
| const historyNodes = (history?.nodes as Array<Record<string, unknown>> | undefined) || []; | ||
| for (const node of historyNodes) { | ||
| const author = node?.author as Record<string, unknown> | undefined; | ||
| if (typeof author?.date === 'string') { | ||
| dates.push(author.date); | ||
| } | ||
| } | ||
| return dates; | ||
| } | ||
|
Comment on lines
+163
to
+176
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 current implementation of By defining a dedicated interface for the expected GraphQL response structure and leveraging optional chaining alongside interface RepoCommitHistory {
defaultBranchRef?: {
target?: {
history?: {
nodes?: Array<{
author?: {
date?: string;
};
}>;
};
};
};
}
function extractCommitDatesFromRepo(repoData: unknown): string[] {
const dates: string[] = [];
const nodes = (repoData as RepoCommitHistory | undefined)?.defaultBranchRef?.target?.history?.nodes;
if (Array.isArray(nodes)) {
for (const node of nodes) {
const date = node?.author?.date;
if (typeof date === 'string') {
dates.push(date);
}
}
}
return dates;
}References
Comment on lines
+163
to
+176
Contributor
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. PRの説明に「Added verification of extraction via tests」とありますが、差分に新しいテストファイルが含まれていません。 Prompt To Fix With AIThis is a comment left during a code review.
Path: src/lib/githubYearInReview.ts
Line: 163-176
Comment:
**抽出関数の単体テストが見当たらない**
PRの説明に「Added verification of extraction via tests」とありますが、差分に新しいテストファイルが含まれていません。`buildRepoFragment` と `extractCommitDatesFromRepo` はいずれも非公開関数のため、既存の `fetchCommitDatesForTopRepos` の統合テスト経由でしか検証されません。抽出したロジックの境界値(`repoData` が `undefined`、`nodes` が空配列、`date` フィールドが文字列以外の型など)を直接カバーするユニットテストを追加することで、将来の改修時の安全性が高まります。
How can I resolve this? If you propose a fix, please make it concise. |
||
|
|
||
| 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<string, unknown> | undefined; | ||
| const defaultBranchRef = repoData?.defaultBranchRef as Record<string, unknown> | undefined; | ||
| const target = defaultBranchRef?.target as Record<string, unknown> | undefined; | ||
| const history = target?.history as Record<string, unknown> | undefined; | ||
| const historyNodes = (history?.nodes as Array<Record<string, unknown>> | undefined) || []; | ||
| for (const node of historyNodes) { | ||
| const author = node?.author as Record<string, unknown> | 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 | ||
|
|
||
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.
Prompt To Fix With AI
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!