From 03c31fb3314bbfbdaf9be06dff24aeff78e9b9be Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 22:09:54 -0600 Subject: [PATCH] fix(repo-universe): validate issue fields in check_issues (#6757) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit normalize_issue projected fields without validating them: a fieldless fetched issue became a phantom None state entry, a string number crashed sorted() and the whole run, and a field-missing persisted entry passed _valid_prior_entry and re-crashed daily. Add a shared _issue_field_error rule (number is a non-bool int; state/title/updated_at/html_url are strings; persisted key must equal str(number)) used by both normalize_issue (raises FetchError, per-repo ERROR) and _valid_prior_entry (malformed-state per-repo error). Regression tests cover fixture {}, string number alongside int, wrong-typed fields, and persisted {} — all fail on the old code, 19/19 pass after. --- scripts/fleet/repo-universe/check_issues.py | 38 ++++++++++-- .../fleet/repo-universe/test_check_issues.py | 62 +++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) diff --git a/scripts/fleet/repo-universe/check_issues.py b/scripts/fleet/repo-universe/check_issues.py index 7bf833ad12..b664ed974a 100755 --- a/scripts/fleet/repo-universe/check_issues.py +++ b/scripts/fleet/repo-universe/check_issues.py @@ -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]: @@ -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. diff --git a/scripts/fleet/repo-universe/test_check_issues.py b/scripts/fleet/repo-universe/test_check_issues.py index 3b716e27ca..9b48c69c51 100755 --- a/scripts/fleet/repo-universe/test_check_issues.py +++ b/scripts/fleet/repo-universe/test_check_issues.py @@ -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: