From 8d30a664780dc2134828e62e206fc0c2a1b58997 Mon Sep 17 00:00:00 2001 From: Marvin Pohl Date: Tue, 4 Sep 2018 12:40:36 +0200 Subject: [PATCH] Added setup and initial webhook implementation --- gitea/__init__.py | 0 gitea/webhook.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 2 ++ setup.py | 17 ++++++++++ 4 files changed, 99 insertions(+) create mode 100644 gitea/__init__.py create mode 100644 gitea/webhook.py create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/gitea/__init__.py b/gitea/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/gitea/webhook.py b/gitea/webhook.py new file mode 100644 index 0000000..2bb7d18 --- /dev/null +++ b/gitea/webhook.py @@ -0,0 +1,80 @@ +import json +import re +from buildbot.util import bytes2unicode +from buildbot.www.hooks.base import BaseHookHandler + +from twisted.python import log + +_HEADER_EVENT_TYPE = 'X-Gitea-Event' + + +class GiteaHandler(BaseHookHandler): + + def processPushEvent(self, payload, event_type, codebase): + refname = payload["ref"] + + changes = [] + + # We only care about regular heads or tags + match = re.match(r"^refs/(heads|tags)/(.+)$", refname) + if not match: + log.msg("Ignoring refname `%s': Not a branch" % refname) + return changes + + branch = match.group(2) + + repository = payload["repository"] + repo_url = repository["html_url"] + project = repository["full_name"] + + for commit in payload["commits"]: + timestamp = commit["timestamp"] + change = { + 'author': '%s <%s>'.format((commit['author']['name'], + commit['author']['email'])), + 'comments': commit['message'], + 'revision': commit['id'], + 'when_timestamp': timestamp, + 'branch': branch, + 'revlink': commit['url'], + 'repository': repo_url, + 'project': project, + 'category': event_type, + 'properties': { + 'event': event_type, + }, + } + log.msg("Adding commit: {}".format(str(change))) + if codebase is not None: + change['codebase'] = codebase + changes.append(change) + return changes + + def getChanges(self, request): + secret = None + if self.options is dict: + secret = self.options.get("secret") + + try: + content = request.content.read() + payload = json.loads(bytes2unicode(content)) + log.msg("Payload:") + log.msg(payload) + except Exception as e: + raise ValueError("Error loading JSON: " + str(e)) + if secret is not None and secret != payload["secret"]: + raise ValueError("Invalid secret") + + event_type = bytes2unicode(request.getHeader(_HEADER_EVENT_TYPE)) + log.msg("Received event_type: {}".format(event_type)) + + codebases = request.args.get("codebase", [None]) + codebase = bytes2unicode(codebases[0]) + + changes = self.processPushEvent(payload, event_type, codebase) + + return (changes, "git") + + +# Plugin name +gitea = GiteaHandler diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..f5ea6cb --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +buildbot +distutils \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..ac26e5b --- /dev/null +++ b/setup.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python + +from distutils.core import setup + +setup(name='buildbot-gitea', + version='0.0.1', + description='buildbot plugin for integration with Gitea.', + author='Marvin Pohl', + author_email='marvin@lab132.com', + url='https://lab132.com', + packages=['gitea'], + entry_points={ + "buildbot.webhooks": [ + "Gitea = gitea.webhook:gitea" + ] + }, + )