Skip to content

Commit fcc45a0

Browse files
committed
do pull request as async
1 parent fe508a2 commit fcc45a0

File tree

6 files changed

+65
-28
lines changed

6 files changed

+65
-28
lines changed

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from app.core.config import (
1414
BOT_MESSAGES,
1515
constants,
16+
get_bot_token,
1617
get_logger,
1718
)
1819
from app.services.bedrock import query_bedrock
@@ -175,7 +176,7 @@ def process_async_slack_event(slack_event_data: Dict[str, Any]):
175176
"""
176177
event = slack_event_data["event"]
177178
event_id = slack_event_data["event_id"]
178-
token = slack_event_data["bot_token"]
179+
token = get_bot_token()
179180

180181
client = WebClient(token=token)
181182

packages/slackBotFunction/app/slack/slack_handlers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,6 @@ def _common_message_handler(
316316
):
317317
channel_id = event["channel"]
318318
user_id = event.get("user", "unknown")
319-
bot_token = get_bot_token()
320319
if message_text.lower().startswith(constants.FEEDBACK_PREFIX):
321320
feedback_text = message_text.split(":", 1)[1].strip() if ":" in message_text else ""
322321
try:
@@ -349,9 +348,9 @@ def _common_message_handler(
349348
if message_text.lower().startswith(constants.PULL_REQUEST_PREFIX):
350349
try:
351350
pull_request_id, extracted_message = _extract_pull_request_id(message_text)
352-
trigger_pull_request_processing(pull_request_id=pull_request_id, body=body)
351+
trigger_pull_request_processing(pull_request_id=pull_request_id, event=event, event_id=event_id)
353352
except Exception as e:
354353
logger.error(f"Can not find pull request details: {e}", extra={"error": traceback.format_exc()})
355354
return
356355

357-
trigger_async_processing({"event": event, "event_id": event_id, "bot_token": bot_token})
356+
trigger_async_processing(event=event, event_id=event_id)

packages/slackBotFunction/app/utils/handler_utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def is_duplicate_event(event_id):
4040
return False
4141

4242

43-
def trigger_async_processing(event_data):
43+
def trigger_async_processing(event: Dict[str, Any], event_id: str):
4444
"""
4545
Trigger asynchronous Lambda invocation to process Slack events
4646
@@ -49,14 +49,16 @@ def trigger_async_processing(event_data):
4949
actual AI processing without blocking the initial Slack response.
5050
"""
5151
# incase we fail to re-invoke the lambda we should log an error
52+
lambda_client: LambdaClient = boto3.client("lambda")
5253
try:
53-
lambda_client: LambdaClient = boto3.client("lambda")
54+
logger.debug("Triggering async lambda processing")
55+
lambda_payload = {"async_processing": True, "slack_event": {"event": event, "event_id": event_id}}
5456
lambda_client.invoke(
5557
FunctionName=os.environ["AWS_LAMBDA_FUNCTION_NAME"],
5658
InvocationType="Event",
57-
Payload=json.dumps({"async_processing": True, "slack_event": event_data}),
59+
Payload=json.dumps(lambda_payload),
5860
)
59-
logger.info("Async processing triggered successfully")
61+
logger.debug("Async processing triggered successfully")
6062
except Exception:
6163
logger.error("Failed to trigger async processing", extra={"error": traceback.format_exc()})
6264

@@ -72,7 +74,7 @@ def respond_with_eyes(bot_token: str, event: Dict[str, Any]):
7274
logger.warning("Failed to respond with eyes", extra={"error": traceback.format_exc()})
7375

7476

75-
def trigger_pull_request_processing(pull_request_id: str, body: Dict[str, Any]):
77+
def trigger_pull_request_processing(pull_request_id: str, event: Dict[str, Any], event_id: str):
7678
cloudformation_client: CloudFormationClient = boto3.client("cloudformation")
7779
lambda_client: LambdaClient = boto3.client("lambda")
7880
try:
@@ -82,7 +84,7 @@ def trigger_pull_request_processing(pull_request_id: str, body: Dict[str, Any]):
8284

8385
pull_request_lambda_arn = outputs.get("SlackBotLambdaArn")
8486
logger.debug("Triggering pull request lambda", extra={"lambda_arn": pull_request_lambda_arn})
85-
lambda_payload = {"body": json.dumps(body)}
87+
lambda_payload = {"async_processing": True, "slack_event": {"event": event, "event_id": event_id}}
8688
response = lambda_client.invoke(
8789
FunctionName=pull_request_lambda_arn, InvocationType="Event", Payload=json.dumps(lambda_payload)
8890
)

packages/slackBotFunction/tests/test_async_processing.py

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
@patch("app.services.query_reformulator.reformulate_query")
99
@patch("app.slack.slack_events.get_conversation_session")
1010
def test_process_async_slack_event_success(
11-
mock_get_session, mock_reformulate_query, mock_query_bedrock, mock_get_state_information, mock_webclient, mock_env
11+
mock_get_session: Mock,
12+
mock_reformulate_query: Mock,
13+
mock_query_bedrock: Mock,
14+
mock_get_state_information: Mock,
15+
mock_webclient: Mock,
16+
mock_get_parameter: Mock,
17+
mock_env: Mock,
1218
):
1319
"""Test successful async event processing"""
1420
# set up mocks
@@ -42,7 +48,7 @@ def test_process_async_slack_event_success(
4248

4349

4450
@patch("slack_sdk.WebClient")
45-
def test_process_async_slack_event_empty_query(mock_webclient, mock_env):
51+
def test_process_async_slack_event_empty_query(mock_webclient: Mock, mock_get_parameter: Mock, mock_env: Mock):
4652
"""Test async event processing with empty query"""
4753
# set up mocks
4854
mock_client = Mock()
@@ -80,7 +86,13 @@ def test_process_async_slack_event_empty_query(mock_webclient, mock_env):
8086
@patch("app.services.query_reformulator.reformulate_query")
8187
@patch("app.slack.slack_events.get_conversation_session")
8288
def test_process_async_slack_event_error(
83-
mock_get_session, mock_reformulate_query, mock_query_bedrock, mock_get_state_information, mock_webclient, mock_env
89+
mock_get_session: Mock,
90+
mock_reformulate_query: Mock,
91+
mock_query_bedrock: Mock,
92+
mock_get_state_information: Mock,
93+
mock_webclient: Mock,
94+
mock_get_parameter: Mock,
95+
mock_env: Mock,
8496
):
8597
"""Test async event processing with error"""
8698
# set up mocks
@@ -117,7 +129,13 @@ def test_process_async_slack_event_error(
117129
@patch("app.services.query_reformulator.reformulate_query")
118130
@patch("app.slack.slack_events.get_conversation_session")
119131
def test_process_async_slack_event_with_thread_ts(
120-
mock_get_session, mock_reformulate_query, mock_query_bedrock, mock_get_state_information, mock_webclient, mock_env
132+
mock_get_session: Mock,
133+
mock_reformulate_query: Mock,
134+
mock_query_bedrock: Mock,
135+
mock_get_state_information: Mock,
136+
mock_webclient: Mock,
137+
mock_get_parameter: Mock,
138+
mock_env: Mock,
121139
):
122140
"""Test async event processing with existing thread_ts"""
123141
# set up mocks
@@ -162,7 +180,13 @@ def test_process_async_slack_event_with_thread_ts(
162180
@patch("app.services.query_reformulator.reformulate_query")
163181
@patch("app.slack.slack_events.get_conversation_session")
164182
def test_regex_text_processing(
165-
mock_get_session, mock_reformulate_query, mock_query_bedrock, mock_get_state_information, mock_webclient, mock_env
183+
mock_get_session: Mock,
184+
mock_reformulate_query: Mock,
185+
mock_query_bedrock: Mock,
186+
mock_get_state_information: Mock,
187+
mock_webclient: Mock,
188+
mock_get_parameter: Mock,
189+
mock_env: Mock,
166190
):
167191
"""Test regex text processing functionality within process_async_slack_event"""
168192
# set up mocks
@@ -199,12 +223,13 @@ def test_regex_text_processing(
199223
@patch("app.services.bedrock.query_bedrock")
200224
@patch("app.services.query_reformulator.reformulate_query")
201225
def test_process_async_slack_event_with_session_storage(
202-
mock_reformulate_query,
203-
mock_query_bedrock,
204-
mock_store_state_information,
205-
mock_get_state_information,
206-
mock_webclient,
207-
mock_env,
226+
mock_reformulate_query: Mock,
227+
mock_query_bedrock: Mock,
228+
mock_store_state_information: Mock,
229+
mock_get_state_information: Mock,
230+
mock_webclient: Mock,
231+
mock_get_parameter: Mock,
232+
mock_env: Mock,
208233
):
209234
"""Test async event processing that stores a new session"""
210235
# set up mocks
@@ -242,7 +267,13 @@ def test_process_async_slack_event_with_session_storage(
242267
@patch("app.services.query_reformulator.reformulate_query")
243268
@patch("app.slack.slack_events.get_conversation_session")
244269
def test_process_async_slack_event_chat_update_error(
245-
mock_get_session, mock_reformulate_query, mock_query_bedrock, mock_get_state_information, mock_webclient, mock_env
270+
mock_get_session: Mock,
271+
mock_reformulate_query: Mock,
272+
mock_query_bedrock: Mock,
273+
mock_get_state_information: Mock,
274+
mock_webclient: Mock,
275+
mock_get_parameter: Mock,
276+
mock_env: Mock,
246277
):
247278
"""Test process_async_slack_event with chat_update error"""
248279
# set up mocks
@@ -277,7 +308,13 @@ def test_process_async_slack_event_chat_update_error(
277308
@patch("app.services.query_reformulator.reformulate_query")
278309
@patch("app.slack.slack_events.get_conversation_session")
279310
def test_process_async_slack_event_dm_context(
280-
mock_get_session, mock_reformulate_query, mock_query_bedrock, mock_get_state_information, mock_webclient, mock_env
311+
mock_get_session: Mock,
312+
mock_reformulate_query: Mock,
313+
mock_query_bedrock: Mock,
314+
mock_get_state_information: Mock,
315+
mock_webclient: Mock,
316+
mock_get_parameter: Mock,
317+
mock_env: Mock,
281318
):
282319
"""Test process_async_slack_event with DM context"""
283320
# set up mocks

packages/slackBotFunction/tests/test_slack_handlers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ def decorator(func):
5353

5454
# assertions
5555
mock_ack.assert_called_once()
56-
mock_trigger_async_processing.assert_called_once_with(
57-
{"event": mock_event, "event_id": "evt123", "bot_token": "test-token"}
58-
)
56+
mock_trigger_async_processing.assert_called_once_with(event=mock_event, event_id="evt123")
5957
mock_respond_with_eyes.assert_called_once()
6058

6159

packages/slackBotFunction/tests/test_state_management.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_trigger_async_processing_error(mock_boto_client, mock_env):
7373

7474
event_data = {"test": "data"}
7575
# Should not raise exception even if Lambda invoke fails
76-
trigger_async_processing(event_data)
76+
trigger_async_processing(event=event_data, event_id="evt123")
7777

7878
mock_boto_client.assert_called_once_with("lambda")
7979
mock_lambda_client.invoke.assert_called_once()
@@ -93,7 +93,7 @@ def test_trigger_async_processing(
9393
from app.utils.handler_utils import trigger_async_processing
9494

9595
event_data = {"test": "data"}
96-
trigger_async_processing(event_data)
96+
trigger_async_processing(event=event_data, event_id="evt123")
9797

9898
mock_boto_client.assert_called_once_with("lambda")
9999
mock_lambda_client.invoke.assert_called_once()

0 commit comments

Comments
 (0)