mirror of
https://github.com/mattermost/mattermost.git
synced 2026-11-29 08:09:58 +08:00
* feat(docs-ai): remind authors when merged PRs still carry Docs/Needed On the 8th and 12th of each month, comment on PRs merged in the last 180 days that still have Docs/Needed. Ask the author (or, for bot PRs, the human assignee or merger) to submit and merge the docs, or to swap the label for Docs/Done if that already happened. Link the writer's open docs PR when one exists. One comment per PR per run date. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs-ai reminder: pace comment posts, default to 45-day lookback - Space reminder comment posts one second apart and retry a rate-limited post with backoff; a post that still cannot go through is logged, the run continues with the next PR and exits non-zero at the end. - Grant pull-requests: write alongside issues: write for the remind job. - Default lookback is now 45 days (script, workflow input, README, tests). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs-ai reminder: wait per Retry-After / x-ratelimit-reset headers The shared gh helper now attaches the response status and headers to the error it throws (message unchanged). The reminder waits for Retry-After, then x-ratelimit-reset when the quota is spent, and falls back to the one-minute backoff otherwise. It also waits before moving on to the next PR once retries run out. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> * docs-ai reminder: skip writer docs PRs by branch, not title The docs/pr-<n> head branch is set by the writer workflow and is what it already uses as its own loop guard; the PR title can be edited by anyone. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
79 lines
2.9 KiB
JavaScript
79 lines
2.9 KiB
JavaScript
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;
|
|
}
|
|
}
|