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
115 changes: 104 additions & 11 deletions infra/fleet-paperwork/fleet_paperwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,42 @@ def http_raw(
raise AssertionError("unreachable")


def _shape_name(value: object) -> str:
"""The JSON shape of a parsed body, for diagnostics (#6864)."""
if isinstance(value, dict):
return "object"
if isinstance(value, list):
return "array"
if isinstance(value, str):
return "string"
if isinstance(value, bool):
return "boolean"
if isinstance(value, (int, float)):
return "number"
if value is None:
return "null"
return type(value).__name__


def http(
method: str, path: str, token: str, body: dict | None = None
) -> tuple[int, dict]:
method: str,
path: str,
token: str,
body: dict | None = None,
expect: type = dict,
) -> tuple[int, dict | list]:
"""Die-on-error wrapper over http_raw with shape validation (#6864).

A parseable-but-wrong-shaped body — an array, string, or number
where the caller needs an object (or the reverse) — dies here with
a diagnostic naming the method, URL, HTTP status, and received
shape, instead of crashing every consumer downstream with a raw
AttributeError/KeyError. The error path is held to the same rule:
a non-object error body surfaces the HTTP status, the shape, and
the raw excerpt, never a crash inside the error handler itself.
`expect` is the one legitimate exception carved out by name:
/app/installations returns a top-level array.
"""
url = path if path.startswith("http") else API + path
data = json.dumps(body).encode() if body is not None else None
status, raw = http_raw(method, path, token, data)
Expand All @@ -175,8 +208,36 @@ def http(
detail = json.loads(raw)
except Exception:
detail = {"raw": raw}
die(f"{method} {url} -> HTTP {status}: {detail.get('message', raw[:300])}")
return status, (json.loads(raw) if raw else {})
if isinstance(detail, dict):
die(f"{method} {url} -> HTTP {status}: {detail.get('message', raw[:300])}")
die(
f"{method} {url} -> HTTP {status}: expected a JSON object error "
f"body, got {_shape_name(detail)}: {raw[:300]}"
)
if not raw:
payload: object = {}
else:
try:
payload = json.loads(raw)
except Exception:
die(f"{method} {url} -> HTTP {status}: response body is not JSON: {raw[:300]}")
if not isinstance(payload, expect):
die(
f"{method} {url} -> HTTP {status}: expected a JSON "
f"{'object' if expect is dict else 'array'}, got {_shape_name(payload)}"
)
return status, payload


def _account_login(inst: dict) -> str:
"""An installation's account login, or "" for any other shape: an
absent account, an explicit null, or a non-string login (#6864)."""
account = inst.get("account")
if isinstance(account, dict):
login = account.get("login")
if isinstance(login, str):
return login
return ""


class AppClient:
Expand All @@ -189,23 +250,33 @@ def __init__(self, sops_path: str):

def installations(self) -> list[dict]:
if self._installations is None:
_, data = http("GET", "/app/installations?per_page=100", self.jwt)
_, data = http(
"GET", "/app/installations?per_page=100", self.jwt, expect=list
)
for inst in data:
if not isinstance(inst, dict):
die(
"installation entry is not a JSON object "
f"(got {_shape_name(inst)})"
)
self._installations = data
return self._installations

def installation_for(self, owner: str) -> dict:
for inst in self.installations():
if inst.get("account", {}).get("login", "").lower() == owner.lower():
if _account_login(inst).lower() == owner.lower():
return inst
die(
f"no installation of the app found for owner {owner!r} "
f"(installations: {[i.get('account', {}).get('login') for i in self.installations()]})"
f"(installations: {[_account_login(i) for i in self.installations()]})"
)
raise AssertionError("unreachable")

def token_for(self, owner: str) -> str:
inst = self.installation_for(owner)
iid = inst["id"]
iid = inst.get("id")
if isinstance(iid, bool) or not isinstance(iid, int):
die(f"installation for owner {owner!r} has no numeric id")
if iid not in self._tokens:
_, data = http(
"POST", f"/app/installations/{iid}/access_tokens", self.jwt, {}
Expand All @@ -224,8 +295,26 @@ def assert_repo_in_scope(self, repo: str) -> None:
_, data = http(
"GET", f"/installation/repositories?per_page=100&page={page}", token
)
seen += [r["full_name"] for r in data.get("repositories", [])]
if len(seen) >= data.get("total_count", 0) or not data.get("repositories"):
repos = data.get("repositories", [])
if not isinstance(repos, list):
die(
"installation repositories response is not a JSON array "
f"(got {_shape_name(repos)})"
)
for r in repos:
if not isinstance(r, dict) or not isinstance(r.get("full_name"), str):
die(
"installation repository entry has no string "
f"full_name (got {_shape_name(r)}): {str(r)[:120]}"
)
seen.append(r["full_name"])
total = data.get("total_count", 0)
if isinstance(total, bool) or not isinstance(total, (int, float)):
die(
"installation repositories total_count is not a number "
f"(got {_shape_name(total)})"
)
if len(seen) >= total or not repos:
break
page += 1
if repo not in seen:
Expand Down Expand Up @@ -296,7 +385,11 @@ def find_by_exact_title(client: AppClient, repo: str, title: str) -> dict | None
"GET", f"/search/issues?q={q}&per_page=100&page={page}", token
)
items = data.get("items", [])
if not isinstance(items, list):
die(f"search response items is not a JSON array (got {_shape_name(items)})")
for item in items:
if not isinstance(item, dict):
continue
if "pull_request" in item:
continue
if item.get("title") == title:
Expand Down Expand Up @@ -325,7 +418,7 @@ def cmd_check(args) -> int:
print(f"app id {client.app_id} ({APP_SLUG_HINT})")
for inst in client.installations():
print(
f"installation {inst['id']}: {inst.get('account', {}).get('login')} "
f"installation {inst.get('id')}: {_account_login(inst)} "
f"permissions={inst.get('permissions')}"
)
if args.repo:
Expand Down
139 changes: 139 additions & 0 deletions infra/fleet-paperwork/tests/test_fleet_paperwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,144 @@ def test_token_requires_repo(self):
fp.main(["token"])


class TestResponseShapes(unittest.TestCase):
"""#6864: parseable-but-wrong-shaped API bodies die with the tool's
own diagnostic (method, URL, HTTP status, shape) — never a raw
AttributeError/KeyError/TypeError, including inside http()'s own
error handler. Transport (http_raw) is stubbed, as in the probe.
"""

def call_http(self, status, raw, **kw):
import io
from contextlib import redirect_stderr

err = io.StringIO()
result = None
with unittest.mock.patch.object(fp, "http_raw", return_value=(status, raw)):
with redirect_stderr(err):
try:
result = fp.http("GET", "/repos/x/y/issues/1", "tok", **kw)
except SystemExit:
result = None
return "died", err.getvalue()
return result, err.getvalue()

def test_error_body_list_dies_with_status_and_shape(self):
outcome, err = self.call_http(403, '["a","b"]')
self.assertEqual(outcome, "died")
self.assertIn("HTTP 403", err)
self.assertIn("array", err)
self.assertIn('["a","b"]', err) # raw excerpt still surfaced

def test_error_body_string_and_number_die_cleanly(self):
for raw, shape in [('"edge string"', "string"), ("42", "number")]:
with self.subTest(raw=raw):
outcome, err = self.call_http(502, raw)
self.assertEqual(outcome, "died")
self.assertIn("HTTP 502", err)
self.assertIn(shape, err)

def test_error_body_object_keeps_message(self):
outcome, err = self.call_http(403, '{"message": "boom-rate"}')
self.assertEqual(outcome, "died")
self.assertIn("boom-rate", err)

def test_success_body_wrong_shape_dies_naming_shape(self):
for raw, shape in [('"weird"', "string"), ('["weird"]', "array"), ("42", "number")]:
with self.subTest(raw=raw):
outcome, err = self.call_http(200, raw)
self.assertEqual(outcome, "died")
self.assertIn("expected a JSON object", err)
self.assertIn(shape, err)

def test_success_body_not_json_dies_cleanly(self):
outcome, err = self.call_http(200, "not json")
self.assertEqual(outcome, "died")
self.assertIn("not JSON", err)

def test_success_object_and_expected_array_pass(self):
result, _ = self.call_http(200, '{"a": 1}')
self.assertEqual(result, (200, {"a": 1}))
result, _ = self.call_http(200, '[{"id": 1}]', expect=list)
self.assertEqual(result, (200, [{"id": 1}]))

def test_installations_dict_instead_of_list_dies(self):
client = object.__new__(fp.AppClient)
client.jwt = "jwt"
client._installations = None
with unittest.mock.patch.object(
fp, "http", return_value=(200, {"installations": []})
):
# Bypass the real http() shape guard to prove the call site
# asks for a list: the stub ignores expect, so instead assert
# the real guard fires via http_raw.
pass
with unittest.mock.patch.object(
fp, "http_raw", return_value=(200, '{"installations": []}')
):
with self.assertRaises(SystemExit):
client.installations()

def test_installations_string_item_dies(self):
client = object.__new__(fp.AppClient)
client.jwt = "jwt"
client._installations = None
with unittest.mock.patch.object(
fp, "http_raw", return_value=(200, '["a-string-item"]')
):
with self.assertRaises(SystemExit):
client.installations()

def test_installation_for_tolerates_null_account(self):
client = object.__new__(fp.AppClient)
client._installations = [
{"id": 1, "account": None},
{"id": 2, "account": {"login": "Developing-Today"}},
]
inst = client.installation_for("developing-today")
self.assertEqual(inst["id"], 2)

def test_assert_repo_in_scope_dies_on_entry_without_full_name(self):
client = object.__new__(fp.AppClient)
with unittest.mock.patch.object(
fp.AppClient, "token_for", return_value="tok"
), unittest.mock.patch.object(
fp,
"http",
return_value=(
200,
{"repositories": [{"name": "no-full-name"}], "total_count": 1},
),
):
with self.assertRaises(SystemExit):
client.assert_repo_in_scope("developing-today/code")

def test_assert_repo_in_scope_happy_path(self):
client = object.__new__(fp.AppClient)
with unittest.mock.patch.object(
fp.AppClient, "token_for", return_value="tok"
), unittest.mock.patch.object(
fp,
"http",
return_value=(
200,
{
"repositories": [{"full_name": "developing-today/code"}],
"total_count": 1,
},
),
):
client.assert_repo_in_scope("developing-today/code") # no SystemExit

def test_find_by_exact_title_skips_non_object_items(self):
def fake_http(method, path, token, payload=None, **kw):
return 200, {"items": ["bare-string", {"title": "T", "number": 5}]}

with unittest.mock.patch.object(fp, "http", fake_http):
hit = fp.find_by_exact_title(_FakeClient(), "developing-today/code", "T")
self.assertEqual(hit["number"], 5)



if __name__ == "__main__":
unittest.main()
Loading