2012-08-31 19:46:12 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
2012-10-30 21:15:57 +01:00
|
|
|
# Humbug's post-receive hook. There is a symlink
|
|
|
|
# from /srv/git/humbug.git/hooks/post-receive
|
|
|
|
# to ~humbug/humbug/tools/post-receive
|
|
|
|
# on git.humbughq.com. So to deploy changes to this script, run
|
|
|
|
#
|
|
|
|
# ssh humbug@git.humbughq.com 'cd humbug; git pull'
|
2012-08-31 19:46:12 +02:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# <oldrev> <newrev> <refname>
|
|
|
|
# For example:
|
|
|
|
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
|
|
|
|
#
|
|
|
|
# see contrib/hooks/ for a sample
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
2012-10-03 23:01:15 +02:00
|
|
|
import time
|
2012-08-31 19:46:12 +02:00
|
|
|
|
2012-10-03 20:31:59 +02:00
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.readlink(__file__))))
|
|
|
|
import api.common
|
|
|
|
client = api.common.HumbugAPI(email="humbug+commits@humbughq.com",
|
2012-10-03 20:48:51 +02:00
|
|
|
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
|
2012-10-03 20:31:59 +02:00
|
|
|
|
2012-11-06 17:09:07 +01:00
|
|
|
def update_deployment(server, oldrev, newrev, refname):
|
|
|
|
subprocess.check_call(["ssh", server, "--", "env", "-u", "GIT_DIR",
|
2012-09-17 17:47:20 +02:00
|
|
|
"/home/humbug/humbug/tools/update-deployment",
|
2012-08-31 20:16:50 +02:00
|
|
|
oldrev, newrev, refname])
|
2012-10-17 03:02:21 +02:00
|
|
|
|
2012-10-17 05:39:43 +02:00
|
|
|
def git_commit_range(oldrev, newrev):
|
|
|
|
log_cmd = ["git", "log", "--reverse",
|
2012-10-17 05:57:57 +02:00
|
|
|
"--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
|
2012-10-17 05:39:43 +02:00
|
|
|
|
2012-10-17 03:02:21 +02:00
|
|
|
def send_bot_message(oldrev, newrev, refname):
|
2012-10-17 05:39:43 +02:00
|
|
|
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)
|
2012-10-17 03:42:58 +02:00
|
|
|
|
2012-10-03 20:31:59 +02:00
|
|
|
message_data = {
|
2012-10-10 22:57:21 +02:00
|
|
|
"type": "stream",
|
2012-10-10 23:09:16 +02:00
|
|
|
"stream": "test" if refname == "refs/heads/test-post-receive" else "devel",
|
2012-10-17 06:02:27 +02:00
|
|
|
"subject": "push-" + new_head,
|
2012-10-17 05:39:43 +02:00
|
|
|
"content": message,
|
2012-10-03 20:31:59 +02:00
|
|
|
"recipient": "tabbott@humbughq.com",
|
|
|
|
}
|
2012-10-03 23:01:15 +02:00
|
|
|
# Sleep a bit to give time for the server to restart.
|
|
|
|
# TODO: Make this work without the sleep
|
|
|
|
time.sleep(1)
|
2012-10-03 20:31:59 +02:00
|
|
|
client.send_message(message_data)
|
2012-10-03 21:18:16 +02:00
|
|
|
|
|
|
|
for ln in sys.stdin:
|
|
|
|
oldrev, newrev, refname = ln.strip().split()
|
2012-11-06 17:09:07 +01:00
|
|
|
if refname in ["refs/heads/prod"]:
|
2012-10-17 03:02:21 +02:00
|
|
|
send_bot_message(oldrev, newrev, refname)
|
2012-11-06 17:09:07 +01:00
|
|
|
update_deployment("humbughq.com", oldrev, newrev, "origin/prod")
|
|
|
|
elif refname in ["refs/heads/master", "refs/heads/test-post-receive"]:
|
|
|
|
send_bot_message(oldrev, newrev, refname)
|
|
|
|
update_deployment("staging.humbughq.com", oldrev, newrev, "origin/master")
|