Skip to content
Open
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
8 changes: 7 additions & 1 deletion services/hackbot-api/app/routers/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Response,
status,
)
from hackbot_client import HackbotClient
from hackbot_client import HackbotClient, Identity, IdentityProvider, Source
from phabricator_client import PhabricatorClient
from slack_bolt.adapter.fastapi.async_handler import AsyncSlackRequestHandler
from sqlalchemy.ext.asyncio import AsyncSession
Expand Down Expand Up @@ -354,6 +354,12 @@ async def _trigger_github_mention(
"comment": mention.comment,
},
dedupe_key=mention.dedupe_key,
source=Source.github,
identity=Identity(
identity_provider=IdentityProvider.github,
user_id=str(mention.sender["id"]),
username=mention.sender["login"],
),
)
if not run.is_new:
log.info(
Expand Down
35 changes: 29 additions & 6 deletions services/hackbot-api/tests/test_github_webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from fastapi.testclient import TestClient
from hackbot_client import RunStatus, TriggeredRun
from hackbot_client import (
Identity,
IdentityProvider,
RunStatus,
Source,
TriggeredRun,
)
from pydantic import ValidationError

SECRET = "test-github-webhook-secret"
Expand Down Expand Up @@ -165,7 +171,7 @@ def _base(action: str, sender: dict | None = None) -> dict:
"full_name": "mozilla/gecko-dev",
"owner": {"login": "mozilla"},
},
"sender": sender or {"login": "dev", "type": "User"},
"sender": sender or {"id": 42, "login": "dev", "type": "User"},
}


Expand Down Expand Up @@ -224,7 +230,7 @@ def test_detect_issue_comment_on_pr():
owner="mozilla",
repo="gecko-dev",
pr_number=7,
sender={"login": "dev", "type": "User"},
sender={"id": 42, "login": "dev", "type": "User"},
comment=(
' <comment comment_id="1001" type="regular">\n'
" @hackbot fix\n"
Expand Down Expand Up @@ -399,10 +405,14 @@ class _FakeHackbotClient:
def __init__(self):
self.calls = []
self.dedupe_keys = []
self.identities = []
self.delay = 0.0

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))
is_new = dedupe_key is None or dedupe_key not in self.dedupe_keys
self.dedupe_keys.append(dedupe_key)
# After recording: like the real API, the run exists even if the caller
Expand Down Expand Up @@ -562,7 +572,12 @@ def test_route_ignores_unauthorized_sender(client, api, authorizer, head_lookups
resp = _post(client, "issue_comment", _issue_comment())
assert resp.json() == {"status": "ignored", "reason": "unauthorized user"}
assert authorizer.checked == [
(INSTALLATION_ID, "mozilla", "gecko-dev", {"login": "dev", "type": "User"})
(
INSTALLATION_ID,
"mozilla",
"gecko-dev",
{"id": 42, "login": "dev", "type": "User"},
)
]
# Unauthorized senders cost no further GitHub calls.
assert head_lookups == []
Expand Down Expand Up @@ -635,6 +650,14 @@ def test_route_triggers_run_for_conversation_comment(
)
]
assert api.dedupe_keys == ["github-issue-comment:1001"]
assert api.identities == [
(
Source.github,
Identity(
identity_provider=IdentityProvider.github, user_id="42", username="dev"
),
)
]


@pytest.mark.parametrize(
Expand Down Expand Up @@ -744,7 +767,7 @@ def test_route_surfaces_trigger_failure(client):
# A failure before the run exists spends no key, so a manual redelivery from
# the App's settings is processed rather than deduped away.
class _FailingClient:
async def trigger_run(self, agent_name, inputs, *, dedupe_key=None):
async def trigger_run(self, agent_name, inputs, **kwargs):
raise RuntimeError("api down")

app.dependency_overrides[get_hackbot_client] = lambda: _FailingClient()
Expand Down