Files
Eva SarafianouandClaude Opus 5.5 bbbc593cb1 feat(docs-ai): remind authors when merged PRs still carry Docs/Needed (#40009)
* 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>
2026-11-26 12:48:37 +02:00

187 lines
7.0 KiB
JavaScript

import {test} from 'node:test';
import assert from 'node:assert/strict';
import {addLabel, gh, issueLabels, removeLabel, upsertStickyComment} from './github.mjs';
const MARKER = '<!-- docs-ai-review:v1 -->';
const REPO = 'mattermost/mattermost';
const PR = '42';
const human = (id) => ({id, body: 'looks good to me', user: {type: 'User', login: 'reviewer'}});
const bot = (id) => ({id, body: `${MARKER}\n## Docs review`, user: {type: 'Bot', login: 'github-actions[bot]'}});
// A full page, so pagination continues past it.
const fullPage = (start) => Array.from({length: 100}, (_, i) => human(start + i));
function stub(pages) {
const reads = [];
const writes = [];
const request = async (path, {method, body} = {}) => {
if (method) {
writes.push({path, method, body});
return {id: 9001};
}
reads.push(path);
const page = Number(new URLSearchParams(path.split('?')[1]).get('page'));
return pages[page - 1] ?? [];
};
return {request, reads, writes};
}
test('updates the sticky comment when it sits beyond the first page', async () => {
// The bug this guards: one page of comments was fetched and the search gave
// up there, so a long review thread got a new comment on every push.
const s = stub([fullPage(1), fullPage(101), [...fullPage(201).slice(0, 40), bot(500)]]);
const result = await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.deepEqual(result, {action: 'updated', id: 500});
assert.deepEqual(s.writes, [
{path: `/repos/${REPO}/issues/comments/500`, method: 'PATCH', body: {body: 'fresh'}},
]);
assert.equal(s.reads.length, 3);
});
test('stops reading pages once the sticky comment is found', async () => {
const s = stub([[...fullPage(1).slice(0, 99), bot(7)], fullPage(101), fullPage(201)]);
await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.equal(s.reads.length, 1, 'later pages cannot contain an earlier comment');
});
test('walks every page before concluding there is no comment to update', async () => {
const s = stub([fullPage(1), fullPage(101), [human(201)]]);
const result = await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.equal(result.action, 'created');
assert.deepEqual(s.writes, [
{path: `/repos/${REPO}/issues/${PR}/comments`, method: 'POST', body: {body: 'fresh'}},
]);
assert.equal(s.reads.length, 3);
});
test('a short page ends the walk', async () => {
const s = stub([[human(1), human(2)]]);
await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.equal(s.reads.length, 1);
});
test('a comment count that is an exact multiple of the page size terminates', async () => {
// GitHub answers the page past the end with [], and nothing in the response
// says it was the last one — so the empty page is what stops the loop.
const s = stub([fullPage(1)]);
const result = await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.equal(result.action, 'created');
assert.equal(s.reads.length, 2);
});
test('paginates from the first page in the documented page size', async () => {
const s = stub([[human(1)]]);
await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.equal(s.reads[0], `/repos/${REPO}/issues/${PR}/comments?per_page=100&page=1`);
});
test('a marker planted by a human is skipped on every page', async () => {
// Pagination widens the search, so it also widens the reach of a planted
// marker. The bot check has to hold on the later pages too.
const planted = {id: 3, body: `${MARKER} overwrite me`, user: {type: 'User', login: 'attacker'}};
const s = stub([[...fullPage(1).slice(0, 99), planted], [human(101), bot(500)]]);
const result = await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.deepEqual(result, {action: 'updated', id: 500});
});
test('a marker planted by a different bot is skipped', async () => {
const planted = {
id: 3,
body: `${MARKER} overwrite me`,
user: {type: 'Bot', login: 'some-other[bot]'},
};
const s = stub([[planted, bot(500)]]);
const result = await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.deepEqual(result, {action: 'updated', id: 500});
});
test('a human marker alone yields a new comment rather than an edit', async () => {
const planted = {id: 3, body: `${MARKER} overwrite me`, user: {type: 'User', login: 'attacker'}};
const s = stub([[human(1), planted]]);
const result = await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.equal(result.action, 'created');
});
test('comments without a body do not throw', async () => {
// The API omits body when the caller lacks permission to read it.
const s = stub([[{id: 1, user: {type: 'User'}}, {id: 2, user: null}, bot(3)]]);
const result = await upsertStickyComment(REPO, PR, {marker: MARKER, body: 'fresh', request: s.request});
assert.deepEqual(result, {action: 'updated', id: 3});
});
test('labels come back as bare names', async () => {
const request = async () => [{name: 'Docs/Needed', color: 'ededed'}, {name: 'release-note-none'}];
assert.deepEqual(await issueLabels(REPO, PR, {request}), ['Docs/Needed', 'release-note-none']);
});
test('adding a label posts one name', async () => {
const calls = [];
const request = async (path, opts) => calls.push({path, ...opts});
await addLabel(REPO, PR, 'Docs/Needed', {request});
assert.deepEqual(calls, [
{path: `/repos/${REPO}/issues/${PR}/labels`, method: 'POST', body: {labels: ['Docs/Needed']}},
]);
});
test('a label name with a slash is encoded into the delete path', async () => {
// Unencoded, the slash reads as another path segment and the delete misses.
const calls = [];
const request = async (path, opts) => calls.push({path, ...opts});
await removeLabel(REPO, PR, 'Docs/Needed', {request});
assert.deepEqual(calls, [{path: `/repos/${REPO}/issues/${PR}/labels/Docs%2FNeeded`, method: 'DELETE'}]);
});
test('removing a label a human already removed is not an error', async () => {
const request = async () => {
throw new Error(`GitHub DELETE /x -> 404: {"message":"Label does not exist"}`);
};
await removeLabel(REPO, PR, 'Docs/Needed', {request});
});
test('any other failure to remove a label still throws', async () => {
const request = async () => {
throw new Error(`GitHub DELETE /x -> 403: {"message":"Resource not accessible"}`);
};
await assert.rejects(() => removeLabel(REPO, PR, 'Docs/Needed', {request}), /403/);
});
test('gh errors carry the response status and headers', async (t) => {
t.mock.method(globalThis, 'fetch', async () =>
new Response('slow down', {status: 429, headers: {'retry-after': '5'}}),
);
await assert.rejects(gh('/x'), (e) => {
assert.match(e.message, /-> 429: slow down/);
assert.equal(e.status, 429);
assert.equal(e.headers['retry-after'], '5');
return true;
});
});