Skip to content

Commit

Permalink
integrations: Remove avatar from git commits showing author name inst…
Browse files Browse the repository at this point in the history
…ead.

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: zulip#632
  • Loading branch information
ahmedabuamra committed Feb 5, 2021
1 parent 641623e commit d40a41a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
20 changes: 20 additions & 0 deletions zulip/integrations/git/git_utility.py
Original file line number Diff line number Diff line change
@@ -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]
28 changes: 16 additions & 12 deletions zulip/integrations/git/post-receive
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -59,20 +66,17 @@ 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 = ''
else:
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)
Expand Down
8 changes: 0 additions & 8 deletions zulip/integrations/git/zulip_git_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d40a41a

Please sign in to comment.