ci(pr-health): keep a failed base-file comparison from clearing the rebase label

The base-file comparison fell back to `[]` when the API call failed, so
`drift_state()` saw a positive `behind_by` with no moved files, found no
overlap and returned `current`. The label step then removed
`status: needs rebase` from a pull request that was genuinely stale, which
contradicts the fail-safe behaviour the first comparison already has. The
same hole existed when the first comparison answered without a merge base.

The workflow now sets an empty-string sentinel before the call and resets to
it when the call fails, so partial output from a call that died midway is
discarded too. `parse_base_files()` maps that sentinel (and any unusable
payload) to `None`, and `drift_state()` returns `unknown` when the branch is
behind but the comparison never answered, which neither adds nor removes the
label. A successful comparison that returns no files still classifies as
`current`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Parideboy
2026-08-27 17:29:49 +02:00
co-authored by Claude Opus 5
parent d229eec1ce
commit 738f984d33
4 changed files with 166 additions and 10 deletions
+29 -7
View File
@@ -72,10 +72,23 @@ def parse_behind_by(value: str) -> int | None:
return None
def parse_base_files(value: str) -> list[str] | None:
"""Read the compared file list from a compare call that may have failed."""
if not value or not value.strip():
return None
try:
parsed = json.loads(value)
except json.JSONDecodeError:
return None
if not isinstance(parsed, list):
return None
return [str(item) for item in parsed]
def drift_state(
payload: dict[str, Any],
behind_by: int | None,
base_files: Iterable[Any],
base_files: Iterable[Any] | None,
) -> str:
"""Classify a pull request branch against the current tip of its base branch.
@@ -85,7 +98,10 @@ def drift_state(
being behind on files the pull request also modifies, because that is where a
semantic merge conflict hides from per-pull-request checks.
Returns "stale", "current", or "unknown" when the comparison is unavailable.
Returns "stale", "current", or "unknown" when either comparison is unavailable.
A `base_files` of None means the comparison never answered, which is not the same
as a comparison that answered with no files, so the label is left alone instead of
being cleared.
"""
if _merge_state(payload) == "BEHIND":
return "stale"
@@ -93,8 +109,10 @@ def drift_state(
return "unknown"
if behind_by <= 0:
return "current"
if base_files is None:
return "unknown"
moved = {str(path) for path in base_files or []}
moved = {str(path) for path in base_files}
touched = {
str(entry.get("path")) for entry in payload.get("files") or [] if isinstance(entry, dict)
}
@@ -121,8 +139,11 @@ def parse_args(argv: list[str]) -> argparse.Namespace:
)
parser.add_argument(
"--base-files",
default="[]",
help="JSON array of files the base branch changed since the merge base",
default="",
help=(
"JSON array of files the base branch changed since the merge base, "
"empty when the comparison was unavailable"
),
)
return parser.parse_args(argv)
@@ -131,8 +152,9 @@ def main(argv: list[str] | None = None) -> int:
args = parse_args(argv or sys.argv[1:])
payload = json.loads(args.state_json)
if args.field == "drift":
base_files = json.loads(args.base_files or "[]")
print(drift_state(payload, parse_behind_by(args.behind_by), base_files))
behind_by = parse_behind_by(args.behind_by)
base_files = parse_base_files(args.base_files)
print(drift_state(payload, behind_by, base_files))
else:
print(check_state(payload))
return 0
+8 -3
View File
@@ -218,10 +218,15 @@ jobs:
merge_base="$(jq -r '.merge_base // ""' <<<"$head_compare")"
# Files the base branch changed since this pull request forked off it.
# An empty string means the comparison never produced an answer; it must
# not read as "the base moved nothing", which would clear a correct label.
base_files='[]'
if [[ -n "$merge_base" && -n "$behind_by" && "$behind_by" != "0" ]]; then
base_files="$(gh api "repos/$REPO/compare/$merge_base...$base_ref" \
--jq '[.files[]? | .filename]' 2>/dev/null || echo '[]')"
if [[ -n "$behind_by" && "$behind_by" != "0" ]]; then
base_files=''
if [[ -n "$merge_base" ]]; then
base_files="$(gh api "repos/$REPO/compare/$merge_base...$base_ref" \
--jq '[.files[]? | .filename]' 2>/dev/null)" || base_files=''
fi
fi
drift="$(python3 .github/scripts/pr-health-labels.py \
+55
View File
@@ -173,3 +173,58 @@ def test_main_prints_the_drift_state() -> None:
)
== 0
)
def test_drift_state_is_unknown_when_the_base_file_comparison_failed() -> None:
module = _load_module()
payload = {
"mergeStateStatus": "CLEAN",
"files": [{"path": "headroom/proxy/server.py"}],
}
assert module.drift_state(payload, 13, None) == "unknown"
def test_drift_state_is_current_when_the_base_moved_no_files() -> None:
module = _load_module()
payload = {
"mergeStateStatus": "CLEAN",
"files": [{"path": "headroom/proxy/server.py"}],
}
assert module.drift_state(payload, 13, []) == "current"
def test_parse_base_files_reads_a_failed_comparison_as_unknown() -> None:
module = _load_module()
assert module.parse_base_files("") is None
assert module.parse_base_files(" ") is None
assert module.parse_base_files("not json") is None
assert module.parse_base_files('{"files": []}') is None
assert module.parse_base_files("[]") == []
assert module.parse_base_files('["headroom/proxy/server.py"]') == ["headroom/proxy/server.py"]
def test_main_prints_unknown_when_the_base_file_comparison_failed(capsys) -> None:
module = _load_module()
state_json = json.dumps(
{"mergeStateStatus": "CLEAN", "files": [{"path": "headroom/proxy/server.py"}]}
)
assert (
module.main(
[
"--state-json",
state_json,
"--field",
"drift",
"--behind-by",
"13",
"--base-files",
"",
]
)
== 0
)
assert capsys.readouterr().out.strip() == "unknown"
+74
View File
@@ -2,7 +2,14 @@
from __future__ import annotations
import json
import shutil
import subprocess
import sys
from pathlib import Path
from textwrap import dedent
import pytest
def test_incomplete_pr_template_is_reported_without_failing_job() -> None:
@@ -45,3 +52,70 @@ def test_unknown_drift_does_not_clear_the_rebase_label() -> None:
workflow = Path(".github/workflows/pr-health.yml").read_text(encoding="utf-8")
assert 'elif [[ "$drift" == "current" ]]; then' in workflow
def _base_files_snippet(workflow: str) -> str:
"""The shell block that asks GitHub which files the base branch moved."""
start = workflow.index(" # Files the base branch changed")
end = workflow.index(' drift="$(python3', start)
return dedent(workflow[start:end])
def test_failed_base_file_comparison_keeps_an_unknown_sentinel() -> None:
workflow = Path(".github/workflows/pr-health.yml").read_text(encoding="utf-8")
assert "echo '[]'" not in workflow
assert "base_files=''" in workflow
assert "|| base_files=''" in workflow
def test_failed_base_file_comparison_does_not_clear_the_rebase_label() -> None:
bash = shutil.which("bash")
if bash is None:
pytest.skip("bash is unavailable")
workflow = Path(".github/workflows/pr-health.yml").read_text(encoding="utf-8")
script = "\n".join(
[
"set -euo pipefail",
# Stands in for a throttled API call that writes partial output and fails.
"gh() { printf 'partia'; return 1; }",
'REPO="headroomlabs-ai/headroom"',
'base_ref="main"',
'behind_by="13"',
'merge_base="0123456789abcdef0123456789abcdef01234567"',
_base_files_snippet(workflow),
'printf "%s" "$base_files"',
]
)
result = subprocess.run([bash, "-c", script], capture_output=True, text=True)
assert result.returncode == 0, result.stderr
assert result.stdout == ""
drift = subprocess.run(
[
sys.executable,
".github/scripts/pr-health-labels.py",
"--state-json",
json.dumps(
{"mergeStateStatus": "CLEAN", "files": [{"path": "headroom/proxy/server.py"}]}
),
"--field",
"drift",
"--behind-by",
"13",
"--base-files",
result.stdout,
],
capture_output=True,
text=True,
)
assert drift.returncode == 0, drift.stderr
assert drift.stdout.strip() == "unknown"
# An unknown verdict matches neither branch, so no gh pr edit runs for the label.
start = workflow.index(' if [[ "$drift" == "stale" ]]; then')
dispatch = workflow[start : workflow.index("\n fi\n", start)]
assert 'elif [[ "$drift" == "current" ]]; then' in dispatch
assert "else" not in dispatch