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-12-03 18:23:06 +01:00
|
|
|
from os import path
|
2012-08-31 19:46:12 +02:00
|
|
|
|
2012-12-03 18:23:06 +01:00
|
|
|
sys.path.append(path.join(path.dirname(os.readlink(__file__)), '../api'))
|
|
|
|
import humbug
|
2012-12-03 18:24:49 +01:00
|
|
|
client = humbug.Client(
|
2012-12-03 18:23:06 +01:00
|
|
|
email="humbug+commits@humbughq.com",
|
|
|
|
site="https://staging.humbughq.com",
|
|
|
|
api_key="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
|
|
|
|
verbose=False)
|
2012-10-03 20:48:51 +02:00
|
|
|
|
|
|
|
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-11-14 23:21:46 +01:00
|
|
|
"to": "test" if refname == "refs/heads/test-post-receive" else "devel",
|
2012-11-09 20:54:49 +01:00
|
|
|
"subject": u"deploy \u21D2 " + branch,
|
2012-10-17 05:39:43 +02:00
|
|
|
"content": message,
|
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")
|