|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * a11y-credits.mjs - print the release-notes block of accessibility credits. |
| 4 | + * |
| 5 | + * node .github/scripts/a11y-credits.mjs --since <tag-or-YYYY-MM-DD> [--repo owner/name] |
| 6 | + * |
| 7 | + * Lists items labelled "credit: yes" or "credit: anonymous" that closed since |
| 8 | + * the tag or date, as the contributor chose to be credited, grouped as code |
| 9 | + * (pull requests) and other contributions (reports and other issues). Each |
| 10 | + * line describes the work by the item's title, never the person. |
| 11 | + * |
| 12 | + * The block goes to standard output, ready to paste. Warnings about credit |
| 13 | + * questions unanswered for more than 14 days go to standard error. |
| 14 | + * |
| 15 | + * Needs a token with read access: GITHUB_TOKEN or GH_TOKEN, or a signed-in |
| 16 | + * GitHub CLI (`gh auth token`). No dependencies. |
| 17 | + */ |
| 18 | + |
| 19 | +import { execFileSync } from 'node:child_process'; |
| 20 | +import path from 'node:path'; |
| 21 | +import { fileURLToPath } from 'node:url'; |
| 22 | +import { |
| 23 | + collectCredits, formatCredits, formatWarnings, loadConfig, makeClient, resolveSince, |
| 24 | +} from './a11y-community.mjs'; |
| 25 | + |
| 26 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 27 | + |
| 28 | +function arg(name) { |
| 29 | + const i = process.argv.indexOf(name); |
| 30 | + return i === -1 ? null : process.argv[i + 1]; |
| 31 | +} |
| 32 | + |
| 33 | +function token() { |
| 34 | + if (process.env.GITHUB_TOKEN) return process.env.GITHUB_TOKEN; |
| 35 | + if (process.env.GH_TOKEN) return process.env.GH_TOKEN; |
| 36 | + try { |
| 37 | + return execFileSync('gh', ['auth', 'token'], { encoding: 'utf8' }).trim(); |
| 38 | + } catch { |
| 39 | + return ''; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function repoFromGit() { |
| 44 | + const url = execFileSync('git', ['remote', 'get-url', 'origin'], { cwd: HERE, encoding: 'utf8' }).trim(); |
| 45 | + const m = url.match(/github\.com[:/]([^/]+)\/(.+?)(?:\.git)?$/); |
| 46 | + if (!m) throw new Error(`cannot tell the repository from ${url}; pass --repo owner/name`); |
| 47 | + return `${m[1]}/${m[2]}`; |
| 48 | +} |
| 49 | + |
| 50 | +/** request and paginate over fetch, matching what makeClient expects. */ |
| 51 | +function fetchTransport(auth) { |
| 52 | + const headers = { |
| 53 | + accept: 'application/vnd.github+json', |
| 54 | + 'x-github-api-version': '2022-11-28', |
| 55 | + 'user-agent': 'a11y-credits', |
| 56 | + ...(auth ? { authorization: `Bearer ${auth}` } : {}), |
| 57 | + }; |
| 58 | + const build = (route, params) => { |
| 59 | + const [method, template] = route.split(' '); |
| 60 | + const rest = { ...params }; |
| 61 | + const pathname = template.replace(/\{(\w+)\}/g, (_, key) => { |
| 62 | + const value = rest[key]; |
| 63 | + delete rest[key]; |
| 64 | + return encodeURIComponent(value); |
| 65 | + }); |
| 66 | + const url = new URL(`https://api.github.com${pathname}`); |
| 67 | + for (const [key, value] of Object.entries(rest)) url.searchParams.set(key, value); |
| 68 | + return { method, url }; |
| 69 | + }; |
| 70 | + const call = async (method, url) => { |
| 71 | + const res = await fetch(url, { method, headers }); |
| 72 | + if (!res.ok) { |
| 73 | + const error = new Error(`${method} ${url.pathname}: ${res.status} ${await res.text()}`); |
| 74 | + error.status = res.status; |
| 75 | + throw error; |
| 76 | + } |
| 77 | + return res; |
| 78 | + }; |
| 79 | + return { |
| 80 | + request: async (route, params) => { |
| 81 | + const { method, url } = build(route, params); |
| 82 | + return (await call(method, url)).json(); |
| 83 | + }, |
| 84 | + paginate: async (route, params) => { |
| 85 | + let { method, url } = build(route, params); |
| 86 | + const all = []; |
| 87 | + while (url) { |
| 88 | + const res = await call(method, url); |
| 89 | + all.push(...(await res.json())); |
| 90 | + const next = (res.headers.get('link') || '').match(/<([^>]+)>;\s*rel="next"/); |
| 91 | + url = next ? new URL(next[1]) : null; |
| 92 | + } |
| 93 | + return all; |
| 94 | + }, |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +async function main() { |
| 99 | + const sinceArg = arg('--since'); |
| 100 | + if (!sinceArg) { |
| 101 | + console.error('Usage: node .github/scripts/a11y-credits.mjs --since <tag-or-YYYY-MM-DD> [--repo owner/name]'); |
| 102 | + process.exit(2); |
| 103 | + } |
| 104 | + const fullName = arg('--repo') || repoFromGit(); |
| 105 | + const [owner, repo] = fullName.split('/'); |
| 106 | + const config = loadConfig(path.join(HERE, '..', 'a11y-community.json')); |
| 107 | + const client = makeClient(fetchTransport(token()), { owner, repo }); |
| 108 | + |
| 109 | + const since = await resolveSince(client, sinceArg); |
| 110 | + const result = await collectCredits(client, config, since); |
| 111 | + const block = formatCredits(result, config); |
| 112 | + if (block) process.stdout.write(block); |
| 113 | + else console.error(`No accessibility contributions to credit since ${since.toISOString().slice(0, 10)}.`); |
| 114 | + const warnings = formatWarnings(result, config); |
| 115 | + if (warnings) console.error(`\n${warnings}`); |
| 116 | +} |
| 117 | + |
| 118 | +main().catch((error) => { |
| 119 | + console.error(error.message); |
| 120 | + process.exit(1); |
| 121 | +}); |
0 commit comments