Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions .github/actions/review/review.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// The Review check (action.yml). It runs as a job of the pull request's own
// workflow, so a new push cancels it and a rerun resets it.
// workflow, and a rerun resets it. Its `always()` keeps it running when a new
// push cancels the run, and the new run waits for it in the concurrency group,
// so it stops waiting once the PR moved on.
module.exports = async ({github, context, core, env, sleep = ms => new Promise(resolve => setTimeout(resolve, ms)), now = Date.now}) => {
const pull = context.payload.pull_request;
if (!pull) throw new Error('Review runs on pull_request events only.');
Expand All @@ -20,6 +22,7 @@ module.exports = async ({github, context, core, env, sleep = ms => new Promise(r
const previews = JSON.parse(env.NETLIFY_CHECKS || '[]');
const minutes = Number(env.TIMEOUT_MINUTES || 15);
const deadline = now() + minutes * 60_000;
const current = async () => (await github.rest.pulls.get({owner, repo, pull_number: pull.number})).data;
for (;;) {
const {data} = await github.rest.repos.getCombinedStatusForRef({owner, repo, ref: sha, per_page: 100});
const state = name => data.statuses.find(status => status.context === name)?.state;
Expand All @@ -30,6 +33,13 @@ module.exports = async ({github, context, core, env, sleep = ms => new Promise(r
if (now() > deadline) {
return core.setFailed(`Previews did not finish within ${minutes} minutes: ${waiting.join(', ')}. Re-run Review once they have.`);
}
// A newer push, a draft or a closed PR starts no preview of this head
// that this run should wait for; the PR's newer run reviews it. Without
// `pull-requests: read`, or when GitHub fails the read, Review waits on.
const pr = await current().catch(error => core.info(`Could not read the PR: ${error.message}`));
if (pr && (pr.state !== 'open' || pr.draft || pr.head.sha !== sha)) {
return core.setFailed('The PR changed while Review waited for its previews; a newer run reviews it.');
}
core.info(`Waiting for ${waiting.join(', ')}`);
await sleep(15_000);
}
Expand All @@ -41,13 +51,13 @@ module.exports = async ({github, context, core, env, sleep = ms => new Promise(r
&& pull.head.repo?.full_name === `${owner}/${repo}`;
if (!publication) return;
// The PR may have changed while Review waited for the previews.
const {data: current} = await github.rest.pulls.get({owner, repo, pull_number: pull.number});
if (current.state !== 'open' || current.draft || current.head.sha !== sha || current.base.ref !== pull.base.ref) {
const pr = await current();
if (pr.state !== 'open' || pr.draft || pr.head.sha !== sha || pr.base.ref !== pull.base.ref) {
return core.setFailed('The PR changed while Review waited. Re-run Review.');
}
if (current.auto_merge) {
if (current.auto_merge.merge_method === 'merge') return;
return core.setFailed(`Auto-merge is on as ${current.auto_merge.merge_method}; a publication from preview merges with a merge commit.`);
if (pr.auto_merge) {
if (pr.auto_merge.merge_method === 'merge') return;
return core.setFailed(`Auto-merge is on as ${pr.auto_merge.merge_method}; a publication from preview merges with a merge commit.`);
}
try {
await github.graphql(`mutation($id: ID!, $sha: GitObjectID!) {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ After a push to main, Check runs source checks and unit/service tests again, Net

Other repositories retain their existing `check:source`, `check:build` and Test caller contracts until explicitly migrated. The reusable defaults remain compatible with them. Non-Node repositories keep their existing language-specific check command with dependency installation disabled.

Organization rules require `Check / Check` and `Review`, without requiring synchronization with main. `Review` is the last job of the PR workflow, named `Review` and calling [the review action](.github/actions/review). It needs every other job of the run and fails when one failed, was cancelled or was skipped, and on a draft, since GitHub counts a skipped required check as passed. It then waits for every `netlify-checks` preview of the PR head, so no rule names a site. A new push cancels the run and a rerun resets it. A workflow with `Review` has no `workflow_dispatch`: a skipped `Review` on a PR head would pass. The PR's own workflow file runs, so a PR that changes it changes its own gate; such a PR needs an independent review before it merges. Skipped URL work passes only for an explicitly empty selection. Independent agent review owns code assessment.
Organization rules require `Check / Check` and `Review`, without requiring synchronization with main. `Review` is the last job of the PR workflow, named `Review` and calling [the review action](.github/actions/review). It needs every other job of the run and fails when one failed, was cancelled or was skipped, and on a draft, since GitHub counts a skipped required check as passed. It then waits for every `netlify-checks` preview of the PR head, so no rule names a site. A new push cancels the run and a rerun resets it. Review's `always()` keeps it running after that cancel, and the newer run waits for it in the concurrency group, so Review stops waiting once the PR's head moved on or the PR became a draft or closed. That needs `pull-requests: read` on the Review job; without it Review waits on. A workflow with `Review` has no `workflow_dispatch`: a skipped `Review` on a PR head would pass. The PR's own workflow file runs, so a PR that changes it changes its own gate; such a PR needs an independent review before it merges. Skipped URL work passes only for an explicitly empty selection. Independent agent review owns code assessment.

Jobs that mostly wait, such as Test, Test live and Review, run on Blacksmith's ARM runners in private repositories.

Expand Down
22 changes: 20 additions & 2 deletions tests/review.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const review = createRequire(import.meta.url)('../.github/actions/review/review.

// One Review run against canned statuses: `statuses` gives the head's
// statuses per poll, the last one repeating.
async function run({needs = {check: {result: 'success'}}, previews = [], statuses = [[]], pull = {}, current = {}, autoMerge = null, refuse} = {}) {
async function run({needs = {check: {result: 'success'}}, previews = [], statuses = [[]], pull = {}, current = {}, autoMerge = null, refuse, unreadable} = {}) {
const failures = [];
const merges = [];
let polls = 0;
Expand All @@ -17,7 +17,10 @@ async function run({needs = {check: {result: 'success'}}, previews = [], statuse
assert.equal(ref, 'head');
return {data: {statuses: statuses[Math.min(polls++, statuses.length - 1)]}};
}},
pulls: {get: async () => ({data: {state: 'open', draft: false, head: {sha: 'head'}, base: {ref: 'main'}, auto_merge: autoMerge, ...current}})},
pulls: {get: async () => {
if (unreadable) throw new Error('Resource not accessible by integration');
return {data: {state: 'open', draft: false, head: {sha: 'head'}, base: {ref: 'main'}, auto_merge: autoMerge, ...current}};
}},
},
graphql: async (query, variables) => {
if (refuse) throw new Error(refuse);
Expand Down Expand Up @@ -60,6 +63,21 @@ test('Review waits for the previews of the head and fails on a failed or missing
assert.match(missing.failures[0], /did not finish within 1 minutes/);
});

test('Review stops waiting once the PR moved on, so the newer run is not held up', async () => {
const pending = [[{context: preview, state: 'pending'}]];
for (const current of [{head: {sha: 'newer'}}, {draft: true}, {state: 'closed'}]) {
const stopped = await run({previews: [preview], statuses: pending, current});
assert.match(stopped.failures[0], /newer run reviews it/);
assert.equal(stopped.polls, 1);
}
// Without `pull-requests: read`, Review waits for the previews as before.
const unreadable = await run({previews: [preview], statuses: [...pending, [{context: preview, state: 'success'}]], unreadable: true});
assert.deepEqual(unreadable.failures, []);
// A retargeted PR starts no new run, so Review keeps waiting.
const retargeted = await run({previews: [preview], statuses: pending, current: {base: {ref: 'other'}}});
assert.match(retargeted.failures[0], /did not finish within 1 minutes/);
});

test('Review turns on auto-merge only for a passed CloudCannon publication from the same repository', async () => {
assert.deepEqual((await run({pull: cloudcannon})).merges, [{id: 'PR1', sha: 'head'}]);
assert.deepEqual((await run({pull: cloudcannon, autoMerge: {merge_method: 'merge'}})).merges, []);
Expand Down