-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 [Code Health] Refactor fetchContributions to reduce complexity #454
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -485,6 +485,57 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const CONTRIBUTIONS_QUERY = `query($login: String!, $from: DateTime!, $to: DateTime!) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user(login: $login) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contributionsCollection(from: $from, to: $to) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalCommitContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalPullRequestContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalIssueContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalPullRequestReviewContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contributionCalendar { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| weeks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contributionDays { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| date | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contributionCount | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type ContributionWeek = NonNullable<ContributionsResponse["user"]>["contributionsCollection"]["contributionCalendar"]["weeks"][number]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function processContributionsCalendar(weeks: NonNullable<ContributionsResponse["user"]>["contributionsCollection"]["contributionCalendar"]["weeks"]) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. To improve readability and type safety, you can introduce a First, define type ContributionDay = { date: string; count: number };Then, you can update this function's signature. You should also update
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const calendar = weeks.flatMap((w) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w.contributionDays.map((d) => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| date: d.date, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count: d.contributionCount, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calendar.sort((a, b) => a.date.localeCompare(b.date)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return calendar; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| function calculateRecentContributions(calendar: { date: string; count: number }[], sevenDaysAgoStr: string, thirtyDaysAgoStr: string) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
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. To align with the suggested change in
Suggested change
References
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let weeklyContributions = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let monthlyContributions = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let i = calendar.length - 1; i >= 0; i--) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const day = calendar[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (day.date < thirtyDaysAgoStr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| monthlyContributions += day.count; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (day.date >= sevenDaysAgoStr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| weeklyContributions += day.count; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return { weeklyContributions, monthlyContributions }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Task⑥: 過去1年間のコントリビューション統計を取得 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * GraphQL contributionsCollection (認証必須) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -509,27 +560,9 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const sevenDaysAgoStr = new Date(now.getTime() - 6 * 24 * 60 * 60 * 1000).toISOString().split("T")[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const thirtyDaysAgoStr = new Date(now.getTime() - 29 * 24 * 60 * 60 * 1000).toISOString().split("T")[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const query = `query($login: String!, $from: DateTime!, $to: DateTime!) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| user(login: $login) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contributionsCollection(from: $from, to: $to) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalCommitContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalPullRequestContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalIssueContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalPullRequestReviewContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contributionCalendar { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| totalContributions | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| weeks { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contributionDays { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| date | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| contributionCount | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| }`; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const data = await graphql<ContributionsResponse>(query, token, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const data = await graphql<ContributionsResponse>(CONTRIBUTIONS_QUERY, token, { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
560
to
+565
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. インラインクエリを削除した後、
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/lib/github.ts
Line: 560-565
Comment:
**余分な空行**
インラインクエリを削除した後、`fetchContributions` 内に連続する空行が2行残っています。1行にまとめてください。
```suggestion
const sevenDaysAgoStr = new Date(now.getTime() - 6 * 24 * 60 * 60 * 1000).toISOString().split("T")[0];
const thirtyDaysAgoStr = new Date(now.getTime() - 29 * 24 * 60 * 60 * 1000).toISOString().split("T")[0];
const data = await graphql<ContributionsResponse>(CONTRIBUTIONS_QUERY, token, {
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| login: username, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from: oneYearAgo.toISOString(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| to: now.toISOString(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -539,32 +572,8 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const cc = data.user.contributionsCollection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const calendar = cc.contributionCalendar.weeks.flatMap((w) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| w.contributionDays.map((d) => ({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| date: d.date, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| count: d.contributionCount, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| })) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| calendar.sort((a, b) => a.date.localeCompare(b.date)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let weeklyContributions = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| let monthlyContributions = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (let i = calendar.length - 1; i >= 0; i--) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const day = calendar[i]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (day.date < thirtyDaysAgoStr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| monthlyContributions += day.count; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (day.date >= sevenDaysAgoStr) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| weeklyContributions += day.count; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const calendar = processContributionsCalendar(cc.contributionCalendar.weeks); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { weeklyContributions, monthlyContributions } = calculateRecentContributions(calendar, sevenDaysAgoStr, thirtyDaysAgoStr); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { longestStreak, currentStreak } = calculateStreaks(calendar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const mostActiveDay = calculateMostActiveDay(calendar); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
ContributionWeek型が定義されていますが、コード内のどこからも参照されていません。processContributionsCalendarの引数型は同じ型を直接NonNullable<ContributionsResponse["user"]>["contributionsCollection"]["contributionCalendar"]["weeks"]として展開しているため、この型エイリアスは現状デッドコードになっています。削除するか、関数の引数型をContributionWeek[]に置き換えて活用してください。Context Used: 日本語で!!! (source)
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!