Changeset 3279396c8cd6…
by Benjamin Pollack
Changes to 2 files · Browse files at 3279396c8cd6 Diff from another changeset...
|
|
|
@@ -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')
|
Loading...