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
29 changes: 29 additions & 0 deletions .github/actions/review/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Review
description: >-
The required Review check of a pull request. It passes when the PR is ready
for review, every job the calling job needs succeeded and each listed
Netlify preview of the head built. A CloudCannon publication then gets
GitHub's auto-merge, which merges it once the required checks pass.
inputs:
needs:
description: The calling job's needs, as toJSON(needs).
required: true
netlify-checks:
description: JSON array of the Netlify status contexts the PR head must report as success.
default: "[]"
timeout-minutes:
description: How long to wait for the previews.
default: "15"
runs:
using: composite
steps:
- uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
env:
ACTION_PATH: ${{ github.action_path }}
NEEDS: ${{ inputs.needs }}
NETLIFY_CHECKS: ${{ inputs.netlify-checks }}
TIMEOUT_MINUTES: ${{ inputs.timeout-minutes }}
with:
script: |
const review = require(`${process.env.ACTION_PATH}/review.cjs`)
await review({github, context, core, env: process.env})
61 changes: 61 additions & 0 deletions .github/actions/review/review.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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.
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.');
// A skipped required check counts as passed, so a draft fails instead.
if (pull.draft) return core.setFailed('A draft is not reviewed. Mark the PR ready for review.');

// So does a skipped, cancelled or failed job, and a caller that names none.
const needs = JSON.parse(env.NEEDS || '{}');
if (!Object.keys(needs).length) return core.setFailed('Review needs the jobs it reviews, as toJSON(needs).');
const unfinished = Object.entries(needs).filter(([, job]) => job.result !== 'success')
.map(([name, job]) => `${name} (${job.result})`);
if (unfinished.length) return core.setFailed(`Did not succeed: ${unfinished.join(', ')}.`);

// Netlify reports each preview as a commit status of the head, often after
// the checks. A skipped preview reports success too.
const {owner, repo} = context.repo;
const sha = pull.head.sha;
const previews = JSON.parse(env.NETLIFY_CHECKS || '[]');
const minutes = Number(env.TIMEOUT_MINUTES || 15);
const deadline = now() + minutes * 60_000;
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;
const failed = previews.filter(name => ['error', 'failure'].includes(state(name)));
if (failed.length) return core.setFailed(`Preview failed: ${failed.join(', ')}.`);
const waiting = previews.filter(name => state(name) !== 'success');
if (!waiting.length) break;
if (now() > deadline) {
return core.setFailed(`Previews did not finish within ${minutes} minutes: ${waiting.join(', ')}. Re-run Review once they have.`);
}
core.info(`Waiting for ${waiting.join(', ')}`);
await sleep(15_000);
}

// CloudCannon's Publish button opens a same-repository PR from `preview`;
// GitHub merges it once the required checks pass. Without auto-merge it
// would wait unseen, so a refusal fails Review.
const publication = pull.user?.login === 'cloudcannon[bot]' && pull.head.ref === 'preview'
&& 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) {
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.`);
}
try {
await github.graphql(`mutation($id: ID!, $sha: GitObjectID!) {
enablePullRequestAutoMerge(input: {pullRequestId: $id, expectedHeadOid: $sha, mergeMethod: MERGE}) {
pullRequest { number }
}
}`, {id: pull.node_id, sha});
} catch (error) {
core.setFailed(`Auto-merge could not be turned on, so this publication would not merge: ${error.message}`);
}
};
21 changes: 19 additions & 2 deletions .github/workflows/ci-check.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
name: Check

on:
workflow_dispatch:
push:
branches: [main]
pull_request:
Expand All @@ -12,7 +11,7 @@ permissions:
contents: read

concurrency:
group: check-${{ github.event.pull_request.number || github.ref }}-${{ github.event.pull_request.draft && 'draft' || 'ready' }}
group: check-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: true

jobs:
Expand All @@ -22,3 +21,21 @@ jobs:
with:
install: false
command: node --test tests/*.test.mjs

review:
name: Review
if: ${{ always() && github.event_name == 'pull_request' }}
needs: [check]
runs-on: ubuntu-24.04
timeout-minutes: 5
permissions:
contents: read
statuses: read
steps:
# This repository tests the action of its own PR; other callers pin it.
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: ./.github/actions/review
with:
needs: ${{ toJSON(needs) }}
20 changes: 0 additions & 20 deletions .github/workflows/ci-ready.yml

This file was deleted.

Loading