返回 CodeWhale
repo-activity-lib.mjs
根目录 / web / scripts / repo-activity-lib.mjs
1 export const ACTIVITY_WINDOW_DAYS = 30;
2 export const RELEASES_CREDITED = 5;
3 export const SEARCH_RESULT_CAP = 1000;
4 export const READ_DEADLINE_MS = 20_000;
5 export const READ_MAX_BYTES = 2_000_000;
6
7 const DAY_MS = 24 * 60 * 60 * 1000;
8
9 export async function readCappedJson(url, { headers = {}, fetchImpl = fetch, deadlineMs = READ_DEADLINE_MS, maxBytes = READ_MAX_BYTES } = {}) {
10 const response = await fetchImpl(url, { headers, signal: AbortSignal.timeout(deadlineMs) });
11 if (!response.ok) throw new Error(`GitHub returned ${response.status} for ${url}`);
12 const declared = Number(response.headers.get("content-length"));
13 if (Number.isFinite(declared) && declared > maxBytes) throw new Error(`response for ${url} is larger than ${maxBytes} bytes`);
14 const reader = response.body?.getReader();
15 if (!reader) throw new Error(`response for ${url} has no body`);
16 const chunks = [];
17 let total = 0;
18 for (;;) {
19 const { done, value } = await reader.read();
20 if (done) break;
21 total += value.byteLength;
22 if (total > maxBytes) {
23 await reader.cancel();
24 throw new Error(`response for ${url} is larger than ${maxBytes} bytes`);
25 }
26 chunks.push(value);
27 }
28 return JSON.parse(Buffer.concat(chunks).toString("utf8"));
29 }
30
31 export function windowStart(readAtIso, days = ACTIVITY_WINDOW_DAYS) {
32 const readAt = Date.parse(readAtIso);
33 if (!Number.isFinite(readAt)) throw new Error("read date is not a date");
34 return new Date(readAt - days * DAY_MS).toISOString();
35 }
36
37 export function mergedPullFromSearchItem(item) {
38 if (!item || typeof item !== "object") return null;
39 const mergedAt = item.pull_request?.merged_at;
40 const login = item.user?.login;
41 const avatar = item.user?.avatar_url;
42 if (typeof mergedAt !== "string" || !Number.isFinite(Date.parse(mergedAt))) return null;
43 if (typeof login !== "string" || !login || typeof avatar !== "string" || !avatar.startsWith("https://")) return null;
44 if (!Number.isSafeInteger(item.number) || typeof item.title !== "string" || typeof item.html_url !== "string") return null;
45 return {
46 number: item.number,
47 title: item.title,
48 url: item.html_url,
49 login,
50 avatar,
51 mergedAt,
52 firstTime: item.author_association === "FIRST_TIME_CONTRIBUTOR",
53 };
54 }
55
56 export function authorsBetween(pulls, afterIso, untilIso) {
57 const after = afterIso === null ? -Infinity : Date.parse(afterIso);
58 const until = Date.parse(untilIso);
59 const seen = new Map();
60 for (const pull of [...pulls].sort((a, b) => Date.parse(a.mergedAt) - Date.parse(b.mergedAt))) {
61 const mergedAt = Date.parse(pull.mergedAt);
62 if (!(mergedAt > after && mergedAt <= until)) continue;
63 if (!seen.has(pull.login)) seen.set(pull.login, { login: pull.login, avatar: pull.avatar });
64 }
65 return [...seen.values()];
66 }
67
68 export function versionedMilestones(rows) {
69 const candidates = [];
70 for (const row of Array.isArray(rows) ? rows : []) {
71 const match = typeof row?.title === "string" ? row.title.trim().match(/^v?(\d+(?:\.\d+)*)$/) : null;
72 if (!match || row.state === "closed") continue;
73 const open = row.open_issues;
74 const closed = row.closed_issues;
75 if (!Number.isSafeInteger(open) || !Number.isSafeInteger(closed) || open < 0 || closed < 0) continue;
76 if (typeof row.html_url !== "string" || !row.html_url.startsWith("https://github.com/")) continue;
77 candidates.push({ parts: match[1].split(".").map(Number), title: row.title.trim(), url: row.html_url, open, closed });
78 }
79 candidates.sort((a, b) => {
80 for (let i = 0; i < Math.max(a.parts.length, b.parts.length); i += 1) {
81 const diff = (a.parts[i] ?? 0) - (b.parts[i] ?? 0);
82 if (diff !== 0) return diff;
83 }
84 return 0;
85 });
86 return candidates.map(({ title, url, open, closed }) => ({ title, url, open, closed }));
87 }
88
89 export function creditedPairs(releases) {
90 const published = releases
91 .filter((release) => release && !release.draft && !release.prerelease && typeof release.tag_name === "string" && Number.isFinite(Date.parse(release.published_at)))
92 .sort((a, b) => Date.parse(b.published_at) - Date.parse(a.published_at));
93 const pairs = [];
94 for (let index = 0; index < published.length - 1 && pairs.length < RELEASES_CREDITED; index += 1) {
95 pairs.push({ release: published[index], previous: published[index + 1] });
96 }
97 return pairs;
98 }
99
100 export function releaseCredits(pairs, pullsFor) {
101 return pairs.map(({ release, previous }) => ({
102 tag: release.tag_name,
103 url: release.html_url,
104 publishedAt: release.published_at,
105 previousPublishedAt: previous.published_at,
106 authors: authorsBetween(pullsFor(release, previous), previous.published_at, release.published_at),
107 }));
108 }
109
109 lines Plain Text