]>
Commit | Line | Data |
---|---|---|
2e3d139c GB |
1 | #!/usr/bin/python |
2 | ||
3 | import cherrypy | |
4 | from flup.server.fcgi import WSGIServer | |
5 | import subprocess | |
6 | import sys | |
7 | ||
8 | def zephyr(klass, instance, zsig, msg): | |
9 | # TODO: spoof the sender | |
10 | cmd = ['zwrite', '-c', klass, '-i', instance, | |
11 | '-s', zsig, '-d', '-m', msg] | |
12 | subprocess.check_call(cmd) | |
13 | ||
14 | class Application(object): | |
15 | @cherrypy.expose | |
16 | def index(self): | |
17 | return 'Hello world!' | |
18 | ||
19 | @cherrypy.expose | |
20 | def default(self, *args, **kwargs): | |
21 | return 'hello' | |
22 | ||
23 | class Github(object): | |
24 | @cherrypy.expose | |
25 | def default(self, *args, **query): | |
26 | opts = {} | |
27 | if len(args) % 2: | |
28 | raise cherrypy.HTTPError(400, 'Invalid submission URL') | |
29 | for i in xrange(0, len(args), 2): | |
30 | opts[args[i]] = args[i + 1] | |
31 | ||
32 | if 'class' not in opts: | |
33 | raise cherrypy.HTTPError(400, 'Must specify a zephyr class name') | |
34 | ||
35 | if cherrypy.request.method == 'POST': | |
36 | payload = json.loads(query['payload']) | |
37 | ||
38 | zsig = payload['ref'] | |
39 | if 'zsig' in opts: | |
40 | zsig = '%s: %s' % (opts['zsig'], zsig) | |
41 | else: | |
42 | zsig = 'zcommit bot' | |
43 | ||
44 | for c in reversed(payload['commits']): | |
45 | inst = opts.get('instance', c['id'][:8]) | |
46 | c['added_as_str'] = '\n'.join(c['added']) | |
47 | msg = """%(name)s <%(email)s> | |
48 | %(message)s | |
49 | %(timestamp)s | |
50 | -- | |
51 | %(added)s""" % c | |
52 | zephyr(opts['class'], inst, zsig, msg) | |
53 | else: | |
54 | msg = ('If you had sent a POST request to this URL, would have sent' | |
55 | ' a zepyhr to -c %s' % opts['class']) | |
56 | return msg | |
57 | ||
58 | github = Github() | |
59 | ||
60 | def main(): | |
61 | app = cherrypy.tree.mount(Application(), '/zcommit') | |
62 | cherrypy.server.unsubscribe() | |
63 | cherrypy.engine.start() | |
64 | try: | |
65 | WSGIServer(app, environ={'SCRIPT_NAME' : '/zcommit'}).run() | |
66 | finally: | |
67 | cherrypy.engine.stop() | |
68 | ||
69 | if __name__ == '__main__': | |
70 | sys.exit(main()) |