2019-07-19 17:39:10 +00:00
|
|
|
import json
|
2019-07-19 14:45:58 +00:00
|
|
|
import re
|
2019-07-21 13:26:30 +00:00
|
|
|
from datetime import datetime
|
2019-07-19 14:45:58 +00:00
|
|
|
|
2019-07-18 12:45:03 +00:00
|
|
|
import yaml
|
|
|
|
from flask import Flask, request, abort
|
|
|
|
from matrix_client.client import MatrixClient
|
2019-07-19 12:14:16 +00:00
|
|
|
from matrix_client.errors import MatrixRequestError
|
2019-07-18 12:45:03 +00:00
|
|
|
|
2019-07-18 13:09:10 +00:00
|
|
|
application = Flask(__name__)
|
2019-07-18 12:45:03 +00:00
|
|
|
|
2019-07-19 14:45:58 +00:00
|
|
|
# Not going to care for specifics like the underscore.
|
|
|
|
# Generally match !anything:example.com with unicode support.
|
|
|
|
room_pattern = re.compile(r'^!\w+:[\w\-.]+$')
|
|
|
|
|
2019-07-18 12:47:28 +00:00
|
|
|
"""
|
|
|
|
config.yml Example:
|
|
|
|
|
|
|
|
secret: "..."
|
|
|
|
matrix:
|
2019-07-18 13:46:22 +00:00
|
|
|
server: https://matrix.org
|
2019-07-18 12:47:28 +00:00
|
|
|
username: ...
|
|
|
|
password: "..."
|
|
|
|
"""
|
2019-07-18 12:45:03 +00:00
|
|
|
with open("config.yml", 'r') as ymlfile:
|
|
|
|
cfg = yaml.safe_load(ymlfile)
|
|
|
|
|
|
|
|
|
2019-07-19 14:45:58 +00:00
|
|
|
def check_token(header_field: str):
|
|
|
|
token = request.headers.get(header_field)
|
|
|
|
if token != cfg['secret']:
|
|
|
|
abort(401)
|
|
|
|
|
|
|
|
|
|
|
|
def get_a_room():
|
|
|
|
if 'channel' not in request.args:
|
2019-07-19 12:14:16 +00:00
|
|
|
abort(400)
|
2019-07-19 14:45:58 +00:00
|
|
|
room = request.args.get('channel')
|
|
|
|
# sanitize input
|
|
|
|
if room_pattern.fullmatch(room) is None:
|
|
|
|
abort(400)
|
|
|
|
return room
|
|
|
|
|
|
|
|
|
2019-07-22 00:00:30 +00:00
|
|
|
def get_msg_type():
|
|
|
|
if 'msgtype' not in request.args:
|
|
|
|
return "m.notice"
|
2019-07-25 21:18:32 +00:00
|
|
|
msgtype = request.args.get('msgtype')
|
2019-07-22 00:00:30 +00:00
|
|
|
if msgtype in ["m.text", "m.notice"]:
|
|
|
|
return msgtype
|
|
|
|
else:
|
|
|
|
abort(400)
|
|
|
|
|
|
|
|
|
2019-07-19 17:39:10 +00:00
|
|
|
def iter_first_line(string: str):
|
|
|
|
return iter(map(str.rstrip, string.lstrip().splitlines(keepends=False)))
|
|
|
|
|
|
|
|
|
|
|
|
def shorten(string: str, max_len: int = 80, appendix: str = "..."):
|
|
|
|
if len(string) > max_len:
|
|
|
|
return string[:max_len - len(appendix)] + appendix
|
|
|
|
else:
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
|
|
|
def matrix_error(error: MatrixRequestError):
|
|
|
|
# see Flask.make_response, this will be interpreted as (body, status)
|
|
|
|
return f"Error from Matrix: {error.content}", error.code
|
|
|
|
|
|
|
|
|
2019-07-19 14:45:58 +00:00
|
|
|
def process_gitlab_request():
|
|
|
|
check_token('X-Gitlab-Token')
|
2019-07-22 00:00:30 +00:00
|
|
|
msgtype = get_msg_type()
|
2019-07-19 14:45:58 +00:00
|
|
|
room = get_a_room()
|
2019-07-18 14:11:34 +00:00
|
|
|
gitlab_event = request.headers.get("X-Gitlab-Event")
|
|
|
|
|
|
|
|
if gitlab_event == "Push Hook":
|
2019-07-19 12:14:16 +00:00
|
|
|
try:
|
|
|
|
client = MatrixClient(cfg["matrix"]["server"])
|
|
|
|
client.login(username=cfg["matrix"]["username"], password=cfg["matrix"]["password"])
|
2019-07-18 14:11:34 +00:00
|
|
|
|
2019-07-19 14:45:58 +00:00
|
|
|
room = client.join_room(room_id_or_alias=room)
|
2019-07-19 12:14:16 +00:00
|
|
|
except MatrixRequestError as e:
|
2019-07-19 17:39:10 +00:00
|
|
|
return matrix_error(e)
|
2019-07-18 14:11:34 +00:00
|
|
|
|
2019-07-18 23:47:11 +00:00
|
|
|
def sort_commits_by_time(commits):
|
|
|
|
return sorted(commits, key=lambda commit: commit["timestamp"])
|
|
|
|
|
2019-08-09 11:30:22 +00:00
|
|
|
def extract_commit_info(commit):
|
2019-08-08 08:05:58 +00:00
|
|
|
msg = shorten(next(iter_first_line(commit["message"]), "$EMPTY_COMMIT_MESSAGE - impossibruh"))
|
|
|
|
url = commit["url"]
|
|
|
|
return msg, url
|
2019-07-18 23:47:11 +00:00
|
|
|
|
2019-07-18 14:11:34 +00:00
|
|
|
username = request.json["user_name"]
|
2019-07-18 23:47:11 +00:00
|
|
|
commit_messages = list(map(extract_commit_message, sort_commits_by_time(request.json["commits"])))
|
2019-07-18 14:11:34 +00:00
|
|
|
project_name = request.json["project"]["name"]
|
2019-08-08 08:05:58 +00:00
|
|
|
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))
|
2019-07-19 17:39:10 +00:00
|
|
|
try:
|
|
|
|
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",
|
2019-07-22 00:00:30 +00:00
|
|
|
msgtype=msgtype)
|
2019-07-19 17:39:10 +00:00
|
|
|
except MatrixRequestError as e:
|
|
|
|
return matrix_error(e)
|
2019-07-18 12:45:03 +00:00
|
|
|
|
2019-07-19 11:53:00 +00:00
|
|
|
# see Flask.make_response, this is interpreted as (body, status)
|
|
|
|
return "", 204
|
2019-07-19 12:14:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
def process_jenkins_request():
|
2019-07-19 14:45:58 +00:00
|
|
|
check_token('X-Jenkins-Token')
|
2019-07-22 00:00:30 +00:00
|
|
|
msgtype = get_msg_type()
|
2019-07-19 17:39:10 +00:00
|
|
|
room = get_a_room()
|
|
|
|
jenkins_event = request.headers.get("X-Jenkins-Event")
|
|
|
|
|
|
|
|
if jenkins_event == "Post Build Hook":
|
|
|
|
try:
|
|
|
|
client = MatrixClient(cfg["matrix"]["server"])
|
|
|
|
client.login(username=cfg["matrix"]["username"], password=cfg["matrix"]["password"])
|
2019-07-19 12:14:16 +00:00
|
|
|
|
2019-07-19 17:39:10 +00:00
|
|
|
room = client.join_room(room_id_or_alias=room)
|
|
|
|
except MatrixRequestError as e:
|
|
|
|
return matrix_error(e)
|
|
|
|
|
|
|
|
def extract_change_message(change):
|
|
|
|
change_message = next(iter_first_line(change["message"]), "")
|
|
|
|
if len(change_message) > 0:
|
2019-07-21 13:26:30 +00:00
|
|
|
htimestamp = datetime.fromtimestamp(change['timestamp'] / 1000).strftime("%d. %b %y %H:%M")
|
2019-07-19 17:39:10 +00:00
|
|
|
return f"{shorten(change_message)} " \
|
|
|
|
f"({shorten(change['commitId'], 7, appendix='')}) " \
|
|
|
|
f"by {change['author']} " \
|
2019-07-21 13:26:30 +00:00
|
|
|
f"at {htimestamp}"
|
2019-07-19 17:39:10 +00:00
|
|
|
else:
|
|
|
|
return shorten(json.dumps(change), appendix="...}")
|
|
|
|
|
|
|
|
build_name = request.json["displayName"]
|
|
|
|
project_name = request.json["project"]["fullDisplayName"]
|
|
|
|
result_type = request.json["result"]["type"]
|
|
|
|
result_color = request.json["result"]["color"]
|
|
|
|
change_messages = list(map(extract_change_message, request.json["changes"]))
|
|
|
|
html_changes = "\n".join((f" <li>{msg}</li>" for msg in change_messages))
|
|
|
|
text_changes = "\n".join((f"- {msg}" for msg in change_messages))
|
|
|
|
try:
|
2019-07-19 18:18:18 +00:00
|
|
|
room.send_html(f"<p><strong>Build {build_name} on project {project_name} complete: "
|
2019-07-22 00:00:45 +00:00
|
|
|
f"<font color=\"{result_color}\">{result_type}</font></strong>, "
|
2019-07-19 18:18:18 +00:00
|
|
|
f"{len(change_messages)} commits</p>\n"
|
|
|
|
"" + (f"<ul>\n{html_changes}\n</ul>\n" if len(change_messages) > 0 else ""),
|
|
|
|
body=f"**Build {build_name} on project {project_name} complete: {result_type}**, "
|
|
|
|
f"{len(change_messages)} commits\n"
|
|
|
|
"" + (f"{text_changes}\n" if len(change_messages) > 0 else ""),
|
2019-07-21 13:26:30 +00:00
|
|
|
msgtype=msgtype)
|
2019-07-19 17:39:10 +00:00
|
|
|
except MatrixRequestError as e:
|
|
|
|
return matrix_error(e)
|
2019-07-19 12:14:16 +00:00
|
|
|
|
|
|
|
# see Flask.make_response, this is interpreted as (body, status)
|
|
|
|
return "", 204
|
|
|
|
|
|
|
|
|
|
|
|
@application.route('/matrix', methods=("POST",))
|
|
|
|
def notify():
|
|
|
|
if 'X-Gitlab-Token' in request.headers:
|
|
|
|
return process_gitlab_request()
|
|
|
|
elif 'X-Jenkins-Token' in request.headers:
|
|
|
|
return process_jenkins_request()
|
|
|
|
else:
|
|
|
|
return "Cannot determine the request's webhook cause", 400
|