|
@@ -18,6 +18,7 @@ from sentry.models import (
|
|
|
IdentityStatus,
|
|
|
Integration,
|
|
|
NotificationSetting,
|
|
|
+ ProjectOwnership,
|
|
|
Release,
|
|
|
Rule,
|
|
|
UserOption,
|
|
@@ -38,6 +39,9 @@ from sentry.notifications.types import (
|
|
|
NotificationSettingOptionValues,
|
|
|
NotificationSettingTypes,
|
|
|
)
|
|
|
+from sentry.ownership.grammar import Matcher, Owner
|
|
|
+from sentry.ownership.grammar import Rule as GrammarRule
|
|
|
+from sentry.ownership.grammar import dump_schema
|
|
|
from sentry.plugins.base import Notification
|
|
|
from sentry.tasks.digests import deliver_digest
|
|
|
from sentry.testutils import TestCase
|
|
@@ -569,6 +573,96 @@ class SlackActivityNotificationTest(ActivityTestCase, TestCase):
|
|
|
== f"{self.project.slug} | <http://testserver/settings/account/notifications/alerts/?referrer=AlertRuleSlack|Notification Settings>"
|
|
|
)
|
|
|
|
|
|
+ @responses.activate
|
|
|
+ @mock.patch("sentry.notifications.notify.notify", side_effect=send_notification)
|
|
|
+ def test_issue_alert_team_issue_owners(self, mock_func):
|
|
|
+ """Test that issue alerts are sent to a team in Slack via an Issue Owners rule action."""
|
|
|
+
|
|
|
+ # add a second user to the team so we can be sure it's only
|
|
|
+ # sent once (to the team, and not to each individual user)
|
|
|
+ user2 = self.create_user(is_superuser=False)
|
|
|
+ self.create_member(teams=[self.team], user=user2, organization=self.organization)
|
|
|
+ self.idp = IdentityProvider.objects.create(type="slack", external_id="TXXXXXXX2", config={})
|
|
|
+ self.identity = Identity.objects.create(
|
|
|
+ external_id="UXXXXXXX2",
|
|
|
+ idp=self.idp,
|
|
|
+ user=user2,
|
|
|
+ status=IdentityStatus.VALID,
|
|
|
+ scopes=[],
|
|
|
+ )
|
|
|
+ NotificationSetting.objects.update_settings(
|
|
|
+ ExternalProviders.SLACK,
|
|
|
+ NotificationSettingTypes.ISSUE_ALERTS,
|
|
|
+ NotificationSettingOptionValues.ALWAYS,
|
|
|
+ user=user2,
|
|
|
+ )
|
|
|
+ # update the team's notification settings
|
|
|
+ ExternalActor.objects.create(
|
|
|
+ actor=self.team.actor,
|
|
|
+ organization=self.organization,
|
|
|
+ integration=self.integration,
|
|
|
+ provider=ExternalProviders.SLACK.value,
|
|
|
+ external_name="goma",
|
|
|
+ external_id="CXXXXXXX2",
|
|
|
+ )
|
|
|
+ NotificationSetting.objects.update_settings(
|
|
|
+ ExternalProviders.SLACK,
|
|
|
+ NotificationSettingTypes.ISSUE_ALERTS,
|
|
|
+ NotificationSettingOptionValues.ALWAYS,
|
|
|
+ team=self.team,
|
|
|
+ )
|
|
|
+
|
|
|
+ rule = GrammarRule(Matcher("path", "*"), [Owner("team", self.team.slug)])
|
|
|
+ ProjectOwnership.objects.create(
|
|
|
+ project_id=self.project.id, schema=dump_schema([rule]), fallthrough=True
|
|
|
+ )
|
|
|
+
|
|
|
+ event = self.store_event(
|
|
|
+ data={
|
|
|
+ "message": "Hello world",
|
|
|
+ "level": "error",
|
|
|
+ "stacktrace": {"frames": [{"filename": "foo.py"}]},
|
|
|
+ },
|
|
|
+ project_id=self.project.id,
|
|
|
+ )
|
|
|
+
|
|
|
+ action_data = {
|
|
|
+ "id": "sentry.mail.actions.NotifyEmailAction",
|
|
|
+ "targetType": "IssueOwners",
|
|
|
+ "targetIdentifier": "",
|
|
|
+ }
|
|
|
+ rule = Rule.objects.create(
|
|
|
+ project=self.project,
|
|
|
+ label="ja rule",
|
|
|
+ data={
|
|
|
+ "match": "all",
|
|
|
+ "actions": [action_data],
|
|
|
+ },
|
|
|
+ )
|
|
|
+
|
|
|
+ notification = AlertRuleNotification(
|
|
|
+ Notification(event=event, rule=rule), ActionTargetType.ISSUE_OWNERS, self.team.id
|
|
|
+ )
|
|
|
+
|
|
|
+ with self.tasks():
|
|
|
+ notification.send()
|
|
|
+
|
|
|
+ # check that only one was sent out - more would mean each user is being notified
|
|
|
+ # rather than the team
|
|
|
+ assert len(responses.calls) == 1
|
|
|
+
|
|
|
+ # check that the team got a notification
|
|
|
+ data = parse_qs(responses.calls[0].request.body)
|
|
|
+ assert data["channel"] == ["CXXXXXXX2"]
|
|
|
+ assert "attachments" in data
|
|
|
+ attachments = json.loads(data["attachments"][0])
|
|
|
+ assert len(attachments) == 1
|
|
|
+ assert attachments[0]["title"] == "Hello world"
|
|
|
+ assert (
|
|
|
+ attachments[0]["footer"]
|
|
|
+ == f"{self.project.slug} | <http://testserver/settings/{self.organization.slug}/teams/{self.team.slug}/notifications/?referrer=AlertRuleSlack|Notification Settings>"
|
|
|
+ )
|
|
|
+
|
|
|
@responses.activate
|
|
|
@mock.patch("sentry.notifications.notify.notify", side_effect=send_notification)
|
|
|
def test_issue_alert_team(self, mock_func):
|