generated from google-gemini/aistudio-repository-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithubService.ts
More file actions
622 lines (521 loc) · 22.3 KB
/
githubService.ts
File metadata and controls
622 lines (521 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
import { GitHubStats, GitHubRepo } from '../types';
/**
* Client-side timeout: 25s total (allows for 4 parallel calls + processing)
* Netlify function timeout: 10s per call
*/
const CLIENT_TIMEOUT_MS = 25000;
const GITHUB_API_BASE = 'https://api.github.com';
/**
* In dev (Vite only), call GitHub API directly. In production or with Netlify Dev, use the proxy.
*/
const fetchFromGitHub = async (endpoint: string, username: string, timeoutMs: number, signal: AbortSignal): Promise<any> => {
const url = `${GITHUB_API_BASE}${endpoint.replace('{username}', username)}`;
const response = await fetch(url, {
headers: {
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'DevWrapped-2025',
},
signal,
});
if (!response.ok) {
const data = await response.json().catch(() => ({}));
const message = data.message || `HTTP ${response.status}`;
if (response.status === 404) throw new Error(`GITHUB_USER_NOT_FOUND: The user profile "${username}" does not exist.`);
if (response.status === 403 && response.headers.get('x-ratelimit-remaining') === '0') throw new Error('GITHUB_RATE_LIMIT: API quota exceeded. Please wait a moment before trying again.');
throw new Error(message);
}
return response.json();
};
/**
* Calls the Netlify serverless function to proxy GitHub API calls (production).
* In development without Netlify, calls GitHub API directly so localhost works.
*/
const fetchViaProxy = async (endpoint: string, username: string, timeoutMs: number = 8000): Promise<any> => {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
try {
if (import.meta.env.DEV) {
const result = await fetchFromGitHub(endpoint, username, timeoutMs, controller.signal);
clearTimeout(timeoutId);
return result;
}
const response = await fetch('/.netlify/functions/github-proxy', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username,
endpoint,
}),
signal: controller.signal,
});
clearTimeout(timeoutId);
if (!response.ok) {
const errorData = await response.json().catch(() => ({ error: 'Unknown error' }));
throw new Error(errorData.error || `HTTP ${response.status}`);
}
return response.json();
} catch (error: any) {
clearTimeout(timeoutId);
if (error.name === 'AbortError' || error.message?.includes('timeout')) {
throw new Error('GITHUB_API_TIMEOUT: Request timed out. GitHub API may be slow. Please retry.');
}
throw error;
}
};
/**
* Fetch all repositories for a user, handling pagination (public repos only)
*/
const fetchAllRepos = async (username: string): Promise<any[]> => {
let allRepos: any[] = [];
let page = 1;
const perPage = 100;
console.log(`Starting repository fetch for ${username} (public repos only)`);
while (true) {
const endpoint = `/users/${username}/repos?sort=updated&per_page=${perPage}&page=${page}`;
console.log(`Fetching page ${page} with endpoint: ${endpoint}`);
const repos = await fetchViaProxy(endpoint, username, 8000);
if (!repos || repos.length === 0) {
console.log(`No more repositories found on page ${page}`);
break;
}
console.log(`Page ${page}: Found ${repos.length} repositories`);
console.log(`Sample repos from page ${page}:`, repos.slice(0, 3).map((r: any) => ({
name: r.name,
private: r.private,
owner: r.owner.login
})));
allRepos = allRepos.concat(repos);
// If we got less than perPage results, we've reached the end
if (repos.length < perPage) {
console.log(`Reached end of repositories (got ${repos.length} < ${perPage})`);
break;
}
page++;
// Safety limit to prevent infinite loops
if (page > 10) {
console.warn('Reached maximum page limit (10) for repository fetching');
break;
}
}
const privateCount = allRepos.filter(r => r.private).length;
const publicCount = allRepos.filter(r => !r.private).length;
console.log(`Final repository summary:`, {
total: allRepos.length,
public: publicCount,
private: privateCount,
pages: page
});
return allRepos;
};
/**
* Centralized contribution calculation to ensure consistency across all sections
*/
const calculateContributionStats = (events: any[], repos: any[], analysisYear: number) => {
// Use selected year instead of current year
const yearStart = new Date(`${analysisYear}-01-01`);
const yearEnd = new Date(`${analysisYear}-12-31`);
const today = new Date();
// For current year, don't go beyond today
const endDate = analysisYear === new Date().getFullYear() ? today : yearEnd;
// Filter events for selected year only
const eventsThisYear = events.filter(event => {
const eventDate = new Date(event.created_at);
return eventDate >= yearStart && eventDate <= endDate;
});
console.log(`Calculating stats from ${eventsThisYear.length} events in ${analysisYear}`);
// Count different types of contributions consistently
let totalContributions = 0;
const dailyContributions = new Map<string, number>();
const monthlyContributions = new Map<string, number>();
const activeDaysSet = new Set<string>();
eventsThisYear.forEach(event => {
const eventDate = new Date(event.created_at);
const dateStr = eventDate.toISOString().split('T')[0];
const monthKey = eventDate.toISOString().slice(0, 7); // YYYY-MM
activeDaysSet.add(dateStr);
let contributionCount = 0;
switch (event.type) {
case 'PushEvent':
// Count actual commits in push event
contributionCount = event.payload?.commits?.length || 1;
break;
case 'CreateEvent':
contributionCount = 1;
break;
case 'IssuesEvent':
contributionCount = 1;
break;
case 'PullRequestEvent':
contributionCount = 1;
break;
case 'IssueCommentEvent':
case 'PullRequestReviewEvent':
case 'PullRequestReviewCommentEvent':
contributionCount = 1;
break;
default:
contributionCount = 0;
}
if (contributionCount > 0) {
totalContributions += contributionCount;
dailyContributions.set(dateStr, (dailyContributions.get(dateStr) || 0) + contributionCount);
monthlyContributions.set(monthKey, (monthlyContributions.get(monthKey) || 0) + contributionCount);
}
});
// Add repository activity contributions
repos.forEach(repo => {
if (repo.updated_at) {
const updateDate = new Date(repo.updated_at);
if (updateDate >= yearStart && updateDate <= endDate) {
const dateStr = updateDate.toISOString().split('T')[0];
const monthKey = updateDate.toISOString().slice(0, 7);
activeDaysSet.add(dateStr);
totalContributions += 1;
dailyContributions.set(dateStr, (dailyContributions.get(dateStr) || 0) + 1);
monthlyContributions.set(monthKey, (monthlyContributions.get(monthKey) || 0) + 1);
}
}
});
// Generate monthly activity data
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const monthlyActivity = [];
for (let i = 0; i < 12; i++) {
const monthKey = `${analysisYear}-${String(i + 1).padStart(2, '0')}`;
const currentMonth = new Date(analysisYear, i, 1);
if (currentMonth <= endDate) {
const count = monthlyContributions.get(monthKey) || 0;
let level = 0;
if (count >= 1) level = 1;
if (count >= 5) level = 2;
if (count >= 15) level = 3;
if (count >= 30) level = 4;
monthlyActivity.push({
month: months[i],
count,
level
});
}
}
return {
totalContributions,
activeDays: activeDaysSet.size,
dailyContributions,
monthlyContributions,
monthlyActivity,
eventsThisYear: eventsThisYear.length,
analysisYear
};
};
/**
* Try to get more comprehensive activity data by combining multiple sources
* and attempting to estimate missing contributions
*/
const getEnhancedActivityData = async (username: string, analysisYear: number): Promise<any[]> => {
try {
// Try to get more comprehensive public activity from multiple endpoints
const [publicEvents, receivedEvents, userRepos] = await Promise.all([
fetchViaProxy(`/users/${username}/events/public?per_page=100`, username, 8000),
// Try to get received events (activity on user's repos)
fetchViaProxy(`/users/${username}/received_events/public?per_page=100`, username, 8000).catch(() => []),
// Get user's repositories to estimate additional activity
fetchViaProxy(`/users/${username}/repos?per_page=100&sort=updated`, username, 8000).catch(() => [])
]);
console.log(`Enhanced activity: ${publicEvents.length} public events, ${receivedEvents.length} received events, ${userRepos.length} repos`);
// Combine and deduplicate events
const allEvents = [...publicEvents, ...receivedEvents];
const uniqueEvents = allEvents.filter((event, index, self) =>
index === self.findIndex(e => e.id === event.id)
);
// Try to estimate additional contributions from repository data
const recentRepos = userRepos.filter((repo: any) => {
const updatedDate = new Date(repo.updated_at);
const yearStart = new Date(`${analysisYear}-01-01`);
const yearEnd = new Date(`${analysisYear}-12-31`);
return updatedDate >= yearStart && updatedDate <= yearEnd;
});
// Add estimated contributions for repositories updated in selected year
// This helps account for activity not captured in events
const estimatedRepoContributions = recentRepos.map((repo: any) => ({
id: `repo-${repo.id}`,
type: 'EstimatedRepoActivity',
created_at: repo.updated_at,
payload: { estimated: true, repo_name: repo.name }
}));
console.log(`Added ${estimatedRepoContributions.length} estimated repo contributions`);
return [...uniqueEvents, ...estimatedRepoContributions];
} catch (error) {
console.warn('Enhanced activity fetch failed, using basic events:', error);
return [];
}
};
const fetchYearEvents = async (username: string, analysisYear: number): Promise<any[]> => {
let allEvents: any[] = [];
let page = 1;
const perPage = 100;
const yearStart = new Date(`${analysisYear}-01-01`);
const yearEnd = new Date(`${analysisYear}-12-31`);
const today = new Date();
// For current year, don't go beyond today
const endDate = analysisYear === new Date().getFullYear() ? today : yearEnd;
console.log(`Fetching public events for ${username} from ${analysisYear}-01-01 to ${endDate.toISOString().split('T')[0]}`);
while (true) {
const events = await fetchViaProxy(
`/users/${username}/events/public?per_page=${perPage}&page=${page}`,
username,
8000
);
if (!events || events.length === 0) {
console.log(`No more events found on page ${page}`);
break;
}
// Filter events for selected year only
const eventsThisYear = events.filter((event: any) => {
const eventDate = new Date(event.created_at);
return eventDate >= yearStart && eventDate <= endDate;
});
console.log(`Page ${page}: Found ${events.length} total events, ${eventsThisYear.length} from ${analysisYear}`);
allEvents = allEvents.concat(eventsThisYear);
// If we found events older than selected year, we can stop
const oldestEventDate = new Date(events[events.length - 1].created_at);
if (oldestEventDate < yearStart) {
console.log(`Reached events before ${analysisYear} (${oldestEventDate.toISOString().split('T')[0]}), stopping`);
break;
}
// If we got less than perPage results, we've reached the end
if (events.length < perPage) {
console.log(`Reached end of events (got ${events.length} < ${perPage})`);
break;
}
page++;
// Safety limit - GitHub public events API typically goes back ~90 days
if (page > 30) {
console.warn('Reached maximum page limit (30) for event fetching');
break;
}
}
console.log(`Fetched ${allEvents.length} public events from ${analysisYear} across ${page} pages`);
return allEvents;
};
export const fetchGitHubData = async (username: string, selectedYear?: number): Promise<GitHubStats> => {
const startTime = Date.now();
const analysisYear = selectedYear || new Date().getFullYear();
try {
// Debug logging
console.log('fetchGitHubData called with:', { username, analysisYear });
// STEP 1: Fetch user data first
const userData = await fetchViaProxy(`/users/${username}`, username, 5000);
const user = userData;
// STEP 2: Fetch repositories and enhanced activity data in parallel
console.log(`Fetching public repositories and enhanced ${analysisYear} activity...`);
const [repos, basicEvents, enhancedEvents] = await Promise.all([
fetchAllRepos(username),
fetchYearEvents(username, analysisYear),
getEnhancedActivityData(username, analysisYear)
]);
// Combine all events and remove duplicates
const allEvents = [...basicEvents, ...enhancedEvents];
const uniqueEvents = allEvents.filter((event, index, self) =>
index === self.findIndex(e => e.id === event.id)
);
const events = uniqueEvents.filter(event => {
const eventDate = new Date(event.created_at);
const yearStart = new Date(`${analysisYear}-01-01`);
const yearEnd = new Date(`${analysisYear}-12-31`);
return eventDate >= yearStart && eventDate <= yearEnd;
});
console.log(`Data fetched: ${repos.length} repos, ${events.length} unique events from ${analysisYear}`);
// Check if we're running out of time
if (Date.now() - startTime > CLIENT_TIMEOUT_MS - 2000) {
throw new Error('GITHUB_FETCH_TIMEOUT: Data fetching exceeded time limit. Please retry.');
}
// CENTRALIZED CONTRIBUTION CALCULATION
// This ensures all sections show the same numbers
const contributionStats = calculateContributionStats(events, repos, analysisYear);
console.log('Centralized contribution stats:', contributionStats);
const recentRepos: GitHubRepo[] = repos.slice(0, 5).map((repo: any) => ({
name: repo.name,
url: repo.html_url,
description: repo.description || 'No description provided.',
language: repo.language || 'Unknown',
stars: repo.stargazers_count
}));
// Count repos created in 2024/2025
const currentYear = new Date().getFullYear();
const reposCreatedThisYear = repos.filter((repo: any) => {
if (!repo.created_at) return false;
const createdYear = new Date(repo.created_at).getFullYear();
return createdYear === currentYear || createdYear === 2024; // Include both 2024 and current year
}).length;
// Calculate total stars received across all repos
const totalStarsReceived = repos.reduce((total: number, repo: any) => {
return total + (repo.stargazers_count || 0);
}, 0);
// Calculate account age in years
const accountCreatedDate = new Date(user.created_at);
const currentDate = new Date();
const accountAge = Math.max(1, Math.floor((currentDate.getTime() - accountCreatedDate.getTime()) / (1000 * 60 * 60 * 24 * 365)));
// Process repos data for language analysis
const langMap: Record<string, number> = {};
repos.forEach((repo: any) => {
if (repo.language) {
langMap[repo.language] = (langMap[repo.language] || 0) + 1;
}
});
const topLanguages = Object.entries(langMap)
.map(([name, count]) => ({ name, count }))
.sort((a, b) => b.count - a.count)
.slice(0, 3);
// Keep all languages for tech stack cloud
const allLanguages = Object.entries(langMap)
.map(([name, count]) => ({ name, count }))
.sort((a, b) => b.count - a.count);
// Process events data for better activity tracking
const activeDaysSet = new Set<string>();
const commitDates: string[] = [];
// Filter for actual commit/push events and extract dates
events.forEach((e: any) => {
const date = e.created_at.split('T')[0];
activeDaysSet.add(date);
// Focus on commit-related events for streak calculation
if (e.type === 'PushEvent' || e.type === 'CreateEvent') {
commitDates.push(date);
}
});
// Use centralized activity data for streak calculation
const activeDaysArray = Array.from(contributionStats.dailyContributions.keys()).sort().reverse();
// Enhanced streak calculation
let currentStreak = 0;
let longestStreak = 0;
if (activeDaysSet.size > 0) {
// Convert to sorted array of unique dates (most recent first)
const sortedDates = Array.from(activeDaysSet).sort().reverse();
// Calculate current streak (from most recent date backwards)
const today = new Date();
const todayStr = today.toISOString().split('T')[0];
const yesterdayStr = new Date(today.getTime() - 24 * 60 * 60 * 1000).toISOString().split('T')[0];
// Check if streak is current (today or yesterday)
let streakStart = -1;
if (sortedDates[0] === todayStr || sortedDates[0] === yesterdayStr) {
streakStart = 0;
} else if (sortedDates.includes(todayStr)) {
streakStart = sortedDates.indexOf(todayStr);
} else if (sortedDates.includes(yesterdayStr)) {
streakStart = sortedDates.indexOf(yesterdayStr);
}
if (streakStart >= 0) {
currentStreak = 1;
let lastDate = new Date(sortedDates[streakStart]);
for (let i = streakStart + 1; i < sortedDates.length; i++) {
const currentDate = new Date(sortedDates[i]);
const diffDays = Math.round((lastDate.getTime() - currentDate.getTime()) / (1000 * 60 * 60 * 24));
if (diffDays === 1) {
currentStreak++;
lastDate = currentDate;
} else {
break;
}
}
}
// Calculate longest streak in the data
let tempStreak = 1;
longestStreak = 1;
for (let i = 1; i < sortedDates.length; i++) {
const prevDate = new Date(sortedDates[i - 1]);
const currentDate = new Date(sortedDates[i]);
const diffDays = Math.round((prevDate.getTime() - currentDate.getTime()) / (1000 * 60 * 60 * 24));
if (diffDays === 1) {
tempStreak++;
longestStreak = Math.max(longestStreak, tempStreak);
} else {
tempStreak = 1;
}
}
}
console.log('Streak calculation debug:', {
totalActiveDays: activeDaysSet.size,
currentStreak,
longestStreak,
recentDates: Array.from(activeDaysSet).sort().reverse().slice(0, 10)
});
const activityPattern = activeDaysSet.size > 15 ? 'consistent' : activeDaysSet.size > 5 ? 'burst' : 'sporadic';
const monthCounts: Record<string, number> = {};
events.forEach((e: any) => {
const m = new Date(e.created_at).toLocaleString('en-US', { month: 'long' });
monthCounts[m] = (monthCounts[m] || 0) + 1;
});
const mostActiveMonth = Object.entries(monthCounts).sort((a, b) => b[1] - a[1])[0]?.[0] || 'October';
// OPTIMIZATION: Commit search for 2025 - run it last and make it optional
// Use a shorter timeout since we already have most data
let totalCommits = 0;
try {
// Search for commits from selected year (public repos only)
const commitSearchResult = await Promise.race([
fetchViaProxy(
`/search/commits?q=author:${username}+committer-date:>=${analysisYear}-01-01&per_page=1`,
username,
5000 // Shorter timeout - this is the slowest endpoint
),
new Promise((_, reject) =>
setTimeout(() => reject(new Error('Commit search timeout')), 5000)
),
]) as any;
totalCommits = commitSearchResult?.total_count || 0;
console.log(`Found ${totalCommits} total commits in ${analysisYear} via search API`);
} catch (commitError) {
// Commit search failed - estimate from events data
console.warn('Commit search timed out, using estimate from events:', commitError);
// Count push events and estimate commits
const pushEvents = events.filter((e: any) => e.type === 'PushEvent');
totalCommits = pushEvents.reduce((total: number, event: any) => {
return total + (event.payload?.commits?.length || 1);
}, 0);
console.log(`Estimated ${totalCommits} commits from ${pushEvents.length} push events`);
}
const estimatedActiveDays = Math.max(
activeDaysSet.size,
Math.min(totalCommits, Math.floor(totalCommits / 3)) // More conservative estimate
);
console.log('Final statistics calculated:', {
totalCommits: contributionStats.totalContributions,
activeDays: contributionStats.activeDays,
currentStreak,
longestStreak,
eventsAnalyzed: contributionStats.eventsThisYear,
dateRange: `${contributionStats.analysisYear}-01-01 to ${new Date().toISOString().split('T')[0]}`
});
return {
username: user.login,
avatarUrl: user.avatar_url,
profileUrl: user.html_url,
totalCommits: contributionStats.totalContributions, // Use centralized calculation
activeDays: contributionStats.activeDays, // Use centralized calculation
topLanguages: topLanguages.length > 0 ? topLanguages : [{ name: 'Unknown', count: 0 }],
allLanguages: allLanguages.length > 0 ? allLanguages : [{ name: 'Unknown', count: 0 }],
reposContributed: user.public_repos + (user.total_private_repos || 0),
reposCreatedThisYear,
recentRepos,
streak: currentStreak,
longestStreak,
mostActiveMonth,
firstActivity: `${analysisYear}-01-01`,
lastActivity: activeDaysArray[0] || new Date().toISOString().split('T')[0],
activityPattern,
contributionGrid: contributionStats.monthlyActivity, // Use centralized monthly data
// New fields
followers: user.followers || 0,
following: user.following || 0,
totalStarsReceived,
accountAge,
bio: user.bio || undefined,
company: user.company || undefined,
location: user.location || undefined,
// AEO: Include analysis year for AI context
analysisYear: analysisYear,
};
} catch (error: any) {
console.error("GitHub Telemetry Fault:", error);
throw error;
}
};