Open History to see the last commit
1import { readFile } from 'node:fs/promises'
2import type { ToolsetConfig } from './types'
3
4export const DEFAULT_CONFIG_PATH = '.github/maintainers.json'
5
6function isRecord(value: unknown): value is Record<string, unknown> {
7 return typeof value === 'object' && value !== null && !Array.isArray(value)
8}
9
10function fail(message: string): never {
11 throw new Error(`Invalid maintainers config: ${message}`)
12}
13
14export function parseConfig(raw: unknown): ToolsetConfig {
15 if (!isRecord(raw)) fail('root must be an object')
16 const { repo, maintainers, sla, spam, botAllowlist, maxCommentsPerRun } = raw
17
18 if (typeof repo !== 'string' || !/^[^/\s]+\/[^/\s]+$/.test(repo)) {
19 fail('"repo" must be an "owner/name" string')
20 }
21 if (!Array.isArray(maintainers) || maintainers.length === 0) {
22 fail('"maintainers" must be a non-empty array')
23 }
24 const parsedMaintainers = maintainers.map((m, idx) => {
25 if (!isRecord(m)) fail(`maintainers[${idx}] must be an object`)
26 if (typeof m.github !== 'string' || m.github.length === 0) {
27 fail(`maintainers[${idx}].github must be a string`)
28 }
29 const areas = m.areas ?? []
30 if (!Array.isArray(areas) || areas.some((a) => typeof a !== 'string')) {
31 fail(`maintainers[${idx}].areas must be an array of glob strings`)
32 }
33 return {
34 github: m.github,
35 discord: typeof m.discord === 'string' ? m.discord : null,
36 areas: areas as Array<string>,
37 maxOpenAssignments:
38 typeof m.maxOpenAssignments === 'number' ? m.maxOpenAssignments : 10,
39 }
40 })
41
42 if (!isRecord(sla)) fail('"sla" must be an object')
43 const slaNumber = (key: string): number => {
44 const v = sla[key]
45 if (typeof v !== 'number' || v <= 0)
46 fail(`sla.${key} must be a positive number`)
47 return v
48 }
49 if (!isRecord(spam)) fail('"spam" must be an object')
50 const spamNumber = (key: string): number => {
51 const v = spam[key]
52 if (typeof v !== 'number' || v <= 0)
53 fail(`spam.${key} must be a positive number`)
54 return v
55 }
56
57 return {
58 repo,
59 maintainers: parsedMaintainers,
60 sla: {
61 firstResponseHours: slaNumber('firstResponseHours'),
62 followUpResponseHours: slaNumber('followUpResponseHours'),
63 staleAuthorDays: slaNumber('staleAuthorDays'),
64 },
65 spam: {
66 maxAccountAgeDays: spamNumber('maxAccountAgeDays'),
67 maxChangedLines: spamNumber('maxChangedLines'),
68 },
69 botAllowlist: Array.isArray(botAllowlist)
70 ? botAllowlist.filter((b): b is string => typeof b === 'string')
71 : [],
72 maxCommentsPerRun:
73 typeof maxCommentsPerRun === 'number' ? maxCommentsPerRun : 25,
74 }
75}
76
77export async function loadConfig(
78 path: string = DEFAULT_CONFIG_PATH,
79): Promise<ToolsetConfig> {
80 const text = await readFile(path, 'utf8')
81 return parseConfig(JSON.parse(text))
82}
83
84export function rosterLogins(config: ToolsetConfig): Set<string> {
85 return new Set(config.maintainers.map((m) => m.github.toLowerCase()))
86}
87
88export function isRosterMaintainer(
89 login: string | null,
90 config: ToolsetConfig,
91): boolean {
92 return login !== null && rosterLogins(config).has(login.toLowerCase())
93}
94
95export function isBotLogin(
96 login: string | null,
97 config: ToolsetConfig,
98): boolean {
99 if (login === null) return false
100 const normalized = login.toLowerCase().replace(/\[bot\]$/, '')
101 return (
102 login.toLowerCase().endsWith('[bot]') ||
103 normalized.startsWith('app/') ||
104 config.botAllowlist.some((b) => b.toLowerCase() === normalized)
105 )
106}