Skip to content
Snippets Groups Projects
Commit 16f6013c authored by jurgenhaas's avatar jurgenhaas
Browse files

ansible-roles/alerta#9 Utilize GitLab python library

parent ffc14076
Branches
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
- 'alerta-server' - 'alerta-server'
- 'alerta' - 'alerta'
- 'uwsgi' - 'uwsgi'
- 'python-gitlab'
- name: "Remove directories for web UI" - name: "Remove directories for web UI"
file: file:
......
import gitlab
import json import json
import logging import logging
import requests import requests
...@@ -7,7 +8,7 @@ from alerta.plugins import PluginBase, app ...@@ -7,7 +8,7 @@ from alerta.plugins import PluginBase, app
LOG = logging.getLogger('alerta.plugins') LOG = logging.getLogger('alerta.plugins')
ALERTA_URL = 'https://{{ alerta_domain }}' ALERTA_URL = 'https://{{ alerta_domain }}'
GITLAB_URL = '{{ gitlab_issue.url }}/api/v4' GITLAB_URL = '{{ gitlab_issue.url }}'
GITLAB_ACCESS_TOKEN = '{{ gitlab_issue.token }}' GITLAB_ACCESS_TOKEN = '{{ gitlab_issue.token }}'
...@@ -30,21 +31,13 @@ class GitlabIssue(PluginBase): ...@@ -30,21 +31,13 @@ class GitlabIssue(PluginBase):
def take_action(self, alert, action, text, **kwargs): def take_action(self, alert, action, text, **kwargs):
"""should return internal id of external system""" """should return internal id of external system"""
namespace = 'ansible-inventories/hosts/{}'.format(alert.environment) LOG.info('Create GitLab issue for %s' % alert.environment)
base_url = '{}/projects'.format(GITLAB_URL) git = gitlab.Gitlab(GITLAB_URL, GITLAB_ACCESS_TOKEN, ssl_verify=True) # type: gitlab
git.auth()
project = git.projects.get('ansible-inventories/hosts/{}/{}'.format(alert.environment, alert.origin.split('/').pop()))
origin = alert.origin.split('/').pop() if project:
url = base_url + '?search={}'.format(origin) LOG.info('Project ID %s' % project.id)
r = requests.get(url, headers=self.headers)
pid = False
for p in r.json():
if p.get('namespace').get('full_path') == namespace:
pid = p.get('id', None)
break
LOG.info('Create GitLab issue for %s / %s / %s' % (alert.environment, origin, pid))
if pid:
project_url = '{}/{}'.format(base_url, pid)
if action == 'createIssue': if action == 'createIssue':
if 'issue_iid' not in alert.attributes: if 'issue_iid' not in alert.attributes:
...@@ -54,18 +47,14 @@ class GitlabIssue(PluginBase): ...@@ -54,18 +47,14 @@ class GitlabIssue(PluginBase):
except Exception as e: except Exception as e:
LOG.info('Exception %s' % str(e)) LOG.info('Exception %s' % str(e))
raw_data = alert.raw_data raw_data = alert.raw_data
query = urllib.parse.urlencode({ issue = project.issues.create({
'title': alert.text[0:128], '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) 'description': '{}/alert/{}\n\nService: {}\n\nOrigin: {}\n\n```\n{}\n```\n'.format(ALERTA_URL, alert.id, alert.service, alert.origin, raw_data)
}) }) # type: gitlab.Issue
url = '{}/issues?{}'.format(project_url, query) alert.attributes['issue_iid'] = issue.id
#LOG.info('Post to %s' % url)
r = requests.post(url, headers=self.headers)
#LOG.info('Response %s' % r.headers)
alert.attributes['issue_iid'] = r.json().get('iid', None)
alert.attributes['gitlabUrl'] = '<a href="{}" target="_blank">Issue #{}</a>'.format( alert.attributes['gitlabUrl'] = '<a href="{}" target="_blank">Issue #{}</a>'.format(
r.json().get('web_url', None), issue.web_url,
r.json().get('iid', None) issue.iid
) )
return alert, action, text return alert, action, text
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment