0
0
#!/usr/bin/env bash
# ci-check-wait.sh [--once] <workflow> <commit> [<ref>]: the knotless check of <workflow> at <commit> on <ref>
# (refs/heads/main), read from the public records, no credential and no knotless CLI: the repository's record names its
# key and its runners, and a runner keeps the check in its own repo as dev.knotless.check/<owner hash>.<key>.<ref
# hash>.<commit>.<workflow> (knotless crates/knotless/src/keys.rs check_rkey; a hash is 26 symbols of the base32 SHA-256).
# (no flag) waits while the check is queued or running, a line each time its status changes; exit 0 on a success that
# ran, 1 on any other end (failure, error, cancelled, skipped) and when no runner has a check there (it never
# ran). A workflow none of whose `paths` the push changed is filed `success` with a `skipped` field and no
# step run (knotless-run run_at): that is `success-skipped` here, exit 1 (nothing was tested). It has no
# time of its own: the step that calls it has
# --once one read: the status now (or `none`), exit 0
# promote.yml's first step (`scripts/promote.sh test`, before the release step moves channel rc) calls it: channel rc
# moves and stable can move only when the same commit's `test` ran green (plan 0040 decision 28; in series since 2026-11-19).
set -euo pipefail
once=0
if [ "${1:-}" = --once ]; then once=1; shift; fi
workflow=${1:?usage: ci-check-wait.sh [--once] <workflow> <commit> [<ref>]} commit=${2:?commit} ref=${3:-refs/heads/main}
owner=did:plc:gmlkrhimoueejcertevr4hl3 name=socialweb-computer-kcp
pds_of() { curl -fsSL --retry 3 "https://plc.directory/$1" | jq -r '.service[] | select(.id == "#atproto_pds") | .serviceEndpoint'; }
hash26() { printf "$(printf %s "$1" | sha256sum | cut -c1-34 | sed 's/../\\x&/g')" | base32 | tr A-Z a-z | cut -c1-26; }
repo=$(curl -fsSL --retry 3 "$(pds_of "$owner")/xrpc/com.atproto.repo.getRecord?repo=$owner&collection=dev.knotless.repo&rkey=$name")
key=$(jq -r '.value.key' <<<"$repo")
rkey=$(hash26 "$owner").$key.$(hash26 "$ref").$commit.$workflow
runners=()
for did in $(jq -r '.value.runners[].did' <<<"$repo"); do runners+=("$did $(pds_of "$did")"); done
[ ${#runners[@]} -gt 0 ] || { echo "ci-check-wait: $name names no runner" >&2; exit 1; }
# the check's status on the first runner that has it; `none` when every runner answered and none has it; `unread` when a
# read failed in a way that may pass
status() {
local r did pds body code unread=0
for r in "${runners[@]}"; do
did=${r%% *} pds=${r#* }
body=$(curl -sS -m 20 -w '\n%{http_code}' "$pds/xrpc/com.atproto.repo.getRecord?repo=$did&collection=dev.knotless.check&rkey=$rkey" 2>/dev/null) || { unread=1; continue; }
code=${body##*$'\n'} body=${body%$'\n'*}
if [ "$code" = 200 ]; then jq -r 'if .value.status == "success" and .value.skipped != null then "success-skipped" else .value.status end' <<<"$body"; return; fi
[ "$code" = 400 ] && [ "$(jq -r '.error // ""' <<<"$body" 2>/dev/null)" = RecordNotFound ] || unread=1
done
if [ "$unread" = 1 ]; then echo unread; else echo none; fi
}
if [ "$once" = 1 ]; then echo "==> $workflow at $commit ($ref): $(status) now"; exit 0; fi
last=
while :; do
s=$(status)
[ "$s" = "$last" ] || echo "==> $workflow at $commit ($ref): $s"
last=$s
case $s in
success) exit 0 ;;
queued|running|unread) sleep 30 ;;
none) echo "==> $workflow never ran at $commit: no check of it on $ref" >&2; exit 1 ;;
success-skipped) echo "==> $workflow at $commit ran nothing: filed success because the push changed none of its paths (not green: nothing was tested)" >&2; exit 1 ;;
*) echo "==> $workflow at $commit ended $s" >&2; exit 1 ;;
esac
done