From 2d492d9d2ecb47298703cc5ad38d551367eea741 Mon Sep 17 00:00:00 2001 From: timmy Date: Thu, 6 Aug 2026 08:17:42 +0000 Subject: [PATCH] fix: normalize malformed pull request authors (#87) --- src/main.py | 13 ++++++++++++- tests/test_context_timeout.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index e4270c7..bbd83a6 100644 --- a/src/main.py +++ b/src/main.py @@ -132,7 +132,18 @@ async def context() -> JSONResponse: ) ] pr_models = [ - PullRequest(id=p["id"], number=p["number"], title=p["title"], state=p["state"], user=(p.get("user") or {}).get("login", ""), url=p["html_url"]) + PullRequest( + id=p["id"], + number=p["number"], + title=p["title"], + state=p["state"], + user=( + p["user"].get("login", "") + if isinstance(p.get("user"), dict) + else "" + ), + url=p["html_url"], + ) for p in (prs_data or [])[:50] if isinstance(p, dict) and all( diff --git a/tests/test_context_timeout.py b/tests/test_context_timeout.py index 5fbc2bb..6c95292 100644 --- a/tests/test_context_timeout.py +++ b/tests/test_context_timeout.py @@ -153,6 +153,35 @@ async def test_context_normalizes_nullable_pull_request_author(monkeypatch): assert pull_request["user"] == "" +@pytest.mark.anyio +async def test_context_normalizes_malformed_pull_request_author(monkeypatch): + async def user(): + return {"id": 1, "login": "timmy"} + + async def empty_collection(): + return [] + + async def pull_request_collection(): + return [{ + "id": 87, + "number": 87, + "title": "Malformed author", + "state": "open", + "user": "unknown", + "html_url": "https://forge.example/pulls/87", + }] + + monkeypatch.setattr(main, "current_user", user) + monkeypatch.setattr(main, "repos", empty_collection) + monkeypatch.setattr(main, "issues", empty_collection) + monkeypatch.setattr(main, "pull_requests", pull_request_collection) + + response = await main.context() + pull_request = json.loads(response.body)["pull_requests"][0] + + assert pull_request["user"] == "" + + @pytest.mark.anyio async def test_context_normalizes_null_collection_payloads(monkeypatch): async def user(): -- 2.43.0