Created
November 2, 2017 06:44
-
-
Save shuLhan/0bdbe7e22c2df4bfc5b6d6c79ee282b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import print_function | |
from buildbot.www.hooks.base import BaseHookHandler | |
import logging | |
class MattermostHandler(BaseHookHandler): | |
def __init__(self, master, options): | |
if options is None: | |
raise ValueError('Missing options!') | |
self.master = master | |
self.options = options | |
## Get option values | |
self.opt_token = options.get("token", None) | |
if self.opt_token is None: | |
raise ValueError("Missing option 'token'") | |
self.opt_repo_base_url = options.get("repo_base_url", None) | |
if self.opt_repo_base_url is None: | |
raise ValueError("Missing option 'repo_base_url'") | |
self.opt_channel = options.get('channel', None) | |
def getChanges(self, request): | |
## | |
## Check token payload. | |
## | |
arg_token = firstOrNothing(request.args.get('token')) | |
if self.opt_token != arg_token: | |
raise ValueError('Invalid token!') | |
## | |
## Only allow specific channel to run the command. | |
## | |
arg_channel = firstOrNothing(request.args.get('channel_name')) | |
if self.opt_channel != arg_channel: | |
raise ValueError('Only specific channel can run the command %s %s' % (opt_channel, arg_channel)) | |
## parsing command parameters | |
command = firstOrNothing(request.args.get('command')) | |
text = firstOrNothing(request.args.get('text')) | |
cmd_args = text.split() | |
if len(cmd_args) == 0: | |
raise ValueError('Missing parameters...') | |
if command != "/build": | |
raise ValueError("Wrong command line!") | |
return do_build(cmd_args) | |
def firstOrNothing(value): | |
""" | |
Small helper function to return the first value (if value is a list) | |
or return the whole thing otherwise | |
""" | |
if (isinstance(value, type([]))): | |
return value[0] | |
else: | |
return value | |
def do_build(cmd_args): | |
author = "buildbot" | |
category = "mattermost" | |
comments = "Build from mattermost" | |
files = [] | |
properties = {} | |
revlink = "" | |
repo = cmd_args[0] | |
if len(cmd_args) >= 2: | |
branch = cmd_args[1] | |
else: | |
branch = "master" | |
## which revision? | |
if len(cmd_args) >= 3: | |
revision = cmd_args[2] | |
else: | |
revision = '' | |
project = "mattermost_build_%s_%s" % (repo, branch) | |
## which repository? | |
repository = self.opt_repo_base_url + repo | |
chdict = dict( | |
author=author | |
, branch=branch | |
, category=category | |
, comments=comments | |
, files=files | |
, project=project | |
, properties=properties | |
, repository=repository | |
, revision=revision | |
, revlink=revlink | |
) | |
return ([chdict], None) | |
mattermost = MattermostHandler |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment