Added setup and initial webhook implementation

This commit is contained in:
Marvin Pohl 2018-09-04 12:40:36 +02:00
parent eb5ae32dc9
commit 8d30a66478
4 changed files with 99 additions and 0 deletions

0
gitea/__init__.py Normal file
View File

80
gitea/webhook.py Normal file
View File

@ -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

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
buildbot
distutils

17
setup.py Normal file
View File

@ -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"
]
},
)