Skip to content

Commit

Permalink
fix(integrations) Fix jira sync always failing (#61936)
Browse files Browse the repository at this point in the history
Getting an installation requires an organization_id. Provide one if we
have an organization linked to the integration.

I've also fixed the integration icon being saved as a tuple. The JS
types and API serializer imply that this value should be a string.

Fixes SENTRY-1680
  • Loading branch information
markstory authored Dec 18, 2023
1 parent 82ddca3 commit 0516268
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/sentry/integrations/jira/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ def get_config_data(self):
def sync_metadata(self):
client = self.get_client()

server_info = {}
projects = []
try:
server_info = client.get_server_info()
projects = client.get_projects_list()
Expand All @@ -301,7 +303,7 @@ def sync_metadata(self):
# possible to query that with the API). So instead we just use the first
# project Icon.
if len(projects) > 0:
avatar = (projects[0]["avatarUrls"]["48x48"],)
avatar = projects[0]["avatarUrls"]["48x48"]
self.model.metadata.update({"icon": avatar})

self.model.save()
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 @@ -458,7 +458,7 @@ def sync_metadata(self):
# possible to query that with the API). So instead we just use the first
# project Icon.
if len(projects) > 0:
avatar = (projects[0]["avatarUrls"]["48x48"],)
avatar = projects[0]["avatarUrls"]["48x48"]
self.model.metadata.update({"icon": avatar})

self.model.save()
Expand Down
5 changes: 4 additions & 1 deletion src/sentry/tasks/integrations/sync_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
@retry(on=(IntegrationError,), exclude=(Integration.DoesNotExist,))
def sync_metadata(integration_id: int) -> None:
integration = Integration.objects.get(id=integration_id)
installation = integration.get_installation(None)
org_install = integration.organizationintegration_set.first()
if not org_install:
return
installation = integration.get_installation(org_install.organization_id)
installation.sync_metadata()
56 changes: 56 additions & 0 deletions tests/sentry/tasks/integrations/test_sync_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import responses

from sentry.tasks.integrations.sync_metadata import sync_metadata
from sentry.testutils.cases import TestCase
from sentry.testutils.silo import control_silo_test


@control_silo_test
class SyncMetadataTest(TestCase):
@responses.activate
def test_no_sync_with_no_org(self):
integration = self.create_integration(
organization=self.organization,
provider="jira",
external_id="abc123",
metadata={
"base_url": "https://acme.atlassian.net",
"shared_secret": "super-sekret",
},
)
org_integration = integration.organizationintegration_set.first()
if org_integration:
org_integration.delete()
sync_metadata(integration.id)
assert len(responses.calls) == 0

@responses.activate
def test_success(self):
responses.add(
responses.GET,
"https://acme.atlassian.net/rest/api/2/serverInfo",
json={
"serverTitle": "Acme Jira",
},
)
responses.add(
responses.GET,
"https://acme.atlassian.net/rest/api/2/project",
json=[
{"avatarUrls": {"48x48": "https://example.com/avatar.jpg"}},
],
)
integration = self.create_integration(
organization=self.organization,
provider="jira",
external_id="abc123",
metadata={
"base_url": "https://acme.atlassian.net",
"shared_secret": "super-sekret",
},
)
sync_metadata(integration.id)

integration.refresh_from_db()
assert integration.name == "Acme Jira"
assert integration.metadata["icon"] == "https://example.com/avatar.jpg"

0 comments on commit 0516268

Please sign in to comment.