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
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,7 @@
- 'alerta-server'
- 'alerta'
- 'uwsgi'
- 'python-gitlab'
- name: "Remove directories for web UI"
file:
......
import gitlab
import json
import logging
import requests
......@@ -7,7 +8,7 @@ from alerta.plugins import PluginBase, app
LOG = logging.getLogger('alerta.plugins')
ALERTA_URL = 'https://{{ alerta_domain }}'
GITLAB_URL = '{{ gitlab_issue.url }}/api/v4'
GITLAB_URL = '{{ gitlab_issue.url }}'
GITLAB_ACCESS_TOKEN = '{{ gitlab_issue.token }}'
......@@ -30,21 +31,13 @@ class GitlabIssue(PluginBase):
def take_action(self, alert, action, text, **kwargs):
"""should return internal id of external system"""
namespace = 'ansible-inventories/hosts/{}'.format(alert.environment)
base_url = '{}/projects'.format(GITLAB_URL)
LOG.info('Create GitLab issue for %s' % alert.environment)
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()
url = base_url + '?search={}'.format(origin)
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 project:
LOG.info('Project ID %s' % project.id)
if action == 'createIssue':
if 'issue_iid' not in alert.attributes:
......@@ -54,18 +47,14 @@ class GitlabIssue(PluginBase):
except Exception as e:
LOG.info('Exception %s' % str(e))
raw_data = alert.raw_data
query = urllib.parse.urlencode({
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)
})
url = '{}/issues?{}'.format(project_url, query)
#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)
}) # type: gitlab.Issue
alert.attributes['issue_iid'] = issue.id
alert.attributes['gitlabUrl'] = '<a href="{}" target="_blank">Issue #{}</a>'.format(
r.json().get('web_url', None),
r.json().get('iid', None)
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