Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(review-signal): a backfill reads each listed PR in full, and stops before the job token runs low (#9094)

Cindy Zhang6e39742e88d9
diff --git a/.github/scripts/review-signal-workflow.test.mjs b/.github/scripts/review-signal-workflow.test.mjs
index 0120918d79..05d8015d04 100644
--- a/.github/scripts/review-signal-workflow.test.mjs
+++ b/.github/scripts/review-signal-workflow.test.mjs
@@ -35,6 +35,7 @@ async function runSignal({
reviews = [],
reviewApp = '',
permission = 'write',
+ rateRemaining = 5000,
// The spec-owner job's decision for a PR that changes no spec record.
specStatuses = [
{
@@ -88,7 +89,8 @@ async function runSignal({
rest: {
pulls: {
get: vi.fn(async () => ({data: pr})),
- list: vi.fn(async () => ({data: [pr]})),
+ // Like GitHub's list endpoint, the listed PR omits changed_files.
+ list: vi.fn(async () => ({data: [{...pr, changed_files: undefined}]})),
listFiles: vi.fn(async () => ({
data: [{filename: 'README.md', status: 'modified'}],
})),
@@ -111,6 +113,15 @@ async function runSignal({
addLabels: mutations.addLabels,
removeLabel: mutations.removeLabel,
},
+ rateLimit: {
+ get: vi.fn(async () => ({
+ data: {
+ resources: {
+ core: {remaining: rateRemaining, limit: 5000, reset: 1793412000},
+ },
+ },
+ })),
+ },
checks: {
listForRef: async () => ({data: {check_runs: []}}),
update: mutations.updateCheck,
@@ -206,15 +217,32 @@ describe('review-signal trusted helper ref', () => {
description: 'Waiting on code review: community contribution',
});
if (options.eventName === 'workflow_dispatch') {
- expect(
- h.github.rest.pulls[options.backfill ? 'list' : 'get'],
- ).toHaveBeenCalledOnce();
+ // A backfill lists the open PRs, then reads each in full.
+ if (options.backfill) {
+ expect(h.github.rest.pulls.list).toHaveBeenCalledOnce();
+ }
+ expect(h.github.rest.pulls.get).toHaveBeenCalledOnce();
} else {
expect(h.github.rest.pulls.get).not.toHaveBeenCalled();
}
},
);
+ it('stops a backfill before the token runs low, and says so', async () => {
+ const h = await runSignal({
+ eventName: 'workflow_dispatch',
+ backfill: true,
+ rateRemaining: 1000,
+ });
+ expect(h.core.setFailed).toHaveBeenCalledExactlyOnceWith(
+ expect.stringContaining(
+ 'Backfill stopped before PR #42 (1 left): 1000 of 5000',
+ ),
+ );
+ expect(h.github.rest.pulls.get).not.toHaveBeenCalled();
+ expect(h.mutations.createCommitStatus).not.toHaveBeenCalled();
+ });
+
it.each(['workflow_dispatch', 'pull_request_target'])(
'fails closed if the helper is missing at the workflow SHA for %s',
async eventName => {
diff --git a/.github/workflows/review-signal.yml b/.github/workflows/review-signal.yml
index e50a00a709..647a22a8d0 100644
--- a/.github/workflows/review-signal.yml
+++ b/.github/workflows/review-signal.yml
@@ -153,12 +153,37 @@ jobs:
}
let hadError = false;
- for (const pr of prs) {
+ const isBackfill = prs.length > 1 ||
+ (context.eventName === 'workflow_dispatch' &&
+ !(context.payload.inputs.pr || '').trim());
+ for (const [index, listed] of prs.entries()) {
+ if (isBackfill) {
+ // A backfill makes several calls per PR on the job token's
+ // hourly budget. Stop with headroom left for PR events
+ // (reading /rate_limit costs nothing).
+ const {data: limits} = await github.rest.rateLimit.get();
+ const {remaining, limit, reset} = limits.resources.core;
+ if (remaining < Math.min(1500, Math.floor(limit / 3))) {
+ core.setFailed(
+ `Backfill stopped before PR #${listed.number} (${prs.length - index} left): ` +
+ `${remaining} of ${limit} API requests remain until ` +
+ `${new Date(reset * 1000).toISOString()}; re-run after the reset.`,
+ );
+ return;
+ }
+ }
try {
+ // The list endpoint omits changed_files, which the file check
+ // below needs, so a backfill reads each PR in full.
+ const pr = listed.changed_files == null
+ ? (await github.rest.pulls.get({
+ owner, repo, pull_number: listed.number,
+ })).data
+ : listed;
await flagOne(pr);
} catch (e) {
hadError = true;
- core.warning(`Failed to flag PR #${pr.number}: ${e.message}`);
+ core.warning(`Failed to flag PR #${listed.number}: ${e.message}`);
}
}
if (hadError) core.setFailed('One or more PRs failed to flag; see warnings.');