diff --git a/templates/gitlab/alerta_gitlab.py b/templates/gitlab/alerta_gitlab.py index 717c59ba5c1d55d49b45d5606ad83b03f232a9ff..5a71e4381e142d27c2e0853e4db41b77d9d597b6 100644 --- a/templates/gitlab/alerta_gitlab.py +++ b/templates/gitlab/alerta_gitlab.py @@ -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