Skip to content

[WIP][Slack] Catch All - Code cleanup #70169

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

Closed
wants to merge 12 commits into from
Closed

Conversation

ykamo001
Copy link
Contributor

@ykamo001 ykamo001 commented May 2, 2024

While we were trying to add and make changes for features or bug fixes, we noticed a lot of complexity and low hanging issues that could be fixed to help make our lives and this code area easier and safer to work with.

In that vain, we're using this branch as a way to capture and check potential changes

@ykamo001 ykamo001 requested a review from a team May 2, 2024 19:30
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label May 2, 2024
# Optional label. This falls back to name.
label: str | None = None
@dataclass(kw_only=True)
class _BaseMessageAction:
Copy link
Contributor Author

@ykamo001 ykamo001 May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created a base class to reduce repeated fields and help ensure changes are consistent. Also helps understand what the difference is between the block kit variant and the original class

@ykamo001 ykamo001 force-pushed the refactor-integrations branch from 9f9e34e to 1efe721 Compare May 2, 2024 21:50
from sentry.notifications.utils.actions import MessageAction


def get_slack_button(action: MessageAction) -> Mapping[str, Any]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove a bunch of unused methods and functions

Copy link

codecov bot commented May 2, 2024

Codecov Report

Attention: Patch coverage is 98.21429% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 79.87%. Comparing base (0c692d5) to head (c4cc46e).
Report is 985 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #70169      +/-   ##
==========================================
+ Coverage   79.84%   79.87%   +0.03%     
==========================================
  Files        6509     6507       -2     
  Lines      289811   289998     +187     
  Branches    49928    49930       +2     
==========================================
+ Hits       231406   231649     +243     
+ Misses      57992    57936      -56     
  Partials      413      413              
Files Coverage Δ
src/sentry/integrations/slack/__init__.py 100.00% <ø> (ø)
...entry/integrations/slack/actions/message_action.py 100.00% <100.00%> (ø)
...ry/integrations/slack/message_builder/base/base.py 100.00% <100.00%> (+35.71%) ⬆️
...entry/integrations/slack/message_builder/issues.py 89.50% <100.00%> (-0.42%) ⬇️
src/sentry/integrations/slack/webhooks/action.py 89.76% <100.00%> (+0.26%) ⬆️
src/sentry/notifications/utils/actions.py 100.00% <100.00%> (ø)
...ations/slack/message_builder/notifications/base.py 95.08% <93.54%> (-2.65%) ⬇️

... and 79 files with indirect coverage changes

Comment on lines 25 to 28
def _get_button_text(self) -> str:
return self.name

# TODO(mgaeta): Refactor this to be provider-agnostic
selected_options: Sequence[Mapping[str, Any]] | None = None
option_groups: Sequence[Mapping[str, Any]] | None = None
block_id: str | None = None
elements: Sequence[Mapping[str, Any]] | None = None
def get_button(self) -> Mapping[str, Any]:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't need to be at the base class, but everything is so hard typed to MessageAction that we can't simply move this to the block kit class, because this is really just needed for slack flows

Comment on lines 106 to 121
@staticmethod
def get_link_button(action: MessageAction) -> SlackBlock:
return {
"type": "section",
"text": {
"type": "mrkdwn",
"text": action.label,
},
"accessory": {
"type": "button",
"text": {"type": "plain_text", "text": action.name, "emoji": True},
"style": action.style,
"value": action.value,
"url": action.url,
},
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is needed in getsentry, will need to move slowly



@dataclass
class SlackMessageAction(BaseMessageAction):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this over from BlockKitMessageAction to be more accurate to Slack specific namespace, and added helper functions so it's easier to enact on the object, and add tests to verify functionality and contracts

Comment on lines +51 to +64
button = {
"type": "button",
"text": self._get_button_text(),
}
if self.value:
button["action_id"] = self.value
button["value"] = self.value

if self.action_id:
button["action_id"] = self.action_id

if self.url:
button["url"] = self.url
button["value"] = "link_clicked"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could add some constants here to keep it safer

Comment on lines +376 to +403
def _ignore_action(group: Group) -> SlackMessageAction | None:
if group.issue_category == GroupCategory.FEEDBACK:
return None

if group.get_status() == GroupStatus.IGNORED:
return SlackMessageAction(
name="status", label="Mark as Ongoing", value="unresolved:ongoing"
)

return SlackMessageAction(name="status", label="Archive", value="archive_dialog")


def _resolve_action(group: Group, project: Project) -> SlackMessageAction:
if group.get_status() == GroupStatus.RESOLVED:
return SlackMessageAction(
name="unresolved:ongoing", label="Unresolve", value="unresolved:ongoing"
)
if not project.flags.has_releases:
return SlackMessageAction(name="status", label="Resolve", value="resolved")

return SlackMessageAction(
name="status",
label="Resolve",
value="resolve_dialog",
)


def _assign_action(group: Group) -> SlackMessageAction:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved these out from being anonymous closures in a function to named functions that we can now test and enforce contracts


return {"type": "actions", "elements": actions_block}

def build(self) -> SlackBlock:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

broke this function down and added helper methods to make it easier to read and understand the flow. We can now also add tests to enforce individual logic and force contracts

if "name" in action_data
]

@classmethod
def get_action_list(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored to use early returns for easier reading, and less indentations. Also pulled some common code out for more reusability

# Optional label. This falls back to name.
label: str | None = None
@dataclass(kw_only=True)
class BaseMessageAction:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

created a base class to help build off and capture commonalities

@ykamo001 ykamo001 added the Do Not Merge Don't merge label May 3, 2024
@ykamo001 ykamo001 changed the title refactor(slack): Code cleanup [WIP] Catch All - Code cleanup May 3, 2024
@ykamo001 ykamo001 changed the title [WIP] Catch All - Code cleanup [WIP][Slack] Catch All - Code cleanup May 3, 2024
@getsantry
Copy link
Contributor

getsantry bot commented May 25, 2024

This pull request has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you add the label WIP, I will leave it alone unless WIP is removed ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@getsantry getsantry bot added the Stale label May 25, 2024
@getsantry getsantry bot closed this Jun 2, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Do Not Merge Don't merge Scope: Backend Automatically applied to PRs that change backend components Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants