-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
338 lines (298 loc) · 9.67 KB
/
script.js
File metadata and controls
338 lines (298 loc) · 9.67 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
const revealNodes = document.querySelectorAll(".reveal");
if ("IntersectionObserver" in window) {
const observer = new IntersectionObserver(
(entries, obs) => {
entries.forEach((entry) => {
if (!entry.isIntersecting) return;
entry.target.classList.add("is-visible");
obs.unobserve(entry.target);
});
},
{
threshold: 0.14,
rootMargin: "0px 0px -8% 0px",
}
);
revealNodes.forEach((node) => observer.observe(node));
} else {
revealNodes.forEach((node) => node.classList.add("is-visible"));
}
const GH_API = "https://api.github.com/repos";
const ghHeaders = { Accept: "application/vnd.github+json" };
function setCommitBar(el, { count, pct, tag, branch, compareUrl, error, noRelease }) {
const countEl = el.querySelector('[data-role="count"]');
const fillEl = el.querySelector('[data-role="fill"]');
const trackEl = el.querySelector(".commit-bar-track");
const metaEl = el.querySelector('[data-role="meta"]');
el.setAttribute("aria-busy", "false");
if (error) {
if (countEl) countEl.textContent = "—";
if (fillEl) fillEl.style.width = "0%";
if (trackEl) {
trackEl.setAttribute("aria-valuenow", "0");
trackEl.setAttribute("aria-valuetext", "Unable to load");
}
if (metaEl) {
metaEl.textContent =
"Could not load stats (network or GitHub API rate limit).";
}
return;
}
if (noRelease) {
if (countEl) countEl.textContent = "—";
if (fillEl) fillEl.style.width = "0%";
if (trackEl) {
trackEl.setAttribute("aria-valuenow", "0");
trackEl.setAttribute("aria-valuetext", "No release");
}
if (metaEl) {
metaEl.textContent =
"No published GitHub release yet; compare is not available.";
}
return;
}
const n = typeof count === "number" ? count : 0;
const width = Math.max(0, Math.min(100, typeof pct === "number" ? pct : 0));
if (countEl) countEl.textContent = String(n);
if (fillEl) fillEl.style.width = `${width}%`;
if (trackEl) {
trackEl.setAttribute("aria-valuenow", String(Math.round(width)));
trackEl.setAttribute(
"aria-valuetext",
`${n} commits since ${tag ?? "release"}`
);
}
if (metaEl) {
const safeTag = tag ? String(tag) : "";
const safeBranch = branch ? String(branch) : "main";
metaEl.textContent = "";
if (safeTag) {
const last = document.createTextNode(`Latest release: ${safeTag} · `);
metaEl.appendChild(last);
}
if (compareUrl) {
const a = document.createElement("a");
a.href = compareUrl;
a.target = "_blank";
a.rel = "noreferrer";
a.textContent = "View diff on GitHub";
metaEl.appendChild(a);
metaEl.appendChild(document.createTextNode(` (${safeBranch}).`));
} else {
metaEl.appendChild(
document.createTextNode(`Default branch: ${safeBranch}.`)
);
}
}
}
async function loadCommitsSinceRelease(fullName) {
const repoRes = await fetch(`${GH_API}/${fullName}`, { headers: ghHeaders });
if (!repoRes.ok) {
throw new Error("repo");
}
const repo = await repoRes.json();
const branch = repo.default_branch || "main";
const relRes = await fetch(`${GH_API}/${fullName}/releases/latest`, {
headers: ghHeaders,
});
if (relRes.status === 404) {
return { noRelease: true, branch };
}
if (!relRes.ok) {
throw new Error("release");
}
const rel = await relRes.json();
const tag = rel.tag_name;
if (!tag) {
return { noRelease: true, branch };
}
const comparePath = `${encodeURIComponent(tag)}...${encodeURIComponent(branch)}`;
const compareRes = await fetch(`${GH_API}/${fullName}/compare/${comparePath}`, {
headers: ghHeaders,
});
if (!compareRes.ok) {
throw new Error("compare");
}
const compare = await compareRes.json();
const count =
typeof compare.ahead_by === "number"
? compare.ahead_by
: typeof compare.total_commits === "number"
? compare.total_commits
: 0;
const compareUrl =
typeof compare.html_url === "string" ? compare.html_url : null;
return { count, tag, branch, compareUrl };
}
const MAX_AVATAR_TILES = 32;
async function fetchContributors(fullName) {
const perPage = 100;
let page = 1;
const contributors = [];
const maxPages = 40;
while (page <= maxPages) {
const res = await fetch(
`${GH_API}/${fullName}/contributors?per_page=${perPage}&page=${page}`,
{ headers: ghHeaders }
);
if (!res.ok) {
throw new Error("contributors");
}
const batch = await res.json();
for (const u of batch) {
if (u && u.login && u.avatar_url) {
contributors.push({
login: String(u.login),
avatar_url: String(u.avatar_url),
html_url:
typeof u.html_url === "string"
? u.html_url
: `https://github.com/${u.login}`,
});
}
}
if (batch.length < perPage) {
break;
}
page += 1;
}
return { count: contributors.length, contributors };
}
function setContributorCard(el, { count, contributors, contributorsUrl, error }) {
const countEl = el.querySelector('[data-role="contributor-count"]');
const avatarsEl = el.querySelector('[data-role="contributor-avatars"]');
const metaEl = el.querySelector('[data-role="contributor-meta"]');
el.setAttribute("aria-busy", "false");
if (error) {
if (countEl) countEl.textContent = "—";
if (avatarsEl) avatarsEl.innerHTML = "";
if (metaEl) {
metaEl.textContent =
"Could not load contributors (network or GitHub API rate limit).";
}
return;
}
if (countEl) countEl.textContent = String(count);
if (avatarsEl) {
avatarsEl.innerHTML = "";
avatarsEl.setAttribute("role", "list");
const list = Array.isArray(contributors) ? contributors : [];
const shown = list.slice(0, MAX_AVATAR_TILES);
shown.forEach((c) => {
const item = document.createElement("span");
item.setAttribute("role", "listitem");
item.className = "contributor-avatar-item";
const a = document.createElement("a");
a.className = "contributor-avatar";
a.href = c.html_url;
a.target = "_blank";
a.rel = "noreferrer";
a.title = c.login;
a.setAttribute("aria-label", `${c.login} on GitHub`);
const img = document.createElement("img");
img.src = c.avatar_url;
img.alt = "";
img.width = 64;
img.height = 64;
img.loading = "lazy";
img.decoding = "async";
a.appendChild(img);
item.appendChild(a);
avatarsEl.appendChild(item);
});
if (list.length > MAX_AVATAR_TILES) {
const more = document.createElement("span");
more.className = "contributor-more";
more.setAttribute("role", "listitem");
more.textContent = `+${list.length - MAX_AVATAR_TILES} more`;
more.title = "Additional contributors on GitHub";
avatarsEl.appendChild(more);
}
}
if (metaEl) {
metaEl.textContent = "";
if (contributorsUrl) {
const a = document.createElement("a");
a.href = contributorsUrl;
a.target = "_blank";
a.rel = "noreferrer";
a.textContent = "View all contributors on GitHub";
metaEl.appendChild(a);
}
}
}
async function initProjectStats() {
const rows = Array.from(document.querySelectorAll(".project-stats[data-repo]"));
if (!rows.length) return;
const results = await Promise.all(
rows.map(async (row) => {
const fullName = row.getAttribute("data-repo");
const commitEl = row.querySelector(".commit-activity");
const contribEl = row.querySelector(".contributor-card");
if (!fullName || !commitEl || !contribEl) {
return {
fullName: null,
commitEl,
contribEl,
commitRes: { status: "rejected" },
contribRes: { status: "rejected" },
};
}
const [commitRes, contribRes] = await Promise.allSettled([
loadCommitsSinceRelease(fullName),
fetchContributors(fullName),
]);
return { fullName, commitEl, contribEl, commitRes, contribRes };
})
);
const numericCounts = results
.map((r) => {
if (!r || r.commitRes.status !== "fulfilled") return null;
const val = r.commitRes.value;
if (!val || val.noRelease) return null;
return val.count;
})
.filter((c) => typeof c === "number");
const maxCount = numericCounts.length ? Math.max(...numericCounts) : 0;
results.forEach((r) => {
if (!r) return;
if (r.commitEl) {
if (r.commitRes.status === "rejected") {
setCommitBar(r.commitEl, { error: true });
} else {
const data = r.commitRes.value;
if (!data) {
setCommitBar(r.commitEl, { error: true });
} else if (data.noRelease) {
setCommitBar(r.commitEl, { noRelease: true, branch: data.branch });
} else {
const { count, tag, branch, compareUrl } = data;
const pct =
maxCount > 0 && typeof count === "number"
? (count / maxCount) * 100
: count && count > 0
? 100
: 0;
setCommitBar(r.commitEl, { count, pct, tag, branch, compareUrl });
}
}
}
if (r.contribEl) {
if (r.contribRes.status === "fulfilled" && r.fullName) {
const payload = r.contribRes.value;
setContributorCard(r.contribEl, {
count: payload.count,
contributors: payload.contributors,
contributorsUrl: `https://github.com/${r.fullName}/graphs/contributors`,
});
} else {
setContributorCard(r.contribEl, { error: true });
}
}
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", initProjectStats);
} else {
initProjectStats();
}