Sort commits and output first-line commit messages

This commit is contained in:
Ben 2019-07-19 01:47:11 +02:00
parent 1b2fc7610c
commit b5c97dd62c
Signed by: ben
GPG key ID: 0F54A7ED232D3319
2 changed files with 13 additions and 6 deletions

View file

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Python 3.7 (webhook-matrix-notifier)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TestRunnerService">

17
wmn.py
View file

@ -33,13 +33,20 @@ def notify():
room = client.join_room(room_id_or_alias=channel)
def sort_commits_by_time(commits):
return sorted(commits, key=lambda commit: commit["timestamp"])
def extract_commit_message(commit):
return next(commit["message"].splitlines(keepends=False), "$EMPTY_COMMIT_MESSAGE - impossibruh").strip()
username = request.json["user_name"]
commit_count = len(request.json["commits"])
commit_messages = list(map(extract_commit_message, sort_commits_by_time(request.json["commits"])))
project_name = request.json["project"]["name"]
room.send_html(f"<strong>{username} pushed {commit_count} commits to {project_name}</strong><br>\n"
"<ul>\n" + "\n".join((f"{commit['message']}" for commit in request.json["commits"])) + "</ul>\n",
body=f"{username} pushed {commit_count} commits to {project_name}\n"
"" + "\n".join((f"- {commit['message']}" for commit in request.json["commits"])) + "\n",
html_commits = "\n".join((f" <li>{msg}</li>" for msg in commit_messages))
text_commits = "\n".join((f"- {msg}" for msg in commit_messages))
room.send_html(f"<strong>{username} pushed {len(commit_messages)} commits to {project_name}</strong><br>\n"
f"<ul>\n{html_commits}\n</ul>\n",
body=f"{username} pushed {len(commit_messages)} commits to {project_name}\n{text_commits}\n",
msgtype="m.notice")
return ""