Skip to content

Commit d79cdfa

Browse files
authored
Merge pull request #9 from PostHog/tom/fix-early-revoker-message
Add logging to debug early revoke not posting message
2 parents 5d89515 + 038e15b commit d79cdfa

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
lines changed

src/main.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,13 @@ def handle_early_revoke_button_click(body: dict, client: WebClient, context: Bol
613613
channel_id = jp.search("channel.id", body)
614614
thread_ts = jp.search("message.thread_ts", body) or jp.search("message.ts", body)
615615

616+
logger.info(
617+
"Early revoke button context",
618+
extra={"thread_ts": thread_ts, "channel_id": channel_id, "clicker_slack_id": clicker_slack_id},
619+
)
620+
if not thread_ts:
621+
logger.warning("Could not extract thread_ts from button click body")
622+
616623
# Parse the button value
617624
button_value = jp.search("actions[0].value", body)
618625
try:

src/revoker.py

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

236250
def 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

338366
def handle_account_assignment_deletion( # noqa: PLR0913

src/slack_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ def build(
672672
InputBlock(
673673
block_id=cls.REASON_BLOCK_ID,
674674
optional=True,
675-
label=PlainTextObject(text="Reason (optional)"),
675+
label=PlainTextObject(text="Reason"),
676676
element=PlainTextInputElement(
677677
action_id=cls.REASON_ACTION_ID,
678678
placeholder=PlainTextObject(text="e.g. Task completed, no longer needed"),

0 commit comments

Comments
 (0)