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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any
from uuid import UUID

from hackbot_client import HackbotClient
from hackbot_client import HackbotClient, Identity, IdentityProvider, Source
from pydantic import AliasChoices, BaseModel, ConfigDict, Field, Json, validate_call
from slack_bolt.context.ack.async_ack import AsyncAck
from slack_bolt.context.async_context import AsyncBoltContext
Expand Down Expand Up @@ -37,7 +37,8 @@ class SlackUser(BaseModel):
https://docs.slack.dev/reference/objects/user-object/
"""

id: str | None = None
id: str
username: str


class BlockActionsPayload(BaseModel):
Expand All @@ -46,7 +47,7 @@ class BlockActionsPayload(BaseModel):
https://docs.slack.dev/reference/interaction-payloads/block_actions-payload/
"""

user: SlackUser | None = None
user: SlackUser
message: dict


Expand Down Expand Up @@ -127,7 +128,7 @@ async def start_agent_run_callback(
should be fixed with https://github.com/mozilla/ai-dev/issues/6468.
"""
client: HackbotClient = context["hackbot_client"]
user = body.user.id if body.user else None
user = body.user.id
value = action.value

if value.apply_run_id:
Expand All @@ -151,7 +152,15 @@ async def start_agent_run_callback(
return

run = await client.trigger_run(
value.agent_name, value.inputs, dedupe_key=value.dedupe_key
value.agent_name,
value.inputs,
dedupe_key=value.dedupe_key,
source=Source.slack,
identity=Identity(
identity_provider=IdentityProvider.slack,
user_id=body.user.id,
username=body.user.username,
),
)

triggered_by = user if run.is_new else None
Expand Down
35 changes: 27 additions & 8 deletions services/hackbot-api/tests/test_slack_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@
StartAgentRunValue,
start_agent_run_callback,
)
from hackbot_client import ApplyActionsResponse, RunAction, RunStatus, TriggeredRun
from hackbot_client import (
ApplyActionsResponse,
Identity,
IdentityProvider,
RunAction,
RunStatus,
Source,
TriggeredRun,
)
from pydantic import ValidationError
from slack_bolt.context.ack.async_ack import AsyncAck
from slack_bolt.context.async_context import AsyncBoltContext
Expand Down Expand Up @@ -99,7 +107,7 @@ def setup_method(self):
)
self.context = AsyncBoltContext({"hackbot_client": self.fake_client})
self.body = {
"user": {"id": "U0CLICKER"},
"user": {"id": "U0CLICKER", "username": "clicker"},
"channel": {"id": "C0TRIAGE"},
"message": _message(_action()),
}
Expand Down Expand Up @@ -133,16 +141,16 @@ async def _call(self, action: dict | None = None):
async def test_starts_the_run_the_button_asks_for(self):
await self._call()

self.fake_client.trigger_run.assert_awaited_once_with(
"bug-fix", {"bug_id": 1234}, dedupe_key=DEDUPE_KEY
)
self.fake_client.trigger_run.assert_awaited_once()
args = self.fake_client.trigger_run.await_args
assert args.args == ("bug-fix", {"bug_id": 1234})
assert args.kwargs["dedupe_key"] == DEDUPE_KEY

async def test_the_agent_and_inputs_come_from_the_buttons_value(self):
await self._call(_action(agent_name="test-repair", inputs={"task_id": "abc"}))

self.fake_client.trigger_run.assert_awaited_once_with(
"test-repair", {"task_id": "abc"}, dedupe_key=DEDUPE_KEY
)
args = self.fake_client.trigger_run.await_args
assert args.args == ("test-repair", {"task_id": "abc"})

async def test_the_key_is_forwarded_so_the_api_can_collapse_repeats(self):
# The whole of #6732: the receiver keeps no state, so this key reaching
Expand All @@ -153,6 +161,17 @@ async def test_the_key_is_forwarded_so_the_api_can_collapse_repeats(self):
"frontend-triage-run:abc"
)

async def test_the_clicker_is_recorded_as_the_runs_account(self):
await self._call()

kwargs = self.fake_client.trigger_run.await_args.kwargs
assert kwargs["source"] == Source.slack
assert kwargs["identity"] == Identity(
identity_provider=IdentityProvider.slack,
user_id="U0CLICKER",
username="clicker",
)

async def test_the_click_is_acknowledged(self):
await self._call()

Expand Down