Wait for a pull request's checks with fj pr checks --watch (CLS-1328) #340

Merged
stephen merged 3 commits from stephen/cls-1328-pr-checks-watch into main 2026-11-28 17:28:37 +00:00
Owner

Adds fj pr checks N --watch for CLS-1328. A self-hosted user asked for a way to block a script on a pull request's CI and get the outcome back. Today that means hand-rolling a loop over fj pr checks.

The command waits for every check on the head commit. Each way the wait can end has its own exit code:

Exit outcome When
0 success Every check passed, and no new run or status appeared for --grace.
1 failed A check failed, was cancelled or could not queue. The wait ends on the first one.
6 timeout --timeout (default 30m) passed with checks still pending.
7 no_checks Nothing reported within --grace (default 2m).

--json prints the outcome field on every exit path, so a script can tell a failed check from fj failing (both exit 1, which keeps gh parity).

What counts as a check:

  • the jobs of the newest Actions run per workflow and event, keyed by job id, so matrix legs that share a name are separate checks and a job rerun in place counts by its latest attempt;
  • commit statuses from external CI;
  • a run with no jobs, as a single check.

Skipped jobs count as passing, and the summary says how many there were.

A failure ends the wait at once. A pass waits until the head has been watched for --grace and no new run or status has appeared for --grace. Forgejo creates a pull request's runs after its test merge, so under on: [push, pull_request] the push run can be green before the pull_request runs exist. The cost is that a pass takes at least --grace (default 2m), even on a pull request that is already green. If not every run for the commit could be read, the wait never passes and says why.

Differences from gh:

  • Fail-fast is always on. --fail-fast is accepted and ignored, so gh-shaped scripts don't trip on an unknown flag.
  • --timeout and --grace are new.
  • --interval takes seconds or 30s/2m.

Behavior while waiting:

  • Server errors, rate limits, network failures, a body that is markup rather than JSON (a proxy's error page) and a lost later page of runs are retried with backoff until the timeout. Only not-found (3) and auth (4) on the pull request, and JSON that does not fit the expected shape (1), end the wait early.
  • --timeout no longer than --grace is a usage error (2), since a pass could never come.
  • A 404 or 403 on the run list falls back to commit statuses.
  • If the head moves, the wait restarts and says so.
  • Both outputs name the SHA that was finally judged.
  • Runs are read at most 4 pages per poll.
  • Progress lines go to stderr when the counts change. There is no spinner, and nothing is printed on that channel with --json.

Every exit, errors included, prints each check's status, name, run number and elapsed time. A missing run is shown as (none). Plain-words notes cover:

  • jobs stuck in waiting (no matching runner);
  • runs awaiting approval;
  • workflows that exist but started nothing for the commit.

Tests: wiremock cases cover delayed enqueue across several runs and jobs, fail-fast, cancelled, never-enqueued until timeout, no checks within grace, a moved head, a retried 503, a missing PR, a server that ignores the head_sha filter, a green run followed by a late failing one, an incomplete run list, a run rerun mid-wait, a lost later page, a markup body (retried), JSON of the wrong shape (fails fast) and a 403 on runs. There are also unit tests for judging, jobs sharing a name, rerun attempts, status dedupe and duration parsing. scripts/e2e-smoke.sh gets a bounded read-only wait on the test repo's newest PR. It accepts any outcome but requires the wait to report one.

I also ran it live against this repo: #337 exited 0 with run #672's three jobs and their times, matching fj run view, and an open PR exited 6 at the timeout. The live run found that Forgejo sends both event and trigger_event, which a serde alias had collided on. That is fixed, and the fixtures now carry both fields.

Docs: help text, docs/exit-codes.md (new section), docs/gh-to-fj.md and CHANGELOG.

Clippy, fmt and the touched tests were run locally. The full suite ran in the pre-push hook.

Follow-up, not in this PR: when the run list is incomplete, fall back to commit statuses instead of never passing.

Adds `fj pr checks N --watch` for CLS-1328. A self-hosted user asked for a way to block a script on a pull request's CI and get the outcome back. Today that means hand-rolling a loop over `fj pr checks`. The command waits for every check on the head commit. Each way the wait can end has its own exit code: | Exit | `outcome` | When | | ---: | --- | --- | | 0 | `success` | Every check passed, and no new run or status appeared for `--grace`. | | 1 | `failed` | A check failed, was cancelled or could not queue. The wait ends on the first one. | | 6 | `timeout` | `--timeout` (default `30m`) passed with checks still pending. | | 7 | `no_checks` | Nothing reported within `--grace` (default `2m`). | `--json` prints the `outcome` field on every exit path, so a script can tell a failed check from fj failing (both exit 1, which keeps `gh` parity). What counts as a check: - the jobs of the newest Actions run per workflow and event, keyed by job id, so matrix legs that share a name are separate checks and a job rerun in place counts by its latest attempt; - commit statuses from external CI; - a run with no jobs, as a single check. Skipped jobs count as passing, and the summary says how many there were. A failure ends the wait at once. A pass waits until the head has been watched for `--grace` and no new run or status has appeared for `--grace`. Forgejo creates a pull request's runs after its test merge, so under `on: [push, pull_request]` the push run can be green before the pull_request runs exist. The cost is that a pass takes at least `--grace` (default `2m`), even on a pull request that is already green. If not every run for the commit could be read, the wait never passes and says why. Differences from `gh`: - Fail-fast is always on. `--fail-fast` is accepted and ignored, so gh-shaped scripts don't trip on an unknown flag. - `--timeout` and `--grace` are new. - `--interval` takes seconds or `30s`/`2m`. Behavior while waiting: - Server errors, rate limits, network failures, a body that is markup rather than JSON (a proxy's error page) and a lost later page of runs are retried with backoff until the timeout. Only not-found (3) and auth (4) on the pull request, and JSON that does not fit the expected shape (1), end the wait early. - `--timeout` no longer than `--grace` is a usage error (2), since a pass could never come. - A 404 or 403 on the run list falls back to commit statuses. - If the head moves, the wait restarts and says so. - Both outputs name the SHA that was finally judged. - Runs are read at most 4 pages per poll. - Progress lines go to stderr when the counts change. There is no spinner, and nothing is printed on that channel with `--json`. Every exit, errors included, prints each check's status, name, run number and elapsed time. A missing run is shown as `(none)`. Plain-words notes cover: - jobs stuck in `waiting` (no matching runner); - runs awaiting approval; - workflows that exist but started nothing for the commit. Tests: wiremock cases cover delayed enqueue across several runs and jobs, fail-fast, cancelled, never-enqueued until timeout, no checks within grace, a moved head, a retried 503, a missing PR, a server that ignores the `head_sha` filter, a green run followed by a late failing one, an incomplete run list, a run rerun mid-wait, a lost later page, a markup body (retried), JSON of the wrong shape (fails fast) and a 403 on runs. There are also unit tests for judging, jobs sharing a name, rerun attempts, status dedupe and duration parsing. `scripts/e2e-smoke.sh` gets a bounded read-only wait on the test repo's newest PR. It accepts any outcome but requires the wait to report one. I also ran it live against this repo: #337 exited 0 with run #672's three jobs and their times, matching `fj run view`, and an open PR exited 6 at the timeout. The live run found that Forgejo sends both `event` and `trigger_event`, which a serde alias had collided on. That is fixed, and the fixtures now carry both fields. Docs: help text, `docs/exit-codes.md` (new section), `docs/gh-to-fj.md` and CHANGELOG. Clippy, fmt and the touched tests were run locally. The full suite ran in the pre-push hook. Follow-up, not in this PR: when the run list is incomplete, fall back to commit statuses instead of never passing.
Wait for a pull request's checks with fj pr checks --watch (CLS-1328)
All checks were successful
ci / check (pull_request) Successful in 16m30s
ci / live-e2e (pull_request) Successful in 3m19s
ci / coverage (pull_request) Successful in 3m46s
867c60f8c2
`fj pr checks N --watch` polls the head commit until its checks settle and
exits 0 when all pass, 1 on the first failure, 6 on --timeout and 7 when
nothing reports within --grace. --json carries an `outcome` field on every
exit path so a failed check can be told from fj itself failing.

