Add branch name to push message

and ignore empty GitLab push hook
This commit is contained in:
Ben 2019-09-20 19:20:40 +02:00
parent 0ccc649d59
commit 5be1f444e1
Signed by: ben
GPG key ID: 0F54A7ED232D3319

14
wmn.py
View file

@ -75,6 +75,9 @@ def process_gitlab_request():
gitlab_event = request.headers.get("X-Gitlab-Event") gitlab_event = request.headers.get("X-Gitlab-Event")
if gitlab_event == "Push Hook": if gitlab_event == "Push Hook":
if request.json["total_commits_count"] < 1:
return "", 204
try: try:
client = MatrixClient(cfg["matrix"]["server"]) client = MatrixClient(cfg["matrix"]["server"])
client.login(username=cfg["matrix"]["username"], password=cfg["matrix"]["password"]) client.login(username=cfg["matrix"]["username"], password=cfg["matrix"]["password"])
@ -92,14 +95,19 @@ def process_gitlab_request():
return msg, url return msg, url
username = request.json["user_name"] username = request.json["user_name"]
commit_messages = list(map(extract_commit_info, sort_commits_by_time(request.json["commits"])))
project_name = request.json["project"]["name"] project_name = request.json["project"]["name"]
if request.json["ref"].startswith("refs/heads/"):
to_str = f" to branch {request.json['ref'][len('refs/heads/'):]} on project {project_name}"
else:
to_str = f" to {project_name}"
commit_messages = list(map(extract_commit_info, sort_commits_by_time(request.json["commits"])))
html_commits = "\n".join((f' <li><a href="{url}">{msg}</a></li>' for (msg, url) in commit_messages)) html_commits = "\n".join((f' <li><a href="{url}">{msg}</a></li>' for (msg, url) in commit_messages))
text_commits = "\n".join((f"- [{msg}]({url})" for (msg, url) in commit_messages)) text_commits = "\n".join((f"- [{msg}]({url})" for (msg, url) in commit_messages))
try: try:
room.send_html(f"<strong>{username} pushed {len(commit_messages)} commits to {project_name}</strong><br>\n" room.send_html(f"<strong>{username} pushed {len(commit_messages)} commits{to_str}</strong><br>\n"
f"<ul>\n{html_commits}\n</ul>\n", f"<ul>\n{html_commits}\n</ul>\n",
body=f"{username} pushed {len(commit_messages)} commits to {project_name}\n{text_commits}\n", body=f"{username} pushed {len(commit_messages)} commits{to_str}\n{text_commits}\n",
msgtype=msgtype) msgtype=msgtype)
except MatrixRequestError as e: except MatrixRequestError as e:
return matrix_error(e) return matrix_error(e)