X-Git-Url: http://andersk.mit.edu/gitweb/zcommit.git/blobdiff_plain/2e3d139cba07b9f39b1e2a59bdedee1647fb1845..15868496d067ac85edbf89b30775c09e56ba89a9:/zcommit.py diff --git a/zcommit.py b/zcommit.py index 59f1678..e18a436 100755 --- a/zcommit.py +++ b/zcommit.py @@ -2,54 +2,132 @@ import cherrypy from flup.server.fcgi import WSGIServer +import logging +import json +import os import subprocess import sys +import traceback -def zephyr(klass, instance, zsig, msg): +HERE = os.path.abspath(os.path.dirname(__file__)) +ZWRITE = os.path.join(HERE, 'bin', 'zsend') +LOG_FILENAME = 'logs/zcommit.log' + +# Set up a specific logger with our desired output level +logger = logging.getLogger(__name__) +logger.setLevel(logging.DEBUG) + +# Add the log message handler to the logger +handler = logging.FileHandler(LOG_FILENAME) +logger.addHandler(handler) + +def zephyr(sender, klass, instance, zsig, msg): # TODO: spoof the sender - cmd = ['zwrite', '-c', klass, '-i', instance, + logger.info("""About to send zephyr: +sender: %(sender)s +class: %(klass)s +instance: %(instance)s +zsig: %(zsig)s +msg: %(msg)s""" % {'sender' : sender, + 'klass' : klass, + 'instance' : instance, + 'zsig' : zsig, + 'msg' : msg}) + cmd = [ZWRITE, '-S', sender, '-c', klass, '-i', instance, '-s', zsig, '-d', '-m', msg] subprocess.check_call(cmd) class Application(object): @cherrypy.expose def index(self): - return 'Hello world!' + logger.debug('Hello world app reached') + return """ +

Welcome to zcommit.

- @cherrypy.expose - def default(self, *args, **kwargs): - return 'hello' +

zcommit allows you to send zephyr notifications by sending an HTTP +POST request to a URL. Currently zcommit supports POST-backs from +github. If you would like it to support another form of POST-back, +please let us know (zcommit@mit.edu).

+ +

URL structure

+ +The URL you post to is structured as follows: +http://zcommit.mit.edu/$type/$key1/$value1/$key2/$value2/.... +So for example, the URL +http://zcommit.mit.edu/github/class/zcommit/instance/commit +is parsed as having type github, class zcommit, and +instance commit. Using this information, zcommit figures out +how to form a useful message which is then sends as a zephyr. + +

Types

+ +

Github

+ +Set your POST-back URL to +http://zcommit.mit.edu/github/class/$classname, followed by +any of the following optional key/value parameters: + + +""" class Github(object): @cherrypy.expose def default(self, *args, **query): + try: + return self._default(*args, **query) + except Exception, e: + logger.error('Caught exception %s:\n%s' % (e, traceback.format_exc())) + raise + + def _default(self, *args, **query): + logger.info('A %s request with args: %r and query: %r' % + (cherrypy.request.method, args, query)) opts = {} if len(args) % 2: raise cherrypy.HTTPError(400, 'Invalid submission URL') + logger.debug('Passed validation') for i in xrange(0, len(args), 2): opts[args[i]] = args[i + 1] - + logger.debug('Set opts') if 'class' not in opts: raise cherrypy.HTTPError(400, 'Must specify a zephyr class name') - + logger.debug('Specified a class') if cherrypy.request.method == 'POST': + logger.debug('About to load data') payload = json.loads(query['payload']) - + logger.debug('Loaded payload data') zsig = payload['ref'] if 'zsig' in opts: zsig = '%s: %s' % (opts['zsig'], zsig) - else: - zsig = 'zcommit bot' - + sender = opts.get('sender', 'daemon.zcommit') + logger.debug('Set zsig') 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 + actions = [] + if c.get('added'): + actions.append('Added: %s\n' % '\n '.join(c['added'])) + if c.get('removed'): + actions.append('Removed: %s\n' % '\n '.join(c['removed'])) + if c.get('modified'): + actions.append('Modified: %s\n' % '\n '.join(c['modified'])) + if not actions: + actions.append('Did not add/remove/modify any nonempty files.') + info = {'name' : c['author']['name'], + 'email' : c['author']['email'], + 'message' : c['message'], + 'timestamp' : c['timestamp'], + 'actions' : '--\n'.join(actions)} + + msg = """%(name)s <%(email)s> (%(timestamp)s) +> %(message)s -- -%(added)s""" % c - zephyr(opts['class'], inst, zsig, msg) +%(actions)s""" % info + zephyr(sender, opts['class'], inst, zsig, msg) + msg = 'Thanks for posting!' else: msg = ('If you had sent a POST request to this URL, would have sent' ' a zepyhr to -c %s' % opts['class'])