Skip to content
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

feat(metric-alerts): Fire webhook to Alert Rule UI Component trigger action #29298

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/sentry/testutils/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,9 +1013,16 @@ def create_alert_rule_trigger_action(
target_identifier=None,
integration=None,
sentry_app=None,
sentry_app_config=None,
):
return create_alert_rule_trigger_action(
trigger, type, target_type, target_identifier, integration, sentry_app
trigger,
type,
target_type,
target_identifier,
integration,
sentry_app,
sentry_app_config=sentry_app_config,
)

@staticmethod
Expand Down
74 changes: 72 additions & 2 deletions tests/sentry/incidents/test_action_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ def run_test(self, incident, method):
raise NotImplementedError

def run_fire_test(self, method="fire"):
alert_rule = self.create_alert_rule()
incident = self.create_incident(alert_rule=alert_rule, status=IncidentStatus.CLOSED.value)
self.alert_rule = self.create_alert_rule()
incident = self.create_incident(
alert_rule=self.alert_rule, status=IncidentStatus.CLOSED.value
)
if method == "resolve":
update_incident_status(
incident, IncidentStatus.CLOSED, status_method=IncidentStatusMethod.MANUAL
Expand Down Expand Up @@ -609,3 +611,71 @@ def test_fire_metric_alert(self):

def test_resolve_metric_alert(self):
self.run_fire_test("resolve")


@freeze_time()
class SentryAppAlerRuleUIComponentActionHandlerTest(FireTest, TestCase):
def setUp(self):
self.sentry_app = self.create_sentry_app(
name="foo",
organization=self.organization,
is_alertable=True,
verify_install=False,
schema={
"elements": [
self.create_alert_rule_action_schema(),
]
},
)
self.create_sentry_app_installation(
slug=self.sentry_app.slug, organization=self.organization, user=self.user
)

@responses.activate
def run_test(self, incident, method):
from sentry.rules.actions.notify_event_service import build_incident_attachment

trigger = self.create_alert_rule_trigger(self.alert_rule, "hi", 1000)
action = self.create_alert_rule_trigger_action(
alert_rule_trigger=trigger,
target_identifier=self.sentry_app.id,
type=AlertRuleTriggerAction.Type.SENTRY_APP,
target_type=AlertRuleTriggerAction.TargetType.SENTRY_APP,
sentry_app=self.sentry_app,
sentry_app_config={
"channel": "#santry",
"workspace": "santrysantrysantry",
"tag": "triage",
"assignee": "Nisanthan Nanthakumar",
},
)

responses.add(
method=responses.POST,
url="https://example.com/webhook",
status=200,
content_type="application/json",
body=json.dumps({"ok": "true"}),
)

handler = SentryAppActionHandler(action, incident, self.project)
metric_value = 1000
with self.tasks():
getattr(handler, method)(metric_value)
data = responses.calls[0].request.body
assert json.dumps(build_incident_attachment(action, incident, metric_value, method)) in data
# Check that the Alert Rule UI Component settings are returned
assert json.loads(data)["data"]["metric_alert"]["alert_rule"]["triggers"][0]["actions"][0][
"settings"
] == {
"channel": "#santry",
"workspace": "santrysantrysantry",
"tag": "triage",
"assignee": "Nisanthan Nanthakumar",
}

def test_fire_metric_alert(self):
self.run_fire_test()

def test_resolve_metric_alert(self):
self.run_fire_test("resolve")