@@ -53,52 +53,60 @@ def respond_with_eyes(event: Dict[str, Any], client: WebClient) -> None:
5353 logger .warning ("Failed to respond with eyes" , extra = {"error" : traceback .format_exc ()})
5454
5555
56- def forward_event_to_pull_request_lambda (
57- pull_request_id : str , event : Dict [str , Any ], event_id : str , store_pull_request_id : bool
58- ) -> None :
56+ def get_pull_request_lambda_arn (pull_request_id : str ) -> str :
5957 cloudformation_client : CloudFormationClient = boto3 .client ("cloudformation" )
60- lambda_client : LambdaClient = boto3 .client ("lambda" )
6158 try :
6259 logger .debug ("Getting arn for pull request" , extra = {"pull_request_id" : pull_request_id })
6360 response = cloudformation_client .describe_stacks (StackName = f"epsam-pr-{ pull_request_id } " )
6461 outputs = {o ["OutputKey" ]: o ["OutputValue" ] for o in response ["Stacks" ][0 ]["Outputs" ]}
65-
6662 pull_request_lambda_arn = outputs .get ("SlackBotLambdaArn" )
67- logger .debug ("Triggering pull request lambda" , extra = {"lambda_arn" : pull_request_lambda_arn })
63+ return pull_request_lambda_arn
64+ except Exception as e :
65+ logger .error ("Failed to get pull request lambda arn" , extra = {"error" : traceback .format_exc ()})
66+ raise e
67+
68+
69+ def forward_event_to_pull_request_lambda (
70+ pull_request_id : str , event : Dict [str , Any ], event_id : str , store_pull_request_id : bool
71+ ) -> None :
72+ lambda_client : LambdaClient = boto3 .client ("lambda" )
73+ try :
74+ pull_request_lambda_arn = get_pull_request_lambda_arn (pull_request_id = pull_request_id )
75+ # strip pull request prefix and id from message text
6876 message_text = event ["text" ]
6977 _ , extracted_message = extract_pull_request_id (message_text )
7078 event ["text" ] = extracted_message
79+
7180 lambda_payload = {"pull_request_event" : True , "slack_event" : {"event" : event , "event_id" : event_id }}
72- response = lambda_client .invoke (
81+ logger .debug (
82+ "Forwarding event to pull request lambda" ,
83+ extra = {"lambda_arn" : pull_request_lambda_arn , "lambda_payload" : lambda_payload },
84+ )
85+ lambda_client .invoke (
7386 FunctionName = pull_request_lambda_arn , InvocationType = "Event" , Payload = json .dumps (lambda_payload )
7487 )
7588 logger .info ("Triggered pull request lambda" , extra = {"lambda_arn" : pull_request_lambda_arn })
7689
7790 if store_pull_request_id :
78- conversation_key , _ , _ = extract_conversation_context (event )
91+ conversation_key , _ = conversation_key_and_root (event )
7992 item = {"pk" : conversation_key , "sk" : constants .PULL_REQUEST_SK , "pull_request_id" : pull_request_id }
93+ store_state_information (item = item )
8094
81- store_state_information (item = item )
8295 except Exception as e :
8396 logger .error ("Failed to trigger pull request lambda" , extra = {"error" : traceback .format_exc ()})
8497 raise e
8598
8699
87100def forward_action_to_pull_request_lambda (body : Dict [str , Any ], pull_request_id : str ) -> None :
88- cloudformation_client : CloudFormationClient = boto3 .client ("cloudformation" )
89101 lambda_client : LambdaClient = boto3 .client ("lambda" )
90102 try :
91- logger .debug ("Getting arn for pull request" , extra = {"pull_request_id" : pull_request_id })
92- response = cloudformation_client .describe_stacks (StackName = f"epsam-pr-{ pull_request_id } " )
93- outputs = {o ["OutputKey" ]: o ["OutputValue" ] for o in response ["Stacks" ][0 ]["Outputs" ]}
94-
95- pull_request_lambda_arn = outputs .get ("SlackBotLambdaArn" )
103+ pull_request_lambda_arn = get_pull_request_lambda_arn (pull_request_id = pull_request_id )
96104 lambda_payload = {"pull_request_action" : True , "slack_body" : body }
97105 logger .debug (
98106 "Forwarding action to pull request lambda" ,
99107 extra = {"lambda_arn" : pull_request_lambda_arn , "lambda_payload" : lambda_payload },
100108 )
101- response = lambda_client .invoke (
109+ lambda_client .invoke (
102110 FunctionName = pull_request_lambda_arn , InvocationType = "Event" , Payload = json .dumps (lambda_payload )
103111 )
104112 logger .info ("Triggered pull request lambda" , extra = {"lambda_arn" : pull_request_lambda_arn })
@@ -152,12 +160,13 @@ def strip_mentions(message_text: str) -> str:
152160 return re .sub (r"<@[UW][A-Z0-9]+(\|[^>]+)?>" , "" , message_text or "" ).strip ()
153161
154162
155- def extract_pull_request_id (text : str ) -> Tuple [str , str ]:
163+ def extract_pull_request_id (text : str ) -> Tuple [str | None , str ]:
156164 # Regex: PULL_REQUEST_PREFIX + optional space + number + space + rest of text
157165 pattern = re .escape (constants .PULL_REQUEST_PREFIX ) + r"\s*(\d+)\s+(.+)"
158166 match = re .match (pattern , text )
159167 if not match :
160- raise ValueError ("Text does not match expected format (#pr <number> <text>)" )
168+ logger .warning ("Can not extract pull request id from text" , extra = {"text" : text })
169+ return None , text
161170 pr_number = int (match .group (1 ))
162171 rest_text = match .group (2 )
163172 return pr_number , rest_text
@@ -181,18 +190,6 @@ def conversation_key_and_root(event: Dict[str, Any]) -> Tuple[str, str]:
181190 return f"{ constants .THREAD_PREFIX } { channel_id } #{ root } " , root
182191
183192
184- def extract_conversation_context (event : Dict [str , Any ]) -> Tuple [str , str , str | None ]:
185- """Extract conversation key and thread context from event"""
186- channel = event ["channel" ]
187- # Determine conversation context: DM vs channel thread
188- if event .get ("channel_type" ) == constants .CHANNEL_TYPE_IM :
189- thread_root = event .get ("thread_ts" , event ["ts" ])
190- return f"{ constants .DM_PREFIX } { channel } #{ thread_root } " , constants .CONTEXT_TYPE_THREAD , thread_root
191- else :
192- thread_root = event .get ("thread_ts" , event ["ts" ])
193- return f"{ constants .THREAD_PREFIX } { channel } #{ thread_root } " , constants .CONTEXT_TYPE_THREAD , thread_root
194-
195-
196193def extract_session_pull_request_id (conversation_key : str ) -> str | None :
197194 """Check if the conversation is associated with a pull request"""
198195 logger .debug ("Checking for existing pull request session" , extra = {"conversation_key" : conversation_key })
0 commit comments