Repository: getsentry/sentry

Issue:
Create comment when linking to existing Jira issue
## Summary

It would be nice if when linking to an existing Jira issue Sentry created a comment with a link back to the Sentry issue.

## Motivation

- Sentry already creates a backlink when creating a _new_ Jira issue
- Being able to navigate from Jira issue to the original Sentry issue is just as important and useful as navigating from Sentry to Jira.

## Additional Context

When creating a new Jira issue, there is a backlink:

![image](https://user-images.githubusercontent.com/52021/98541556-ae574a00-22a0-11eb-9711-4c7f1ef3dbc6.png)

![image](https://user-images.githubusercontent.com/52021/98541575-b31bfe00-22a0-11eb-89dc-0c057ee1519b.png)

![image](https://user-images.githubusercontent.com/52021/98541590-b911df00-22a0-11eb-902f-49981ccbf1bb.png)

When linking to an existing Jira issue, there is no comment and no backlink.

We are using on-premises Jira Server.


Discussion:
Yea, makes sense. I think we'll also have to handle the case where someone unlinks and to add a comment then.

I'll add this to our backlog but will likely not get to it very soon. We'd accept PRs though!

Resolving patch:
diff --git a/src/sentry/integrations/jira_server/integration.py b/src/sentry/integrations/jira_server/integration.py
--- a/src/sentry/integrations/jira_server/integration.py
+++ b/src/sentry/integrations/jira_server/integration.py
@@ -18,8 +18,9 @@
 from sentry.shared_integrations.exceptions import IntegrationError, ApiError
 from sentry.integrations.jira import JiraIntegration
 from sentry.pipeline import PipelineView
-from sentry.utils.hashlib import sha1_text
 from sentry.utils.decorators import classproperty
+from sentry.utils.hashlib import sha1_text
+from sentry.utils.http import absolute_uri
 from sentry.web.helpers import render_to_response
 from .client import JiraServer, JiraServerSetupClient, JiraServerClient
 
@@ -234,7 +235,8 @@ def get_client(self):
         )
 
     def get_link_issue_config(self, group, **kwargs):
-        fields = super(JiraIntegration, self).get_link_issue_config(group, **kwargs)
+        fields = super().get_link_issue_config(group, **kwargs)
+
         org = group.organization
         autocomplete_url = reverse(
             "sentry-extensions-jiraserver-search", args=[org.slug, self.model.id]
@@ -243,11 +245,35 @@ def get_link_issue_config(self, group, **kwargs):
             if field["name"] == "externalIssue":
                 field["url"] = autocomplete_url
                 field["type"] = "select"
+
+        default_comment = "Linked Sentry Issue: [{}|{}]".format(
+            group.qualified_short_id,
+            absolute_uri(group.get_absolute_url(params={"referrer": "jira_server"})),
+        )
+        fields.append(
+            {
+                "name": "comment",
+                "label": "Comment",
+                "default": default_comment,
+                "type": "textarea",
+                "autosize": True,
+                "maxRows": 10,
+            }
+        )
+
         return fields
 
     def search_url(self, org_slug):
         return reverse("sentry-extensions-jiraserver-search", args=[org_slug, self.model.id])
 
+    def after_link_issue(self, external_issue, data=None, **kwargs):
+        super().after_link_issue(external_issue, **kwargs)
+
+        if data:
+            comment = data.get("comment")
+            if comment:
+                self.get_client().create_comment(external_issue.key, comment)
+
 
 class JiraServerIntegrationProvider(IntegrationProvider):
     key = "jira_server"


Test patch: