Skip to content

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

TypeScript

fix(ci): load the review-clear decision helper from the workflow's own commit, not the PR base (#7108)

Cindy Zhang44f7fdac9138
diff --git a/.github/scripts/review-clear-workflow.test.mjs b/.github/scripts/review-clear-workflow.test.mjs
index cc3419d588..e948299226 100644
--- a/.github/scripts/review-clear-workflow.test.mjs
+++ b/.github/scripts/review-clear-workflow.test.mjs
@@ -26,6 +26,11 @@ const execute = new Function(
`return (async () => {\n${script}\n})();`,
);
+// The workflow's own commit (the default branch's head), and a PR base older
+// than the decision helper.
+const trustedSha = 'cccccccccccccccccccccccccccccccccccccccc';
+const oldBaseSha = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
+const helperPath = '.github/scripts/review-signal-decision.cjs';
const head1 = '1111111111111111111111111111111111111111';
const head2 = '2222222222222222222222222222222222222222';
const head3 = '3333333333333333333333333333333333333333';
@@ -50,6 +55,7 @@ const ungatedSuccess = gateStatus('success', 'No code review required.');
function harness(
reviews,
{
+ helperAt = [trustedSha],
labels = ['needs:code-review'],
moveAfterFirstRead = false,
statusFailure = false,
@@ -61,7 +67,7 @@ function harness(
const full = {
number: 17,
head: {sha: head2},
- base: {sha: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'},
+ base: {sha: oldBaseSha},
labels: labels.map(name => ({name})),
};
const methods = {
@@ -87,6 +93,9 @@ function harness(
repos: {
getContent: async ({ref, path: filePath}) => {
calls.push(`read:${ref}:${filePath}`);
+ if (!helperAt.includes(ref)) {
+ throw Object.assign(new Error('Not Found'), {status: 404});
+ }
return {
data: {
type: 'file',
@@ -113,6 +122,7 @@ function harness(
};
const context = {
repo: {owner: 'facebook', repo: 'astryx'},
+ sha: trustedSha,
payload: {
workflow_run: {
pull_requests: [{number: 17}],
@@ -121,6 +131,7 @@ function harness(
};
const core = {
info: message => calls.push(`info:${message}`),
+ setFailed: message => calls.push(`failed:${message}`),
warning: message => calls.push(`warning:${message}`),
};
const processValue = {env: {ENG_OWNERS: '@engineer'}};
@@ -143,9 +154,39 @@ describe('review-clear exact-head workflow', () => {
await run(h);
+ expect(mutations(h.calls)).toEqual([]);
+ expect(h.calls).toContain(`read:${trustedSha}:${helperPath}`);
+ });
+
+ it('resolves the gate when the PR base predates the decision helper', async () => {
+ // The helper exists only at the workflow's own commit, as for a PR
+ // whose base is older than the helper.
+ const h = harness([review('APPROVED')], {helperAt: [trustedSha]});
+
+ await run(h);
+
+ expect(h.calls).not.toContain(`read:${oldBaseSha}:${helperPath}`);
+ expect(h.calls).toContainEqual(
+ expect.objectContaining({type: 'remove-label'}),
+ );
+ expect(h.calls).toContainEqual({
+ type: 'status',
+ input: expect.objectContaining({
+ sha: head2,
+ state: 'success',
+ description: 'Cleared by code-owner approval.',
+ }),
+ });
+ });
+
+ it('fails clearly, without touching the gate, when the decision helper is missing', async () => {
+ const h = harness([review('APPROVED')], {helperAt: []});
+
+ await run(h);
+
expect(mutations(h.calls)).toEqual([]);
expect(h.calls).toContain(
- 'read:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:.github/scripts/review-signal-decision.cjs',
+ `failed:Cannot resolve the code gate for PR #17: ${helperPath} could not be loaded at ${trustedSha} (not found).`,
);
});
diff --git a/.github/workflows/review-clear.yml b/.github/workflows/review-clear.yml
index 5e502b53da..21e67e0e21 100644
--- a/.github/workflows/review-clear.yml
+++ b/.github/workflows/review-clear.yml
@@ -13,7 +13,8 @@
# (read-only token → 403). It therefore runs on workflow_run after Review signal
# anchors approved, changes_requested, and dismissed review events from the base
# repository with a write-capable token. It never checks out or runs
-# PR-controlled code.
+# PR-controlled code: its decision helper comes from this workflow's own trusted
+# commit, never from the PR.
name: Review clear
@@ -109,6 +110,33 @@ jobs:
return;
}
+ // Load the decision helper from this workflow's own commit — for a
+ // workflow_run, the default branch's head — the same trusted version
+ // review-signal runs. A PR's base can predate the helper, and the
+ // PR's own code is never fetched or run. Without the helper the gate
+ // cannot be resolved, so the run fails loudly rather than leaving
+ // the gate silently pending.
+ const DECISION_HELPER = '.github/scripts/review-signal-decision.cjs';
+ let decision;
+ try {
+ const { data: decisionFile } = await github.rest.repos.getContent({
+ owner, repo, ref: context.sha, path: DECISION_HELPER,
+ });
+ const decisionSrc = Buffer.from(decisionFile.content, 'base64').toString('utf8');
+ const decisionModule = { exports: {} };
+ new Function('module', 'exports', 'require', decisionSrc)(
+ decisionModule, decisionModule.exports,
+ () => { throw new Error('review-signal-decision.cjs must stay dependency-free'); },
+ );
+ decision = decisionModule.exports;
+ } catch (e) {
+ const reason = e.status === 404 ? 'not found' : e.message;
+ core.setFailed(
+ `Cannot resolve the code gate for PR #${prNumber}: ${DECISION_HELPER} could not be loaded at ${context.sha} (${reason}).`,
+ );
+ return;
+ }
+
// Resolve the effective exact-head review state with the same
// dependency-free helper review-signal uses. Also read this head's
// latest review-required status: only the existing label or a latest
@@ -120,18 +148,8 @@ jobs:
const reviews = await github.paginate(github.rest.pulls.listReviews, {
owner, repo, pull_number: prNumber, per_page: 100,
});
- const { data: decisionFile } = await github.rest.repos.getContent({
- owner, repo, ref: full.base.sha,
- path: '.github/scripts/review-signal-decision.cjs',
- });
- const decisionSrc = Buffer.from(decisionFile.content, 'base64').toString('utf8');
- const decisionModule = { exports: {} };
- new Function('module', 'exports', 'require', decisionSrc)(
- decisionModule, decisionModule.exports,
- () => { throw new Error('review-signal-decision.cjs must stay dependency-free'); },
- );
({codeApproved, codeWithdrawn} =
- decisionModule.exports.resolveReviewApprovals({
+ decision.resolveReviewApprovals({
reviews,
headSha,
engOwners: [...codeApprovers],