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

#6 Functional Gitlab plugin

parent 7e9b9b2f
Branches
No related tags found
No related merge requests found
......@@ -99,3 +99,16 @@
minute: '0'
job: '/usr/local/bin/alerta housekeeping'
tags: 'cron'
- name: "Copy Gitlab plugin"
template:
src: 'gitlab/{{ item }}'
dest: '/tmp/{{ item }}'
with_items:
- 'alerta_gitlab.py'
- 'setup.py'
- name: "Install Gitlab plugin"
shell: 'python setup.py install'
args:
chdir: '/tmp'
import logging
import requests
import urllib.parse
from alerta.plugins import PluginBase, app
LOG = logging.getLogger('alerta.plugins.gitlab')
ALERTA_URL = 'https://{{ alerta_domain }}'
GITLAB_URL = '{{ gitlab_issue.url }}/api/v4'
GITLAB_ACCESS_TOKEN = '{{ gitlab_issue.token }}'
class GitlabIssue(PluginBase):
def __init__(self):
self.headers = {'Private-Token': GITLAB_ACCESS_TOKEN}
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
return alert
def status_change(self, alert, status, text):
return alert, status, text
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)
url = BASE_URL + '?search={}'.format(alert.resource)
r = requests.get(url, headers=self.headers)
for p in r.json():
if p.get('namespace').get('full_path') == namespace:
pid = p.get('id', None)
break
if pid:
BASE_URL = '{}/{}'.format(BASE_URL, pid)
if action == 'createIssue':
if 'issue_iid' not in alert.attributes:
query = urllib.parse.urlencode({
'title': alert.text,
'description': '{}/#/alert/{}'.format(ALERTA_URL, alert.id)
})
url = '{}/issues?{}'.format(BASE_URL, query)
r = requests.post(url, headers=self.headers)
alert.attributes['issue_iid'] = r.json().get('iid', None)
alert.attributes['gitlabUrl'] = '<a href="{}" target="_blank">Issue #{}</a>'.format(
r.json().get('web_url', None),
r.json().get('iid', None)
)
return alert, action, text
from setuptools import find_packages, setup
version = '1.0.0'
setup(
name='alerta-gitlab',
version=version,
description='LakeDrops Alerta plugin for GitLab Issues',
url='https://gitlab.lakedrops.com/ansible-roles/alerta',
license='MIT',
author='Jürgen Haas',
author_email='juergen.haas@lakedrops.com',
packages=find_packages(),
py_modules=['alerta_gitlab'],
install_requires=[
'requests'
],
include_package_data=True,
zip_safe=True,
entry_points={
'alerta.plugins': [
'gitlab = alerta_gitlab:GitlabIssue'
]
}
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment