| 1 | #!/usr/bin/env node |
| 2 | import { renameSync, writeFileSync } from "node:fs"; |
| 3 | import { dirname, resolve } from "node:path"; |
| 4 | import { fileURLToPath } from "node:url"; |
| 5 | import { |
| 6 | ACTIVITY_WINDOW_DAYS, |
| 7 | SEARCH_RESULT_CAP, |
| 8 | creditedPairs, |
| 9 | mergedPullFromSearchItem, |
| 10 | readCappedJson, |
| 11 | releaseCredits, |
| 12 | versionedMilestones, |
| 13 | windowStart, |
| 14 | } from "./repo-activity-lib.mjs"; |
| 15 | |
| 16 | const REPO = "codewhale-hq/CodeWhale"; |
| 17 | const target = resolve(dirname(fileURLToPath(import.meta.url)), "..", "data", "repo-stats-snapshot.json"); |
| 18 | const headers = { accept: "application/vnd.github+json", "user-agent": "codewhale-facts-sync" }; |
| 19 | if (process.env.GITHUB_TOKEN) headers.authorization = `Bearer ${process.env.GITHUB_TOKEN}`; |
| 20 | const searchGapMs = process.env.GITHUB_TOKEN ? 2_200 : 7_000; |
| 21 | |
| 22 | const read = (path) => readCappedJson(`https://api.github.com${path}`, { headers }); |
| 23 | const count = (value) => (Number.isSafeInteger(value) && value >= 0 ? value : null); |
| 24 | const sleep = (ms) => new Promise((done) => setTimeout(done, ms)); |
| 25 | |
| 26 | let lastSearch = 0; |
| 27 | async function searchMerged(range) { |
| 28 | const query = encodeURIComponent(`repo:${REPO} is:pr is:merged merged:${range}`); |
| 29 | const pulls = []; |
| 30 | let seen = 0; |
| 31 | for (let page = 1; ; page += 1) { |
| 32 | const wait = lastSearch + searchGapMs - Date.now(); |
| 33 | if (wait > 0) await sleep(wait); |
| 34 | lastSearch = Date.now(); |
| 35 | const body = await read(`/search/issues?q=${query}&per_page=100&page=${page}`); |
| 36 | const total = count(body.total_count); |
| 37 | if (body.incomplete_results || total === null || total > SEARCH_RESULT_CAP) { |
| 38 | throw new Error(`GitHub search for merged:${range} is not complete`); |
| 39 | } |
| 40 | const items = Array.isArray(body.items) ? body.items : []; |
| 41 | for (const item of items) { |
| 42 | const pull = mergedPullFromSearchItem(item); |
| 43 | if (pull) pulls.push(pull); |
| 44 | } |
| 45 | seen += items.length; |
| 46 | if (seen >= total) return pulls; |
| 47 | if (items.length === 0) throw new Error(`GitHub search for merged:${range} ended early`); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | const readAt = new Date().toISOString(); |
| 53 | const repo = await read(`/repos/${REPO}`); |
| 54 | const openPullSearch = await read(`/search/issues?q=${encodeURIComponent(`repo:${REPO} is:pr is:open`)}&per_page=1`); |
| 55 | const stars = count(repo.stargazers_count); |
| 56 | const forks = count(repo.forks_count); |
| 57 | const openPulls = count(openPullSearch.total_count); |
| 58 | const openTotal = count(repo.open_issues_count); |
| 59 | if (!stars || forks === null || openPulls === null || openTotal === null) throw new Error("GitHub returned an unusable repository record"); |
| 60 | |
| 61 | const merged = (await searchMerged(`>=${windowStart(readAt)}`)).sort((a, b) => Date.parse(b.mergedAt) - Date.parse(a.mergedAt)); |
| 62 | |
| 63 | const milestoneRows = await read(`/repos/${REPO}/milestones?state=open&per_page=100`); |
| 64 | if (!Array.isArray(milestoneRows)) throw new Error("GitHub returned an unusable milestone list"); |
| 65 | const milestones = versionedMilestones(milestoneRows); |
| 66 | |
| 67 | const releaseRows = await read(`/repos/${REPO}/releases?per_page=12`); |
| 68 | if (!Array.isArray(releaseRows)) throw new Error("GitHub returned an unusable release list"); |
| 69 | const pairs = creditedPairs(releaseRows); |
| 70 | const pullsByTag = new Map(); |
| 71 | for (const { release, previous } of pairs) { |
| 72 | pullsByTag.set(release.tag_name, await searchMerged(`${previous.published_at}..${release.published_at}`)); |
| 73 | } |
| 74 | const releases = releaseCredits(pairs, (release) => pullsByTag.get(release.tag_name) ?? []); |
| 75 | |
| 76 | const snapshot = { |
| 77 | stars, |
| 78 | forks, |
| 79 | openIssues: Math.max(0, openTotal - openPulls), |
| 80 | openPulls, |
| 81 | readAt, |
| 82 | activity: { windowDays: ACTIVITY_WINDOW_DAYS, merged, milestones, releases }, |
| 83 | }; |
| 84 | const temporary = `${target}.${process.pid}.tmp`; |
| 85 | writeFileSync(temporary, `${JSON.stringify(snapshot, null, 2)}\n`); |
| 86 | renameSync(temporary, target); |
| 87 | console.log(`[sync-repo-stats] wrote ${stars} stars, ${merged.length} merged pull requests, ${releases.length} release credit lists, next milestone ${milestones[0]?.title ?? "none"}, read ${readAt}`); |
| 88 | } catch (error) { |
| 89 | console.error(`[sync-repo-stats] could not read repository numbers (${error.message}); leaving the snapshot alone.`); |
| 90 | process.exit(1); |
| 91 | } |
| 92 |