Message commits on push hook into matrix room

This commit is contained in:
Ben 2019-07-18 16:11:34 +02:00
parent 9241782d00
commit 1b2fc7610c
Signed by: ben
GPG key ID: 0F54A7ED232D3319

19
wmn.py
View file

@ -22,19 +22,24 @@ def notify():
channel = request.args.get('channel') channel = request.args.get('channel')
if channel is None or len(channel) == 0: if channel is None or len(channel) == 0:
abort(401) abort(401)
print(f"[DEBUG] Channel: {channel}")
gitlab_token = request.headers.get('X-Gitlab-Token') gitlab_token = request.headers.get('X-Gitlab-Token')
if gitlab_token is None or len(gitlab_token) == 0 or gitlab_token != cfg['secret']: if gitlab_token is None or len(gitlab_token) == 0 or gitlab_token != cfg['secret']:
abort(403) abort(403)
print("[DEBUG] Correct secret") gitlab_event = request.headers.get("X-Gitlab-Event")
server = cfg["matrix"]["server"] if gitlab_event == "Push Hook":
client = MatrixClient(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"])
print(f"[DEBUG] Connected to matrix server {server}")
room = client.join_room(room_id_or_alias=channel) room = client.join_room(room_id_or_alias=channel)
room.send_text("Hello!")
print(f"[DEBUG] Sent text to channel {room}") username = request.json["user_name"]
commit_count = len(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",
msgtype="m.notice")
return "" return ""