]>
Commit | Line | Data |
---|---|---|
2e3d139c GB |
1 | #!/usr/bin/python |
2 | ||
3 | import cherrypy | |
4 | from flup.server.fcgi import WSGIServer | |
08b967d5 GB |
5 | import logging |
6 | import json | |
6e759ed5 | 7 | import os |
2e3d139c GB |
8 | import subprocess |
9 | import sys | |
08b967d5 GB |
10 | import traceback |
11 | ||
2a04f5ae | 12 | HERE = os.path.abspath(os.path.dirname(__file__)) |
9b6ac1af | 13 | ZWRITE = os.path.join(HERE, 'bin', 'zsend') |
08b967d5 GB |
14 | LOG_FILENAME = 'logs/zcommit.log' |
15 | ||
16 | # Set up a specific logger with our desired output level | |
17 | logger = logging.getLogger(__name__) | |
18 | logger.setLevel(logging.DEBUG) | |
19 | ||
20 | # Add the log message handler to the logger | |
21 | handler = logging.FileHandler(LOG_FILENAME) | |
22 | logger.addHandler(handler) | |
2e3d139c | 23 | |
9b6ac1af | 24 | def zephyr(sender, klass, instance, zsig, msg): |
2e3d139c | 25 | # TODO: spoof the sender |
08b967d5 | 26 | logger.info("""About to send zephyr: |
9b6ac1af | 27 | sender: %(sender)s |
08b967d5 GB |
28 | class: %(klass)s |
29 | instance: %(instance)s | |
30 | zsig: %(zsig)s | |
9b6ac1af GB |
31 | msg: %(msg)s""" % {'sender' : sender, |
32 | 'klass' : klass, | |
33 | 'instance' : instance, | |
34 | 'zsig' : zsig, | |
35 | 'msg' : msg}) | |
36 | cmd = [ZWRITE, '-S', sender, '-c', klass, '-i', instance, | |
2e3d139c GB |
37 | '-s', zsig, '-d', '-m', msg] |
38 | subprocess.check_call(cmd) | |
39 | ||
40 | class Application(object): | |
41 | @cherrypy.expose | |
42 | def index(self): | |
08b967d5 | 43 | logger.debug('Hello world app reached') |
2a5885f5 GB |
44 | return """ |
45 | <p> <i>Welcome to zcommit.</i> </p> | |
46 | ||
47 | <p> zcommit allows you to send zephyr notifications by sending an HTTP | |
48 | POST request to a URL. Currently zcommit supports POST-backs from | |
49 | github. If you would like it to support another form of POST-back, | |
50 | please let us know (zcommit@mit.edu). </p> | |
51 | ||
52 | <h1> URL structure </h1> | |
53 | ||
54 | The URL you post to is structured as follows: | |
55 | <tt>http://zcommit.mit.edu/$type/$key1/$value1/$key2/$value2/...</tt>. | |
56 | So for example, the URL | |
57 | <tt>http://zcommit.mit.edu/github/class/zcommit/instance/commit</tt> | |
58 | is parsed as having type <tt>github</tt>, class <tt>zcommit</tt>, and | |
59 | instance <tt>commit</tt>. Using this information, zcommit figures out | |
60 | how to form a useful message which is then sends as a zephyr. | |
61 | ||
15868496 GB |
62 | <h1> Types </h1> |
63 | ||
64 | <h2> Github </h2> | |
2a5885f5 GB |
65 | |
66 | Set your POST-back URL to | |
67 | <tt>http://zcommit.mit.edu/github/class/$classname</tt>, followed by | |
68 | any of the following optional key/value parameters: | |
69 | ||
70 | <ul> | |
71 | <li> <tt>/instance/$instance</tt> </li> | |
72 | <li> <tt>/zsig/$zsig</tt> </li> | |
73 | <li> <tt>/sender/$sender</tt> </li> | |
74 | </ul> | |
75 | """ | |
2e3d139c | 76 | |
2e3d139c GB |
77 | class Github(object): |
78 | @cherrypy.expose | |
79 | def default(self, *args, **query): | |
08b967d5 GB |
80 | try: |
81 | return self._default(*args, **query) | |
82 | except Exception, e: | |
83 | logger.error('Caught exception %s:\n%s' % (e, traceback.format_exc())) | |
84 | raise | |
85 | ||
86 | def _default(self, *args, **query): | |
87 | logger.info('A %s request with args: %r and query: %r' % | |
88 | (cherrypy.request.method, args, query)) | |
2e3d139c GB |
89 | opts = {} |
90 | if len(args) % 2: | |
91 | raise cherrypy.HTTPError(400, 'Invalid submission URL') | |
08b967d5 | 92 | logger.debug('Passed validation') |
2e3d139c GB |
93 | for i in xrange(0, len(args), 2): |
94 | opts[args[i]] = args[i + 1] | |
08b967d5 | 95 | logger.debug('Set opts') |
2e3d139c GB |
96 | if 'class' not in opts: |
97 | raise cherrypy.HTTPError(400, 'Must specify a zephyr class name') | |
08b967d5 | 98 | logger.debug('Specified a class') |
2e3d139c | 99 | if cherrypy.request.method == 'POST': |
08b967d5 | 100 | logger.debug('About to load data') |
2e3d139c | 101 | payload = json.loads(query['payload']) |
08b967d5 | 102 | logger.debug('Loaded payload data') |
2e3d139c GB |
103 | zsig = payload['ref'] |
104 | if 'zsig' in opts: | |
105 | zsig = '%s: %s' % (opts['zsig'], zsig) | |
9b6ac1af | 106 | sender = opts.get('sender', 'daemon.zcommit') |
08b967d5 | 107 | logger.debug('Set zsig') |
2e3d139c GB |
108 | for c in reversed(payload['commits']): |
109 | inst = opts.get('instance', c['id'][:8]) | |
8ab83150 GB |
110 | actions = [] |
111 | if c.get('added'): | |
c3d0ddba | 112 | actions.append('Added: %s\n' % '\n '.join(c['added'])) |
8ab83150 | 113 | if c.get('removed'): |
c3d0ddba | 114 | actions.append('Removed: %s\n' % '\n '.join(c['removed'])) |
8ab83150 | 115 | if c.get('modified'): |
c3d0ddba | 116 | actions.append('Modified: %s\n' % '\n '.join(c['modified'])) |
8ab83150 | 117 | if not actions: |
1f347cd0 | 118 | actions.append('Did not add/remove/modify any nonempty files.') |
08b967d5 GB |
119 | info = {'name' : c['author']['name'], |
120 | 'email' : c['author']['email'], | |
121 | 'message' : c['message'], | |
122 | 'timestamp' : c['timestamp'], | |
f62371e5 GB |
123 | 'actions' : '--\n'.join(actions), |
124 | 'url' : c['url']} | |
8ab83150 | 125 | |
a4c4e67d | 126 | msg = """%(name)s <%(email)s> (%(timestamp)s) |
f62371e5 | 127 | %(url)s |
a4c4e67d | 128 | > %(message)s |
2e3d139c | 129 | -- |
8ab83150 | 130 | %(actions)s""" % info |
9b6ac1af | 131 | zephyr(sender, opts['class'], inst, zsig, msg) |
08b967d5 | 132 | msg = 'Thanks for posting!' |
2e3d139c GB |
133 | else: |
134 | msg = ('If you had sent a POST request to this URL, would have sent' | |
135 | ' a zepyhr to -c %s' % opts['class']) | |
136 | return msg | |
137 | ||
138 | github = Github() | |
139 | ||
140 | def main(): | |
141 | app = cherrypy.tree.mount(Application(), '/zcommit') | |
142 | cherrypy.server.unsubscribe() | |
143 | cherrypy.engine.start() | |
144 | try: | |
145 | WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run() | |
146 | finally: | |
147 | cherrypy.engine.stop() | |
148 | ||
149 | if __name__ == '__main__': | |
150 | sys.exit(main()) |