| 1 | #!/usr/bin/env bash |
| 2 | # Succeed only when a completed, green, full ci.yml run exists for an exact |
| 3 | # commit, and print its URL. Exit 0 with a receipt, 1 without one (including |
| 4 | # any API failure), 2 on bad usage. |
| 5 | # |
| 6 | # Usage: require-full-ci-receipt.sh <owner/repo> <40-char sha> |
| 7 | # |
| 8 | # The receipt is a workflow_dispatch ci.yml run on this repository for <sha> |
| 9 | # that concluded success and whose latest job attempts show the full suite: |
| 10 | # every job succeeded or was skipped, no scoped or shared-process job ran, and |
| 11 | # Change detection, Lint and the three `Suite check` jobs succeeded. |
| 12 | # |
| 13 | # FULL_CI_RECEIPT_GH overrides the gh executable (tests only). |
| 14 | set -euo pipefail |
| 15 | |
| 16 | repo="${1:-}" |
| 17 | sha="${2:-}" |
| 18 | gh_bin="${FULL_CI_RECEIPT_GH:-gh}" |
| 19 | |
| 20 | if [[ -z "${repo}" || -z "${sha}" ]]; then |
| 21 | echo "usage: $0 <owner/repo> <sha>" >&2 |
| 22 | exit 2 |
| 23 | fi |
| 24 | if ! [[ "${repo}" =~ ^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$ ]]; then |
| 25 | echo "::error::Repository '${repo}' must be owner/name." >&2 |
| 26 | exit 2 |
| 27 | fi |
| 28 | if [[ "${#sha}" -ne 40 || "${sha}" =~ [^0-9a-f] ]]; then |
| 29 | echo "::error::SHA must be a full 40-character lowercase commit SHA, got '${sha}'." >&2 |
| 30 | exit 2 |
| 31 | fi |
| 32 | |
| 33 | repo_lower="$(printf '%s' "${repo}" | tr '[:upper:]' '[:lower:]')" |
| 34 | if ! runs="$("${gh_bin}" api \ |
| 35 | "repos/${repo}/actions/workflows/ci.yml/runs?head_sha=${sha}&event=workflow_dispatch&status=success&per_page=100" \ |
| 36 | --jq ".workflow_runs[] | select(.head_sha == \"${sha}\" and .event == \"workflow_dispatch\" and .status == \"completed\" and .conclusion == \"success\" and .path == \".github/workflows/ci.yml\" and ((.head_repository.full_name // \"\") | ascii_downcase) == \"${repo_lower}\") | [.id, .html_url] | @tsv")"; then |
| 37 | echo "No full CI receipt for ${sha}: the Actions API call failed." >&2 |
| 38 | exit 1 |
| 39 | fi |
| 40 | |
| 41 | jobs_verdict='. as $run |
| 42 | | def ok($name): any($run.jobs[]; .name == $name and .conclusion == "success"); |
| 43 | if (.jobs | length) != .total_count then "incomplete job list" |
| 44 | elif any(.jobs[]; .conclusion != "success" and .conclusion != "skipped") then "a job did not succeed" |
| 45 | elif any(.jobs[]; (.name | startswith("Scoped test")) and .conclusion != "skipped") then "scoped run" |
| 46 | elif any(.jobs[]; .name == "Shared-process tests" and .conclusion != "skipped") then "shared-process run" |
| 47 | elif ([ |
| 48 | "Change detection", |
| 49 | "Lint", |
| 50 | "Sharded tests (ubuntu-latest) / Suite check", |
| 51 | "Sharded tests (macos-latest) / Suite check", |
| 52 | "Sharded tests (windows-latest) / Suite check" |
| 53 | ] | all(.[]; ok(.))) | not then "a full-suite job did not succeed" |
| 54 | else "full" |
| 55 | end' |
| 56 | |
| 57 | while IFS=$'\t' read -r run_id run_url; do |
| 58 | [[ -n "${run_id}" ]] || continue |
| 59 | if ! [[ "${run_id}" =~ ^[0-9]+$ ]]; then |
| 60 | echo "No full CI receipt for ${sha}: unexpected run id '${run_id}' from the Actions API." >&2 |
| 61 | exit 1 |
| 62 | fi |
| 63 | if ! verdict="$("${gh_bin}" api \ |
| 64 | "repos/${repo}/actions/runs/${run_id}/jobs?filter=latest&per_page=100" \ |
| 65 | --jq "${jobs_verdict}")"; then |
| 66 | echo "Run ${run_url} not counted: its job list could not be read." >&2 |
| 67 | continue |
| 68 | fi |
| 69 | if [[ "${verdict}" == "full" ]]; then |
| 70 | echo "Full CI receipt for ${sha}: ${run_url}" |
| 71 | exit 0 |
| 72 | fi |
| 73 | echo "Run ${run_url} is green but not a full-suite receipt (${verdict})." >&2 |
| 74 | done <<< "${runs}" |
| 75 | |
| 76 | echo "No completed, green, full ci.yml run for ${sha}." >&2 |
| 77 | exit 1 |
| 78 |