Skip to content
Snippets Groups Projects
Commit 1d77cfcb authored by jurgenhaas's avatar jurgenhaas
Browse files

Improve code quality

parent a7f84f7e
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,6 @@ GITLAB_ACCESS_TOKEN = '{{ gitlab_issue.token }}'
class GitlabIssue(PluginBase):
name = 'alerta-gitlab'
def __init__(self):
......@@ -34,33 +33,46 @@ class GitlabIssue(PluginBase):
"""should return internal id of external system"""
LOG.info('Create GitLab issue for %s' % alert.environment)
git = gitlab.Gitlab(GITLAB_URL, GITLAB_ACCESS_TOKEN, ssl_verify=True) # type: gitlab
git = gitlab.Gitlab(GITLAB_URL, GITLAB_ACCESS_TOKEN,
ssl_verify=True) # type: gitlab
git.auth()
if 'gitlab_project_id' in alert.attributes:
project = git.projects.get(alert.attributes['gitlab_project_id'])
project = git.projects.get(
alert.attributes['gitlab_project_id'])
else:
project = git.projects.get('ansible-inventories/hosts/{}/{}'.format(alert.environment, alert.origin.split('/').pop()))
origin = alert.origin.split('/').pop()
project = git.projects.get(
'ansible-inventories/hosts/{}/{}'.format(alert.environment,
origin))
if not project:
projects = git.projects.list(search=origin)
if projects:
project = projects[0]
if project:
LOG.info('Project ID %s' % project.id)
if 'issue_iid' not in alert.attributes:
# noinspection PyBroadException
try:
raw_data = json.dumps(alert.raw_data, indent=2, sort_keys=True)
raw_data = json.dumps(alert.raw_data, indent=2,
sort_keys=True)
except Exception as e:
LOG.info('Exception %s' % str(e))
raw_data = alert.raw_data
issue = project.issues.create({
'title': alert.text[0:128],
'description': '{}/alert/{}\n\nService: {}\n\nOrigin: {}\n\n```\n{}\n```\n'.format(ALERTA_URL, alert.id, alert.service, alert.origin, raw_data)
}) # type: Issue
'description': '{}/alert/{}\n\nService: {}\n\nOrigin: '
'{}\n\n```\n{}\n```\n'.format(
ALERTA_URL, alert.id, alert.service, alert.origin,
raw_data)
}) # type: Issue
alert.attributes['issue_iid'] = issue.id
alert.attributes['gitlabUrl'] = '<a href="{}" target="_blank">Issue #{}</a>'.format(
issue.web_url,
issue.iid
)
alert.attributes['gitlabUrl'] = \
'<a href="{}" target="_blank">Issue #{}</a>'.format(
issue.web_url,
issue.iid
)
return alert, action, text
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment