From cf6e4ea670994f454eac7d66db03eb1e8626e5c3 Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Wed, 9 Oct 2019 18:33:06 +0200 Subject: [PATCH] Extend the webhook to easily support custom subclasses The design is based on the one used by Buildbot's own GitHubEventHandler. --- README.md | 33 +++++++++++++++++++++++++++++++++ buildbot_gitea/webhook.py | 33 +++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1777abd..3755e16 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/buildbot_gitea/webhook.py b/buildbot_gitea/webhook.py index cad92d9..a8bacee 100644 --- a/buildbot_gitea/webhook.py +++ b/buildbot_gitea/webhook.py @@ -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