Skip to content

Commit ecfe56d

Browse files
boneliferclaude
andauthored
pr-wait.py: wait for finished checks to settle before reporting (#31)
Right after a pull request is opened, GitHub can show only an earlier push run, already finished, before it adds the pull request's own run; pr-wait.py then reported "passed" too early. The result now counts only once the same finished checks have been seen for --settle seconds (default 30). Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4564f3b commit ecfe56d

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Waits until every check on a pull request has finished, then exits with a status
113113
| 5 | No check appeared on the latest commit within `--first-check-wait` (default 3 minutes) |
114114
| 6 | `gh` is not installed, or failed five times in a row (not logged in, no such pull request, no network) |
115115

116-
It asks GitHub every `--interval` seconds (default 30). Progress lines carry the time in UTC.
116+
It asks GitHub every `--interval` seconds (default 30). A result counts only once the same finished checks have been seen for `--settle` seconds (default 30), because GitHub can add a run a few seconds late, such as a pull request's own run after an earlier push run has already finished. Progress lines carry the time in UTC.
117117

118118
### Testing extensions and styles: `phpbb-test-board/`
119119

‎pr-wait.py‎

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
Polls the pull request with the GitHub CLI (`gh pr view`) every --interval
77
seconds. The checks followed are always those of the pull request's latest
88
commit, so a new push while waiting restarts the wait on the new commit.
9+
The result counts only once the same finished checks have been seen for
10+
--settle seconds: GitHub can take a few seconds to add a run (for example a
11+
pull request's own run after an earlier push run already finished).
912
Prints one line whenever the progress changes, and a summary at the end that
1013
lists every failed check with its link. Meant for a person or an agent that
1114
starts it in the background and acts on the exit status.
@@ -61,11 +64,14 @@ def parse_args(argv: list) -> argparse.Namespace:
6164
parser.add_argument('-w', '--first-check-wait', type=float, default=3,
6265
help='minutes to wait for the first check to appear on a commit '
6366
'before deciding the repository runs none (default: 3)')
67+
parser.add_argument('-s', '--settle', type=float, default=30,
68+
help='seconds the same finished checks must stay unchanged before the '
69+
'result counts, so a run GitHub adds late is not missed (default: 30)')
6470
if not argv:
6571
parser.print_help(sys.stderr)
6672
sys.exit(2)
6773
args = parser.parse_args(argv)
68-
for name in ('timeout', 'interval', 'first_check_wait'):
74+
for name in ('timeout', 'interval', 'first_check_wait', 'settle'):
6975
if not getattr(args, name) > 0:
7076
parser.error(f"--{name.replace('_', '-')} must be greater than 0")
7177
return args
@@ -131,6 +137,9 @@ def wait(args: argparse.Namespace) -> int:
131137
head = None
132138
head_seen_at = start
133139
last_progress = None
140+
# The finished checks last seen, and since when, for --settle
141+
finished = None
142+
finished_since = start
134143
gh_failures = 0
135144
waiting_for = 'an answer from gh'
136145
while True:
@@ -155,6 +164,7 @@ def wait(args: argparse.Namespace) -> int:
155164
head = data['headRefOid']
156165
head_seen_at = time.monotonic()
157166
last_progress = None
167+
finished = None
158168

159169
checks = [classify(check) for check in data.get('statusCheckRollup') or []]
160170
pending = [c for c in checks if c[1] == 'pending']
@@ -164,14 +174,28 @@ def wait(args: argparse.Namespace) -> int:
164174
say(f"{url} is {data['state'].lower()}; stopped waiting")
165175
return EXIT_CLOSED
166176

167-
waiting_for = f'{len(pending)} running checks' if checks else 'the first check'
177+
waiting_for = 'the first check'
178+
if pending:
179+
waiting_for = f'{len(pending)} running checks'
180+
elif checks:
181+
waiting_for = 'the finished checks to settle'
168182
progress = (len(checks), len(pending), len(failed))
169183
if checks and progress != last_progress:
170184
say(f'{head[:7]}: {len(checks) - len(pending)} of {len(checks)} checks done, '
171185
f'{len(failed)} failed')
172186
last_progress = progress
173187

174-
if checks and not pending:
188+
settled = False
189+
if not checks or pending:
190+
finished = None
191+
else:
192+
outcome = sorted((name, result) for name, _, result, _ in checks)
193+
if outcome != finished:
194+
finished = outcome
195+
finished_since = time.monotonic()
196+
settled = time.monotonic() - finished_since >= args.settle
197+
198+
if checks and not pending and settled:
175199
if failed:
176200
say(f'{url}: {len(failed)} of {len(checks)} checks failed on {head[:7]}')
177201
for name, _, result, link in failed:
@@ -189,7 +213,11 @@ def wait(args: argparse.Namespace) -> int:
189213
if remaining <= 0:
190214
say(f'Gave up after {args.timeout:g} minutes, still waiting for {waiting_for}')
191215
return EXIT_TIMEOUT
192-
time.sleep(min(args.interval, remaining))
216+
pause = min(args.interval, remaining)
217+
if finished is not None:
218+
# Look again as soon as the finished checks have settled
219+
pause = min(pause, max(args.settle - (time.monotonic() - finished_since), 0.01))
220+
time.sleep(pause)
193221

194222

195223
def main() -> int:

‎tests/test-pr-wait.sh‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ scenario() {
7171

7272
A=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
7373
B=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
74-
fast=(-i 0.01)
74+
fast=(-i 0.01 -s 0.03)
7575

7676
scenario 'no arguments print help' 2 'usage: pr-wait.py' </dev/null
7777
scenario 'an interval of 0 is refused' 2 'must be greater than 0' 1 -i 0 </dev/null
@@ -87,6 +87,14 @@ scenario 'lists failed checks with their links' 1 'failure: CI / test https://e
8787
$(pr OPEN "$A" run:lint:COMPLETED:SUCCESS run:test:COMPLETED:FAILURE)
8888
EOF
8989

90+
scenario 'waits for a run GitHub adds after the others finished' 0 'all 2 checks passed' 1 -i 0.01 -s 0.05 <<EOF
91+
$(pr OPEN "$A" run:push-tests:COMPLETED:SUCCESS)
92+
$(pr OPEN "$A" run:push-tests:COMPLETED:SUCCESS run:pr-tests:QUEUED:)
93+
$(pr OPEN "$A" run:push-tests:COMPLETED:SUCCESS run:pr-tests:COMPLETED:SUCCESS)
94+
EOF
95+
96+
scenario 'a settle time of 0 is refused' 2 'must be greater than 0' 1 -s 0 </dev/null
97+
9098
scenario 'a cancelled check counts as failed' 1 'cancelled: CI / test' 1 "${fast[@]}" <<EOF
9199
$(pr OPEN "$A" run:test:COMPLETED:CANCELLED)
92100
EOF

0 commit comments

Comments
 (0)