2020-04-02 09:59:28 -04:00
|
|
|
#!/usr/bin/env python3
|
2020-03-23 00:55:21 -04:00
|
|
|
|
2013-08-06 15:32:15 -04:00
|
|
|
# Zulip notification post-receive hook.
|
2013-02-15 17:01:55 -05: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:
|
2021-08-24 15:50:52 -04:00
|
|
|
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/main
|
2013-02-15 17:01:55 -05:00
|
|
|
|
|
|
|
import os
|
|
|
|
import os.path
|
2021-05-28 05:00:04 -04:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2013-02-15 17:01:55 -05:00
|
|
|
|
|
|
|
sys.path.insert(0, os.path.dirname(__file__))
|
2016-09-15 01:36:48 -04:00
|
|
|
import zulip_git_config as config
|
2021-05-28 05:00:04 -04:00
|
|
|
|
2013-12-05 17:42:33 -05:00
|
|
|
VERSION = "0.9"
|
2013-02-15 17:01:55 -05:00
|
|
|
|
2013-08-07 12:26:11 -04:00
|
|
|
if config.ZULIP_API_PATH is not None:
|
|
|
|
sys.path.append(config.ZULIP_API_PATH)
|
2013-02-15 17:01:55 -05:00
|
|
|
|
2013-08-07 11:51:03 -04:00
|
|
|
import zulip
|
2021-05-28 05:00:04 -04:00
|
|
|
|
2013-08-07 11:51:03 -04:00
|
|
|
client = zulip.Client(
|
2013-08-07 12:26:11 -04:00
|
|
|
email=config.ZULIP_USER,
|
|
|
|
site=config.ZULIP_SITE,
|
2013-12-05 17:42:33 -05:00
|
|
|
api_key=config.ZULIP_API_KEY,
|
2021-05-28 05:03:46 -04:00
|
|
|
client="ZulipGit/" + VERSION,
|
|
|
|
)
|
|
|
|
|
2013-02-15 17:01:55 -05:00
|
|
|
|
2021-05-28 07:13:13 -04:00
|
|
|
def git_repository_name() -> str:
|
2013-02-15 17:01:55 -05:00
|
|
|
output = subprocess.check_output(["git", "rev-parse", "--is-bare-repository"])
|
|
|
|
if output.strip() == "true":
|
2021-05-28 05:03:46 -04:00
|
|
|
return os.path.basename(os.getcwd())[: -len(".git")]
|
2013-02-15 17:01:55 -05:00
|
|
|
else:
|
|
|
|
return os.path.basename(os.path.dirname(os.getcwd()))
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def git_commit_range(oldrev: str, newrev: str) -> str:
|
2021-05-28 07:19:40 -04:00
|
|
|
log_cmd = ["git", "log", "--reverse", "--pretty=%aE %H %s", f"{oldrev}..{newrev}"]
|
2021-05-28 05:05:11 -04:00
|
|
|
commits = ""
|
2019-11-06 09:34:21 -05:00
|
|
|
for ln in subprocess.check_output(log_cmd, universal_newlines=True).splitlines():
|
2021-05-28 05:03:46 -04:00
|
|
|
author_email, commit_id, subject = ln.split(None, 2)
|
2013-10-08 14:45:05 -04:00
|
|
|
if hasattr(config, "format_commit_message"):
|
|
|
|
commits += config.format_commit_message(author_email, subject, commit_id)
|
|
|
|
else:
|
2021-05-28 07:19:40 -04:00
|
|
|
commits += f"!avatar({author_email}) {subject}\n"
|
2013-02-15 17:01:55 -05:00
|
|
|
return commits
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2020-04-18 18:59:12 -04:00
|
|
|
def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
|
2021-05-28 05:03:46 -04:00
|
|
|
repo_name = git_repository_name()
|
2021-05-28 05:05:11 -04:00
|
|
|
branch = refname.replace("refs/heads/", "")
|
2013-02-15 17:01:55 -05:00
|
|
|
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 13:17:48 -05:00
|
|
|
old_head = oldrev[:12]
|
|
|
|
|
2020-04-18 20:33:01 -04:00
|
|
|
if (
|
2021-05-28 05:05:11 -04:00
|
|
|
oldrev == "0000000000000000000000000000000000000000"
|
|
|
|
or newrev == "0000000000000000000000000000000000000000"
|
2020-04-18 20:33:01 -04:00
|
|
|
):
|
2013-02-19 13:17:48 -05:00
|
|
|
# New branch pushed or old branch removed
|
2021-05-28 05:05:11 -04:00
|
|
|
added = ""
|
|
|
|
removed = ""
|
2013-02-19 13:17:48 -05:00
|
|
|
else:
|
2021-05-28 05:03:46 -04:00
|
|
|
added = git_commit_range(oldrev, newrev)
|
2013-02-19 13:17:48 -05:00
|
|
|
removed = git_commit_range(newrev, oldrev)
|
2013-02-15 17:01:55 -05:00
|
|
|
|
2021-05-28 05:05:11 -04:00
|
|
|
if oldrev == "0000000000000000000000000000000000000000":
|
2021-05-28 07:19:40 -04:00
|
|
|
message = f"`{new_head}` was pushed to new branch `{branch}`"
|
2021-05-28 05:05:11 -04:00
|
|
|
elif newrev == "0000000000000000000000000000000000000000":
|
2021-05-28 07:19:40 -04:00
|
|
|
message = f"branch `{branch}` was removed (was `{old_head}`)"
|
2013-02-19 13:17:48 -05:00
|
|
|
elif removed:
|
2021-05-28 07:19:40 -04:00
|
|
|
message = f"`{new_head}` was pushed to `{branch}`, **REMOVING**:\n\n{removed}"
|
2013-02-15 17:01:55 -05:00
|
|
|
if added:
|
2021-05-28 05:05:11 -04:00
|
|
|
message += "\n**and adding**:\n\n" + added
|
|
|
|
message += "\n**A HISTORY REWRITE HAS OCCURRED!**"
|
|
|
|
message += "\n@everyone: Please check your local branches to deal with this."
|
2013-02-15 17:01:55 -05:00
|
|
|
elif added:
|
2021-05-28 07:19:40 -04:00
|
|
|
message = f"`{new_head}` was deployed to `{branch}` with:\n\n{added}"
|
2013-02-15 17:01:55 -05:00
|
|
|
else:
|
2021-05-28 07:19:40 -04:00
|
|
|
message = f"`{new_head}` was pushed to `{branch}`... but nothing changed?"
|
2013-02-15 17:01:55 -05:00
|
|
|
|
|
|
|
message_data = {
|
|
|
|
"type": "stream",
|
|
|
|
"to": destination["stream"],
|
|
|
|
"subject": destination["subject"],
|
|
|
|
"content": message,
|
|
|
|
}
|
|
|
|
client.send_message(message_data)
|
|
|
|
|
2021-05-28 05:03:46 -04:00
|
|
|
|
2013-02-15 17:01:55 -05:00
|
|
|
for ln in sys.stdin:
|
|
|
|
oldrev, newrev, refname = ln.strip().split()
|
|
|
|
send_bot_message(oldrev, newrev, refname)
|