Skip to content

fix(jira) Improve logging when jira has problems #13521

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

Merged
merged 1 commit into from
Jun 5, 2019
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
21 changes: 18 additions & 3 deletions src/sentry/integrations/jira/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,17 +433,27 @@ def get_issue_create_meta(self, client, project_id, jira_projects):
# all project metadata is expensive and wasteful. In the first run experience,
# the user won't have a 'last used' project id so we need to iterate available
# projects until we find one that we can get metadata for.
attempts = 0
if len(jira_projects):
attempts = 0
for fallback in jira_projects:
attempts += 1
meta = self.fetch_issue_create_meta(client, fallback['id'])
if meta:
logging.info('jira.issue-create-meta.attempts', extra={
logger.info('jira.get-issue-create-meta.attempts', extra={
'organization_id': self.organization_id,
'attempts': attempts,
})
return meta

jira_project_ids = 'no projects'
if len(jira_projects):
jira_project_ids = ','.join([project['key'] for project in jira_projects])

logger.info('jira.get-issue-create-meta.no-metadata', extra={
'organization_id': self.organization_id,
'attempts': attempts,
'jira_projects': jira_project_ids,
})
raise IntegrationError(
'Could not get issue create metadata for any Jira projects. '
'Ensure that your project permissions are correct.'
Expand All @@ -453,16 +463,21 @@ def fetch_issue_create_meta(self, client, project_id):
try:
meta = client.get_create_meta_for_project(project_id)
except ApiUnauthorized:
logger.info('jira.fetch-issue-create-meta.unauthorized', extra={
'organization_id': self.organization_id,
'jira_project': project_id,
})
raise IntegrationError(
'Jira returned: Unauthorized. '
'Please check your configuration settings.'
)
except ApiError as exc:
logger.info(
'jira.error-fetching-issue-config',
'jira.fetch-issue-create-meta.error',
extra={
'integration_id': self.model.id,
'organization_id': self.organization_id,
'jira_project': project_id,
'error': exc.message,
}
)
Expand Down
64 changes: 64 additions & 0 deletions tests/sentry/integrations/jira/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import mock
import responses
import six
import pytest

from django.core.urlresolvers import reverse
from django.utils import timezone
from exam import fixture
from mock import Mock

Expand Down Expand Up @@ -596,6 +598,68 @@ def get_client():
'default': label_default,
}

@responses.activate
def test_get_create_issue_config__no_projects(self):
org = self.organization
self.login_as(self.user)

event = self.store_event(
data={
'message': 'oh no',
'timestamp': timezone.now().isoformat()
},
project_id=self.project.id
)

installation = self.integration.get_installation(org.id)

# Simulate no projects available.
responses.add(
responses.GET,
'https://example.atlassian.net/rest/api/2/project',
content_type='json',
match_querystring=False,
body='{}'
)
with pytest.raises(IntegrationError):
installation.get_create_issue_config(event.group)

@responses.activate
def test_get_create_issue_config__no_issue_config(self):
org = self.organization
self.login_as(self.user)

event = self.store_event(
data={
'message': 'oh no',
'timestamp': timezone.now().isoformat()
},
project_id=self.project.id
)

installation = self.integration.get_installation(org.id)

responses.add(
responses.GET,
'https://example.atlassian.net/rest/api/2/project',
content_type='json',
match_querystring=False,
body="""[
{"id": "10000", "key": "SAMP"}
]"""
)
# Fail to return metadata
responses.add(
responses.GET,
'https://example.atlassian.net/rest/api/2/issue/createmeta',
content_type='json',
match_querystring=False,
status=401,
body='',
)
with pytest.raises(IntegrationError):
installation.get_create_issue_config(event.group)

def test_get_link_issue_config(self):
org = self.organization
self.login_as(self.user)
Expand Down