#!/usr/bin/python # # Humbug's post-receive hook. Deploy it by symlinking it to # /srv/git/humbug.git/hooks/post-receive # # The "post-receive" script is run after receive-pack has accepted a pack # and the repository has been updated. It is passed arguments in through # stdin in the form # # For example: # aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master # # see contrib/hooks/ for a sample import os import sys import subprocess import time sys.path.append(os.path.dirname(os.path.dirname(os.readlink(__file__)))) import api.common client = api.common.HumbugAPI(email="humbug+commits@humbughq.com", api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", verbose=False) def check_output(*popenargs, **kwargs): if 'stdout' in kwargs: raise ValueError('stdout argument not allowed, it will be overridden.') process = subprocess.Popen(stdout=subprocess.PIPE, *popenargs, **kwargs) output, unused_err = process.communicate() retcode = process.poll() if retcode: cmd = kwargs.get("args") if cmd is None: cmd = popenargs[0] raise subprocess.CalledProcessError(retcode, cmd, output=output) return output subprocess.check_output = check_output def update_deployment(oldrev, newrev, refname): subprocess.check_call(["ssh", "humbughq.com", "--", "env", "-u", "GIT_DIR", "/home/humbug/humbug/tools/update-deployment", oldrev, newrev, refname]) def git_commit_range(oldrev, newrev): log_cmd = ["git", "log", "--reverse", "--pretty=%aE %s", "%s..%s" % (oldrev, newrev)] commits = '' for ln in subprocess.check_output(log_cmd).splitlines(): email, subject = ln.split(None, 1) # This truncation approximately fits the normal message width in the browser. # It would be nicer to move this into the client via CSS and a Markdown extension. if len(subject) > 60: subject = subject[:58].rstrip() + '...' commits += '!gravatar(%s) %s\n' % (email, subject) return commits def send_bot_message(oldrev, newrev, refname): added = git_commit_range(oldrev, newrev) removed = git_commit_range(newrev, oldrev) new_head = newrev[:12] branch = refname.replace('refs/heads/', '') if removed: message = '`%s` was pushed to `%s`, **REMOVING**:\n\n%s' % (new_head, branch, removed) if added: message += '\n**and adding**:\n\n' + added message += '\n**A HISTORY REWRITE HAS OCCURRED!**' message += '\nPlease check your local branches to deal with this.' elif added: message = '`%s` was deployed to `%s` with:\n\n%s' % (new_head, branch, added) else: message = '`%s` was pushed to `%s`... but nothing changed?' % (new_head, branch) message_data = { "type": "stream", "stream": "test" if refname == "refs/heads/test-post-receive" else "devel", "subject": "push-" + new_head, "content": message, "recipient": "tabbott@humbughq.com", } # Sleep a bit to give time for the server to restart. # TODO: Make this work without the sleep time.sleep(1) client.send_message(message_data) for ln in sys.stdin: oldrev, newrev, refname = ln.strip().split() if refname in ["refs/heads/master", "refs/heads/test-post-receive"]: send_bot_message(oldrev, newrev, refname) update_deployment(oldrev, newrev, refname)