zulip/tools/post-receive

76 lines
3.3 KiB
Python
Executable File

#!/usr/bin/env python
#
# Zulip's post-receive hook. There is a symlink
# from /home/git/repositories/eng/zulip.git/hooks/post-receive
# to ~zulip/zulip/tools/post-receive
# on git.zulip.net. So to deploy changes to this script, run
#
# ssh zulip@git.zulip.net 'cd zulip; git pull'
#
# To send the Zulip notices, this script calls out to our
# for-distribution git hook (under api/integrations/); since the git
# hook needs to be in the same directory as the configuration script
# for the git hook, this means that we need to have a shared directory
# containing (a symlink to) both the the for-distribution git hook and
# the Zulip configuration; for the moment those are
# bots/githook-post-receive and bots/zulip_git_config.py,
# respectively. We need the intermediate symlink because the git hook
# looks for its configuration in the directory that it sits in, and
# api/integrations/git/ has the example configuration.
#
#
# 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 sys
import subprocess
from zulip_tools import check_output, ENDC, FAIL
def update_deployment(server, refname):
return subprocess.call(["ssh", "-l", "zulip", server, "--", "env", "-u", "GIT_DIR",
"/home/zulip/deployments/current/tools/update-deployment", refname])
def send_deployment_finished_message(branch, message):
subprocess.check_call(["/home/zulip/zulip/api/bin/zulip-send", "--user=commit-bot@zulip.com",
"--api-key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "--stream=commits",
"--site=https://staging.zulip.com",
(u"--subject=deploy \u21D2 %s" % (branch,)).encode("utf-8"), "--message=%s" % (message,)])
deployments = {
'refs/heads/prod': ('prod0.zulip.net', 'prod'),
'refs/heads/master': ('staging.zulip.net', 'master'),
'refs/heads/test-post-receive': None,
}
for ln in sys.stdin:
oldrev, newrev, refname = ln.strip().split()
if refname in deployments:
p = subprocess.Popen("/home/zulip/zulip/bots/githook-post-receive",
stdin=subprocess.PIPE)
p.communicate(input=ln)
if deployments[refname]:
server, branch = deployments[refname]
ret = update_deployment(server, branch)
if ret == 0:
send_deployment_finished_message(branch, "deployment of `%s` finished successfully!" % (newrev[:12],))
else:
send_deployment_finished_message(branch, "deployment of `%s` failed!" % (newrev[:12],))
if newrev == '0000000000000000000000000000000000000000':
# 0000000000000000000000000000000000000000 means we're deleting the ref
commits = ''
else:
commits = check_output(["git", "log", "%s..%s" % (oldrev, newrev)])
if '[schema]' in commits:
print
print FAIL + "Schema change detected! Please make the appropriate changes manually." + ENDC
print