Skip to content

Commit 64cfaf1

Browse files
committed
Merge branch 'pull_request_conversation_1' into pull_request_conversation_2
2 parents d285c52 + 0040e3c commit 64cfaf1

File tree

4 files changed

+35
-10
lines changed

4 files changed

+35
-10
lines changed

packages/slackBotFunction/app/slack/slack_handlers.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
conversation_key_and_root,
2626
extract_conversation_context,
2727
extract_pull_request_id,
28+
extract_session_pull_request_id,
2829
gate_common,
2930
is_latest_message,
3031
strip_mentions,
@@ -182,6 +183,12 @@ def feedback_handler(body: Dict[str, Any], client: WebClient) -> None:
182183
# Check if this is the latest message in the conversation
183184
conversation_key = feedback_data["ck"]
184185
message_ts = feedback_data.get("mt")
186+
session_pull_request_id = extract_session_pull_request_id(conversation_key)
187+
if session_pull_request_id:
188+
logger.info(
189+
f"Feedback in pull request session {session_pull_request_id}",
190+
extra={"session_pull_request_id": session_pull_request_id},
191+
)
185192

186193
if message_ts and not is_latest_message(conversation_key=conversation_key, message_ts=message_ts):
187194
logger.info(f"Feedback ignored - not latest message: {message_ts}")
@@ -251,6 +258,12 @@ def _common_message_handler(
251258
channel_id = event["channel"]
252259
user_id = event.get("user", "unknown")
253260
conversation_key, _, thread_ts = extract_conversation_context(event)
261+
session_pull_request_id = extract_session_pull_request_id(conversation_key)
262+
if session_pull_request_id:
263+
logger.info(
264+
f"Message in pull request session {session_pull_request_id} from user {user_id}",
265+
extra={"session_pull_request_id": session_pull_request_id},
266+
)
254267
if message_text.lower().startswith(constants.FEEDBACK_PREFIX):
255268
feedback_text = message_text.split(":", 1)[1].strip() if ":" in message_text else ""
256269
try:
@@ -264,9 +277,7 @@ def _common_message_handler(
264277
feedback_text=feedback_text,
265278
client=client,
266279
)
267-
except Exception as e:
268-
logger.error(f"Failed to store channel feedback via mention: {e}", extra={"error": traceback.format_exc()})
269-
try:
280+
270281
params = {
271282
"channel": channel_id,
272283
"text": bot_messages.FEEDBACK_THANKS,

packages/slackBotFunction/app/utils/handler_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,16 @@ def extract_conversation_context(event: Dict[str, Any]) -> Tuple[str, str, str |
161161
else:
162162
thread_root = event.get("thread_ts", event["ts"])
163163
return f"{constants.THREAD_PREFIX}{channel}#{thread_root}", constants.CONTEXT_TYPE_THREAD, thread_root
164+
165+
166+
def extract_session_pull_request_id(conversation_key: str) -> str | None:
167+
"""Check if the conversation is associated with a pull request"""
168+
try:
169+
response = get_state_information({"pk": conversation_key, "sk": constants.PULL_REQUEST_SK})
170+
if "Item" in response:
171+
logger.info("Found existing pull request session", extra={"conversation_key": conversation_key})
172+
return response["Item"]["pull_request_id"]
173+
return None
174+
except Exception as e:
175+
logger.error(f"Error checking pull request session: {e}", extra={"error": traceback.format_exc()})
176+
return None

packages/slackBotFunction/tests/test_slack_handlers.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,7 @@ def test_common_message_handler_store_feedback_error_handling(
244244
)
245245

246246
# assertions
247-
# Should still try to post message
248-
mock_client.chat_postMessage.assert_called_once_with(
249-
channel="C789", text=bot_messages.FEEDBACK_THANKS, thread_ts="bar"
250-
)
251-
mock_post_error_message.assert_not_called()
247+
mock_post_error_message.assert_called()
252248

253249

254250
@patch("app.slack.slack_events.store_feedback")

packages/slackBotFunction/tests/test_trigger_processing.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77

88
@patch("boto3.client")
9+
@patch("app.services.dynamo.store_state_information")
910
def test_trigger_pull_request_processing(
11+
mock_store_state_information: Mock,
1012
mock_boto_client: Mock,
1113
mock_env: Mock,
1214
):
@@ -41,18 +43,21 @@ def client_side_effect(service_name, *args, **kwargs):
4143
from app.utils.handler_utils import trigger_pull_request_processing
4244

4345
# perform operation
44-
event_data = {"test": "data"}
46+
event_data = {"test": "data", "channel": "C123", "ts": "12345.6789"}
4547
trigger_pull_request_processing(pull_request_id="123", event=event_data, event_id="evt123")
4648

4749
# assertions
4850
expected_lambda_payload = {
4951
"pull_request_processing": True,
50-
"slack_event": {"event": {"test": "data"}, "event_id": "evt123"},
52+
"slack_event": {"event": {"test": "data", "channel": "C123", "ts": "12345.6789"}, "event_id": "evt123"},
5153
}
5254

5355
mock_lambda_client.invoke.assert_called_once_with(
5456
FunctionName="output_SlackBotLambdaArn", InvocationType="Event", Payload=json.dumps(expected_lambda_payload)
5557
)
58+
mock_store_state_information.assert_called_once_with(
59+
item={"pk": "thread#C123#12345.6789", "sk": "pull_request", "pull_request_id": "123"}
60+
)
5661

5762

5863
@patch("boto3.client")

0 commit comments

Comments
 (0)