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

ref: unify signatures of get_issue_label / get_issue_url #74892

Merged
merged 1 commit into from
Jul 25, 2024
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
2 changes: 1 addition & 1 deletion src/sentry/integrations/bitbucket/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


class BitbucketIssueBasicMixin(IssueBasicMixin):
def get_issue_url(self, key):
def get_issue_url(self, key: str) -> str:
repo, issue_id = key.split("#")
return f"https://bitbucket.org/{repo}/issues/{issue_id}"

Expand Down
2 changes: 1 addition & 1 deletion src/sentry/integrations/example/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ExampleIntegration(IntegrationInstallation, IssueSyncMixin, RepositoryMixi
outbound_assignee_key = "sync_assignee_outbound"
inbound_assignee_key = "sync_assignee_inbound"

def get_issue_url(self, key):
def get_issue_url(self, key: str) -> str:
return f"https://example/issues/{key}"

def create_comment(self, issue_id, user_id, group_note):
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/integrations/gitlab/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GitlabIssueBasic(IssueBasicMixin):
def make_external_key(self, data):
return "{}:{}".format(self.model.metadata["domain_name"], data["key"])

def get_issue_url(self, key):
def get_issue_url(self, key: str) -> str:
match = ISSUE_EXTERNAL_KEY_FORMAT.match(key)
project, issue_id = match.group(1), match.group(2)
return "{}/{}/issues/{}".format(self.model.metadata["base_url"], project, issue_id)
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/integrations/jira/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def get_link_issue_config(self, group, **kwargs):
field["type"] = "select"
return fields

def get_issue_url(self, key, **kwargs):
def get_issue_url(self, key: str) -> str:
return "{}/browse/{}".format(self.model.metadata["base_url"], key)

def get_persisted_default_config_fields(self) -> Sequence[str]:
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/integrations/jira_server/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ def get_link_issue_config(self, group, **kwargs):

return fields

def get_issue_url(self, key, **kwargs):
def get_issue_url(self, key: str) -> str:
return "{}/browse/{}".format(self.model.metadata["base_url"], key)

def get_persisted_default_config_fields(self) -> Sequence[str]:
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/integrations/mixins/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def should_sync(self, attribute):
def get_group_title(self, group, event, **kwargs):
return get_notification_group_title(group, event, **kwargs)

def get_issue_url(self, key):
def get_issue_url(self, key: str) -> str:
"""
Given the key of the external_issue return the external issue link.
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sentry/integrations/vsts/issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def get_link_issue_config(self, group: "Group", **kwargs: Any) -> Sequence[Mappi
field["type"] = "select"
return fields

def get_issue_url(self, key: str, **kwargs: Any) -> str:
def get_issue_url(self, key: str) -> str:
return f"{self.instance}_workitems/edit/{key}"

def create_issue(self, data: Mapping[str, str], **kwargs: Any) -> Mapping[str, Any]:
Expand Down
4 changes: 2 additions & 2 deletions src/sentry/plugins/bases/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def get_link_existing_issue_form(self, request: Request, group, event, **kwargs)
request.POST or None, initial=self.get_initial_link_form_data(request, group, event)
)

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
"""
Given an issue_id (string) return an absolute URL to the issue's details
page.
Expand All @@ -135,7 +135,7 @@ def get_issue_title_by_id(self, request: Request, group, issue_id):
"""
raise NotImplementedError

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id) -> str:
"""
Given an issue_id (string) return a string representing the issue.

Expand Down
48 changes: 18 additions & 30 deletions src/sentry/plugins/bases/issue2.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,32 +146,20 @@ def get_new_issue_fields(self, request: Request, group, event, **kwargs):
def get_link_existing_issue_fields(self, request: Request, group, event, **kwargs):
return []

def _get_issue_url_compat(self, group, issue, **kwargs):
if self.issue_fields is None:
return self.get_issue_url(group, issue["id"])
return self.get_issue_url(group, issue)

def _get_issue_label_compat(self, group, issue, **kwargs):
if self.issue_fields is None:
return self.get_issue_label(group, issue["id"])
return self.get_issue_label(group, issue)

def get_issue_url(self, group, issue, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
"""
Given an issue context (issue_id string or issue dict) return an absolute URL to the issue's details
Given an issue context (issue_id string) return an absolute URL to the issue's details
page.
"""
raise NotImplementedError

def get_issue_label(self, group, issue, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
"""
Given an issue context (issue_id string or issue dict) return a string representing the issue.
Given an issue context (issue_id string) return a string representing the issue.

e.g. GitHub represents issues as GH-XXX
"""
if isinstance(issue, dict):
return "#{}".format(issue["id"])
return f"#{issue}"
return f"#{issue_id}"

def create_issue(self, request: Request, group, form_data, **kwargs):
"""
Expand Down Expand Up @@ -274,8 +262,8 @@ def view_create(self, request: Request, group, **kwargs):
or request.data.get("title")
or self._get_issue_label_compat(group, issue),
"provider": self.get_title(),
"location": self._get_issue_url_compat(group, issue),
"label": self._get_issue_label_compat(group, issue),
"location": self.get_issue_url(group, issue["id"]),
"label": self.get_issue_label(group, issue["id"]),
}
Activity.objects.create(
project=group.project,
Expand All @@ -290,9 +278,9 @@ def view_create(self, request: Request, group, **kwargs):
)
return Response(
{
"issue_url": self.get_issue_url(group, issue),
"link": self._get_issue_url_compat(group, issue),
"label": self._get_issue_label_compat(group, issue),
"issue_url": self.get_issue_url(group, issue["id"]),
"link": self.get_issue_url(group, issue["id"]),
"label": self.get_issue_label(group, issue["id"]),
"id": issue["id"],
}
)
Expand Down Expand Up @@ -341,8 +329,8 @@ def view_link(self, request: Request, group, **kwargs):
issue_information = {
"title": issue.get("title") or self._get_issue_label_compat(group, issue),
"provider": self.get_title(),
"location": self._get_issue_url_compat(group, issue),
"label": self._get_issue_label_compat(group, issue),
"location": self.get_issue_url(group, issue["id"]),
"label": self.get_issue_label(group, issue["id"]),
}
Activity.objects.create(
project=group.project,
Expand All @@ -354,8 +342,8 @@ def view_link(self, request: Request, group, **kwargs):
return Response(
{
"message": "Successfully linked issue.",
"link": self._get_issue_url_compat(group, issue),
"label": self._get_issue_label_compat(group, issue),
"link": self.get_issue_url(group, issue["id"]),
"label": self.get_issue_label(group, issue["id"]),
"id": issue["id"],
}
)
Expand Down Expand Up @@ -383,8 +371,8 @@ def plugin_issues(self, request: Request, group, plugin_issues, **kwargs) -> Non
if issue:
item["issue"] = {
"issue_id": issue.get("id"),
"url": self._get_issue_url_compat(group, issue),
"label": self._get_issue_label_compat(group, issue),
"url": self.get_issue_url(group, issue["id"]),
"label": self.get_issue_label(group, issue["id"]),
}

item.update(PluginSerializer(group.project).serialize(self, None, request.user))
Expand Down Expand Up @@ -428,8 +416,8 @@ def tags(self, request: Request, group, tag_list, **kwargs):

tag_list.append(
{
"url": self._get_issue_url_compat(group, issue),
"displayName": self._get_issue_label_compat(group, issue),
"url": self.get_issue_url(group, issue["id"]),
"displayName": self.get_issue_label(group, issue["id"]),
}
)

Expand Down
4 changes: 2 additions & 2 deletions src/sentry/plugins/examples/issue_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def get_new_issue_fields(self, request: Request, group, event, **kwargs):
def create_issue(self, request: Request, group, form_data, **kwargs):
return "1"

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
return "Example-%s" % issue_id

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
tracker_url = self.get_option("tracker_url", group.project)

return f"{tracker_url}?issueID={issue_id}"
Expand Down
4 changes: 2 additions & 2 deletions src/sentry_plugins/asana/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ def link_issue(self, request: Request, group, form_data, **kwargs):

return {"title": issue["name"]}

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
return "Asana Issue"

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
return "https://app.asana.com/0/0/%s" % issue_id

def validate_config(self, project, config, actor):
Expand Down
4 changes: 2 additions & 2 deletions src/sentry_plugins/bitbucket/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ def link_issue(self, request: Request, group, form_data, **kwargs):

return {"title": issue["title"]}

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
return "Bitbucket-%s" % issue_id

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
repo = self.get_option("repo", group.project)
return f"https://bitbucket.org/{repo}/issue/{issue_id}/"

Expand Down
4 changes: 2 additions & 2 deletions src/sentry_plugins/github/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,10 @@ def link_issue(self, request: Request, group, form_data, **kwargs):

return {"title": issue["title"]}

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
return f"GH-{issue_id}"

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
# XXX: get_option may need tweaked in Sentry so that it can be pre-fetched in bulk
repo = self.get_option("repo", group.project)

Expand Down
6 changes: 3 additions & 3 deletions src/sentry_plugins/gitlab/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ def link_issue(self, request: Request, group, form_data, **kwargs):

return {"title": issue["title"]}

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
return f"GL-{issue_id}"

def get_issue_url(self, group, issue_iid, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
url = self.get_option("gitlab_url", group.project).rstrip("/")
repo = self.get_option("gitlab_repo", group.project)

return f"{url}/{repo}/issues/{issue_iid}"
return f"{url}/{repo}/issues/{issue_id}"

def get_configure_plugin_fields(self, project, **kwargs):
gitlab_token = self.get_option("gitlab_token", project)
Expand Down
4 changes: 2 additions & 2 deletions src/sentry_plugins/jira/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ def link_issue(self, request: Request, group, form_data, **kwargs):

return {"title": issue["fields"]["summary"]}

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
return issue_id

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
instance = self.get_option("instance_url", group.project)
return f"{instance}/browse/{issue_id}"

Expand Down
4 changes: 2 additions & 2 deletions src/sentry_plugins/phabricator/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ def is_configured(self, request: Request, project, **kwargs):
def get_new_issue_title(self, **kwargs):
return "Create Maniphest Task"

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
return "T%s" % issue_id

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
host = self.get_option("host", group.project)
return urljoin(host, "T%s" % issue_id)

Expand Down
4 changes: 2 additions & 2 deletions src/sentry_plugins/pivotal/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,10 @@ def create_issue(self, request: Request, group, form_data, **kwargs):

return json_resp["id"]

def get_issue_label(self, group, issue_id, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
return "#%s" % issue_id

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
return "https://www.pivotaltracker.com/story/show/%s" % issue_id

def get_issue_title_by_id(self, request: Request, group, issue_id):
Expand Down
2 changes: 1 addition & 1 deletion src/sentry_plugins/redmine/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def create_issue(self, group, form_data, **kwargs):
response = client.create_issue(issue_dict)
return response["issue"]["id"]

def get_issue_url(self, group, issue_id, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
host = self.get_option("host", group.project)
return "{}/issues/{}".format(host.rstrip("/"), issue_id)

Expand Down
19 changes: 8 additions & 11 deletions src/sentry_plugins/trello/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,27 +233,24 @@ def link_issue(self, request: Request, group, form_data, **kwargs):

return {"title": card["name"], "id": card["shortLink"]}

def get_issue_label(self, group, issue, **kwargs):
def get_issue_label(self, group, issue_id: str) -> str:
"""
Return label of the linked issue we show in the UI from the issue string
"""
# the old version of the plugin stores the url in the issue
if LABLEX_REGEX.search(issue):
short_issue = issue.split("/", 1)[0]
if LABLEX_REGEX.search(issue_id):
short_issue = issue_id.split("/", 1)[0]
return "Trello-%s" % short_issue
return "Trello-%s" % issue
return "Trello-%s" % issue_id

def get_issue_url(self, group, issue, **kwargs):
def get_issue_url(self, group, issue_id: str) -> str:
"""
Return label of the url of card in Trello based off the issue object or issue ID
"""
# TODO(Steve): figure out why we sometimes get a string and sometimes a dict
if isinstance(issue, dict):
issue = issue["id"]
# the old version of the plugin stores the url in the issue
if LABLEX_REGEX.search(issue):
return issue.split("/", 1)[1]
return "https://trello.com/c/%s" % issue
if LABLEX_REGEX.search(issue_id):
return issue_id.split("/", 1)[1]
return "https://trello.com/c/%s" % issue_id

def view_options(self, request: Request, group, **kwargs):
"""
Expand Down
5 changes: 0 additions & 5 deletions tests/sentry/plugins/bases/test_issue2.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ class PluginWithoutFields(IssueTrackingPlugin2):


class IssueTrackingPlugin2Test(TestCase):
def test_issue_label_as_dict(self):
plugin = PluginWithFields()
result = plugin.get_issue_label(mock.Mock(), {"id": "1"})
assert result == "#1"

def test_issue_label_legacy(self):
plugin = PluginWithoutFields()
result = plugin.get_issue_label(mock.Mock(), "1")
Expand Down
2 changes: 0 additions & 2 deletions tests/sentry_plugins/trello/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ def test_get_issue_label(self):

def test_get_issue_url(self):
group = self.create_group(message="Hello world", culprit="foo.bar")
# test new and old format
assert self.plugin.get_issue_url(group, "rPPDb") == "https://trello.com/c/rPPDb"
assert self.plugin.get_issue_url(group, {"id": "rPPDb"}) == "https://trello.com/c/rPPDb"
assert (
self.plugin.get_issue_url(group, "5dafd/https://trello.com/c/rPPDb/75-title")
== "https://trello.com/c/rPPDb/75-title"
Expand Down
Loading