Skip to content

Commit 3625d3d

Browse files
committed
integrations: Change Git commit url format.
1 parent 75bea9f commit 3625d3d

File tree

4 files changed

+691
-954
lines changed

4 files changed

+691
-954
lines changed

zulip/integrations/git/post-receive

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,53 +7,64 @@
77
# stdin in the form
88
# <oldrev> <newrev> <refname>
99
# For example:
10-
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/main
10+
# aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
1111

12+
from typing import Text
1213
import os
13-
import os.path
14-
import subprocess
1514
import sys
15+
import subprocess
16+
import os.path
1617

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

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)
2427

2528
import zulip
26-
2729
client = zulip.Client(
2830
email=config.ZULIP_USER,
2931
site=config.ZULIP_SITE,
3032
api_key=config.ZULIP_API_KEY,
31-
client="ZulipGit/" + VERSION,
32-
)
33-
34-
35-
def git_repository_name() -> str:
36-
path, name = os.path.split(os.getcwd())
37-
if name == ".git":
38-
name = os.path.basename(path)
39-
return name[: -len(".git")] if name.endswith(".git") else name
33+
client="ZulipGit/" + VERSION)
4034

35+
def git_repository_name() -> Text:
36+
output = subprocess.check_output(["git", "rev-parse", "--is-bare-repository"])
37+
if output.strip() == "true":
38+
return os.path.basename(os.getcwd())[:-len(".git")]
39+
else:
40+
return os.path.basename(os.path.dirname(os.getcwd()))
4141

4242
def git_commit_range(oldrev: str, newrev: str) -> str:
43-
log_cmd = ["git", "log", "--reverse", "--pretty=%aE %H %s", f"{oldrev}..{newrev}"]
44-
commits = ""
45-
for ln in subprocess.check_output(log_cmd, universal_newlines=True).splitlines():
46-
author_email, commit_id, subject = ln.split(None, 2)
47-
if hasattr(config, "format_commit_message"):
48-
commits += config.format_commit_message(author_email, subject, commit_id)
49-
else:
50-
commits += f"!avatar({author_email}) {subject}\n"
51-
return commits
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+
46+
log_cmd = ["git", "log", "--reverse",
47+
"--pretty=%aN%n%H%n%h%n%s", "%s..%s" % (oldrev, newrev)]
48+
commits = ''
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+
)
5262

63+
return commits
5364

5465
def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
55-
repo_name = git_repository_name()
56-
branch = refname.replace("refs/heads/", "")
66+
repo_name = git_repository_name()
67+
branch = refname.replace('refs/heads/', '')
5768
destination = config.commit_notice_destination(repo_name, branch, newrev)
5869
if destination is None:
5970
# Don't forward the notice anywhere
@@ -62,31 +73,28 @@ def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
6273
new_head = newrev[:12]
6374
old_head = oldrev[:12]
6475

65-
if (
66-
oldrev == "0000000000000000000000000000000000000000"
67-
or newrev == "0000000000000000000000000000000000000000"
68-
):
76+
if oldrev == EMPTY_SHA or newrev == EMPTY_SHA:
6977
# New branch pushed or old branch removed
70-
added = ""
71-
removed = ""
78+
added = ''
79+
removed = ''
7280
else:
73-
added = git_commit_range(oldrev, newrev)
81+
added = git_commit_range(oldrev, newrev)
7482
removed = git_commit_range(newrev, oldrev)
7583

76-
if oldrev == "0000000000000000000000000000000000000000":
77-
message = f"`{new_head}` was pushed to new branch `{branch}`"
78-
elif newrev == "0000000000000000000000000000000000000000":
79-
message = f"branch `{branch}` was removed (was `{old_head}`)"
84+
if oldrev == EMPTY_SHA:
85+
message = '`%s` was pushed to new branch `%s`' % (new_head, branch)
86+
elif newrev == EMPTY_SHA:
87+
message = 'branch `%s` was removed (was `%s`)' % (branch, old_head)
8088
elif removed:
81-
message = f"`{new_head}` was pushed to `{branch}`, **REMOVING**:\n\n{removed}"
89+
message = '`%s` was pushed to `%s`, **REMOVING**:\n\n%s' % (new_head, branch, removed)
8290
if added:
83-
message += "\n**and adding**:\n\n" + added
84-
message += "\n**A HISTORY REWRITE HAS OCCURRED!**"
85-
message += "\n@everyone: Please check your local branches to deal with this."
91+
message += '\n**and adding**:\n\n' + added
92+
message += '\n**A HISTORY REWRITE HAS OCCURRED!**'
93+
message += '\n@everyone: Please check your local branches to deal with this.'
8694
elif added:
87-
message = f"`{new_head}` was deployed to `{branch}` with:\n\n{added}"
95+
message = '`%s` was deployed to `%s` with:\n\n%s' % (new_head, branch, added)
8896
else:
89-
message = f"`{new_head}` was pushed to `{branch}`... but nothing changed?"
97+
message = '`%s` was pushed to `%s`... but nothing changed?' % (new_head, branch)
9098

9199
message_data = {
92100
"type": "stream",
@@ -96,7 +104,6 @@ def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
96104
}
97105
client.send_message(message_data)
98106

99-
100107
for ln in sys.stdin:
101108
oldrev, newrev, refname = ln.strip().split()
102-
send_bot_message(oldrev, newrev, refname)
109+
send_bot_message(oldrev, newrev, refname)
Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#
2-
from typing import Dict, Optional
2+
from typing import Dict, Text, Optional
33

44
# Name of the stream to send notifications to, default is "commits"
5-
STREAM_NAME = "commits"
5+
STREAM_NAME = 'commits'
66

77
# Change these values to configure authentication for the plugin
88
ZULIP_USER = "[email protected]"
99
ZULIP_API_KEY = "0123456789abcdef0123456789abcdef"
1010

11-
1211
# commit_notice_destination() lets you customize where commit notices
1312
# are sent to with the full power of a Python function.
1413
#
@@ -20,30 +19,21 @@
2019
# Returns a dictionary encoding the stream and subject to send the
2120
# notification to (or None to send no notification).
2221
#
23-
# The default code below will send every commit pushed to "main" to
22+
# The default code below will send every commit pushed to "master" to
2423
# * stream "commits"
25-
# * topic "main"
24+
# * topic "master"
2625
# And similarly for branch "test-post-receive" (for use when testing).
27-
def commit_notice_destination(repo: str, branch: str, commit: str) -> Optional[Dict[str, str]]:
28-
if branch in ["main", "master", "test-post-receive"]:
29-
return dict(stream=STREAM_NAME, subject=f"{branch}")
26+
def commit_notice_destination(repo: Text, branch: Text, commit: Text) -> Optional[Dict[Text, Text]]:
27+
if branch in ["master", "test-post-receive"]:
28+
return dict(stream = STREAM_NAME,
29+
subject = "%s" % (branch,))
3030

3131
# Return None for cases where you don't want a notice sent
3232
return None
3333

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

4838
# Set this to your Zulip server's API URI
49-
ZULIP_SITE = "https://zulip.example.com"
39+
ZULIP_SITE = "https://zulip.example.com"

0 commit comments

Comments
 (0)