Skip to content
Closed
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
77 changes: 77 additions & 0 deletions .github/workflows/required.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
# The one status check branch protection requires,

Check failure on line 2 in .github/workflows/required.yml

View workflow job for this annotation

GitHub Actions / dogfood

removable line comment
# identical in every P4suta repository.

Check failure on line 3 in .github/workflows/required.yml

View workflow job for this annotation

GitHub Actions / dogfood

removable line comment
# It waits until every other check on the head commit has finished,

Check failure on line 4 in .github/workflows/required.yml

View workflow job for this annotation

GitHub Actions / dogfood

removable line comment
# then fails if any of them failed,

Check failure on line 5 in .github/workflows/required.yml

View workflow job for this annotation

GitHub Actions / dogfood

removable line comment
# so the ruleset never has to list a repository's own job names.

Check failure on line 6 in .github/workflows/required.yml

View workflow job for this annotation

GitHub Actions / dogfood

removable line comment
name: required

on:
pull_request:

permissions:
checks: read
statuses: read

concurrency:
group: required-${{ github.event.pull_request.number }}
cancel-in-progress: true

jobs:
required:
name: required
runs-on: ubuntu-latest
timeout-minutes: 300
steps:
- name: Wait for every other check, then fail if any failed
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
api="repos/${REPO}/commits/${SHA}"
runs_jq='.check_runs[] | select(.name != "required")
| "\(.name)\t\(.status)\t\(.conclusion // "")"'
status_jq='.statuses[] | "\(.context)\t\(.state)"'
# Checks register within seconds of the pull request event.
sleep 45
previous=""
while :; do
runs=$(gh api --paginate "${api}/check-runs?filter=latest" \
--jq "${runs_jq}" | sort)
statuses=$(gh api "${api}/status" --jq "${status_jq}" | sort)
running=$(printf '%s\n' "${runs}" \
| awk -F'\t' 'NF && $2 != "completed"' | wc -l)
queued=$(printf '%s\n' "${statuses}" \
| awk -F'\t' 'NF && $2 == "pending"' | wc -l)
pending=$((running + queued))
snapshot="${runs}"$'\n'"${statuses}"
# Finish only when nothing is running,
# and nothing new appeared since the last look.
if [[ "${pending}" -eq 0 ]]; then
if [[ "${snapshot}" == "${previous}" ]]; then
break
fi
previous=${snapshot}
else
previous=""
fi
echo "waiting: ${pending} check(s) still running"
sleep 30
done
ok='^(success|neutral|skipped)$'
failed=$(
printf '%s\n' "${runs}" \
| awk -F'\t' -v ok="${ok}" 'NF && $3 !~ ok {print $1}'
printf '%s\n' "${statuses}" \
| awk -F'\t' 'NF && $2 != "success" {print $1}'
)
echo "checks seen:"
printf '%s\n' "${runs}" | awk -F'\t' 'NF {print " " $1 ": " $3}'
printf '%s\n' "${statuses}" | awk -F'\t' 'NF {print " " $1 ": " $2}'
if [[ -n "${failed}" ]]; then
echo "::error::failed checks:"
printf ' %s\n' "${failed}"
exit 1
fi
Loading