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 667c4ac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
37 changes: 24 additions & 13 deletions zulip/integrations/git/post-receive
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import os.path

sys.path.insert(0, os.path.dirname(__file__))
import zulip_git_config as config

VERSION = "0.9"
EMPTY_SHA = "0000000000000000000000000000000000000000"
COMMIT_ROW_TEMPLATE = '* {author_name} commited {subject} ([{commit_short_hash}]({commit_url}))\n'

if config.ZULIP_API_PATH is not None:
sys.path.append(config.ZULIP_API_PATH)
Expand All @@ -37,15 +40,26 @@ 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=%aN%n%H%n%h%n%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)
output = subprocess.check_output(log_cmd, universal_newlines=True).splitlines()
it = iter(output)
for _ in range(len(output)//4):
author_name = next(iter(it))
commit_hash = next(iter(it))
commit_short_hash = next(iter(it))
subject = next(iter(it))
commits += COMMIT_ROW_TEMPLATE.format(
author_name=author_name,
commit_short_hash=commit_short_hash,
subject=subject,
commit_url="{}/commit/{}".format(remote_repo_url, commit_hash)
)

return commits

def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
Expand All @@ -59,20 +73,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 == EMPTY_SHA or newrev == 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 == EMPTY_SHA:
message = '`%s` was pushed to new branch `%s`' % (new_head, branch)
elif newrev == '0000000000000000000000000000000000000000':
elif newrev == 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 667c4ac

Please sign in to comment.