2021-03-10 03:47:04 +01:00
|
|
|
# Webhooks for external integrations.
|
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
|
|
|
|
from zerver.decorator import webhook_view
|
|
|
|
from zerver.lib.response import json_success
|
2023-09-27 19:01:31 +02:00
|
|
|
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
|
2023-08-12 09:34:31 +02:00
|
|
|
from zerver.lib.validator import WildValue, check_none_or, check_string
|
2021-03-10 03:47:04 +01:00
|
|
|
from zerver.lib.webhooks.common import check_send_webhook_message
|
|
|
|
from zerver.models import UserProfile
|
|
|
|
|
|
|
|
TOPIC_WITH_BRANCH = "{} / {}"
|
|
|
|
|
|
|
|
MESSAGE_WITH_BRANCH_AND_CONDITIONS = "Project [{}]({}) analysis of branch {} resulted in {}:\n"
|
|
|
|
MESSAGE_WITH_BRANCH_AND_WITHOUT_CONDITIONS = (
|
|
|
|
"Project [{}]({}) analysis of branch {} resulted in {}."
|
|
|
|
)
|
|
|
|
MESSAGE_WITHOUT_BRANCH_AND_WITH_CONDITIONS = "Project [{}]({}) analysis resulted in {}:\n"
|
|
|
|
MESSAGE_WITHOUT_BRANCH_AND_CONDITIONS = "Project [{}]({}) analysis resulted in {}."
|
|
|
|
|
|
|
|
INVERSE_OPERATORS = {
|
|
|
|
"WORSE_THAN": "should be better or equal to",
|
|
|
|
"GREATER_THAN": "should be less than or equal to",
|
|
|
|
"LESS_THAN": "should be greater than or equal to",
|
|
|
|
}
|
|
|
|
|
|
|
|
TEMPLATES = {
|
|
|
|
"default": "* {}: **{}** {} {} {}.",
|
|
|
|
"no_value": "* {}: **{}**.",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def parse_metric_name(metric_name: str) -> str:
|
|
|
|
return " ".join(metric_name.split("_"))
|
|
|
|
|
|
|
|
|
2022-06-23 17:21:26 +02:00
|
|
|
def parse_condition(condition: WildValue) -> str:
|
|
|
|
metric = condition["metric"].tame(check_string)
|
2021-03-10 03:47:04 +01:00
|
|
|
|
|
|
|
metric_name = parse_metric_name(metric)
|
2022-06-23 17:21:26 +02:00
|
|
|
operator = condition["operator"].tame(check_string)
|
2021-03-10 03:47:04 +01:00
|
|
|
operator = INVERSE_OPERATORS.get(operator, operator)
|
2022-06-23 17:21:26 +02:00
|
|
|
value = condition.get("value", "no value").tame(check_string)
|
|
|
|
status = condition["status"].tame(check_string).lower()
|
|
|
|
threshold = condition["errorThreshold"].tame(check_string)
|
2021-03-10 03:47:04 +01:00
|
|
|
|
|
|
|
if value == "no value":
|
|
|
|
return TEMPLATES["no_value"].format(metric_name, status)
|
|
|
|
|
|
|
|
template = TEMPLATES["default"]
|
|
|
|
|
|
|
|
return template.format(metric_name, status, value, operator, threshold)
|
|
|
|
|
|
|
|
|
2022-06-23 17:21:26 +02:00
|
|
|
def parse_conditions(conditions: WildValue) -> str:
|
2021-03-10 03:47:04 +01:00
|
|
|
return "\n".join(
|
|
|
|
[
|
|
|
|
parse_condition(condition)
|
|
|
|
for condition in conditions
|
2022-06-23 17:21:26 +02:00
|
|
|
if condition["status"].tame(check_string).lower() != "ok"
|
|
|
|
and condition["status"].tame(check_string).lower() != "no_value"
|
2021-03-10 03:47:04 +01:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-06-23 17:21:26 +02:00
|
|
|
def render_body_with_branch(payload: WildValue) -> str:
|
|
|
|
project_name = payload["project"]["name"].tame(check_string)
|
|
|
|
project_url = payload["project"]["url"].tame(check_string)
|
|
|
|
quality_gate_status = payload["qualityGate"]["status"].tame(check_string).lower()
|
2021-03-10 03:47:04 +01:00
|
|
|
if quality_gate_status == "ok":
|
|
|
|
quality_gate_status = "success"
|
|
|
|
else:
|
|
|
|
quality_gate_status = "error"
|
2022-06-23 17:21:26 +02:00
|
|
|
branch = payload["branch"]["name"].tame(check_string)
|
2021-03-10 03:47:04 +01:00
|
|
|
|
2022-06-23 17:21:26 +02:00
|
|
|
conditions = parse_conditions(payload["qualityGate"]["conditions"])
|
2021-03-10 03:47:04 +01:00
|
|
|
|
|
|
|
if not conditions:
|
|
|
|
return MESSAGE_WITH_BRANCH_AND_WITHOUT_CONDITIONS.format(
|
|
|
|
project_name, project_url, branch, quality_gate_status
|
|
|
|
)
|
|
|
|
msg = MESSAGE_WITH_BRANCH_AND_CONDITIONS.format(
|
|
|
|
project_name, project_url, branch, quality_gate_status
|
|
|
|
)
|
|
|
|
msg += conditions
|
|
|
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
2022-06-23 17:21:26 +02:00
|
|
|
def render_body_without_branch(payload: WildValue) -> str:
|
|
|
|
project_name = payload["project"]["name"].tame(check_string)
|
|
|
|
project_url = payload["project"]["url"].tame(check_string)
|
|
|
|
quality_gate_status = payload["qualityGate"]["status"].tame(check_string).lower()
|
2021-03-10 03:47:04 +01:00
|
|
|
if quality_gate_status == "ok":
|
|
|
|
quality_gate_status = "success"
|
|
|
|
else:
|
|
|
|
quality_gate_status = "error"
|
2022-06-23 17:21:26 +02:00
|
|
|
conditions = parse_conditions(payload["qualityGate"]["conditions"])
|
2021-03-10 03:47:04 +01:00
|
|
|
|
|
|
|
if not conditions:
|
|
|
|
return MESSAGE_WITHOUT_BRANCH_AND_CONDITIONS.format(
|
|
|
|
project_name, project_url, quality_gate_status
|
|
|
|
)
|
|
|
|
msg = MESSAGE_WITHOUT_BRANCH_AND_WITH_CONDITIONS.format(
|
|
|
|
project_name, project_url, quality_gate_status
|
|
|
|
)
|
|
|
|
msg += conditions
|
|
|
|
|
|
|
|
return msg
|
|
|
|
|
|
|
|
|
|
|
|
@webhook_view("Sonarqube")
|
2023-08-12 09:34:31 +02:00
|
|
|
@typed_endpoint
|
2021-03-10 03:47:04 +01:00
|
|
|
def api_sonarqube_webhook(
|
|
|
|
request: HttpRequest,
|
|
|
|
user_profile: UserProfile,
|
2023-08-12 09:34:31 +02:00
|
|
|
*,
|
2023-09-27 19:01:31 +02:00
|
|
|
payload: JsonBodyPayload[WildValue],
|
2021-03-10 03:47:04 +01:00
|
|
|
) -> HttpResponse:
|
2022-06-23 17:21:26 +02:00
|
|
|
project = payload["project"]["name"].tame(check_string)
|
2021-03-10 03:47:04 +01:00
|
|
|
branch = None
|
2022-12-12 03:39:16 +01:00
|
|
|
if "branch" in payload:
|
2022-06-23 17:21:26 +02:00
|
|
|
branch = payload["branch"].get("name").tame(check_none_or(check_string))
|
2021-03-10 03:47:04 +01:00
|
|
|
if branch:
|
|
|
|
topic = TOPIC_WITH_BRANCH.format(project, branch)
|
|
|
|
message = render_body_with_branch(payload)
|
|
|
|
else:
|
|
|
|
topic = project
|
|
|
|
message = render_body_without_branch(payload)
|
|
|
|
check_send_webhook_message(request, user_profile, topic, message)
|
2022-01-31 13:44:02 +01:00
|
|
|
return json_success(request)
|