From 2e3d139cba07b9f39b1e2a59bdedee1647fb1845 Mon Sep 17 00:00:00 2001 From: Greg Brockman Date: Tue, 23 Mar 2010 03:25:38 -0400 Subject: [PATCH] Initial checkin --- zcommit.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100755 zcommit.py diff --git a/zcommit.py b/zcommit.py new file mode 100755 index 0000000..59f1678 --- /dev/null +++ b/zcommit.py @@ -0,0 +1,70 @@ +#!/usr/bin/python + +import cherrypy +from flup.server.fcgi import WSGIServer +import subprocess +import sys + +def zephyr(klass, instance, zsig, msg): + # TODO: spoof the sender + cmd = ['zwrite', '-c', klass, '-i', instance, + '-s', zsig, '-d', '-m', msg] + subprocess.check_call(cmd) + +class Application(object): + @cherrypy.expose + def index(self): + return 'Hello world!' + + @cherrypy.expose + def default(self, *args, **kwargs): + return 'hello' + + class Github(object): + @cherrypy.expose + def default(self, *args, **query): + opts = {} + if len(args) % 2: + raise cherrypy.HTTPError(400, 'Invalid submission URL') + for i in xrange(0, len(args), 2): + opts[args[i]] = args[i + 1] + + if 'class' not in opts: + raise cherrypy.HTTPError(400, 'Must specify a zephyr class name') + + if cherrypy.request.method == 'POST': + payload = json.loads(query['payload']) + + zsig = payload['ref'] + if 'zsig' in opts: + zsig = '%s: %s' % (opts['zsig'], zsig) + else: + zsig = 'zcommit bot' + + for c in reversed(payload['commits']): + inst = opts.get('instance', c['id'][:8]) + c['added_as_str'] = '\n'.join(c['added']) + msg = """%(name)s <%(email)s> +%(message)s +%(timestamp)s +-- +%(added)s""" % c + zephyr(opts['class'], inst, zsig, msg) + else: + msg = ('If you had sent a POST request to this URL, would have sent' + ' a zepyhr to -c %s' % opts['class']) + return msg + + github = Github() + +def main(): + app = cherrypy.tree.mount(Application(), '/zcommit') + cherrypy.server.unsubscribe() + cherrypy.engine.start() + try: + WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run() + finally: + cherrypy.engine.stop() + +if __name__ == '__main__': + sys.exit(main()) -- 2.45.1