Last active
August 29, 2015 14:04
-
-
Save ecarreras/2683edc2ca7902803710 to your computer and use it in GitHub Desktop.
Gitbucket - HipChat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding:utf-8 -*- | |
"""Credits to: https://gist.github.com/FiloSottile/7634541 | |
""" | |
import os | |
import BaseHTTPServer | |
import sys | |
import time | |
import urlparse | |
import json | |
from hipster import Hipster | |
HOST_NAME = sys.argv[1] | |
PORT_NUMBER = int(sys.argv[2]) | |
IPS_OK = None | |
HIPCHAT_TOKEN = os.getenv('HIPCHAT_TOKEN', False) | |
HIPCHAT_ROOM = os.getenv('HIPCHAT_ROOM', '') | |
def handle_hook(payload): | |
hipchat = Hipster(HIPCHAT_TOKEN) | |
values = { | |
'user': payload['pusher']['name'], | |
'branch': payload['ref'].split('/')[-1] or 'unknown', | |
'repo': payload['repository']['name'], | |
'owner': payload['repository']['owner']['name'] | |
} | |
message = ('<a href="http://git.gisce.lan/%(user)s">%(user)s</a> pushed to ' | |
'<a href="http://git.gisce.lan/%(owner)s/%(repo)s/tree/%(branch)s">' | |
'%(branch)s</a> of <a href="http://git.gisce.lan/%(owner)s/%(repo)s">' | |
'%(repo)s</a>') % values | |
for commit in payload['commits']: | |
value = { | |
'username': commit['author']['name'].decode('utf-8'), | |
'user': commit['author']['email'].split('@')[0], | |
'message': commit['message'], | |
'hash': commit['id'][0:7], | |
'url': commit['url'] | |
} | |
message += '<br/>' | |
message += (' - %(message)s by <a href="http://git.gisce.lan/%(user)s">' | |
'%(username)s</a> (<a href=%(url)s>%(hash)s</a>)') % value | |
hipchat.send_messages({ | |
'sender': 'GitBucket', | |
'room_id': HIPCHAT_ROOM, | |
'message': message, | |
}) | |
class HookHandler(BaseHTTPServer.BaseHTTPRequestHandler): | |
server_version = "HookHandler/0.1" | |
def do_GET(s): | |
s.send_response(200) | |
s.wfile.write('Hello!') | |
def do_POST(s): | |
print s.headers | |
# Check that the IP is within the GH ranges | |
if IPS_OK and not any(s.client_address[0].startswith(IP) | |
for IP in IPS_OK): | |
s.send_error(403) | |
length = int(s.headers['Content-Length']) | |
post_data = urlparse.parse_qs(s.rfile.read(length)) | |
payload = json.loads(post_data['payload'][0]) | |
handle_hook(payload) | |
s.send_response(200) | |
if __name__ == '__main__': | |
server_class = BaseHTTPServer.HTTPServer | |
httpd = server_class((HOST_NAME, PORT_NUMBER), HookHandler) | |
print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER) | |
try: | |
httpd.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
httpd.server_close() | |
print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
actual_encoding = sys.getdefaultencoding() | |
sys.stdout.write("Actual encoding is %s\n" % actual_encoding) | |
try: | |
import locale | |
loc = locale.getdefaultlocale() | |
if loc[1]: | |
encoding = loc[1] | |
sys.stdout.write('encoding from "locale": %s\n' % (encoding,)) | |
sys.setdefaultencoding(encoding) | |
except ImportError: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment