2021-06-28 07:56:48 +00:00
|
|
|
# Copyright 2019-2021 Benedikt Ziemons
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
# this software and associated documentation files (the "Software"), to deal in
|
|
|
|
# the Software without restriction, including without limitation the rights to
|
|
|
|
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
# the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
# subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2019-07-19 17:39:10 +00:00
|
|
|
import json
|
2023-10-27 11:46:58 +00:00
|
|
|
import logging
|
2019-07-19 14:45:58 +00:00
|
|
|
import re
|
2019-07-21 13:26:30 +00:00
|
|
|
from datetime import datetime
|
2021-06-28 07:56:48 +00:00
|
|
|
from typing import Tuple, Optional, Dict, Any
|
2019-07-19 14:45:58 +00:00
|
|
|
|
2021-10-28 20:58:37 +00:00
|
|
|
import dateutil.parser
|
2021-06-28 07:56:48 +00:00
|
|
|
import nio
|
2019-07-18 12:45:03 +00:00
|
|
|
from flask import Flask, request, abort
|
2021-06-28 07:56:48 +00:00
|
|
|
from werkzeug.datastructures import MultiDict
|
|
|
|
|
2021-10-28 20:58:37 +00:00
|
|
|
from .common import (
|
2021-10-28 18:48:06 +00:00
|
|
|
client_login,
|
|
|
|
send_message,
|
|
|
|
Cfg,
|
|
|
|
resolve_room,
|
|
|
|
format_response,
|
|
|
|
load_configuration,
|
|
|
|
MatrixException,
|
|
|
|
)
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
RequestArgs = MultiDict[str, str]
|
2019-07-18 12:45:03 +00:00
|
|
|
|
2023-10-27 11:46:58 +00:00
|
|
|
logging.basicConfig()
|
|
|
|
|
|
|
|
# application is the wsgi variable name
|
|
|
|
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.
|
2020-04-03 14:41:11 +00:00
|
|
|
# Generally match room alias or id [!#]anything:example.com with unicode support.
|
2020-09-27 15:56:36 +00:00
|
|
|
room_pattern = re.compile(r"^[!#]\w+:[\w\-.]+$")
|
2019-07-19 14:45:58 +00:00
|
|
|
|
2019-07-18 12:47:28 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
def check_token(configuration: Cfg, token: str):
|
|
|
|
if token != configuration["secret"]:
|
2023-10-27 11:46:58 +00:00
|
|
|
logging.warning("request denied (401): check_token failed, because token did not match")
|
2019-07-19 14:45:58 +00:00
|
|
|
abort(401)
|
|
|
|
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
async def get_a_room(client: nio.AsyncClient, request_args: RequestArgs) -> str:
|
|
|
|
"""Takes a nio.AsyncClient and the request args to return a room id."""
|
|
|
|
|
2023-10-27 11:46:58 +00:00
|
|
|
if "channel" not in request_args and "room" not in request_args:
|
|
|
|
logging.warning("request denied (400): get_a_room failed, because room was not in request args")
|
2019-07-19 12:14:16 +00:00
|
|
|
abort(400)
|
2023-10-27 11:46:58 +00:00
|
|
|
room = request_args.get("channel", "")
|
|
|
|
room = request_args.get("room", room)
|
2021-06-28 07:56:48 +00:00
|
|
|
if not room:
|
2023-10-27 11:46:58 +00:00
|
|
|
logging.warning("request denied (400): get_a_room failed, because room was empty")
|
2021-06-28 07:56:48 +00:00
|
|
|
abort(400)
|
|
|
|
|
2019-07-19 14:45:58 +00:00
|
|
|
# sanitize input
|
|
|
|
if room_pattern.fullmatch(room) is None:
|
2023-10-27 11:46:58 +00:00
|
|
|
logging.warning("request denied (400): get_a_room failed, because room '%s' did not match room pattern '%s'", room, room_pattern)
|
2019-07-19 14:45:58 +00:00
|
|
|
abort(400)
|
|
|
|
|
2021-10-28 18:48:06 +00:00
|
|
|
try:
|
|
|
|
return await resolve_room(client=client, room=room)
|
|
|
|
except MatrixException as error:
|
2023-10-27 11:46:58 +00:00
|
|
|
abort(application.make_response(error.format_response()))
|
2019-07-19 14:45:58 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
|
|
|
|
def get_msg_type(request_args: RequestArgs):
|
|
|
|
if "msgtype" not in request_args:
|
2019-07-22 00:00:30 +00:00
|
|
|
return "m.notice"
|
2021-06-28 07:56:48 +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:
|
2023-10-27 11:46:58 +00:00
|
|
|
logging.warning("request denied (400): get_msg_type failed, because msgtype '%s' is not known", msgtype)
|
2019-07-22 00:00:30 +00:00
|
|
|
abort(400)
|
|
|
|
|
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
def color_format_html(color_hex: str, text: str):
|
|
|
|
return f'<font color="#{color_hex}">{text}</font>'
|
|
|
|
|
|
|
|
|
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:
|
2020-09-27 15:56:36 +00:00
|
|
|
return string[: max_len - len(appendix)] + appendix
|
2019-07-19 17:39:10 +00:00
|
|
|
else:
|
|
|
|
return string
|
|
|
|
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
async def process_gitlab_request():
|
|
|
|
cfg = load_configuration()
|
|
|
|
check_token(configuration=cfg, token=request.headers.get("X-Gitlab-Token"))
|
2019-07-18 14:11:34 +00:00
|
|
|
gitlab_event = request.headers.get("X-Gitlab-Event")
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
try:
|
|
|
|
client = await client_login(cfg)
|
2021-10-28 18:48:06 +00:00
|
|
|
except MatrixException as error:
|
|
|
|
return error.format_response()
|
2019-07-18 14:11:34 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
try:
|
|
|
|
room_id = await get_a_room(client, request.args)
|
|
|
|
msgtype = get_msg_type(request_args=request.args)
|
|
|
|
|
|
|
|
if gitlab_event == "Push Hook":
|
|
|
|
if request.json["total_commits_count"] < 1:
|
|
|
|
return "", 204
|
|
|
|
|
|
|
|
response = await client.join(room_id=room_id)
|
|
|
|
if isinstance(response, nio.ErrorResponse):
|
2021-10-28 18:48:06 +00:00
|
|
|
return format_response(response)
|
2021-06-28 07:56:48 +00:00
|
|
|
|
|
|
|
def sort_commits_by_time(commits):
|
|
|
|
return sorted(commits, key=lambda commit: commit["timestamp"])
|
|
|
|
|
|
|
|
def extract_commit_info(commit):
|
|
|
|
msg = shorten(
|
|
|
|
next(
|
|
|
|
iter_first_line(commit["message"]),
|
|
|
|
"$EMPTY_COMMIT_MESSAGE - impossibruh",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
url = commit["url"]
|
|
|
|
return msg, url
|
2019-07-18 14:11:34 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
username = request.json["user_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}"
|
2019-07-18 23:47:11 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
commit_messages = list(
|
|
|
|
map(extract_commit_info, sort_commits_by_time(request.json["commits"]))
|
|
|
|
)
|
|
|
|
html_commits = "\n".join(
|
2021-10-28 18:48:06 +00:00
|
|
|
(
|
|
|
|
f' <li><a href="{url}">{msg}</a></li>'
|
|
|
|
for (msg, url) in commit_messages
|
|
|
|
)
|
2021-06-28 07:56:48 +00:00
|
|
|
)
|
|
|
|
text_commits = "\n".join(
|
|
|
|
(f"- [{msg}]({url})" for (msg, url) in commit_messages)
|
2020-09-27 15:56:36 +00:00
|
|
|
)
|
2019-09-20 17:20:40 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
response = await client.room_send(
|
|
|
|
room_id=room_id,
|
|
|
|
message_type="m.room.message",
|
|
|
|
content={
|
|
|
|
"msgtype": msgtype,
|
|
|
|
"format": "org.matrix.custom.html",
|
|
|
|
"formatted_body": f"<strong>{username} pushed {len(commit_messages)} commits{to_str}</strong><br>\n"
|
|
|
|
f"<ul>\n{html_commits}\n</ul>\n",
|
|
|
|
"body": f"{username} pushed {len(commit_messages)} commits{to_str}\n{text_commits}\n",
|
|
|
|
},
|
|
|
|
ignore_unverified_devices=True,
|
2020-09-27 15:56:36 +00:00
|
|
|
)
|
2021-06-28 07:56:48 +00:00
|
|
|
if isinstance(response, nio.ErrorResponse):
|
2021-10-28 18:48:06 +00:00
|
|
|
return format_response(response)
|
2021-06-28 07:56:48 +00:00
|
|
|
|
2021-10-28 18:48:06 +00:00
|
|
|
except MatrixException as error:
|
2023-10-27 11:46:58 +00:00
|
|
|
abort(application.make_response(error.format_response()))
|
2021-06-28 07:56:48 +00:00
|
|
|
finally:
|
|
|
|
await client.close()
|
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
|
|
|
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
async def process_jenkins_request():
|
|
|
|
cfg = load_configuration()
|
|
|
|
check_token(configuration=cfg, token=request.headers.get("X-Jenkins-Token"))
|
|
|
|
msgtype = get_msg_type(request_args=request.args)
|
2019-07-19 17:39:10 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
try:
|
|
|
|
client = await client_login(cfg)
|
2021-10-28 18:48:06 +00:00
|
|
|
except MatrixException as error:
|
|
|
|
return error.format_response()
|
2019-07-19 12:14:16 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
try:
|
|
|
|
room_id = await get_a_room(client, request.args)
|
|
|
|
jenkins_event = request.headers.get("X-Jenkins-Event")
|
|
|
|
|
|
|
|
if jenkins_event == "Post Build Hook":
|
|
|
|
project_url = request.json["githubProjectUrl"]
|
|
|
|
|
|
|
|
def extract_change_message(change):
|
|
|
|
change_message = next(iter_first_line(change["message"]), "")
|
|
|
|
if len(change_message) > 0:
|
|
|
|
htimestamp = datetime.fromtimestamp(
|
|
|
|
change["timestamp"] / 1000
|
|
|
|
).strftime("%d. %b %y %H:%M")
|
2021-10-28 18:48:06 +00:00
|
|
|
bare_commit_link = (
|
|
|
|
f"({shorten(change['commitId'], 7, appendix='')})"
|
|
|
|
)
|
2021-06-28 07:56:48 +00:00
|
|
|
if project_url is not None and project_url:
|
|
|
|
commit_link = f"<a href=\"{project_url}commit/{change['commitId']}\">{bare_commit_link}</a>"
|
|
|
|
else:
|
|
|
|
commit_link = bare_commit_link
|
|
|
|
return (
|
|
|
|
f"- {shorten(change_message)} {bare_commit_link} by {change['author']} at {htimestamp}",
|
|
|
|
f" <li>{shorten(change_message)} {commit_link} by {change['author']} at {htimestamp}</li>",
|
|
|
|
)
|
2020-04-29 00:59:27 +00:00
|
|
|
else:
|
2021-06-28 07:56:48 +00:00
|
|
|
dump = shorten(json.dumps(change), appendix="...}")
|
|
|
|
return (dump, dump.replace("<", "<").replace(">", ">"))
|
|
|
|
|
|
|
|
build_name = request.json["displayName"]
|
|
|
|
project_name = request.json["project"]["fullDisplayName"]
|
|
|
|
result_type = request.json["result"]["type"]
|
|
|
|
result_color = request.json["result"]["color"]
|
|
|
|
changes = request.json["changes"]
|
|
|
|
if len(changes) > 0:
|
|
|
|
text_change_messages, html_change_messages = zip(
|
|
|
|
*map(extract_change_message, changes)
|
2020-04-30 22:22:07 +00:00
|
|
|
)
|
2019-07-19 17:39:10 +00:00
|
|
|
else:
|
2021-06-28 07:56:48 +00:00
|
|
|
text_change_messages, html_change_messages = (), () # it's an owl!
|
2020-05-16 15:53:57 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
newline = "\n" # expressions inside f-strings cannot contain backslashes...
|
|
|
|
html_changes = (
|
|
|
|
f"<ul>\n{newline.join(html_change_messages)}\n</ul>\n"
|
|
|
|
if len(html_change_messages) > 0
|
|
|
|
else ""
|
|
|
|
)
|
|
|
|
text_changes = (
|
|
|
|
f"{newline.join(text_change_messages)}\n"
|
|
|
|
if len(text_change_messages) > 0
|
|
|
|
else ""
|
|
|
|
)
|
|
|
|
await send_message(
|
|
|
|
client=client,
|
|
|
|
room_id=room_id,
|
|
|
|
text=(
|
|
|
|
f"**Build {build_name} on project {project_name} complete: {result_type}**, "
|
|
|
|
f"{len(changes)} commits\n"
|
|
|
|
f"{text_changes}"
|
2020-09-27 15:56:36 +00:00
|
|
|
),
|
|
|
|
msgtype=msgtype,
|
2021-06-28 07:56:48 +00:00
|
|
|
html=(
|
|
|
|
f"<p><strong>Build {build_name} on project {project_name} complete: "
|
|
|
|
f'<font color="{result_color}">{result_type}</font></strong>, '
|
|
|
|
f"{len(changes)} commits</p>\n"
|
|
|
|
f"{html_changes}"
|
|
|
|
),
|
2020-09-27 15:56:36 +00:00
|
|
|
)
|
2021-06-28 07:56:48 +00:00
|
|
|
|
2021-10-28 18:48:06 +00:00
|
|
|
except MatrixException as error:
|
2023-10-27 11:46:58 +00:00
|
|
|
abort(application.make_response(error.format_response()))
|
2021-06-28 07:56:48 +00:00
|
|
|
finally:
|
|
|
|
await client.close()
|
2019-07-19 12:14:16 +00:00
|
|
|
|
|
|
|
# see Flask.make_response, this is interpreted as (body, status)
|
|
|
|
return "", 204
|
|
|
|
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
async def process_prometheus_request():
|
2020-04-03 14:32:17 +00:00
|
|
|
# written for version 4 of the alertmanager webhook JSON
|
|
|
|
# https://prometheus.io/docs/alerting/configuration/#webhook_config
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
def color_status_html(status: str, text: Optional[str] = None):
|
2020-04-03 14:32:17 +00:00
|
|
|
_status_colors = {"resolved": "34A91D", "firing": "EF2929"}
|
|
|
|
if text is None:
|
|
|
|
text = status
|
2020-09-27 15:55:20 +00:00
|
|
|
return color_format_html(_status_colors.get(status, "FFFFFF"), text)
|
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
def color_severity_html(severity: str, text: Optional[str] = None):
|
2020-09-27 15:55:20 +00:00
|
|
|
_severity_colors = {"warning": "EFAC29", "critical": "EF2929"}
|
|
|
|
if text is None:
|
|
|
|
text = severity
|
|
|
|
return color_format_html(_severity_colors.get(severity, "FFFFFF"), text)
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
def alert_title(status: str, alertname: str, generator_url: str):
|
|
|
|
if alertname:
|
|
|
|
alertname = " alert " + alertname
|
|
|
|
|
|
|
|
if status:
|
|
|
|
status_msg = status.upper() if status == "firing" else status.title()
|
|
|
|
title = status_msg + alertname
|
|
|
|
html_title = color_status_html(status, title)
|
|
|
|
elif alertname:
|
|
|
|
title = alertname
|
|
|
|
html_title = title
|
|
|
|
else:
|
|
|
|
title = ""
|
|
|
|
html_title = title
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
if title:
|
|
|
|
title = f"*{title}*"
|
|
|
|
if generator_url:
|
|
|
|
title = f"{title} {generator_url}"
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
if html_title:
|
|
|
|
html_title = f"<strong>{html_title}</strong>"
|
|
|
|
if generator_url:
|
|
|
|
html_title = f'<a href="{generator_url}">{html_title}</a>'
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
return title, html_title
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
def extract_alert_message(alert: Dict[str, Any]) -> Tuple[str, str]:
|
2020-09-27 15:55:20 +00:00
|
|
|
"""Takes the alert object and returns (text, html) as a string tuple."""
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
labels = alert.get("labels", {})
|
|
|
|
severity = labels.get("severity", "")
|
|
|
|
annotations = alert.get("annotations", {})
|
|
|
|
description = annotations.get("description", "")
|
|
|
|
if not description:
|
|
|
|
description = annotations.get("summary", "")
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
alert_daterange = []
|
2020-09-27 15:56:36 +00:00
|
|
|
if "startsAt" in alert and alert["startsAt"] != "0001-01-01T00:00:00Z":
|
|
|
|
alert_start = (
|
2021-10-28 18:48:06 +00:00
|
|
|
dateutil.parser.isoparse(alert["startsAt"])
|
|
|
|
.strftime("%d. %b %y %H:%M %Z")
|
|
|
|
.rstrip()
|
2020-09-27 15:56:36 +00:00
|
|
|
)
|
|
|
|
alert_daterange.append(f"started at {alert_start}")
|
|
|
|
if "endsAt" in alert and alert["endsAt"] != "0001-01-01T00:00:00Z":
|
|
|
|
alert_end = (
|
2021-10-28 18:48:06 +00:00
|
|
|
dateutil.parser.isoparse(alert["endsAt"])
|
|
|
|
.strftime("%d. %b %y %H:%M %Z")
|
|
|
|
.rstrip()
|
2020-09-27 15:56:36 +00:00
|
|
|
)
|
|
|
|
alert_daterange.append(f"ended at {alert_end}")
|
2020-09-27 15:55:20 +00:00
|
|
|
alert_daterange = ", ".join(alert_daterange)
|
|
|
|
|
|
|
|
title, html_title = alert_title(
|
|
|
|
status=alert.get("status", ""),
|
|
|
|
alertname=labels.get("alertname", ""),
|
2020-09-27 15:56:36 +00:00
|
|
|
generator_url=alert.get("generatorURL", ""),
|
2020-04-03 14:32:17 +00:00
|
|
|
)
|
2020-09-27 15:55:20 +00:00
|
|
|
if severity:
|
|
|
|
html_severity = f"Severity: {color_severity_html(severity)}"
|
2020-09-27 15:56:36 +00:00
|
|
|
severity = severity.upper() if severity == "critical" else severity.title()
|
2020-09-27 15:55:20 +00:00
|
|
|
severity = f"Severity: {severity}"
|
|
|
|
else:
|
|
|
|
html_severity = ""
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
html_parts = [html_title, html_severity, description, alert_daterange]
|
|
|
|
html_message = "</p>\n<p>".join(filter(bool, html_parts))
|
|
|
|
html_message = f"<p>{html_message}</p>" if html_message else ""
|
|
|
|
return (
|
|
|
|
" \n".join(filter(bool, [title, severity, description, alert_daterange])),
|
2020-09-27 15:56:36 +00:00
|
|
|
html_message,
|
2020-09-27 15:55:20 +00:00
|
|
|
)
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
cfg = load_configuration()
|
|
|
|
secret = request.args.get("secret")
|
|
|
|
if secret != cfg["secret"]:
|
2023-10-27 11:46:58 +00:00
|
|
|
logging.warning("check_token failed, because token did not match")
|
2021-06-28 07:56:48 +00:00
|
|
|
abort(401)
|
|
|
|
|
|
|
|
try:
|
|
|
|
client = await client_login(cfg)
|
2021-10-28 18:48:06 +00:00
|
|
|
except MatrixException as error:
|
|
|
|
return error.format_response()
|
2021-06-28 07:56:48 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
msgtype = get_msg_type(request_args=request.args)
|
|
|
|
room_id = await get_a_room(client=client, request_args=request.args)
|
|
|
|
|
|
|
|
if not request.json:
|
|
|
|
abort(400)
|
|
|
|
|
2020-09-27 15:55:20 +00:00
|
|
|
try:
|
2021-10-28 18:48:06 +00:00
|
|
|
for text, html in map(
|
|
|
|
extract_alert_message, request.json.get("alerts", [])
|
|
|
|
):
|
2021-06-28 07:56:48 +00:00
|
|
|
if html and text:
|
2021-10-28 18:48:06 +00:00
|
|
|
await send_message(
|
|
|
|
client=client,
|
|
|
|
room_id=room_id,
|
|
|
|
text=text,
|
|
|
|
msgtype=msgtype,
|
|
|
|
html=html,
|
|
|
|
)
|
2021-06-28 07:56:48 +00:00
|
|
|
elif text:
|
2021-10-28 18:48:06 +00:00
|
|
|
await send_message(
|
|
|
|
client=client, room_id=room_id, text=text, msgtype=msgtype
|
|
|
|
)
|
2020-09-27 15:55:20 +00:00
|
|
|
except (LookupError, ValueError, TypeError):
|
2021-10-28 18:48:06 +00:00
|
|
|
await send_message(
|
|
|
|
client=client,
|
|
|
|
room_id=room_id,
|
|
|
|
text="Error parsing data in prometheus request",
|
|
|
|
)
|
2023-10-27 11:46:58 +00:00
|
|
|
logging.exception("Error parsing JSON and forming message")
|
2020-09-27 15:55:20 +00:00
|
|
|
return "Error parsing JSON and forming message", 500
|
2021-10-28 18:48:06 +00:00
|
|
|
except MatrixException as error:
|
2023-10-27 11:46:58 +00:00
|
|
|
abort(application.make_response(error.format_response()))
|
2021-06-28 07:56:48 +00:00
|
|
|
finally:
|
|
|
|
await client.close()
|
2020-04-03 14:32:17 +00:00
|
|
|
|
2021-06-28 07:56:48 +00:00
|
|
|
# see Flask.make_response, this is interpreted as (text, status)
|
2020-04-03 14:32:17 +00:00
|
|
|
return "", 204
|
|
|
|
|
|
|
|
|
2023-10-27 11:46:58 +00:00
|
|
|
@application.post("/matrix")
|
2021-06-28 07:56:48 +00:00
|
|
|
async def notify():
|
2020-09-27 15:56:36 +00:00
|
|
|
if "X-Gitlab-Token" in request.headers:
|
2021-06-28 07:56:48 +00:00
|
|
|
return await process_gitlab_request()
|
2020-09-27 15:56:36 +00:00
|
|
|
elif "X-Jenkins-Token" in request.headers:
|
2021-06-28 07:56:48 +00:00
|
|
|
return await process_jenkins_request()
|
2020-09-27 15:56:36 +00:00
|
|
|
elif "type" in request.args and request.args.get("type") == "prometheus":
|
2021-06-28 07:56:48 +00:00
|
|
|
return await process_prometheus_request()
|
2019-07-19 12:14:16 +00:00
|
|
|
else:
|
|
|
|
return "Cannot determine the request's webhook cause", 400
|