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
5 changes: 4 additions & 1 deletion repo-config/configure.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ ruleset_id() {
return 1
fi
# shellcheck disable=SC2016 # $n is a jq --arg variable, not a shell expansion
ids="$(jqr --arg n "$1" '.[] | select(.name==$n) | .id' <<<"$out")"
if ! ids="$(jqr --arg n "$1" '.[] | if type == "object" then select(.name == $n) | if (.id | type) == "number" and .id == (.id | floor) then .id else error("bad id") end else error("not an object") end' <<<"$out")"; then
echo "Failed for $repo: could not read live ruleset state (an array element was not a ruleset object, or a match had no integer id)." >&2
return 1
fi
if [ -z "$ids" ]; then return 0; fi
# Pre-existing drift can leave more than one ruleset with the same name.
# Use the first and warn, so the duplicates get resolved rather than silently operating on the wrong one.
Expand Down
23 changes: 23 additions & 0 deletions tests/test_configure_ruleset_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ def test_a_non_array_2xx_body_aborts_rather_than_reading_as_not_found(self) -> N
)
self.assertIn("could not read live ruleset state", result.stderr)

def test_a_malformed_array_element_aborts_rather_than_reading_as_not_found(self) -> None:
for label, response in (
("numbers", json.dumps([1, 2])),
("strings", json.dumps(["x"])),
("a matching object with no id", json.dumps([{"name": "fixture"}])),
("null", json.dumps([None])),
("null then a match", json.dumps([None, {"name": "fixture", "id": 3}])),
("an empty id", json.dumps([{"name": "fixture", "id": ""}])),
("a boolean id", json.dumps([{"name": "fixture", "id": False}])),
("a fractional id", json.dumps([{"name": "fixture", "id": 1.5}])),
("an object id", json.dumps([{"name": "fixture", "id": {"a": 1}}])),
("a match then a non-object", json.dumps([{"name": "fixture", "id": 1}, 2])),
):
with self.subTest(label=label):
result = self.lookup(response)
self.assertNotEqual(
result.returncode,
0,
f"ruleset_id must abort on {label} (stdout={result.stdout!r})",
)
self.assertIn("could not read live ruleset state", result.stderr)
self.assertNotIn("null", result.stdout)

def test_a_gh_api_failure_still_aborts(self) -> None:
"""The pre-existing guard this fix sits beside: an outright gh failure already aborts."""
result = self.lookup("", status=1)
Expand Down
Loading