Skip to content

Commit

Permalink
feat(slack): distinguish between linking / creating external issues i…
Browse files Browse the repository at this point in the history
…n activity notifications (#73069)
  • Loading branch information
cathteng committed Jun 21, 2024
1 parent 0c766cc commit 16a0b15
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/sentry/api/endpoints/group_integration_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from sentry.api.bases import GroupEndpoint
from sentry.api.serializers import serialize
from sentry.api.serializers.models.integration import IntegrationSerializer
from sentry.integrations.base import IntegrationFeatures
from sentry.integrations.base import IntegrationFeatures, IntegrationInstallation
from sentry.models.activity import Activity
from sentry.models.group import Group
from sentry.models.grouplink import GroupLink
Expand Down Expand Up @@ -74,12 +74,20 @@ def _has_issue_feature_on_integration(self, integration: RpcIntegration):
feature=IntegrationFeatures.ISSUE_BASIC
) or integration.has_feature(feature=IntegrationFeatures.ISSUE_SYNC)

def create_issue_activity(self, request: Request, group, installation, external_issue):
def create_issue_activity(
self,
request: Request,
group: Group,
installation: IntegrationInstallation,
external_issue: ExternalIssue,
new: bool,
):
issue_information = {
"title": external_issue.title,
"provider": installation.model.get_provider().name,
"location": installation.get_issue_url(external_issue.key),
"label": installation.get_issue_display_name(external_issue) or external_issue.key,
"new": new,
}
Activity.objects.create(
project=group.project,
Expand Down Expand Up @@ -211,7 +219,7 @@ def put(self, request: Request, group, integration_id) -> Response:
except IntegrityError:
return Response({"non_field_errors": ["That issue is already linked"]}, status=400)

self.create_issue_activity(request, group, installation, external_issue)
self.create_issue_activity(request, group, installation, external_issue, new=False)

# TODO(jess): would be helpful to return serialized external issue
# once we have description, title, etc
Expand Down Expand Up @@ -284,7 +292,7 @@ def post(self, request: Request, group, integration_id) -> Response:
)
installation.store_issue_last_defaults(group.project, request.user, request.data)

self.create_issue_activity(request, group, installation, external_issue)
self.create_issue_activity(request, group, installation, external_issue, new=True)

# TODO(jess): return serialized issue
url = data.get("url") or installation.get_issue_url(external_issue.key)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ def get_description(self) -> tuple[str, str | None, Mapping[str, Any]]:
base_template = "<{link}|" + base_template + ">"

# Template should look something like "{author} created <{link}| a/an {provider} issue {ticket}>"
base_template = "{author} created " + base_template
if self.activity.data.get("new", True):
base_template = "{author} created " + base_template
else:
base_template = "{author} linked " + base_template

return base_template, None, {"provider": provider, "ticket": ticket_number, "link": link}
2 changes: 2 additions & 0 deletions tests/sentry/api/endpoints/test_group_integration_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ def test_simple_put(self):
"provider": "Example",
"location": "https://example/issues/APP-123",
"label": "display name: APP-123",
"new": False,
}

def test_put_feature_disabled(self):
Expand Down Expand Up @@ -281,6 +282,7 @@ def test_simple_post(self):
"provider": "Example",
"location": "https://example/issues/APP-123",
"label": "display name: APP-123",
"new": True,
}

def test_post_feature_disabled(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,22 @@ def test_without_link(self) -> None:
assert metadata["provider"] == provider
assert metadata["ticket"] == label
assert metadata["link"] == location

def test_linked_issue(self) -> None:
provider = "Github"
label = "ABC-123"
location = "www.example.com"
self.activity.data = {
"provider": provider,
"label": label,
"location": location,
"new": False,
}

notification = ExternalIssueCreatedActivityNotification(self.activity)
template, _, metadata = notification.get_description()

assert template == "{author} linked <{link}|a {provider} issue {ticket}>"
assert metadata["provider"] == "GitHub" # This is how Github is officially spelled
assert metadata["ticket"] == label
assert metadata["link"] == location

0 comments on commit 16a0b15

Please sign in to comment.