|
5 | 5 |
|
6 | 6 | from botocore.client import BaseClient |
7 | 7 |
|
| 8 | +from slack_sdk.errors import SlackClientConfigurationError |
8 | 9 | from slack_sdk.oauth.installation_store.async_installation_store import ( |
9 | 10 | AsyncInstallationStore, |
10 | 11 | ) |
@@ -252,7 +253,7 @@ def delete_bot( |
252 | 253 | ) |
253 | 254 | except Exception as e: # skipcq: PYL-W0703 |
254 | 255 | message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}" |
255 | | - self.logger.warning(message) |
| 256 | + raise SlackClientConfigurationError(message) |
256 | 257 |
|
257 | 258 | async def async_delete_installation( |
258 | 259 | self, |
@@ -282,15 +283,52 @@ def delete_installation( |
282 | 283 | Bucket=self.bucket_name, |
283 | 284 | Prefix=f"{workspace_path}/installer-{user_id or ''}", |
284 | 285 | ) |
| 286 | + deleted_keys = [] |
285 | 287 | for content in objects.get("Contents", []): |
286 | 288 | key = content.get("Key") |
287 | 289 | if key is not None: |
288 | 290 | self.logger.info(f"Going to delete installation ({key})") |
289 | 291 | try: |
290 | 292 | self.s3_client.delete_object( |
291 | 293 | Bucket=self.bucket_name, |
292 | | - Key=content.get("Key"), |
| 294 | + Key=key, |
293 | 295 | ) |
| 296 | + deleted_keys.append(key) |
| 297 | + except Exception as e: # skipcq: PYL-W0703 |
| 298 | + message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}" |
| 299 | + raise SlackClientConfigurationError(message) |
| 300 | + |
| 301 | + try: |
| 302 | + no_user_id_key = key.replace(f"-{user_id}", "") |
| 303 | + if not no_user_id_key.endswith("installer-latest"): |
| 304 | + self.s3_client.delete_object( |
| 305 | + Bucket=self.bucket_name, |
| 306 | + Key=no_user_id_key, |
| 307 | + ) |
| 308 | + deleted_keys.append(no_user_id_key) |
294 | 309 | except Exception as e: # skipcq: PYL-W0703 |
295 | 310 | message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}" |
296 | | - self.logger.warning(message) |
| 311 | + raise SlackClientConfigurationError(message) |
| 312 | + |
| 313 | + # Check the remaining installation data |
| 314 | + objects = self.s3_client.list_objects( |
| 315 | + Bucket=self.bucket_name, |
| 316 | + Prefix=f"{workspace_path}/installer-", |
| 317 | + MaxKeys=10, # the small number would be enough for this purpose |
| 318 | + ) |
| 319 | + keys = [ |
| 320 | + c.get("Key") |
| 321 | + for c in objects.get("Contents", []) |
| 322 | + if c.get("Key") not in deleted_keys |
| 323 | + ] |
| 324 | + # If only installer-latest remains, we should delete the one as well |
| 325 | + if len(keys) == 1 and keys[0].endswith("installer-latest"): |
| 326 | + content = objects.get("Contents", [])[0] |
| 327 | + try: |
| 328 | + self.s3_client.delete_object( |
| 329 | + Bucket=self.bucket_name, |
| 330 | + Key=content.get("Key"), |
| 331 | + ) |
| 332 | + except Exception as e: # skipcq: PYL-W0703 |
| 333 | + message = f"Failed to find bot installation data for enterprise: {e_id}, team: {t_id}: {e}" |
| 334 | + raise SlackClientConfigurationError(message) |
0 commit comments