diff --git a/zulip/integrations/git/git_utility.py b/zulip/integrations/git/git_utility.py new file mode 100644 index 000000000..c5810cdf0 --- /dev/null +++ b/zulip/integrations/git/git_utility.py @@ -0,0 +1,20 @@ +from typing import List + +EMPTY_SHA = "0000000000000000000000000000000000000000" +COMMIT_ROW_TEMPLATE = '* {user_name} commited {commit_msg} ([{commit_short_sha}]({commit_url}))\n' + +def extract_commit_data(raw_commit: str) -> List[str]: + author_start = len("-[AUTHOR]=") + author_end = raw_commit.find("-[COMMIT]=") + author = raw_commit[author_start:author_end] + raw_commit = raw_commit[author_end:] + + commit_start = len("-[COMMIT]=") + commit_end = raw_commit.find("-[SUBJECT]=") + commit = raw_commit[commit_start:commit_end] + raw_commit = raw_commit[commit_end:] + + subject_start = len("-[SUBJECT]=") + subject = raw_commit[subject_start:] + + return [author, commit, subject] diff --git a/zulip/integrations/git/post-receive b/zulip/integrations/git/post-receive index 95e7633ba..56b267094 100755 --- a/zulip/integrations/git/post-receive +++ b/zulip/integrations/git/post-receive @@ -14,6 +14,7 @@ import os import sys import subprocess import os.path +import git_utility as git sys.path.insert(0, os.path.dirname(__file__)) import zulip_git_config as config @@ -37,15 +38,21 @@ def git_repository_name() -> Text: return os.path.basename(os.path.dirname(os.getcwd())) def git_commit_range(oldrev: str, newrev: str) -> str: + remote_repo_cmd = ["git", "config", "--get", "remote.origin.url"] + remote_repo_url = subprocess.check_output(remote_repo_cmd).strip().decode("utf-8") + log_cmd = ["git", "log", "--reverse", - "--pretty=%aE %H %s", "%s..%s" % (oldrev, newrev)] + "--pretty=-[AUTHOR]=%aN-[COMMIT]=%H-[SUBJECT]=%s", "%s..%s" % (oldrev, newrev)] commits = '' for ln in subprocess.check_output(log_cmd, universal_newlines=True).splitlines(): - 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: - commits += '!avatar(%s) %s\n' % (author_email, subject) + author, commit_sha, subject = git.extract_commit_data(ln) + commits += git.COMMIT_ROW_TEMPLATE.format( + user_name=author, + commit_short_sha=commit_sha[:7], + commit_url="{}/commit/{}".format(remote_repo_url, commit_sha), + commit_msg=subject, + ) + return commits def send_bot_message(oldrev: str, newrev: str, refname: str) -> None: @@ -59,10 +66,7 @@ def send_bot_message(oldrev: str, newrev: str, refname: str) -> None: new_head = newrev[:12] old_head = oldrev[:12] - if ( - oldrev == '0000000000000000000000000000000000000000' - or newrev == '0000000000000000000000000000000000000000' - ): + if (oldrev == git.EMPTY_SHA or newrev == git.EMPTY_SHA): # New branch pushed or old branch removed added = '' removed = '' @@ -70,9 +74,9 @@ def send_bot_message(oldrev: str, newrev: str, refname: str) -> None: added = git_commit_range(oldrev, newrev) removed = git_commit_range(newrev, oldrev) - if oldrev == '0000000000000000000000000000000000000000': + if oldrev == git.EMPTY_SHA: message = '`%s` was pushed to new branch `%s`' % (new_head, branch) - elif newrev == '0000000000000000000000000000000000000000': + elif newrev == git.EMPTY_SHA: message = 'branch `%s` was removed (was `%s`)' % (branch, old_head) elif removed: message = '`%s` was pushed to `%s`, **REMOVING**:\n\n%s' % (new_head, branch, removed) diff --git a/zulip/integrations/git/zulip_git_config.py b/zulip/integrations/git/zulip_git_config.py index 15a93834e..93ea2d9de 100644 --- a/zulip/integrations/git/zulip_git_config.py +++ b/zulip/integrations/git/zulip_git_config.py @@ -31,14 +31,6 @@ def commit_notice_destination(repo: Text, branch: Text, commit: Text) -> Optiona # Return None for cases where you don't want a notice sent return None -# Modify this function to change how commits are displayed; the most -# common customization is to include a link to the commit in your -# graphical repository viewer, e.g. -# -# return '!avatar(%s) [%s](https://example.com/commits/%s)\n' % (author, subject, commit_id) -def format_commit_message(author: Text, subject: Text, commit_id: Text) -> Text: - return '!avatar(%s) %s\n' % (author, subject) - ## If properly installed, the Zulip API should be in your import ## path, but if not, set a custom path below ZULIP_API_PATH = None