/** * @file The brain's report_problem: open an issue on one GitHub repo, assigned to whoever looks after * the box, so they get mail. The authority is a fine-grained token for that repo alone, with Issues * read/write and nothing else. */ export const MAX_TITLE_CHARS = 200; export const MAX_DETAILS_CHARS = 10_000; /** * @param {(path: string, body: object) => Promise<{ status: number, json: any }>} post * POSTs JSON to the GitHub API, authorized with the token * @param {string} repo owner/name * @param {string} assignee who gets the mail */ export const makeReporter = (post, repo, assignee) => /** @param {string} title @param {string} details */ async (title, details) => { if (typeof title !== 'string' || !title.trim()) throw new TypeError('title: a short summary'); if (title.length > MAX_TITLE_CHARS) throw new RangeError(`title over ${MAX_TITLE_CHARS} chars`); if (typeof details !== 'string') throw new TypeError('details: what happened, and what you tried'); if (details.length > MAX_DETAILS_CHARS) throw new RangeError(`details over ${MAX_DETAILS_CHARS} chars`); const { status, json } = await post(`/repos/${repo}/issues`, { title: title.trim(), body: `${details}\n\n— reported by the agent at the TV`, assignees: [assignee], }); if (status !== 201) throw new Error(`GitHub said ${status}: ${json?.message ?? ''}`); return `reported as #${json.number}: ${json.html_url}`; }; /** * POST JSON to the GitHub API with a token. * @param {typeof fetch} fetch * @param {string} token */ export const makeGitHubPost = (fetch, token) => /** @param {string} path @param {object} body */ async (path, body) => { const res = await fetch(`https://api.github.com${path}`, { method: 'POST', headers: { authorization: `Bearer ${token}`, accept: 'application/vnd.github+json', 'content-type': 'application/json', 'user-agent': 'pdtv26-brain (brain/report.js)', }, body: JSON.stringify(body), }); return { status: res.status, json: await res.json().catch(() => undefined) }; };