Created
December 8, 2013 16:07
-
-
Save henrysher/7859543 to your computer and use it in GitHub Desktop.
This file contains 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
# A Github Webhook endpoint that triggers Koji builds | |
# | |
# Add a Webhook to your repo and point it to where you run this. | |
# | |
# It builds packages when you add #kojibuild to the commit message. | |
# If you add #scratch it will scratch build | |
# | |
# It assumes that you have a folder per package + version folder like: | |
# jetty/8.1.9/jetty-8.1.9.spec | |
# | |
# Can be easily adapted to work with one-package-per-repo | |
# | |
# Version: 0.1 | |
# | |
# Authors: | |
# Phil Schuler <[email protected]> | |
import tornado.ioloop | |
import tornado.web | |
import json | |
import re | |
import koji | |
import os | |
BUILD_SCRATCH_FLAG = '#scratch' | |
BUILD_TRIGGER = '#kojibuild' | |
KOJIHUB = 'https://koji.example.net/kojihub' | |
SERVERCA = os.path.expanduser('/home/kojiadmin/.koji/serverca.crt') | |
CLIENTCA = os.path.expanduser('/home/kojiadmin/.koji/clientca.crt') | |
CLIENTCERT = os.path.expanduser('/home/kojiadmin/.koji/client.crt') | |
BUILD_TARGET = 'dist-centos6' | |
class MainHandler(tornado.web.RequestHandler): | |
def koji_build(self,url,opts): | |
kojisession = koji.ClientSession(KOJIHUB) | |
kojisession.ssl_login(CLIENTCERT, CLIENTCA, SERVERCA) | |
result = kojisession.build(url,BUILD_TARGET,opts) | |
print result | |
def post(self): | |
payload = json.loads(self.get_argument('payload')) | |
# parse out what we need | |
commits = payload['head_commit']['modified'] | |
commits.extend(payload['head_commit']['added']) | |
commit_message = payload['head_commit']['message'] | |
repo_url = payload['repository']['url'] | |
revision = payload['after'] | |
if BUILD_TRIGGER in commit_message: | |
repo_path = commits[0].split('/') | |
repo_path = "%s/%s/#%s" % (repo_path[0],repo_path[1],revision) | |
repo_url = re.sub(r'^https?://','git+ssh://git@',repo_url) | |
repo_url = "%s.git?%s" % (repo_url,repo_path) | |
opts = {'scratch':0} | |
if BUILD_SCRATCH_FLAG in commit_message: | |
opts['scratch'] = 1 | |
self.koji_build(repo_url,opts) | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
# start the script on httpd | |
if __name__ == "__main__": | |
application.listen(8080) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment