const PER_PAGE = 100; export async function gh(path, {method = 'GET', body} = {}) { const res = await fetch(`https://api.github.com${path}`, { method, headers: { authorization: `Bearer ${process.env.GITHUB_TOKEN}`, accept: 'application/vnd.github+json', 'x-github-api-version': '2022-11-28', 'content-type': 'application/json', }, body: body ? JSON.stringify(body) : undefined, }); if (!res.ok) { const err = new Error(`GitHub ${method} ${path} -> ${res.status}: ${await res.text()}`); // Callers that back off on rate limits need Retry-After / x-ratelimit-*. err.status = res.status; err.headers = Object.fromEntries(res.headers); throw err; } return res.status === 204 ? null : res.json(); } export async function* issueComments(repo, pr, {request = gh, perPage = PER_PAGE} = {}) { for (let page = 1; ; page++) { const batch = await request(`/repos/${repo}/issues/${pr}/comments?per_page=${perPage}&page=${page}`); yield* batch; if (batch.length < perPage) return; } } // Report posts as github-actions[bot]; matching any Bot lets another steal the sticky. const STICKY_AUTHOR = 'github-actions[bot]'; export async function findStickyComment(repo, pr, {marker, request = gh}) { for await (const c of issueComments(repo, pr, {request})) { if (c.body?.includes(marker) && c.user?.login === STICKY_AUTHOR) return c; } return null; } export async function createComment(repo, pr, body, {request = gh} = {}) { const created = await request(`/repos/${repo}/issues/${pr}/comments`, {method: 'POST', body: {body}}); return created?.id; } export async function updateComment(repo, id, body, {request = gh} = {}) { await request(`/repos/${repo}/issues/comments/${id}`, {method: 'PATCH', body: {body}}); } export async function upsertStickyComment(repo, pr, {marker, body, request = gh}) { const existing = await findStickyComment(repo, pr, {marker, request}); if (existing) { await updateComment(repo, existing.id, body, {request}); return {action: 'updated', id: existing.id}; } return {action: 'created', id: await createComment(repo, pr, body, {request})}; } export async function issueLabels(repo, pr, {request = gh} = {}) { const labels = await request(`/repos/${repo}/issues/${pr}/labels?per_page=${PER_PAGE}`); return labels.map((l) => l.name); } export async function addLabel(repo, pr, label, {request = gh} = {}) { await request(`/repos/${repo}/issues/${pr}/labels`, {method: 'POST', body: {labels: [label]}}); } export async function removeLabel(repo, pr, label, {request = gh} = {}) { // Label names contain a slash, so they must be encoded into the path. const path = `/repos/${repo}/issues/${pr}/labels/${encodeURIComponent(label)}`; try { await request(path, {method: 'DELETE'}); } catch (e) { // A human getting there first is a race we win by doing nothing. if (!/-> 404/.test(e.message)) throw e; } }