-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
351 lines (291 loc) · 12.4 KB
/
app.js
File metadata and controls
351 lines (291 loc) · 12.4 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
'use strict';
const repoInput = document.getElementById('repo-input');
const tokenInput = document.getElementById('token-input');
const fetchBtn = document.getElementById('fetch-btn');
const errorBox = document.getElementById('error-box');
const resultsEl = document.getElementById('results');
const userSearch = document.getElementById('user-search');
const userHighlight = document.getElementById('user-highlight');
const contribList = document.getElementById('contrib-list');
// Stats
const statTotalCommits = document.getElementById('stat-total-commits');
const statContributors = document.getElementById('stat-contributors');
const statTopUser = document.getElementById('stat-top-user');
const statTopPct = document.getElementById('stat-top-pct');
let chartInstance = null;
let allContributors = [];
// ─── Palette ────────────────────────────────────────────────────────────────
const PALETTE = [
'#58a6ff','#f78166','#3fb950','#e3b341','#a371f7',
'#ffa657','#79c0ff','#ff7b72','#56d364','#f0c98a',
'#bc8cff','#ffb77b',
];
function colorFor(i) {
return PALETTE[i % PALETTE.length];
}
// ─── API ─────────────────────────────────────────────────────────────────────
function makeHeaders(token) {
const h = { 'Accept': 'application/vnd.github.v3+json' };
if (token) h['Authorization'] = `Bearer ${token}`;
return h;
}
async function fetchAllContributors(owner, repo, token) {
const headers = makeHeaders(token);
let page = 1;
let all = [];
while (true) {
const url = `https://api.github.com/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/contributors?per_page=100&page=${page}&anon=false`;
const res = await fetch(url, { headers });
if (res.status === 404) throw new Error('Repository not found. Check the owner/repo name.');
if (res.status === 409) throw new Error('Repository is empty — no commits yet.');
if (res.status === 403) {
const body = await res.json().catch(() => ({}));
if (body.message && body.message.includes('too large')) {
throw new TooLargeError();
}
throw new Error('GitHub API rate limit exceeded. Add a personal access token to raise the limit to 5000 req/h.');
}
if (!res.ok) throw new Error(`GitHub API error: ${res.status} ${res.statusText}`);
const data = await res.json();
if (!Array.isArray(data) || data.length === 0) break;
all = all.concat(data);
if (data.length < 100) break;
page++;
}
return all;
}
class TooLargeError extends Error {
constructor() { super('too-large'); }
}
// Fallback for repos too large for /contributors — uses the stats endpoint.
// GitHub computes stats asynchronously: first call may return 202, retry until 200.
async function fetchStatsContributors(owner, repo, token, maxRetries = 8) {
const headers = makeHeaders(token);
const url = `https://api.github.com/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/stats/contributors`;
setStatus('Repository is very large — fetching cached stats (may take a few seconds)…');
for (let attempt = 0; attempt < maxRetries; attempt++) {
const res = await fetch(url, { headers });
if (res.status === 200) {
const data = await res.json();
if (!Array.isArray(data) || data.length === 0) throw new Error('No contributor stats available for this repository.');
return data
.filter(c => c.author)
.map(c => ({
login: c.author.login,
avatar_url: c.author.avatar_url,
html_url: c.author.html_url,
contributions: c.total,
}))
.sort((a, b) => b.contributions - a.contributions);
}
if (res.status === 202) {
// GitHub is computing — wait and retry
setStatus(`Computing stats… (attempt ${attempt + 1}/${maxRetries})`);
await new Promise(r => setTimeout(r, 2500));
continue;
}
if (res.status === 403) {
throw new Error('GitHub API rate limit exceeded. Add a personal access token to raise the limit to 5000 req/h.');
}
throw new Error(`Stats API error: ${res.status} ${res.statusText}`);
}
throw new Error('GitHub is still computing stats for this repository. Wait a few seconds and try again.');
}
function setStatus(msg) {
fetchBtn.textContent = msg.length > 40 ? msg.slice(0, 38) + '…' : msg;
}
// ─── Render chart ────────────────────────────────────────────────────────────
const CHART_TOP_N = 10;
function renderChart(contributors, total) {
const top = contributors.slice(0, CHART_TOP_N);
const others = contributors.slice(CHART_TOP_N);
const othersCount = others.reduce((s, c) => s + c.contributions, 0);
const labels = top.map(c => c.login);
const values = top.map(c => c.contributions);
const colors = top.map((_, i) => colorFor(i));
if (othersCount > 0) {
labels.push(`Others (${others.length})`);
values.push(othersCount);
colors.push('#484f58');
}
const ctx = document.getElementById('contrib-chart').getContext('2d');
if (chartInstance) chartInstance.destroy();
chartInstance = new Chart(ctx, {
type: 'doughnut',
data: {
labels,
datasets: [{
data: values,
backgroundColor: colors,
borderColor: '#161b22',
borderWidth: 3,
hoverOffset: 8,
}],
},
options: {
responsive: true,
maintainAspectRatio: true,
cutout: '60%',
plugins: {
legend: {
position: 'bottom',
labels: {
color: '#8b949e',
font: { size: 11 },
boxWidth: 12,
padding: 10,
},
},
tooltip: {
callbacks: {
label(ctx) {
const pct = ((ctx.parsed / total) * 100).toFixed(1);
return ` ${ctx.label}: ${ctx.parsed.toLocaleString()} commits (${pct}%)`;
},
},
},
},
},
});
}
// ─── Render list ─────────────────────────────────────────────────────────────
function renderList(contributors, total) {
contribList.innerHTML = '';
const maxCommits = contributors[0]?.contributions ?? 1;
contributors.forEach((c, i) => {
const pct = ((c.contributions / total) * 100).toFixed(2);
const barW = ((c.contributions / maxCommits) * 100).toFixed(1);
const li = document.createElement('li');
li.className = 'contrib-item';
li.dataset.login = c.login.toLowerCase();
li.innerHTML = `
<span class="contrib-rank">${i + 1}</span>
<div class="contrib-user">
<img src="${c.avatar_url}&s=56" alt="${c.login}" loading="lazy" />
<a href="${c.html_url}" target="_blank" rel="noopener">${c.login}</a>
</div>
<span class="contrib-commits">${c.contributions.toLocaleString()}</span>
<div class="contrib-pct-cell">
<span class="contrib-pct-label">${pct}%</span>
<div class="contrib-bar">
<div class="contrib-bar-fill" style="width:${barW}%;background:${colorFor(i)}"></div>
</div>
</div>
`;
contribList.appendChild(li);
});
}
// ─── Render stats bar ─────────────────────────────────────────────────────────
function renderStats(contributors, total) {
const top = contributors[0];
statTotalCommits.textContent = total.toLocaleString();
statContributors.textContent = contributors.length.toLocaleString();
statTopUser.textContent = top?.login ?? '—';
statTopPct.textContent = top ? `${((top.contributions / total) * 100).toFixed(1)}%` : '—';
}
// ─── User search ──────────────────────────────────────────────────────────────
function handleSearch(query, contributors, total) {
const q = query.trim().toLowerCase();
// Remove previous highlight
document.querySelectorAll('.contrib-item.highlighted').forEach(el => el.classList.remove('highlighted'));
if (!q) {
userHighlight.classList.add('hidden');
return;
}
const found = contributors.find(c => c.login.toLowerCase() === q)
?? contributors.find(c => c.login.toLowerCase().startsWith(q));
if (!found) {
userHighlight.classList.remove('hidden');
userHighlight.innerHTML = `<p class="not-found">No contributor matching "<strong>${escapeHtml(query)}</strong>"</p>`;
return;
}
const rank = contributors.indexOf(found) + 1;
const pct = ((found.contributions / total) * 100).toFixed(2);
userHighlight.classList.remove('hidden');
userHighlight.innerHTML = `
<div class="uh-avatar">
<img src="${found.avatar_url}&s=88" alt="${found.login}" />
<div>
<div class="uh-name">${found.login}</div>
<a class="uh-link" href="${found.html_url}" target="_blank" rel="noopener">View on GitHub →</a>
</div>
</div>
<div class="uh-stats">
<div class="uh-stat-row"><span>Rank</span><span>#${rank}</span></div>
<div class="uh-stat-row"><span>Commits</span><span>${found.contributions.toLocaleString()}</span></div>
<div class="uh-stat-row"><span>Share</span><span>${pct}%</span></div>
</div>
`;
// Highlight and scroll into view
const item = contribList.querySelector(`[data-login="${found.login.toLowerCase()}"]`);
if (item) {
item.classList.add('highlighted');
item.scrollIntoView({ behavior: 'smooth', block: 'center' });
}
}
// ─── Helpers ──────────────────────────────────────────────────────────────────
function escapeHtml(str) {
return str.replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]));
}
function showError(msg) {
errorBox.textContent = msg;
errorBox.classList.remove('hidden');
}
function clearError() {
errorBox.classList.add('hidden');
errorBox.textContent = '';
}
// ─── Main handler ─────────────────────────────────────────────────────────────
async function analyze() {
clearError();
resultsEl.classList.add('hidden');
userHighlight.classList.add('hidden');
allContributors = [];
const raw = repoInput.value.trim();
const token = tokenInput.value.trim();
if (!raw) { showError('Please enter a repository in the format owner/repo.'); return; }
const parts = raw.replace(/^https?:\/\/github\.com\//, '').replace(/\.git$/, '').split('/');
if (parts.length < 2 || !parts[0] || !parts[1]) {
showError('Invalid format. Use owner/repo or paste the full GitHub URL.');
return;
}
const [owner, repo] = parts;
fetchBtn.disabled = true;
fetchBtn.textContent = 'Loading…';
try {
let contributors;
try {
contributors = await fetchAllContributors(owner, repo, token);
} catch (err) {
if (err instanceof TooLargeError) {
contributors = await fetchStatsContributors(owner, repo, token);
} else {
throw err;
}
}
if (contributors.length === 0) {
showError('No contributors found for this repository.');
return;
}
// API already returns sorted descending, but let's ensure
contributors.sort((a, b) => b.contributions - a.contributions);
const total = contributors.reduce((s, c) => s + c.contributions, 0);
allContributors = contributors;
renderStats(contributors, total);
renderChart(contributors, total);
renderList(contributors, total);
resultsEl.classList.remove('hidden');
// Wire up search
userSearch.value = '';
userSearch.oninput = () => handleSearch(userSearch.value, contributors, total);
} catch (err) {
showError(err.message);
} finally {
fetchBtn.disabled = false;
fetchBtn.textContent = 'Analyze';
}
}
fetchBtn.addEventListener('click', analyze);
repoInput.addEventListener('keydown', e => {
if (e.key === 'Enter') analyze();
});