Public » Squawker
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

tip initial commit

Changeset 3279396c8cd6

by Profile picture of Benjamin PollackBenjamin Pollack

Changes to 2 files · Browse files at 3279396c8cd6 Diff from another changeset...

Change 1 of 1 Show Entire File requirements.txt Stacked
 
 
 
 
 
1
2
3
@@ -0,0 +1,3 @@
+Flask==0.6 +Jinja2==2.5.5 +Werkzeug==0.6.2
Change 1 of 1 Show Entire File squawk.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
@@ -0,0 +1,57 @@
+#!/usr/bin/env python + +import cgi +import json +import re +import urllib +import urllib2 + +from flask import Flask, request +app = Flask(__name__) + +# Make a file called .auth with your HipChat API token +# as the first line and the room number as the second line +with open('.auth') as f: + ROOM_ID, AUTH_TOKEN = [l.strip() for l in f.readlines()] + +def api_url(action): + "Get the appropriate URL, with auth tokens, to talk to HipChat's API" + return 'http://api.hipchat.com/v1/%s?auth_token=%s&format=json' % (action, AUTH_TOKEN) + +def message(room_id, speaker, message): + "Sends a message to the given room ID, injecting appropriate auth tokens" + url = api_url('rooms/message') + data = urllib.urlencode({'room_id': room_id, + 'from': speaker, + 'message': message}) + return json.loads(urllib2.urlopen(url, data).read()) + +@app.route('/squawk', methods=['POST']) +def squawk(): + payload = json.loads(request.form['payload']) + repo = payload['repository'] + if not repo['central']: + return "I DON'T DO BRANCHES" + path = repo['url'].split('/')[-3:] + if path[0] == 'Repo': + path = path[1:] + repo_name = '/'.join(path) + pusher = payload['pusher'] + commits = payload['commits'] + description = '<a href="mailto:%s">%s</a> ' % (pusher['email'], cgi.escape(pusher['fullName'])) + + if len(commits) == 1: + commit = commits[0] + summary = commit['message'].splitlines()[0] + if len(summary) > 50: + summary = summary[:50] + '...' + description += 'pushed changeset <a href="%s">%s</a> (%s) ' % (commit['url'], commit['id'][:10], cgi.escape(summary)) + else: + description += 'pushed %s changesets ' % len(commits) + description += 'to <a href="%s">%s</a>' % (repo['url'], cgi.escape(repo_name)) + if message(ROOM_ID, 'Squawker', description): + return "OK" + return "FAIL" + +if __name__ == '__main__': + app.run(host='0.0.0.0')