From 001eda731dca9e59cd2c20d6cbbbc0a87c80eef0 Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Tue, 24 Nov 2026 19:33:44 +0100 Subject: [PATCH 1/3] Record the Bugzilla account and source on runs started by a needinfo --- services/hackbot-api/app/bugzilla_webhook.py | 10 +++++++++- services/hackbot-api/app/routers/webhooks.py | 6 ++++++ services/hackbot-api/tests/test_webhooks.py | 20 +++++++++++++++++--- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/services/hackbot-api/app/bugzilla_webhook.py b/services/hackbot-api/app/bugzilla_webhook.py index ce4e7a5d63f..e951854176b 100644 --- a/services/hackbot-api/app/bugzilla_webhook.py +++ b/services/hackbot-api/app/bugzilla_webhook.py @@ -13,7 +13,9 @@ class BugzillaNeedinfoEvent: bug_id: int flag_id: int comment: str + user_id: int user_login: str + user_real_name: str | None def detect_needinfo_request( @@ -85,5 +87,11 @@ def detect_needinfo_request( "bug context if none exists." ) return BugzillaNeedinfoEvent( - bug_id=bug_id, flag_id=flag_id, comment=comment, user_login=actor_login + bug_id=bug_id, + flag_id=flag_id, + comment=comment, + user_id=event["user"]["id"], + user_login=actor_login, + # BMO sends "" for a user without a real name. + user_real_name=event["user"]["real_name"] or None, ) diff --git a/services/hackbot-api/app/routers/webhooks.py b/services/hackbot-api/app/routers/webhooks.py index e118ee51a37..81d01109b50 100644 --- a/services/hackbot-api/app/routers/webhooks.py +++ b/services/hackbot-api/app/routers/webhooks.py @@ -244,6 +244,12 @@ async def bugzilla_webhook( "comment": detected.comment, }, dedupe_key=f"ni{detected.flag_id}", + source="bugzilla", + identity=Identity( + identity_provider="bugzilla", + user_id=str(detected.user_id), + username=detected.user_real_name, + ), ) if not run.is_new: log.info( diff --git a/services/hackbot-api/tests/test_webhooks.py b/services/hackbot-api/tests/test_webhooks.py index 38c3145e642..2e1175d9a79 100644 --- a/services/hackbot-api/tests/test_webhooks.py +++ b/services/hackbot-api/tests/test_webhooks.py @@ -33,7 +33,7 @@ ) from app.routers import webhooks from fastapi.testclient import TestClient -from hackbot_client import RunStatus, TriggeredRun +from hackbot_client import Identity, RunStatus, TriggeredRun from sqlalchemy.dialects import postgresql SECRET = "test-secret" @@ -569,9 +569,13 @@ class _FakeHackbotClient: def __init__(self): self.calls = [] self.dedupe_keys = [] + self.identities = [] - async def trigger_run(self, agent_name, inputs, *, dedupe_key=None): + async def trigger_run( + self, agent_name, inputs, *, dedupe_key=None, source=None, identity=None + ): self.calls.append((agent_name, inputs)) + self.identities.append((source, identity)) # Like the API's unique index: a key seen before resolves to the # existing run and is not new to this request. is_new = dedupe_key is None or dedupe_key not in self.dedupe_keys @@ -894,6 +898,16 @@ def test_bugzilla_route_triggers_run(client, bugzilla_authorizer): }, ) ] + assert fake_api.identities == [ + ( + "bugzilla", + Identity( + identity_provider="bugzilla", + user_id="560562", + username="Greg Mierzwinski [:sparky]", + ), + ) + ] assert bugzilla_authorizer.checked == ["gmierzwinski@mozilla.com"] @@ -935,7 +949,7 @@ def test_bugzilla_route_keys_retry_same_but_later_event_differently(client): def test_bugzilla_route_surfaces_failed_dispatch(client): class _FailingClient: - async def trigger_run(self, agent_name, inputs, *, dedupe_key=None): + async def trigger_run(self, agent_name, inputs, **kwargs): raise RuntimeError("run creation failed") app.dependency_overrides[get_hackbot_client] = lambda: _FailingClient() From 39eac5fa61bb39bae6043449d6c393cda95b4c11 Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Tue, 24 Nov 2026 20:23:00 +0100 Subject: [PATCH 2/3] Show the Bugzilla nickname as the account username --- .../hackbot-api/app/bugzilla_authorization.py | 27 ++++++++--------- services/hackbot-api/app/bugzilla_webhook.py | 3 -- services/hackbot-api/app/routers/webhooks.py | 5 ++-- .../tests/test_bugzilla_authorization.py | 30 ++++++++++--------- services/hackbot-api/tests/test_webhooks.py | 10 +++++-- 5 files changed, 40 insertions(+), 35 deletions(-) diff --git a/services/hackbot-api/app/bugzilla_authorization.py b/services/hackbot-api/app/bugzilla_authorization.py index 33bc2e05534..8c0f94ee982 100644 --- a/services/hackbot-api/app/bugzilla_authorization.py +++ b/services/hackbot-api/app/bugzilla_authorization.py @@ -21,33 +21,31 @@ def __init__( self._api_url = api_url.rstrip("/") self._api_key = api_key self._authorized_group_name = authorized_group_name - self._cache: TTLCache[str, bool] = TTLCache( + self._cache: TTLCache[str, str | None] = TTLCache( maxsize=cache_maxsize, ttl=cache_ttl_seconds, ) - async def is_authorized(self, login: str) -> bool: - """Return whether a Bugzilla login belongs to the authorized group.""" + async def is_authorized(self, login: str) -> str | None: + """Return the nickname of a login in the authorized group, else None.""" login = login.lower() - cached = self._cache.get(login) - if cached is not None: - return cached - - authorized = await self._is_user_in_group(login, self._authorized_group_name) - self._cache[login] = authorized - return authorized + if login not in self._cache: + self._cache[login] = await self._is_user_in_group( + login, self._authorized_group_name + ) + return self._cache[login] # TODO: Move this REST call to a shared Bugzilla client library (#6459). - async def _is_user_in_group(self, login: str, group_name: str) -> bool: - """Return whether a Bugzilla account exists and belongs to a group.""" + async def _is_user_in_group(self, login: str, group_name: str) -> str | None: + """Return the nickname of a Bugzilla account in a group, else None.""" async with httpx.AsyncClient(timeout=_REQUEST_TIMEOUT_SECONDS) as client: response = await client.get( f"{self._api_url}/user", params={ "names": login, "groups": group_name, - "include_fields": "name", + "include_fields": "name,nick", # Report an unknown login in ``faults`` instead of failing # the request, so it maps to "not authorized", not a 500. "permissive": "1", @@ -55,4 +53,5 @@ async def _is_user_in_group(self, login: str, group_name: str) -> bool: headers={"X-Bugzilla-API-Key": self._api_key}, ) response.raise_for_status() - return bool(response.json().get("users")) + users = response.json().get("users") + return users[0]["nick"] if users else None diff --git a/services/hackbot-api/app/bugzilla_webhook.py b/services/hackbot-api/app/bugzilla_webhook.py index e951854176b..fe3e083f0d6 100644 --- a/services/hackbot-api/app/bugzilla_webhook.py +++ b/services/hackbot-api/app/bugzilla_webhook.py @@ -15,7 +15,6 @@ class BugzillaNeedinfoEvent: comment: str user_id: int user_login: str - user_real_name: str | None def detect_needinfo_request( @@ -92,6 +91,4 @@ def detect_needinfo_request( comment=comment, user_id=event["user"]["id"], user_login=actor_login, - # BMO sends "" for a user without a real name. - user_real_name=event["user"]["real_name"] or None, ) diff --git a/services/hackbot-api/app/routers/webhooks.py b/services/hackbot-api/app/routers/webhooks.py index 81d01109b50..6e2db0ea003 100644 --- a/services/hackbot-api/app/routers/webhooks.py +++ b/services/hackbot-api/app/routers/webhooks.py @@ -228,7 +228,8 @@ async def bugzilla_webhook( if detected is None: return {"status": "ignored", "reason": "no actionable Hackbot needinfo"} - if not await authorizer.is_authorized(detected.user_login): + nick = await authorizer.is_authorized(detected.user_login) + if nick is None: log.info( "Ignored Bugzilla needinfo webhook for bug %s: %s is not authorized", detected.bug_id, @@ -248,7 +249,7 @@ async def bugzilla_webhook( identity=Identity( identity_provider="bugzilla", user_id=str(detected.user_id), - username=detected.user_real_name, + username=nick, ), ) if not run.is_new: diff --git a/services/hackbot-api/tests/test_bugzilla_authorization.py b/services/hackbot-api/tests/test_bugzilla_authorization.py index 844bf102f9d..a72e9194bf3 100644 --- a/services/hackbot-api/tests/test_bugzilla_authorization.py +++ b/services/hackbot-api/tests/test_bugzilla_authorization.py @@ -10,7 +10,7 @@ BUGZILLA_API_KEY = "test-bugzilla-api-key" -def _authorizer(member: bool) -> tuple[BugzillaAuthorizer, AsyncMock]: +def _authorizer(member: str | None) -> tuple[BugzillaAuthorizer, AsyncMock]: """An authorizer whose membership lookup is stubbed to ``member``.""" authorizer = BugzillaAuthorizer( "https://bugzilla.example.com/rest", @@ -23,26 +23,26 @@ def _authorizer(member: bool) -> tuple[BugzillaAuthorizer, AsyncMock]: async def test_is_authorized_caches_positive_lookup(): - authorizer, lookup = _authorizer(member=True) + authorizer, lookup = _authorizer(member="dev") - assert await authorizer.is_authorized("dev@mozilla.com") is True - assert await authorizer.is_authorized("dev@mozilla.com") is True + assert await authorizer.is_authorized("dev@mozilla.com") == "dev" + assert await authorizer.is_authorized("dev@mozilla.com") == "dev" lookup.assert_awaited_once_with("dev@mozilla.com", AUTHORIZED_GROUP_NAME) async def test_is_authorized_caches_negative_lookup(): - authorizer, lookup = _authorizer(member=False) + authorizer, lookup = _authorizer(member=None) - assert await authorizer.is_authorized("someone@example.com") is False - assert await authorizer.is_authorized("someone@example.com") is False + assert await authorizer.is_authorized("someone@example.com") is None + assert await authorizer.is_authorized("someone@example.com") is None lookup.assert_awaited_once_with("someone@example.com", AUTHORIZED_GROUP_NAME) async def test_is_authorized_normalizes_login_case(): - authorizer, lookup = _authorizer(member=True) + authorizer, lookup = _authorizer(member="dev") - assert await authorizer.is_authorized("Dev@Mozilla.com") is True - assert await authorizer.is_authorized("dev@mozilla.com") is True + assert await authorizer.is_authorized("Dev@Mozilla.com") == "dev" + assert await authorizer.is_authorized("dev@mozilla.com") == "dev" lookup.assert_awaited_once_with("dev@mozilla.com", AUTHORIZED_GROUP_NAME) @@ -77,16 +77,18 @@ def handler(request: httpx.Request) -> httpx.Response: async def test_lookup_authorizes_group_member(monkeypatch): authorizer, requests = _http_authorizer( - monkeypatch, {"users": [{"name": "dev@mozilla.com"}], "faults": []} + monkeypatch, + {"users": [{"name": "dev@mozilla.com", "nick": "dev"}], "faults": []}, ) - assert await authorizer.is_authorized("dev@mozilla.com") is True + assert await authorizer.is_authorized("dev@mozilla.com") == "dev" request = requests[0] assert request.url.host == "bugzilla.example.com" assert request.url.path == "/rest/user" assert request.url.params["names"] == "dev@mozilla.com" assert request.url.params["groups"] == AUTHORIZED_GROUP_NAME + assert request.url.params["include_fields"] == "name,nick" assert request.url.params["permissive"] == "1" assert request.headers["X-Bugzilla-API-Key"] == BUGZILLA_API_KEY @@ -95,7 +97,7 @@ async def test_lookup_rejects_non_member(monkeypatch): # An existing account outside the group is filtered out server-side # (live BMO shape: empty ``users``, empty ``faults``). authorizer, _ = _http_authorizer(monkeypatch, {"users": [], "faults": []}) - assert await authorizer.is_authorized("outsider@example.com") is False + assert await authorizer.is_authorized("outsider@example.com") is None async def test_lookup_rejects_unknown_user(monkeypatch): @@ -114,4 +116,4 @@ async def test_lookup_rejects_unknown_user(monkeypatch): ], }, ) - assert await authorizer.is_authorized("ghost@example.com") is False + assert await authorizer.is_authorized("ghost@example.com") is None diff --git a/services/hackbot-api/tests/test_webhooks.py b/services/hackbot-api/tests/test_webhooks.py index 2e1175d9a79..acaec2b24f7 100644 --- a/services/hackbot-api/tests/test_webhooks.py +++ b/services/hackbot-api/tests/test_webhooks.py @@ -603,9 +603,15 @@ def authorizer(): return _FakeAuthorizer() +class _FakeBugzillaAuthorizer(_FakeAuthorizer): + async def is_authorized(self, actor): + self.checked.append(actor) + return "sparky" if self.allowed else None + + @pytest.fixture def bugzilla_authorizer(): - return _FakeAuthorizer() + return _FakeBugzillaAuthorizer() @pytest.fixture @@ -904,7 +910,7 @@ def test_bugzilla_route_triggers_run(client, bugzilla_authorizer): Identity( identity_provider="bugzilla", user_id="560562", - username="Greg Mierzwinski [:sparky]", + username="sparky", ), ) ] From 158e067ea45f26468e7efa3528fcb6a8d642a99a Mon Sep 17 00:00:00 2001 From: ayoubdiourin7 Date: Thu, 26 Nov 2026 07:04:26 +0100 Subject: [PATCH 3/3] Use the source and identity provider enums for Bugzilla needinfos --- services/hackbot-api/app/routers/webhooks.py | 4 ++-- services/hackbot-api/tests/test_webhooks.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/services/hackbot-api/app/routers/webhooks.py b/services/hackbot-api/app/routers/webhooks.py index 6e2db0ea003..c99a186b43a 100644 --- a/services/hackbot-api/app/routers/webhooks.py +++ b/services/hackbot-api/app/routers/webhooks.py @@ -245,9 +245,9 @@ async def bugzilla_webhook( "comment": detected.comment, }, dedupe_key=f"ni{detected.flag_id}", - source="bugzilla", + source=Source.bugzilla, identity=Identity( - identity_provider="bugzilla", + identity_provider=IdentityProvider.bugzilla, user_id=str(detected.user_id), username=nick, ), diff --git a/services/hackbot-api/tests/test_webhooks.py b/services/hackbot-api/tests/test_webhooks.py index acaec2b24f7..45c11216a7f 100644 --- a/services/hackbot-api/tests/test_webhooks.py +++ b/services/hackbot-api/tests/test_webhooks.py @@ -33,7 +33,13 @@ ) from app.routers import webhooks from fastapi.testclient import TestClient -from hackbot_client import Identity, RunStatus, TriggeredRun +from hackbot_client import ( + Identity, + IdentityProvider, + RunStatus, + Source, + TriggeredRun, +) from sqlalchemy.dialects import postgresql SECRET = "test-secret" @@ -906,9 +912,9 @@ def test_bugzilla_route_triggers_run(client, bugzilla_authorizer): ] assert fake_api.identities == [ ( - "bugzilla", + Source.bugzilla, Identity( - identity_provider="bugzilla", + identity_provider=IdentityProvider.bugzilla, user_id="560562", username="sparky", ),