Skip to content

Commit d40a41a

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 d40a41a

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

zulip/integrations/git/git_utility.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
EMPTY_SHA = "0000000000000000000000000000000000000000"
4+
COMMIT_ROW_TEMPLATE = '* {user_name} commited {commit_msg} ([{commit_short_sha}]({commit_url}))\n'
5+
6+
def extract_commit_data(raw_commit: str) -> List[str]:
7+
author_start = len("-[AUTHOR]=")
8+
author_end = raw_commit.find("-[COMMIT]=")
9+
author = raw_commit[author_start:author_end]
10+
raw_commit = raw_commit[author_end:]
11+
12+
commit_start = len("-[COMMIT]=")
13+
commit_end = raw_commit.find("-[SUBJECT]=")
14+
commit = raw_commit[commit_start:commit_end]
15+
raw_commit = raw_commit[commit_end:]
16+
17+
subject_start = len("-[SUBJECT]=")
18+
subject = raw_commit[subject_start:]
19+
20+
return [author, commit, subject]

zulip/integrations/git/post-receive

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import os
1414
import sys
1515
import subprocess
1616
import os.path
17+
import git_utility as git
1718

1819
sys.path.insert(0, os.path.dirname(__file__))
1920
import zulip_git_config as config
@@ -37,15 +38,21 @@ def git_repository_name() -> Text:
3738
return os.path.basename(os.path.dirname(os.getcwd()))
3839

3940
def git_commit_range(oldrev: str, newrev: str) -> str:
41+
remote_repo_cmd = ["git", "config", "--get", "remote.origin.url"]
42+
remote_repo_url = subprocess.check_output(remote_repo_cmd).strip().decode("utf-8")
43+
4044
log_cmd = ["git", "log", "--reverse",
41-
"--pretty=%aE %H %s", "%s..%s" % (oldrev, newrev)]
45+
"--pretty=-[AUTHOR]=%aN-[COMMIT]=%H-[SUBJECT]=%s", "%s..%s" % (oldrev, newrev)]
4246
commits = ''
4347
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)
48+
author, commit_sha, subject = git.extract_commit_data(ln)
49+
commits += git.COMMIT_ROW_TEMPLATE.format(
50+
user_name=author,
51+
commit_short_sha=commit_sha[:7],
52+
commit_url="{}/commit/{}".format(remote_repo_url, commit_sha),
53+
commit_msg=subject,
54+
)
55+
4956
return commits
5057

5158
def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
@@ -59,20 +66,17 @@ def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
5966
new_head = newrev[:12]
6067
old_head = oldrev[:12]
6168

62-
if (
63-
oldrev == '0000000000000000000000000000000000000000'
64-
or newrev == '0000000000000000000000000000000000000000'
65-
):
69+
if (oldrev == git.EMPTY_SHA or newrev == git.EMPTY_SHA):
6670
# New branch pushed or old branch removed
6771
added = ''
6872
removed = ''
6973
else:
7074
added = git_commit_range(oldrev, newrev)
7175
removed = git_commit_range(newrev, oldrev)
7276

73-
if oldrev == '0000000000000000000000000000000000000000':
77+
if oldrev == git.EMPTY_SHA:
7478
message = '`%s` was pushed to new branch `%s`' % (new_head, branch)
75-
elif newrev == '0000000000000000000000000000000000000000':
79+
elif newrev == git.EMPTY_SHA:
7680
message = 'branch `%s` was removed (was `%s`)' % (branch, old_head)
7781
elif removed:
7882
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)