Skip to content

Commit da99f59

Browse files
authored
Log a warning if the exception in case add thread flow is users not found (Netflix#4484)
* Log a warning if the exception in case add thread flow is users not found * Move to dedicated function and add docstring * Add participant name to warning message * Continue after exception so that we don't raise * Use else statement in except clause
1 parent ad6803c commit da99f59

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/dispatch/plugins/dispatch_slack/plugin.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
create_conversation,
5050
create_slack_client,
5151
does_user_exist,
52+
emails_to_user_ids,
5253
get_user_avatar_url,
5354
get_user_profile_by_email,
5455
rename_conversation,
@@ -283,8 +284,8 @@ def add(self, conversation_id: str, participants: List[str]):
283284
def add_to_thread(self, conversation_id: str, thread_id: str, participants: List[str]):
284285
"""Adds users to a thread conversation."""
285286
client = create_slack_client(self.configuration)
286-
participants = [resolve_user(client, p)["id"] for p in set(participants)]
287-
add_users_to_conversation_thread(client, conversation_id, thread_id, participants)
287+
user_ids = emails_to_user_ids(client=client, participants=participants)
288+
add_users_to_conversation_thread(client, conversation_id, thread_id, user_ids)
288289

289290
def archive(self, conversation_id: str):
290291
"""Archives a conversation."""

src/dispatch/plugins/dispatch_slack/service.py

+46
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,52 @@ def resolve_user(client: WebClient, user_id: str) -> dict:
3434
return {"id": user_id}
3535

3636

37+
def emails_to_user_ids(client: WebClient, participants: list[str]) -> list[str]:
38+
"""
39+
Resolves a list of email addresses to Slack user IDs.
40+
41+
This function takes a list of email addresses and attempts to resolve them to Slack user IDs.
42+
If a user cannot be found for a given email address, it logs a warning and continues with the next email.
43+
If an error other than a user not found occurs, it logs the exception.
44+
45+
Args:
46+
client (WebClient): A Slack WebClient object used to interact with the Slack API.
47+
participants (list[str]): A list of participant email addresses to resolve.
48+
49+
Returns:
50+
list[str]: A list of resolved user IDs.
51+
52+
Raises:
53+
SlackApiError: If an error other than a user not found occurs.
54+
55+
Example:
56+
>>> from slack_sdk import WebClient
57+
>>> client = WebClient(token="your-slack-token")
58+
59+
>>> user_ids = emails_to_user_ids(client, emails)
60+
>>> print(user_ids)
61+
["U01ABCDE1", "U01ABCDE2"]
62+
"""
63+
user_ids = []
64+
65+
for participant in set(participants):
66+
try:
67+
user_id = resolve_user(client, participant)["id"]
68+
except SlackApiError as e:
69+
msg = f"Unable to resolve Slack participant {participant}: {e}"
70+
71+
if e.response["error"] == SlackAPIErrorCode.USERS_NOT_FOUND:
72+
log.warning(msg)
73+
continue
74+
else:
75+
log.exception(msg)
76+
continue
77+
else:
78+
user_ids.append(user_id)
79+
80+
return user_ids
81+
82+
3783
def chunks(ids, n):
3884
"""Yield successive n-sized chunks from l."""
3985
for i in range(0, len(ids), n):

0 commit comments

Comments
 (0)