Skip to content
Merged
Show file tree
Hide file tree
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
38 changes: 34 additions & 4 deletions scripts/fleet/repo-universe/check_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,33 @@ def normalize_issue(raw: dict) -> dict | None:
raise FetchError(f"issue entry is not a JSON object: {raw!r:.80}")
if "pull_request" in raw:
return None
return {field: raw.get(field) for field in ISSUE_FIELDS}
projected = {field: raw.get(field) for field in ISSUE_FIELDS}
error = _issue_field_error(projected)
if error is not None:
raise FetchError(error)
return projected


def _issue_field_error(issue) -> str | None:
"""Field-level shape rule for one issue mapping (#6757).

Shared by the fetch path (via normalize_issue) and the persisted
state path (via _valid_prior_entry), so a malformed issue is the
same defect in both places. Oracle: the GitHub issues API shape
this checker consumes — `number` is a JSON integer (never a bool,
string, or list) and every other persisted field is a string.
Returns a human-readable reason, or None when well-formed.
"""
if not isinstance(issue, dict):
return f"issue entry is not a JSON object: {issue!r:.80}"
number = issue.get("number")
if isinstance(number, bool) or not isinstance(number, int):
return f"issue field 'number' is not an int: {number!r:.80}"
for field in ("state", "title", "updated_at", "html_url"):
value = issue.get(field)
if not isinstance(value, str):
return f"issue field {field!r} is not a string: {value!r:.80}"
return None


def fetch_issues_gh(repo: str) -> list[dict]:
Expand Down Expand Up @@ -212,14 +238,18 @@ def _valid_prior_entry(entry) -> bool:
"""Shape-check one persisted per-repo state entry (#6652).

A wrong-shape persisted entry (an int, a list, an issues mapping
whose values are not objects) is reported per-repo by run_check
instead of crashing diff_repo's subscripts for every repo.
whose values are not objects, or — #6757 — an issue object whose
fields are missing or wrong-typed, or whose key disagrees with its
number) is reported per-repo by run_check instead of crashing
diff_repo's subscripts for every repo.
"""
if not isinstance(entry, dict):
return False
issues = entry.get("issues")
return isinstance(issues, dict) and all(
isinstance(v, dict) for v in issues.values())
isinstance(v, dict) and _issue_field_error(v) is None
and k == str(v["number"])
for k, v in issues.items())

def run_check(inventory_text: str, state: dict, fetch, now: str) -> tuple[dict, list[str], bool]:
"""Run one check over all watched repos.
Expand Down
62 changes: 62 additions & 0 deletions scripts/fleet/repo-universe/test_check_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,68 @@ def test_persisted_issue_entry_int_is_reported_and_preserved(self):
self.assertEqual(
state["repos"]["dezren39/hatch-personal"]["issues"]["1"], 123)

def test_fixture_entry_empty_object_is_a_fetch_error(self):
# #6757: a fieldless object passed the #6652 entry guard and
# became a phantom issue persisted under the key "None".
fixture = {"dezren39/hatch-personal": [{}],
"developing-today/code": [issue(9)]}
state, lines, had_error = check_issues.run_check(
INVENTORY, {"version": 1, "repos": {}}, self.fixture_fetch(fixture), NOW1)
self.assertTrue(had_error)
report = "\n".join(lines)
self.assertIn("- ERROR: fetch failed:", report)
self.assertIn("issue field 'number' is not an int", report)
self.assertNotIn("#None", report)
self.assertIn("baseline: 1 issues recorded", report)
self.assertNotIn("dezren39/hatch-personal", state["repos"])

def test_fixture_string_number_alongside_int_is_a_fetch_error(self):
# #6757: a string number used to reach sorted() in the report
# and crash the whole run with a TypeError.
string_numbered = {"number": "5", "state": "open", "title": "x",
"updated_at": "2026-10-31T00:00:00Z",
"html_url": "https://github.com/x/issues/5"}
fixture = {"dezren39/hatch-personal": [string_numbered, issue(2)],
"developing-today/code": [issue(9)]}
prior = {"version": 1, "repos": {
"dezren39/hatch-personal": {
"watermark": None,
"issues": {"1": check_issues.normalize_issue(issue(1))}}}}
state, lines, had_error = check_issues.run_check(
INVENTORY, prior, self.fixture_fetch(fixture), NOW2)
self.assertTrue(had_error)
self.assertIn("- ERROR: fetch failed:", "\n".join(lines))
# The poisoned repo's prior state is left untouched.
self.assertEqual(state["repos"]["dezren39/hatch-personal"],
prior["repos"]["dezren39/hatch-personal"])

def test_normalize_rejects_wrong_typed_fields(self):
# #6757: oracle is the GitHub issues API shape — number is an
# int (not bool/str/list); the other fields are strings.
for bad in ({"number": True}, {"number": [1]}, {"number": 1.5},
{"number": 1, "state": None},
{"number": 1, "state": "open", "title": 7}):
raw = issue(1)
raw.update(bad)
with self.assertRaises(check_issues.FetchError, msg=repr(bad)):
check_issues.normalize_issue(raw)

def test_persisted_issue_entry_empty_object_is_reported(self):
# #6757: a field-missing persisted entry passed _valid_prior_entry
# and re-crashed diff_repo with KeyError on every daily run.
good = {"dezren39/hatch-personal": [issue(2)],
"developing-today/code": []}
prior = {"version": 1, "repos": {
"dezren39/hatch-personal": {
"watermark": None, "issues": {"2": {}}}}}
state, lines, had_error = check_issues.run_check(
INVENTORY, prior, fetch_from(good), NOW2)
self.assertTrue(had_error)
self.assertIn("- ERROR: persisted state entry is malformed",
"\n".join(lines))
self.assertEqual(
state["repos"]["dezren39/hatch-personal"]["issues"]["2"], {})

def test_main_with_wrong_shape_state_file_exits_named_error(self):
import tempfile
with tempfile.TemporaryDirectory() as td:
Expand Down
Loading