Skip to content

Commit 667c4ac

Browse files
committed
integrations: Remove avatar from git commits showing author name instead.
Getting the logs run in cmd as in function `git_commit_range(oldrev, newrev)` so I had to prefix author name with -[AUTHOR], commit with -[COMMIT] and subject/title with -[SUBJECT] to be able to extract these info correctly. I also tried to stick with the format proposed in this issue zulip/zulip#3968 except for showing the author's name before each commit and without summary. Fixes: #632
1 parent 641623e commit 667c4ac

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

zulip/integrations/git/post-receive

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import os.path
1717

1818
sys.path.insert(0, os.path.dirname(__file__))
1919
import zulip_git_config as config
20+
2021
VERSION = "0.9"
22+
EMPTY_SHA = "0000000000000000000000000000000000000000"
23+
COMMIT_ROW_TEMPLATE = '* {author_name} commited {subject} ([{commit_short_hash}]({commit_url}))\n'
2124

2225
if config.ZULIP_API_PATH is not None:
2326
sys.path.append(config.ZULIP_API_PATH)
@@ -37,15 +40,26 @@ def git_repository_name() -> Text:
3740
return os.path.basename(os.path.dirname(os.getcwd()))
3841

3942
def git_commit_range(oldrev: str, newrev: str) -> str:
43+
remote_repo_cmd = ["git", "config", "--get", "remote.origin.url"]
44+
remote_repo_url = subprocess.check_output(remote_repo_cmd).strip().decode("utf-8")
45+
4046
log_cmd = ["git", "log", "--reverse",
41-
"--pretty=%aE %H %s", "%s..%s" % (oldrev, newrev)]
47+
"--pretty=%aN%n%H%n%h%n%s", "%s..%s" % (oldrev, newrev)]
4248
commits = ''
43-
for ln in subprocess.check_output(log_cmd, universal_newlines=True).splitlines():
44-
author_email, commit_id, subject = ln.split(None, 2)
45-
if hasattr(config, "format_commit_message"):
46-
commits += config.format_commit_message(author_email, subject, commit_id)
47-
else:
48-
commits += '!avatar(%s) %s\n' % (author_email, subject)
49+
output = subprocess.check_output(log_cmd, universal_newlines=True).splitlines()
50+
it = iter(output)
51+
for _ in range(len(output)//4):
52+
author_name = next(iter(it))
53+
commit_hash = next(iter(it))
54+
commit_short_hash = next(iter(it))
55+
subject = next(iter(it))
56+
commits += COMMIT_ROW_TEMPLATE.format(
57+
author_name=author_name,
58+
commit_short_hash=commit_short_hash,
59+
subject=subject,
60+
commit_url="{}/commit/{}".format(remote_repo_url, commit_hash)
61+
)
62+
4963
return commits
5064

5165
def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
@@ -59,20 +73,17 @@ def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
5973
new_head = newrev[:12]
6074
old_head = oldrev[:12]
6175

62-
if (
63-
oldrev == '0000000000000000000000000000000000000000'
64-
or newrev == '0000000000000000000000000000000000000000'
65-
):
76+
if oldrev == EMPTY_SHA or newrev == EMPTY_SHA:
6677
# New branch pushed or old branch removed
6778
added = ''
6879
removed = ''
6980
else:
7081
added = git_commit_range(oldrev, newrev)
7182
removed = git_commit_range(newrev, oldrev)
7283

73-
if oldrev == '0000000000000000000000000000000000000000':
84+
if oldrev == EMPTY_SHA:
7485
message = '`%s` was pushed to new branch `%s`' % (new_head, branch)
75-
elif newrev == '0000000000000000000000000000000000000000':
86+
elif newrev == EMPTY_SHA:
7687
message = 'branch `%s` was removed (was `%s`)' % (branch, old_head)
7788
elif removed:
7889
message = '`%s` was pushed to `%s`, **REMOVING**:\n\n%s' % (new_head, branch, removed)

zulip/integrations/git/zulip_git_config.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,6 @@ def commit_notice_destination(repo: Text, branch: Text, commit: Text) -> Optiona
3131
# Return None for cases where you don't want a notice sent
3232
return None
3333

34-
# Modify this function to change how commits are displayed; the most
35-
# common customization is to include a link to the commit in your
36-
# graphical repository viewer, e.g.
37-
#
38-
# return '!avatar(%s) [%s](https://example.com/commits/%s)\n' % (author, subject, commit_id)
39-
def format_commit_message(author: Text, subject: Text, commit_id: Text) -> Text:
40-
return '!avatar(%s) %s\n' % (author, subject)
41-
4234
## If properly installed, the Zulip API should be in your import
4335
## path, but if not, set a custom path below
4436
ZULIP_API_PATH = None

0 commit comments

Comments
 (0)