@@ -188,6 +188,10 @@ def handle_early_account_revocation( # noqa: PLR0913
188188 )
189189
190190 # 4. Update header and post confirmation to thread
191+ logger .info (
192+ "Posting early revoke Slack updates" ,
193+ extra = {"post_update_to_slack" : cfg .post_update_to_slack , "thread_ts" : thread_ts , "schedule_name" : schedule_name },
194+ )
191195 if cfg .post_update_to_slack and thread_ts :
192196 # Update the original message header to ACCESS ENDED
193197 message = slack_helpers .get_message_from_timestamp (
@@ -216,21 +220,31 @@ def handle_early_account_revocation( # noqa: PLR0913
216220 thread_ts = thread_ts ,
217221 )
218222 elif cfg .post_update_to_slack :
219- # Full message when not in a thread
220- account = organizations .describe_account (org_client , user_account_assignment .account_id )
221- mention = slack_helpers .create_slack_mention_by_principal_id (
222- sso_user_id = user_account_assignment .user_principal_id ,
223- sso_client = sso_client ,
224- cfg = cfg ,
225- identitystore_client = identitystore_client ,
226- slack_client = slack_client ,
227- )
228- reason_text = f" Reason: { reason } " if reason else ""
229- text = f"<@{ revoker_slack_id } > ended the session early for { mention } (role { permission_set .name } in { account .name } ).{ reason_text } "
230- return slack_client .chat_postMessage (
231- channel = cfg .slack_channel_id ,
232- text = text ,
233- )
223+ # Full message when not in a thread (fallback)
224+ logger .info ("No thread_ts available, posting to channel instead" )
225+ try :
226+ account = organizations .describe_account (org_client , user_account_assignment .account_id )
227+ mention = slack_helpers .create_slack_mention_by_principal_id (
228+ sso_user_id = user_account_assignment .user_principal_id ,
229+ sso_client = sso_client ,
230+ cfg = cfg ,
231+ identitystore_client = identitystore_client ,
232+ slack_client = slack_client ,
233+ )
234+ reason_text = f" Reason: { reason } " if reason else ""
235+ text = f"<@{ revoker_slack_id } > ended the session early for { mention } (role { permission_set .name } in { account .name } ).{ reason_text } "
236+ return slack_client .chat_postMessage (
237+ channel = cfg .slack_channel_id ,
238+ text = text ,
239+ )
240+ except Exception as e :
241+ logger .error ("Failed to post early revoke message to channel" , extra = {"error" : str (e )})
242+ return slack_client .chat_postMessage (
243+ channel = cfg .slack_channel_id ,
244+ text = f"<@{ revoker_slack_id } > ended a session early." ,
245+ )
246+ else :
247+ logger .info ("Slack updates disabled (post_update_to_slack=False)" )
234248
235249
236250def handle_early_group_revocation ( # noqa: PLR0913
@@ -291,6 +305,10 @@ def handle_early_group_revocation( # noqa: PLR0913
291305 )
292306
293307 # 4. Update header and post confirmation to thread
308+ logger .info (
309+ "Posting early revoke Slack updates" ,
310+ extra = {"post_update_to_slack" : cfg .post_update_to_slack , "thread_ts" : thread_ts , "schedule_name" : schedule_name },
311+ )
294312 if cfg .post_update_to_slack and thread_ts :
295313 # Update the original message header to ACCESS ENDED
296314 message = slack_helpers .get_message_from_timestamp (
@@ -319,20 +337,30 @@ def handle_early_group_revocation( # noqa: PLR0913
319337 thread_ts = thread_ts ,
320338 )
321339 elif cfg .post_update_to_slack :
322- # Full message when not in a thread
323- mention = slack_helpers .create_slack_mention_by_principal_id (
324- sso_user_id = group_assignment .user_principal_id ,
325- sso_client = sso_client ,
326- cfg = cfg ,
327- identitystore_client = identitystore_client ,
328- slack_client = slack_client ,
329- )
330- reason_text = f" Reason: { reason } " if reason else ""
331- text = f"<@{ revoker_slack_id } > ended the session early for { mention } (group { group_assignment .group_name } ).{ reason_text } "
332- return slack_client .chat_postMessage (
333- channel = cfg .slack_channel_id ,
334- text = text ,
335- )
340+ # Full message when not in a thread (fallback)
341+ logger .info ("No thread_ts available, posting to channel instead" )
342+ try :
343+ mention = slack_helpers .create_slack_mention_by_principal_id (
344+ sso_user_id = group_assignment .user_principal_id ,
345+ sso_client = sso_client ,
346+ cfg = cfg ,
347+ identitystore_client = identitystore_client ,
348+ slack_client = slack_client ,
349+ )
350+ reason_text = f" Reason: { reason } " if reason else ""
351+ text = f"<@{ revoker_slack_id } > ended the session early for { mention } (group { group_assignment .group_name } ).{ reason_text } "
352+ return slack_client .chat_postMessage (
353+ channel = cfg .slack_channel_id ,
354+ text = text ,
355+ )
356+ except Exception as e :
357+ logger .error ("Failed to post early revoke message to channel" , extra = {"error" : str (e )})
358+ return slack_client .chat_postMessage (
359+ channel = cfg .slack_channel_id ,
360+ text = f"<@{ revoker_slack_id } > ended a session early." ,
361+ )
362+ else :
363+ logger .info ("Slack updates disabled (post_update_to_slack=False)" )
336364
337365
338366def handle_account_assignment_deletion ( # noqa: PLR0913
0 commit comments