2016-04-07 15:03:22 +02:00
|
|
|
#!/usr/bin/env python
|
2013-02-15 23:01:55 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2013-08-06 21:32:15 +02:00
|
|
|
# Zulip notification post-receive hook.
|
2014-02-04 20:15:02 +01:00
|
|
|
# Copyright © 2012-2014 Zulip, Inc.
|
2013-02-15 23:01:55 +01:00
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
|
2016-04-03 16:02:27 +02:00
|
|
|
from __future__ import absolute_import
|
2016-12-03 22:58:22 +01:00
|
|
|
from typing import Text
|
2013-02-15 23:01:55 +01:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
sys.path.insert(0, os.path.dirname(__file__))
|
2016-09-15 07:36:48 +02:00
|
|
|
import zulip_git_config as config
|
2013-12-05 23:42:33 +01:00
|
|
|
VERSION = "0.9"
|
2013-02-15 23:01:55 +01:00
|
|
|
|
2013-08-07 18:26:11 +02:00
|
|
|
if config.ZULIP_API_PATH is not None:
|
|
|
|
sys.path.append(config.ZULIP_API_PATH)
|
2013-02-15 23:01:55 +01:00
|
|
|
|
2013-08-07 17:51:03 +02:00
|
|
|
import zulip
|
|
|
|
client = zulip.Client(
|
2013-08-07 18:26:11 +02:00
|
|
|
email=config.ZULIP_USER,
|
|
|
|
site=config.ZULIP_SITE,
|
2013-12-05 23:42:33 +01:00
|
|
|
api_key=config.ZULIP_API_KEY,
|
2013-12-06 23:50:55 +01:00
|
|
|
client="ZulipGit/" + VERSION)
|
2013-02-15 23:01:55 +01:00
|
|
|
|
|
|
|
def git_repository_name():
|
2016-12-03 22:58:22 +01:00
|
|
|
# type: () -> Text
|
2013-02-15 23:01:55 +01:00
|
|
|
output = subprocess.check_output(["git", "rev-parse", "--is-bare-repository"])
|
|
|
|
if output.strip() == "true":
|
|
|
|
return os.path.basename(os.getcwd())[:-len(".git")]
|
|
|
|
else:
|
|
|
|
return os.path.basename(os.path.dirname(os.getcwd()))
|
|
|
|
|
|
|
|
def git_commit_range(oldrev, newrev):
|
2016-12-03 22:58:22 +01:00
|
|
|
# type: (str, str) -> str
|
2013-02-15 23:01:55 +01:00
|
|
|
log_cmd = ["git", "log", "--reverse",
|
2013-10-08 20:45:05 +02:00
|
|
|
"--pretty=%aE %H %s", "%s..%s" % (oldrev, newrev)]
|
2013-02-15 23:01:55 +01:00
|
|
|
commits = ''
|
|
|
|
for ln in subprocess.check_output(log_cmd).splitlines():
|
2013-10-08 20:45:05 +02:00
|
|
|
author_email, commit_id, subject = ln.split(None, 2)
|
|
|
|
if hasattr(config, "format_commit_message"):
|
|
|
|
commits += config.format_commit_message(author_email, subject, commit_id)
|
|
|
|
else:
|
2013-11-20 19:10:43 +01:00
|
|
|
commits += '!avatar(%s) %s\n' % (author_email, subject)
|
2013-02-15 23:01:55 +01:00
|
|
|
return commits
|
|
|
|
|
|
|
|
def send_bot_message(oldrev, newrev, refname):
|
2016-12-03 22:58:22 +01:00
|
|
|
# type: (str, str, str) -> None
|
2013-02-15 23:01:55 +01:00
|
|
|
repo_name = git_repository_name()
|
|
|
|
branch = refname.replace('refs/heads/', '')
|
|
|
|
destination = config.commit_notice_destination(repo_name, branch, newrev)
|
|
|
|
if destination is None:
|
|
|
|
# Don't forward the notice anywhere
|
|
|
|
return
|
|
|
|
|
|
|
|
new_head = newrev[:12]
|
2013-02-19 19:17:48 +01:00
|
|
|
old_head = oldrev[:12]
|
|
|
|
|
|
|
|
if (oldrev == '0000000000000000000000000000000000000000' or
|
2016-12-03 18:19:09 +01:00
|
|
|
newrev == '0000000000000000000000000000000000000000'):
|
2013-02-19 19:17:48 +01:00
|
|
|
# New branch pushed or old branch removed
|
|
|
|
added = ''
|
|
|
|
removed = ''
|
|
|
|
else:
|
|
|
|
added = git_commit_range(oldrev, newrev)
|
|
|
|
removed = git_commit_range(newrev, oldrev)
|
2013-02-15 23:01:55 +01:00
|
|
|
|
2013-02-19 19:17:48 +01:00
|
|
|
if oldrev == '0000000000000000000000000000000000000000':
|
|
|
|
message = '`%s` was pushed to new branch `%s`' % (new_head, branch)
|
|
|
|
elif newrev == '0000000000000000000000000000000000000000':
|
|
|
|
message = 'branch `%s` was removed (was `%s`)' % (branch, old_head)
|
|
|
|
elif removed:
|
2013-02-15 23:01:55 +01:00
|
|
|
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!**'
|
2013-02-19 23:39:55 +01:00
|
|
|
message += '\n@everyone: Please check your local branches to deal with this.'
|
2013-02-15 23:01:55 +01:00
|
|
|
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",
|
|
|
|
"to": destination["stream"],
|
|
|
|
"subject": destination["subject"],
|
|
|
|
"content": message,
|
|
|
|
}
|
|
|
|
client.send_message(message_data)
|
|
|
|
|
|
|
|
for ln in sys.stdin:
|
|
|
|
oldrev, newrev, refname = ln.strip().split()
|
|
|
|
send_bot_message(oldrev, newrev, refname)
|