|
5 | 5 |
|
6 | 6 | from django.core.exceptions import ValidationError
|
7 | 7 |
|
8 |
| -from sentry import features |
9 | 8 | from sentry.integrations.slack.client import SlackClient
|
10 | 9 | from sentry.models import Integration, Organization
|
11 | 10 | from sentry.services.hybrid_cloud.integration import RpcIntegration
|
@@ -61,14 +60,12 @@ def get_channel_id(
|
61 | 60 | else:
|
62 | 61 | timeout = SLACK_DEFAULT_TIMEOUT
|
63 | 62 |
|
64 |
| - # XXX(meredith): For large accounts that have many, many channels it's |
65 |
| - # possible for us to timeout while attempting to paginate through to find the channel id |
| 63 | + # XXX(meredith): For large accounts that have many, many users it's |
| 64 | + # possible for us to timeout while attempting to paginate through to find the user id |
66 | 65 | # This means some users are unable to create/update alert rules. To avoid this, we attempt
|
67 | 66 | # to find the channel id asynchronously if it takes longer than a certain amount of time,
|
68 | 67 | # which I have set as the SLACK_DEFAULT_TIMEOUT - arbitrarily - to 10 seconds.
|
69 | 68 |
|
70 |
| - if features.has("organizations:slack-use-new-lookup", organization): |
71 |
| - return get_channel_id_with_timeout_new(integration, channel_name, timeout) |
72 | 69 | return get_channel_id_with_timeout(integration, channel_name, timeout)
|
73 | 70 |
|
74 | 71 |
|
@@ -106,89 +103,6 @@ def get_channel_id_with_timeout(
|
106 | 103 | integration: Integration | RpcIntegration,
|
107 | 104 | name: Optional[str],
|
108 | 105 | timeout: int,
|
109 |
| -) -> Tuple[str, Optional[str], bool]: |
110 |
| - """ |
111 |
| - Fetches the internal slack id of a channel. |
112 |
| - :param integration: The slack integration |
113 |
| - :param name: The name of the channel |
114 |
| - :param timeout: Our self-imposed time limit. |
115 |
| - :return: a tuple of three values |
116 |
| - 1. prefix: string (`"#"` or `"@"`) |
117 |
| - 2. channel_id: string or `None` |
118 |
| - 3. timed_out: boolean (whether we hit our self-imposed time limit) |
119 |
| - """ |
120 |
| - |
121 |
| - payload = { |
122 |
| - "exclude_archived": False, |
123 |
| - "exclude_members": True, |
124 |
| - "types": "public_channel,private_channel", |
125 |
| - } |
126 |
| - |
127 |
| - list_types = LIST_TYPES |
128 |
| - |
129 |
| - time_to_quit = time.time() + timeout |
130 |
| - |
131 |
| - client = SlackClient(integration_id=integration.id) |
132 |
| - id_data: Optional[Tuple[str, Optional[str], bool]] = None |
133 |
| - found_duplicate = False |
134 |
| - prefix = "" |
135 |
| - for list_type, result_name, prefix in list_types: |
136 |
| - cursor = "" |
137 |
| - while True: |
138 |
| - endpoint = f"/{list_type}.list" |
139 |
| - try: |
140 |
| - # Slack limits the response of `<list_type>.list` to 1000 channels |
141 |
| - items = client.get(endpoint, params=dict(payload, cursor=cursor, limit=1000)) |
142 |
| - except ApiRateLimitedError as e: |
143 |
| - logger.info( |
144 |
| - f"rule.slack.{list_type}_list_rate_limited", |
145 |
| - extra={"error": str(e), "integration_id": integration.id}, |
146 |
| - ) |
147 |
| - raise e |
148 |
| - except ApiError as e: |
149 |
| - logger.info( |
150 |
| - f"rule.slack.{list_type}_list_other_error", |
151 |
| - extra={"error": str(e), "integration_id": integration.id}, |
152 |
| - ) |
153 |
| - return prefix, None, False |
154 |
| - |
155 |
| - if not isinstance(items, dict): |
156 |
| - continue |
157 |
| - |
158 |
| - for c in items[result_name]: |
159 |
| - # The "name" field is unique (this is the username for users) |
160 |
| - # so we return immediately if we find a match. |
161 |
| - # convert to lower case since all names in Slack are lowercase |
162 |
| - if name and c["name"].lower() == name.lower(): |
163 |
| - return prefix, c["id"], False |
164 |
| - # If we don't get a match on a unique identifier, we look through |
165 |
| - # the users' display names, and error if there is a repeat. |
166 |
| - if list_type == "users": |
167 |
| - profile = c.get("profile") |
168 |
| - if profile and profile.get("display_name") == name: |
169 |
| - if id_data: |
170 |
| - found_duplicate = True |
171 |
| - else: |
172 |
| - id_data = (prefix, c["id"], False) |
173 |
| - |
174 |
| - cursor = items.get("response_metadata", {}).get("next_cursor", None) |
175 |
| - if time.time() > time_to_quit: |
176 |
| - return prefix, None, True |
177 |
| - |
178 |
| - if not cursor: |
179 |
| - break |
180 |
| - if found_duplicate: |
181 |
| - raise DuplicateDisplayNameError(name) |
182 |
| - elif id_data: |
183 |
| - return id_data |
184 |
| - |
185 |
| - return prefix, None, False |
186 |
| - |
187 |
| - |
188 |
| -def get_channel_id_with_timeout_new( |
189 |
| - integration: Integration | RpcIntegration, |
190 |
| - name: Optional[str], |
191 |
| - timeout: int, |
192 | 106 | ) -> Tuple[str, Optional[str], bool]:
|
193 | 107 | """
|
194 | 108 | Fetches the internal slack id of a channel using scheduled message.
|
|
0 commit comments