-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
chore(slack): Use Slack SDK Client to Open View for Archive/Resolved #73749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eb70526
updated client, purposely flipping option to see failed tests
iamrajjoshi 60fe3c9
updated tests
iamrajjoshi fb43795
Merge branch 'master' into raj/mes-ref/slack/sdk-2.16_v2
iamrajjoshi 9a54d78
use View object
iamrajjoshi 9b9b4e1
pr feedback
iamrajjoshi bf4b87d
nit
iamrajjoshi c3b38a4
fix typing
iamrajjoshi b0ba8d7
fix typing
iamrajjoshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| from sentry.integrations.slack.message_builder.issues import SlackIssuesMessageBuilder | ||
| from sentry.integrations.slack.requests.action import SlackActionRequest | ||
| from sentry.integrations.slack.requests.base import SlackRequestError | ||
| from sentry.integrations.slack.sdk_client import SlackSdkClient | ||
| from sentry.integrations.slack.views.link_identity import build_linking_url | ||
| from sentry.integrations.slack.views.unlink_identity import build_unlinking_url | ||
| from sentry.integrations.types import ExternalProviderEnum | ||
|
|
@@ -295,7 +296,7 @@ def on_status( | |
| user_id=user.id, | ||
| ) | ||
|
|
||
| def build_resolve_modal_payload(self, callback_id): | ||
| def build_resolve_modal_payload_deprecated(self, callback_id) -> dict[str, Any]: | ||
| formatted_resolve_options = [] | ||
| for text, value in RESOLVE_OPTIONS.items(): | ||
| formatted_resolve_options.append( | ||
|
|
@@ -337,7 +338,7 @@ def build_resolve_modal_payload(self, callback_id): | |
| "callback_id": callback_id, | ||
| } | ||
|
|
||
| def build_archive_modal_payload(self, callback_id): | ||
| def build_archive_modal_payload_deprecated(self, callback_id) -> dict[str, Any]: | ||
| formatted_archive_options = [] | ||
| for text, value in ARCHIVE_OPTIONS.items(): | ||
| formatted_archive_options.append( | ||
|
|
@@ -382,6 +383,78 @@ def build_archive_modal_payload(self, callback_id): | |
| "callback_id": callback_id, | ||
| } | ||
|
|
||
| def build_format_options(self, options: dict[str, str]) -> list[dict[str, Any]]: | ||
| return [ | ||
| { | ||
| "text": { | ||
| "type": "plain_text", | ||
| "text": text, | ||
| "emoji": True, | ||
| }, | ||
| "value": value, | ||
| } | ||
| for text, value in options.items() | ||
| ] | ||
|
|
||
| def build_modal_payload( | ||
| self, | ||
| title: str, | ||
| action_text: str, | ||
| options: dict[str, str], | ||
| initial_option_text: str, | ||
| initial_option_value: str, | ||
| callback_id: str, | ||
| ) -> View: | ||
| formatted_options = self.build_format_options(options) | ||
|
|
||
| return View( | ||
| type="modal", | ||
| title={"type": "plain_text", "text": f"{title} Issue"}, | ||
| blocks=[ | ||
| { | ||
| "type": "section", | ||
| "text": {"type": "mrkdwn", "text": action_text}, | ||
| "accessory": { | ||
| "type": "static_select", | ||
| "initial_option": { | ||
| "text": { | ||
| "type": "plain_text", | ||
| "text": initial_option_text, | ||
| "emoji": True, | ||
| }, | ||
| "value": initial_option_value, | ||
| }, | ||
| "options": formatted_options, | ||
| "action_id": "static_select-action", | ||
| }, | ||
| } | ||
| ], | ||
| close={"type": "plain_text", "text": "Cancel"}, | ||
| submit={"type": "plain_text", "text": title}, | ||
| private_metadata=callback_id, | ||
| callback_id=callback_id, | ||
| ) | ||
|
|
||
| def build_resolve_modal_payload(self, callback_id: str) -> View: | ||
| return self.build_modal_payload( | ||
| title="Resolve", | ||
| action_text="Resolve", | ||
| options=RESOLVE_OPTIONS, | ||
| initial_option_text="Immediately", | ||
| initial_option_value="resolved", | ||
| callback_id=callback_id, | ||
| ) | ||
|
|
||
| def build_archive_modal_payload(self, callback_id: str) -> View: | ||
| return self.build_modal_payload( | ||
| title="Archive", | ||
| action_text="Archive", | ||
| options=ARCHIVE_OPTIONS, | ||
| initial_option_text="Until escalating", | ||
| initial_option_value="ignored:archived_until_escalating", | ||
| callback_id=callback_id, | ||
| ) | ||
|
|
||
| def open_resolve_dialog(self, slack_request: SlackActionRequest, group: Group) -> None: | ||
| # XXX(epurkhiser): In order to update the original message we have to | ||
| # keep track of the response_url in the callback_id. Definitely hacky, | ||
|
|
@@ -399,32 +472,50 @@ def open_resolve_dialog(self, slack_request: SlackActionRequest, group: Group) - | |
| callback_id["rule"] = slack_request.callback_data.get("rule") | ||
| callback_id = orjson.dumps(callback_id).decode() | ||
|
|
||
| slack_client = SlackClient(integration_id=slack_request.integration.id) | ||
|
|
||
| # XXX(CEO): the second you make a selection (without hitting Submit) it sends a slightly different request | ||
| modal_payload = self.build_resolve_modal_payload(callback_id) | ||
| try: | ||
| payload = { | ||
| "view": orjson.dumps(modal_payload).decode(), | ||
| "trigger_id": slack_request.data["trigger_id"], | ||
| } | ||
| headers = {"content-type": "application/json; charset=utf-8"} | ||
| slack_client.post( | ||
| "/views.open", | ||
| data=orjson.dumps(payload).decode(), | ||
| headers=headers, | ||
| ) | ||
| except ApiError as e: | ||
| logger.exception( | ||
| "slack.action.response-error", | ||
| extra={ | ||
| "error": str(e), | ||
| "organization_id": org.id, | ||
| "integration_id": slack_request.integration.id, | ||
| if features.has("organizations:slack-sdk-action-view-open", group.project.organization): | ||
| modal_payload = self.build_resolve_modal_payload(callback_id) | ||
| slack_client = SlackSdkClient(integration_id=slack_request.integration.id) | ||
| try: | ||
| slack_client.views_open( | ||
| trigger_id=slack_request.data["trigger_id"], | ||
| view=modal_payload, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. any chance we can make |
||
| ) | ||
| except SlackApiError: | ||
| logger.exception( | ||
| "slack.action.response-error", | ||
| extra={ | ||
| "organization_id": org.id, | ||
| "integration_id": slack_request.integration.id, | ||
| "trigger_id": slack_request.data["trigger_id"], | ||
| "dialog": "resolve", | ||
| }, | ||
| ) | ||
| else: | ||
| modal_payload = self.build_resolve_modal_payload_deprecated(callback_id) | ||
| slack_client = SlackClient(integration_id=slack_request.integration.id) | ||
| try: | ||
| payload = { | ||
| "view": orjson.dumps(modal_payload).decode(), | ||
| "trigger_id": slack_request.data["trigger_id"], | ||
| "dialog": "resolve", | ||
| }, | ||
| ) | ||
| } | ||
| headers = {"content-type": "application/json; charset=utf-8"} | ||
| slack_client.post( | ||
| "/views.open", | ||
| data=orjson.dumps(payload).decode(), | ||
| headers=headers, | ||
| ) | ||
| except ApiError as e: | ||
| logger.exception( | ||
| "slack.action.response-error", | ||
| extra={ | ||
| "error": str(e), | ||
| "organization_id": org.id, | ||
| "integration_id": slack_request.integration.id, | ||
| "trigger_id": slack_request.data["trigger_id"], | ||
| "dialog": "resolve", | ||
| }, | ||
|
RyanSkonnord marked this conversation as resolved.
|
||
| ) | ||
|
|
||
| def open_archive_dialog(self, slack_request: SlackActionRequest, group: Group) -> None: | ||
| org = group.project.organization | ||
|
|
@@ -440,30 +531,50 @@ def open_archive_dialog(self, slack_request: SlackActionRequest, group: Group) - | |
| callback_id["channel_id"] = slack_request.data["channel"]["id"] | ||
| callback_id = orjson.dumps(callback_id).decode() | ||
|
|
||
| slack_client = SlackClient(integration_id=slack_request.integration.id) | ||
| modal_payload = self.build_archive_modal_payload(callback_id) | ||
| try: | ||
| if features.has("organizations:slack-sdk-action-view-open", group.project.organization): | ||
| modal_payload = self.build_archive_modal_payload(callback_id) | ||
| slack_client = SlackSdkClient(integration_id=slack_request.integration.id) | ||
| try: | ||
| slack_client.views_open( | ||
| trigger_id=slack_request.data["trigger_id"], | ||
| view=modal_payload, | ||
| ) | ||
| except SlackApiError: | ||
| logger.exception( | ||
| "slack.action.response-error", | ||
| extra={ | ||
| "organization_id": org.id, | ||
| "integration_id": slack_request.integration.id, | ||
| "trigger_id": slack_request.data["trigger_id"], | ||
| "dialog": "archive", | ||
| }, | ||
| ) | ||
| else: | ||
| modal_payload = self.build_archive_modal_payload_deprecated(callback_id) | ||
| payload = { | ||
| "view": orjson.dumps(modal_payload).decode(), | ||
| "trigger_id": slack_request.data["trigger_id"], | ||
| } | ||
| headers = {"content-type": "application/json; charset=utf-8"} | ||
| slack_client.post( | ||
| "/views.open", | ||
| data=orjson.dumps(payload).decode(), | ||
| headers=headers, | ||
| ) | ||
| except ApiError as e: | ||
| logger.exception( | ||
| "slack.action.response-error", | ||
| extra={ | ||
| "error": str(e), | ||
| "organization_id": org.id, | ||
| "integration_id": slack_request.integration.id, | ||
| "trigger_id": slack_request.data["trigger_id"], | ||
| "dialog": "archive", | ||
| }, | ||
| ) | ||
|
|
||
| slack_client = SlackClient(integration_id=slack_request.integration.id) | ||
| try: | ||
| headers = {"content-type": "application/json; charset=utf-8"} | ||
| slack_client.post( | ||
| "/views.open", | ||
| data=orjson.dumps(payload).decode(), | ||
| headers=headers, | ||
| ) | ||
| except ApiError as e: | ||
| logger.exception( | ||
| "slack.action.response-error", | ||
| extra={ | ||
| "error": str(e), | ||
| "organization_id": org.id, | ||
| "integration_id": slack_request.integration.id, | ||
| "trigger_id": slack_request.data["trigger_id"], | ||
| "dialog": "archive", | ||
| }, | ||
| ) | ||
|
|
||
| def construct_reply(self, attachment: SlackBody, is_message: bool = False) -> SlackBody: | ||
| # XXX(epurkhiser): Slack is inconsistent about it's expected responses | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!