| 1 | import snapshotJson from "../data/repo-stats-snapshot.json"; |
| 2 | import exclusionsJson from "../data/contributor-exclusions.json"; |
| 3 | |
| 4 | const SNAPSHOT: unknown = snapshotJson; |
| 5 | const EXCLUSIONS_RAW: unknown = exclusionsJson; |
| 6 | |
| 7 | const DAY_MS = 24 * 60 * 60 * 1000; |
| 8 | |
| 9 | export const LIVE_RECORD_MAX_AGE_MS = 7 * DAY_MS; |
| 10 | export const LIVE_RECORD_SOURCE = "github-public-snapshot"; |
| 11 | |
| 12 | export interface ActivityPull { |
| 13 | number: number; |
| 14 | title: string; |
| 15 | url: string; |
| 16 | login: string; |
| 17 | avatar: string; |
| 18 | mergedAt: string; |
| 19 | firstTime: boolean; |
| 20 | } |
| 21 | |
| 22 | export interface ActivityPerson { |
| 23 | login: string; |
| 24 | avatar: string; |
| 25 | } |
| 26 | |
| 27 | export interface ActivityMilestone { |
| 28 | title: string; |
| 29 | url: string; |
| 30 | open: number; |
| 31 | closed: number; |
| 32 | } |
| 33 | |
| 34 | export interface ActivityReleaseCredit { |
| 35 | tag: string; |
| 36 | url: string; |
| 37 | publishedAt: string; |
| 38 | previousPublishedAt: string; |
| 39 | authors: ActivityPerson[]; |
| 40 | } |
| 41 | |
| 42 | export interface RepoActivity { |
| 43 | readAt: string; |
| 44 | windowDays: number; |
| 45 | merged: ActivityPull[]; |
| 46 | milestones: ActivityMilestone[]; |
| 47 | releases: ActivityReleaseCredit[]; |
| 48 | } |
| 49 | |
| 50 | export interface ContributorExclusions { |
| 51 | confirmed: boolean; |
| 52 | logins: string[]; |
| 53 | suffixes: string[]; |
| 54 | } |
| 55 | |
| 56 | export interface RepoActivityView { |
| 57 | readAt: string; |
| 58 | windowDays: number; |
| 59 | pulls: ActivityPull[]; |
| 60 | people: ActivityPerson[]; |
| 61 | count: number | null; |
| 62 | milestone: ActivityMilestone | null; |
| 63 | milestones: ActivityMilestone[]; |
| 64 | } |
| 65 | |
| 66 | export interface LiveRecord { |
| 67 | source: string; |
| 68 | readAt: string | null; |
| 69 | fresh: boolean; |
| 70 | } |
| 71 | |
| 72 | function isRecord(value: unknown): value is Record<string, unknown> { |
| 73 | return !!value && typeof value === "object" && !Array.isArray(value); |
| 74 | } |
| 75 | |
| 76 | function isDate(value: unknown): value is string { |
| 77 | return typeof value === "string" && Number.isFinite(Date.parse(value)); |
| 78 | } |
| 79 | |
| 80 | function isCount(value: unknown): value is number { |
| 81 | return typeof value === "number" && Number.isSafeInteger(value) && value >= 0; |
| 82 | } |
| 83 | |
| 84 | function isHttps(value: unknown): value is string { |
| 85 | return typeof value === "string" && value.startsWith("https://"); |
| 86 | } |
| 87 | |
| 88 | function parsePerson(raw: unknown): ActivityPerson | null { |
| 89 | if (!isRecord(raw) || typeof raw.login !== "string" || !raw.login || !isHttps(raw.avatar)) return null; |
| 90 | return { login: raw.login, avatar: raw.avatar }; |
| 91 | } |
| 92 | |
| 93 | function parsePull(raw: unknown): ActivityPull | null { |
| 94 | if (!isRecord(raw)) return null; |
| 95 | const person = parsePerson(raw); |
| 96 | if (!person || !isDate(raw.mergedAt) || !isHttps(raw.url) || typeof raw.title !== "string") return null; |
| 97 | if (typeof raw.number !== "number" || !Number.isSafeInteger(raw.number)) return null; |
| 98 | return { number: raw.number, title: raw.title, url: raw.url, ...person, mergedAt: raw.mergedAt, firstTime: raw.firstTime === true }; |
| 99 | } |
| 100 | |
| 101 | export function parseExclusions(raw: unknown = EXCLUSIONS_RAW): ContributorExclusions { |
| 102 | const row = isRecord(raw) ? raw : {}; |
| 103 | const logins = Array.isArray(row.logins) |
| 104 | ? row.logins.flatMap((entry) => (isRecord(entry) && typeof entry.login === "string" ? [entry.login.toLowerCase()] : [])) |
| 105 | : []; |
| 106 | const suffixes = Array.isArray(row.suffixes) |
| 107 | ? row.suffixes.flatMap((entry) => (isRecord(entry) && typeof entry.suffix === "string" && entry.suffix ? [entry.suffix.toLowerCase()] : [])) |
| 108 | : []; |
| 109 | return { confirmed: row.confirmed === true, logins, suffixes }; |
| 110 | } |
| 111 | |
| 112 | export function isExcluded(login: string, exclusions: ContributorExclusions): boolean { |
| 113 | const lower = login.toLowerCase(); |
| 114 | return exclusions.logins.includes(lower) || exclusions.suffixes.some((suffix) => lower.endsWith(suffix)); |
| 115 | } |
| 116 | |
| 117 | export function readRepoActivity(raw: unknown = SNAPSHOT): RepoActivity | null { |
| 118 | if (!isRecord(raw) || !isDate(raw.readAt) || !isRecord(raw.activity)) return null; |
| 119 | const activity = raw.activity; |
| 120 | if (!Array.isArray(activity.merged) || !Array.isArray(activity.releases)) return null; |
| 121 | const windowDays = activity.windowDays; |
| 122 | if (typeof windowDays !== "number" || !Number.isSafeInteger(windowDays) || windowDays <= 0) return null; |
| 123 | |
| 124 | const merged = activity.merged.map(parsePull); |
| 125 | if (merged.some((pull) => pull === null)) return null; |
| 126 | |
| 127 | if (!Array.isArray(activity.milestones)) return null; |
| 128 | const milestones: ActivityMilestone[] = []; |
| 129 | for (const row of activity.milestones) { |
| 130 | if (!isRecord(row) || typeof row.title !== "string" || !isHttps(row.url) || !isCount(row.open) || !isCount(row.closed)) return null; |
| 131 | milestones.push({ title: row.title, url: row.url, open: row.open, closed: row.closed }); |
| 132 | } |
| 133 | |
| 134 | const releases: ActivityReleaseCredit[] = []; |
| 135 | for (const entry of activity.releases) { |
| 136 | if (!isRecord(entry) || typeof entry.tag !== "string" || !isHttps(entry.url) || !isDate(entry.publishedAt) || !isDate(entry.previousPublishedAt) || !Array.isArray(entry.authors)) return null; |
| 137 | const authors = entry.authors.map(parsePerson); |
| 138 | if (authors.some((person) => person === null)) return null; |
| 139 | releases.push({ tag: entry.tag, url: entry.url, publishedAt: entry.publishedAt, previousPublishedAt: entry.previousPublishedAt, authors: authors as ActivityPerson[] }); |
| 140 | } |
| 141 | |
| 142 | return { readAt: raw.readAt, windowDays, merged: merged as ActivityPull[], milestones, releases }; |
| 143 | } |
| 144 | |
| 145 | export function repoActivityView( |
| 146 | activity: RepoActivity | null = readRepoActivity(), |
| 147 | exclusions: ContributorExclusions = parseExclusions(), |
| 148 | ): RepoActivityView | null { |
| 149 | if (!activity) return null; |
| 150 | const since = Date.parse(activity.readAt) - activity.windowDays * DAY_MS; |
| 151 | const pulls = activity.merged |
| 152 | .filter((pull) => Date.parse(pull.mergedAt) > since && !isExcluded(pull.login, exclusions)) |
| 153 | .sort((a, b) => Date.parse(b.mergedAt) - Date.parse(a.mergedAt)); |
| 154 | const seen = new Set<string>(); |
| 155 | const people: ActivityPerson[] = []; |
| 156 | for (const pull of pulls) { |
| 157 | if (seen.has(pull.login)) continue; |
| 158 | seen.add(pull.login); |
| 159 | people.push({ login: pull.login, avatar: pull.avatar }); |
| 160 | } |
| 161 | return { |
| 162 | readAt: activity.readAt, |
| 163 | windowDays: activity.windowDays, |
| 164 | pulls, |
| 165 | people, |
| 166 | count: people.length > 0 ? people.length : null, |
| 167 | milestone: activity.milestones[0] ?? null, |
| 168 | milestones: activity.milestones, |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | export function releaseAuthors( |
| 173 | tag: string, |
| 174 | activity: RepoActivity | null = readRepoActivity(), |
| 175 | exclusions: ContributorExclusions = parseExclusions(), |
| 176 | ): ActivityPerson[] | null { |
| 177 | const release = activity?.releases.find((entry) => entry.tag === tag); |
| 178 | if (!release) return null; |
| 179 | const authors = release.authors.filter((person) => !isExcluded(person.login, exclusions)); |
| 180 | return authors.length > 0 ? authors : null; |
| 181 | } |
| 182 | |
| 183 | export function liveRecord(raw: unknown = SNAPSHOT, now: number = Date.now()): LiveRecord { |
| 184 | const readAt = isRecord(raw) && isDate(raw.readAt) ? raw.readAt : null; |
| 185 | const age = readAt === null ? Infinity : now - Date.parse(readAt); |
| 186 | return { source: LIVE_RECORD_SOURCE, readAt, fresh: age >= 0 && age <= LIVE_RECORD_MAX_AGE_MS }; |
| 187 | } |
| 188 |