Skip to content

Commit d55e1ce

Browse files
committed
feat: add fetchUserContributions method to retrieve user contribution data
1 parent e4edf14 commit d55e1ce

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

src/utils/github-client.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,64 @@ export class GitHubClient {
204204
}
205205
}
206206

207+
async fetchUserContributions(username: string, from: string, to: string, cacheKeyExtra: string): Promise<any> {
208+
const query = `
209+
query($username: String!, $from: DateTime!, $to: DateTime!) {
210+
user(login: $username) {
211+
contributionsCollection(from: $from, to: $to) {
212+
contributionCalendar {
213+
totalContributions
214+
weeks {
215+
contributionDays {
216+
contributionCount
217+
date
218+
contributionLevel
219+
}
220+
}
221+
}
222+
}
223+
}
224+
}
225+
`;
226+
227+
try {
228+
return await this.cachedRequest(`user-contributions-${username}-${cacheKeyExtra}`, async () => {
229+
const response: any = await this.octokit.graphql(query, {
230+
username,
231+
from,
232+
to
233+
});
234+
235+
const calendar = response.user.contributionsCollection.contributionCalendar;
236+
237+
return {
238+
username,
239+
totalContributions: calendar.totalContributions,
240+
weeks: calendar.weeks.map((week: any) =>
241+
week.contributionDays.map((day: any) => ({
242+
date: day.date,
243+
count: day.contributionCount,
244+
level: this.parseContributionLevel(day.contributionLevel)
245+
}))
246+
)
247+
};
248+
});
249+
} catch (error: any) {
250+
console.error('Error fetching contributions:', error);
251+
throw new Error(`Failed to fetch contributions for ${username}: ${error.message}`);
252+
}
253+
}
254+
255+
private parseContributionLevel(level: string): number {
256+
switch (level) {
257+
case 'FIRST_QUARTILE': return 1;
258+
case 'SECOND_QUARTILE': return 2;
259+
case 'THIRD_QUARTILE': return 3;
260+
case 'FOURTH_QUARTILE': return 4;
261+
default: return 0;
262+
}
263+
}
264+
207265
private calculateRank(stars: number, commits: number, prs: number, issues: number): { level: string; score: number } {
208266
// Simple ranking algorithm (can be improved)
209267
const score = stars * 2 + commits * 1 + prs * 3 + issues * 1;

0 commit comments

Comments
 (0)