From f49f2f3c683ca5640af4099dd0c8a464c7b95ec4 Mon Sep 17 00:00:00 2001 From: dadada Date: Tue, 15 Feb 2022 22:40:43 +0100 Subject: [PATCH] Initial commit --- .env.local.example | 4 +++ .envrc | 3 ++ .gitignore | 4 +++ LICENSE | 8 ++++++ README.md | 6 ++++ glrmmr/__init__.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++ setup.cfg | 24 ++++++++++++++++ setup.py | 3 ++ 8 files changed, 122 insertions(+) create mode 100644 .env.local.example create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 glrmmr/__init__.py create mode 100644 setup.cfg create mode 100644 setup.py diff --git a/.env.local.example b/.env.local.example new file mode 100644 index 0000000..b459b23 --- /dev/null +++ b/.env.local.example @@ -0,0 +1,4 @@ +REDMINE_URL=http://localhost:8080 +REDMINE_API_KEY=000000000000 +GITLAB_TOKEN=0000000000000 +CODE_CHANGES_CUSTOM_FIELD_ID=1 diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..346c90d --- /dev/null +++ b/.envrc @@ -0,0 +1,3 @@ +export FLASK_APP=glrmmr +export FLASK_ENV=development +layout python3 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..660dc0f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.direnv +__pycache__ +venv +.env.local diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9e721c8 --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +Copyright 2022 Tim Schubert + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..544b202 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Gitlab Redmine Merge Request URL linker + +A webhook that links a Gitlab MR to a Redmine Issue using the issue ID and a custom field in Redmine. + +To get started, edit the `.envrc.local` and run `flask run`. + diff --git a/glrmmr/__init__.py b/glrmmr/__init__.py new file mode 100644 index 0000000..ecd2078 --- /dev/null +++ b/glrmmr/__init__.py @@ -0,0 +1,70 @@ +import re +import requests + +from dotenv import dotenv_values +from flask import Flask, request + + +config = dotenv_values(__name__ + "/../.env.local") +app = Flask(__name__) + + +@app.route("/", methods=['POST']) +def hook(): + headers = request.headers + + gitlab_validation_token = headers.get('X-Gitlab-Token') + if not gitlab_validation_token or gitlab_validation_token != config['GITLAB_TOKEN']: + return 'Invalid Gitlab token\n', 403 + + event_type = headers.get('X-Gitlab-Event') + if not event_type: + return 'Must set X-Gitlab-Event\n', 422 + + data = request.json + if not data: + return 'Must send JSON data\n', 422 + + attrs = data.get('object_attributes') + if not attrs: + return 'Missing object_attributes\n', 422 + + if event_type == 'Merge Request Hook': + mr_title = attrs.get('title') + if not mr_title: + return 'Missing MR title\n', 422 + mr_url = attrs.get('url') + if not mr_url: + return 'Missing MR URL\n', 422 + + fail = 0 + for ticket in parse_tickets(mr_title): + try: + update_ticket(ticket, mr_url) + except Exception as e: + fail = fail + 1 + + if fail: + return 'Failed to update %d tickets\n' % (fail,), 500 + + return 'OK\n', 200 + + +def parse_tickets(string): + pattern = re.compile('#([0-9]+)') + tickets = pattern.findall(string) + + return tickets + + +def update_ticket(ticket, mr_url): + field_id = config['CODE_CHANGES_CUSTOM_FIELD_ID'] + data = {'issue': { 'custom_fields': [{'id': field_id, 'value': mr_url}]}} + result = requests.put( + '%s/issues/%s.json' % (config['REDMINE_URL'], ticket,), + headers={'X-Redmine-API-Key': config['REDMINE_API_KEY']}, + json=data + ) + if result.status_code != 204: + raise Exception('Failed to update %s: %d %s' % (ticket, result.status_code, result.body)) + diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..a1209a7 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,24 @@ +[metadata] +name = glrmmr +version = 0.0.1 +author = Tim Schubert +author_email = dadada@dadada.li +description = A webhook service to update Redmine with a link to a Gitlab MR +long_description = file: README.md +long_description_content_type = text/markdown +url = https://github.com/dadada/glrmmr +project_urls = + Bug Tracker = https://github.com/dadada/glrmmr/issues +classifiers = + Programming Language :: Python :: 3 + License :: OSI Approved :: MIT License + Operating System :: OS Independent + +[options] +packages = :find +python_requires = >=3.6 +include_package_data = true +install_requires = + Flask + python-dotenv + requests diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..6068493 --- /dev/null +++ b/setup.py @@ -0,0 +1,3 @@ +from setuptools import setup + +setup()