Checks are the jobs of the newest Actions run per workflow and event, plus
commit statuses from external CI. Reruns count by their latest attempt,
skipped jobs pass, a moved head restarts the wait, and server or network
errors are retried until the timeout.

Signed-off-by: Stephen Way <stephen@rasterstate.com>
pr checks --watch: wait out late runs before passing, count jobs by id
Some checks failed
ci / check (pull_request) Has been cancelled
ci / coverage (pull_request) Has been cancelled
ci / live-e2e (pull_request) Has been cancelled
a179ccd58d
A pass now needs the head to have been watched for --grace and no new run
or status for another --grace. Forgejo creates a pull request's runs after
its test merge, so under `on: [push, pull_request]` the push run could be
green and the wait would pass before the pull_request runs existed.

Also from review:
- Jobs are keyed by id, not name. Forgejo reruns a job in place, so matrix
  legs sharing a name are separate checks.
- An incomplete run list (page cap, or a server ignoring the commit filter)
  never passes and says so.
- The finished-run cache is used only while the run is still finished, so
  a rerun mid-wait is read again.
- Text output prints the summary on every exit path, errors included.
- A 404 or 403 on a later page of runs, and responses that do not decode,
  are retried. A 403 on the first page falls back to commit statuses, as
  a 404 does.

Signed-off-by: Stephen Way <stephen@rasterstate.com>
pr checks --watch: fail fast on schema mismatches, reject timeout <= grace
All checks were successful
ci / check (pull_request) Successful in 16m45s
ci / live-e2e (pull_request) Successful in 3m26s
ci / coverage (pull_request) Successful in 3m53s
bed1ed8223
A response that fails to decode is now retried only when the body is
markup rather than JSON: an HTML content type or a leading `<`, which is
what a proxy's error page looks like. `Client::json` reads the body and
tags that case with `NonJsonBody` under "decoding JSON response". JSON
that does not fit the expected shape is a schema mismatch no retry will
fix, so it ends the wait with exit 1.

`--timeout` no longer than `--grace` is a usage error (exit 2) at parse
time, since a pass needs `--grace` with no new run and could never come.

The help and docs say a pass needs `--grace` since the last arrival,
dropping the misleading "another".

Signed-off-by: Stephen Way <stephen@rasterstate.com>
Sign in to join this conversation.
No description provided.