Skip to content

Commit d9b918a

Browse files
committed
update: add return type
1 parent 0e1107f commit d9b918a

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

src/core/contributions.ts

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { githubGraphQL } from "../api/api.js";
2+
import { GitHubContribution } from "../types/types.js";
23

34
export const getGitHubContributions = async (
45
username: string,
@@ -59,7 +60,6 @@ export const getGitHubContributions = async (
5960
};
6061
};
6162

62-
6363
type PerTypeYearContributions = {
6464
year: number;
6565
commits: number;
@@ -119,4 +119,65 @@ export const getGitHubContributionTypesPerYear = async (
119119
}
120120

121121
return contributionsPerYear;
122+
};
123+
124+
const CONTRIBUTIONS_QUERY = `
125+
query($username: String!, $from: DateTime, $to: DateTime) {
126+
user(login: $username) {
127+
contributionsCollection(from: $from, to: $to) {
128+
contributionCalendar {
129+
totalContributions
130+
weeks {
131+
contributionDays {
132+
contributionCount
133+
date
134+
color
135+
}
136+
}
137+
}
138+
}
139+
}
140+
}
141+
`;
142+
143+
interface ContributionResponse {
144+
user: {
145+
contributionsCollection: {
146+
contributionCalendar: {
147+
totalContributions: number;
148+
weeks: Array<{
149+
contributionDays: Array<{
150+
contributionCount: number;
151+
date: string;
152+
color: string;
153+
}>;
154+
}>;
155+
};
156+
};
157+
};
158+
}
159+
160+
export const getGitHubYearlyContributions = async (
161+
username: string,
162+
year: number,
163+
token: string
164+
): Promise<GitHubContribution> => {
165+
const from = `${year}-01-01T00:00:00Z`;
166+
const to = `${year}-12-31T23:59:59Z`;
167+
168+
const result = await githubGraphQL<ContributionResponse>(
169+
CONTRIBUTIONS_QUERY,
170+
{ username, from, to },
171+
token
172+
);
173+
174+
const calendar = result.user.contributionsCollection.contributionCalendar;
175+
176+
const dailyData = calendar.weeks.flatMap((week) => week.contributionDays);
177+
178+
return {
179+
year,
180+
totalContributions: calendar.totalContributions,
181+
days: dailyData
182+
};
122183
};

0 commit comments

Comments
 (0)