Extend the webhook to easily support custom subclasses

The design is based on the one used by Buildbot's own GitHubEventHandler.
This commit is contained in:
JamesWrigley 2019-10-09 18:33:06 +02:00 committed by Marvin Pohl
parent 6979276a19
commit cf6e4ea670
2 changed files with 56 additions and 10 deletions

View File

@ -51,6 +51,38 @@ factory.addStep(steps.Gitea(
))
```
The webhook currently supports pushes and pull requests by default, but you can
subclass `buildbot_gitea.webhook.GiteaHandler` to add supports for other events,
and then use your subclass by setting the `class` parameter:
```py
# myhook.py
from buildbot_gitea.webhook import GiteaHandler
class MyGiteaHook(GiteaHandler)
def process_whatever(self, payload, event_type, codebase):
# This should be a list of dicts
changes = []
return changes
# master.cfg
from myhook import MyGiteaHook
c['www'] = {
'change_hook_dialects': {
'gitea': {
'class': MyGiteaHook,
# ...
}
}
}
```
Note that the handlers need to be named according to the scheme:
`process_{event}` (e.g., `process_create`, etc).
# Parameters
## Change Hook
@ -61,6 +93,7 @@ The change hook is set as part of the `www` section in the `change_hook_dialects
| --- | --- |
| `secret` | The secret, which needs to be set in gitea |
| `onlyIncludePushCommit` | A push may have more than one commit associated with it. If this is true, only the newest (latest) commit of all received will be added as a change to buildbot. If this is set to false, all commits will inside the push will be added. |
| `class` | Set this if you want to use your own handler class (see above for details) |
In gitea in your project or organization and add a new webhook of type gitea.
Set the parameters as follows:

View File

@ -11,7 +11,7 @@ _HEADER_EVENT_TYPE = 'X-Gitea-Event'
class GiteaHandler(BaseHookHandler):
def processPushEvent(self, payload, event_type, codebase):
def process_push(self, payload, event_type, codebase):
refname = payload["ref"]
changes = []
@ -56,7 +56,7 @@ class GiteaHandler(BaseHookHandler):
changes.append(change)
return changes
def processPullRequestEvent(self, payload, event_type, codebase):
def process_pull_request(self, payload, event_type, codebase):
action = payload['action']
# Only handle potential new stuff, ignore close/.
@ -128,17 +128,30 @@ class GiteaHandler(BaseHookHandler):
codebases = request.args.get('codebase', [None])
codebase = bytes2unicode(codebases[0])
changes = []
if event_type == 'push':
changes = self.processPushEvent(
payload, event_type, codebase)
elif event_type == 'pull_request':
changes = self.processPullRequestEvent(
payload, event_type, codebase)
else:
handler_function = getattr(self, 'process_{}'.format(event_type), None)
if not handler_function:
log.msg("Ignoring gitea event '{}'".format(event_type))
else:
changes = handler_function(payload, event_type, codebase)
return (changes, 'git')
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)
# Plugin name
gitea = GiteaHandler
gitea = GiteaHandlerPlugin