2018-09-04 12:40:36 +02:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
from buildbot.util import bytes2unicode
|
|
|
|
from buildbot.www.hooks.base import BaseHookHandler
|
|
|
|
|
|
|
|
from twisted.python import log
|
2018-09-04 12:58:05 +02:00
|
|
|
from dateutil.parser import parse as dateparse
|
2018-09-04 12:40:36 +02:00
|
|
|
|
|
|
|
_HEADER_EVENT_TYPE = 'X-Gitea-Event'
|
|
|
|
|
|
|
|
|
|
|
|
class GiteaHandler(BaseHookHandler):
|
|
|
|
|
2019-10-09 18:33:06 +02:00
|
|
|
def process_push(self, payload, event_type, codebase):
|
2018-09-04 12:40:36 +02:00
|
|
|
refname = payload["ref"]
|
|
|
|
|
|
|
|
changes = []
|
|
|
|
|
|
|
|
# We only care about regular heads or tags
|
|
|
|
match = re.match(r"^refs/(heads|tags)/(.+)$", refname)
|
|
|
|
if not match:
|
2018-09-04 15:52:49 +02:00
|
|
|
log.msg("Ignoring refname '{}': Not a branch or tag".format(refname))
|
2018-09-04 12:40:36 +02:00
|
|
|
return changes
|
|
|
|
|
|
|
|
branch = match.group(2)
|
|
|
|
|
2018-09-04 15:04:29 +02:00
|
|
|
repository = payload['repository']
|
|
|
|
repo_url = repository['ssh_url']
|
|
|
|
project = repository['full_name']
|
2018-09-04 12:40:36 +02:00
|
|
|
|
2018-11-05 13:05:57 +01:00
|
|
|
commits = payload['commits']
|
2019-01-21 20:13:38 +01:00
|
|
|
if isinstance(self.options, dict) and self.options.get('onlyIncludePushCommit', False):
|
2018-11-05 13:05:57 +01:00
|
|
|
commits = commits[:1]
|
|
|
|
|
|
|
|
for commit in commits:
|
2018-09-04 15:04:29 +02:00
|
|
|
timestamp = dateparse(commit['timestamp'])
|
2018-09-04 12:40:36 +02:00
|
|
|
change = {
|
2018-09-04 15:10:46 +02:00
|
|
|
'author': '{} <{}>'.format(commit['author']['name'],
|
|
|
|
commit['author']['email']),
|
2018-09-04 12:40:36 +02:00
|
|
|
'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,
|
2018-09-27 14:46:35 +02:00
|
|
|
'repository_name': repository['name'],
|
|
|
|
'owner': repository["owner"]["username"]
|
2018-09-04 12:40:36 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if codebase is not None:
|
|
|
|
change['codebase'] = codebase
|
|
|
|
changes.append(change)
|
|
|
|
return changes
|
|
|
|
|
2019-10-09 18:33:06 +02:00
|
|
|
def process_pull_request(self, payload, event_type, codebase):
|
2018-09-04 15:04:29 +02:00
|
|
|
action = payload['action']
|
|
|
|
|
|
|
|
# Only handle potential new stuff, ignore close/.
|
|
|
|
# Merge itself is handled by the regular branch push message
|
|
|
|
if action not in ['opened', 'synchronized', 'edited', 'reopened']:
|
|
|
|
log.msg("Gitea Pull Request event '{}' ignored".format(action))
|
|
|
|
return []
|
|
|
|
pull_request = payload['pull_request']
|
2018-09-27 13:21:10 +02:00
|
|
|
if not pull_request['mergeable']:
|
|
|
|
log.msg("Gitea Pull Request ignored because it is not mergeable.")
|
|
|
|
return []
|
|
|
|
if pull_request['merged']:
|
|
|
|
log.msg("Gitea Pull Request ignored because it is already merged.")
|
|
|
|
return []
|
2018-09-04 15:04:29 +02:00
|
|
|
timestamp = dateparse(pull_request['updated_at'])
|
|
|
|
base = pull_request['base']
|
|
|
|
head = pull_request['head']
|
2018-09-04 15:08:36 +02:00
|
|
|
repository = payload['repository']
|
2018-09-04 15:04:29 +02:00
|
|
|
change = {
|
2018-09-04 15:10:46 +02:00
|
|
|
'author': '{} <{}>'.format(pull_request['user']['full_name'],
|
|
|
|
pull_request['user']['email']),
|
2018-09-04 15:16:30 +02:00
|
|
|
'comments': 'PR#{}: {}\n\n{}'.format(
|
|
|
|
pull_request['number'],
|
|
|
|
pull_request['title'],
|
|
|
|
pull_request['body']),
|
2018-09-05 14:14:31 +02:00
|
|
|
'revision': head['sha'],
|
2018-09-04 15:04:29 +02:00
|
|
|
'when_timestamp': timestamp,
|
|
|
|
'branch': head['ref'],
|
|
|
|
'revlink': pull_request['html_url'],
|
|
|
|
'repository': repository['ssh_url'],
|
|
|
|
'project': repository['full_name'],
|
|
|
|
'category': event_type,
|
|
|
|
'properties': {
|
|
|
|
'event': event_type,
|
|
|
|
'base_branch': base['ref'],
|
|
|
|
'base_sha': base['sha'],
|
|
|
|
'base_repo_id': base['repo_id'],
|
2018-09-04 15:13:00 +02:00
|
|
|
'base_repository': base['repo']['clone_url'],
|
|
|
|
'base_git_ssh_url': base['repo']['ssh_url'],
|
2018-09-04 15:04:29 +02:00
|
|
|
'head_branch': head['ref'],
|
|
|
|
'head_sha': head['sha'],
|
|
|
|
'head_repo_id': head['repo_id'],
|
2018-09-04 15:13:00 +02:00
|
|
|
'head_repository': head['repo']['clone_url'],
|
|
|
|
'head_git_ssh_url': head['repo']['ssh_url'],
|
2018-09-04 15:04:29 +02:00
|
|
|
'pr_id': pull_request['id'],
|
|
|
|
'pr_number': pull_request['number'],
|
2018-09-27 14:46:35 +02:00
|
|
|
'repository_name': repository['name'],
|
|
|
|
'owner': repository["owner"]["username"],
|
2018-09-04 15:04:29 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
if codebase is not None:
|
|
|
|
change['codebase'] = codebase
|
|
|
|
return [change]
|
|
|
|
|
2018-09-04 12:40:36 +02:00
|
|
|
def getChanges(self, request):
|
|
|
|
secret = None
|
2018-09-04 15:52:49 +02:00
|
|
|
if isinstance(self.options, dict):
|
2018-09-04 15:04:29 +02:00
|
|
|
secret = self.options.get('secret')
|
2018-09-04 12:40:36 +02:00
|
|
|
try:
|
|
|
|
content = request.content.read()
|
|
|
|
payload = json.loads(bytes2unicode(content))
|
2018-09-27 13:21:10 +02:00
|
|
|
except Exception as exception:
|
|
|
|
raise ValueError('Error loading JSON: ' + str(exception))
|
2018-09-04 15:04:29 +02:00
|
|
|
if secret is not None and secret != payload['secret']:
|
|
|
|
raise ValueError('Invalid secret')
|
2018-09-04 12:40:36 +02:00
|
|
|
event_type = bytes2unicode(request.getHeader(_HEADER_EVENT_TYPE))
|
2018-09-04 15:04:29 +02:00
|
|
|
log.msg("Received event '{}' from gitea".format(event_type))
|
2018-09-04 12:40:36 +02:00
|
|
|
|
2018-09-04 15:04:29 +02:00
|
|
|
codebases = request.args.get('codebase', [None])
|
2018-09-04 12:40:36 +02:00
|
|
|
codebase = bytes2unicode(codebases[0])
|
2018-09-04 15:04:29 +02:00
|
|
|
changes = []
|
2019-10-09 18:33:06 +02:00
|
|
|
|
|
|
|
handler_function = getattr(self, 'process_{}'.format(event_type), None)
|
|
|
|
if not handler_function:
|
2018-09-04 15:04:29 +02:00
|
|
|
log.msg("Ignoring gitea event '{}'".format(event_type))
|
2019-10-09 18:33:06 +02:00
|
|
|
else:
|
|
|
|
changes = handler_function(payload, event_type, codebase)
|
2018-09-04 15:04:29 +02:00
|
|
|
|
|
|
|
return (changes, 'git')
|
2018-09-04 12:40:36 +02:00
|
|
|
|
|
|
|
|
2019-10-09 18:33:06 +02:00
|
|
|
class GiteaHandlerPlugin(BaseHookHandler):
|
|
|
|
def __init__(self, master, options):
|
|
|
|
if not options:
|
|
|
|
options = {}
|
|
|
|
super().__init__(master, options)
|
|
|
|
|
|
|
|
handler_class = options.get('class', GiteaHandler)
|
|
|
|
if 'class' in options:
|
|
|
|
del options['class']
|
|
|
|
|
|
|
|
self.handler = handler_class(master, options)
|
|
|
|
|
|
|
|
def getChanges(self, request):
|
|
|
|
return self.handler.getChanges(request)
|
|
|
|
|
2018-09-04 12:40:36 +02:00
|
|
|
# Plugin name
|
2019-10-09 18:33:06 +02:00
|
|
|
gitea = GiteaHandlerPlugin
|