integrations: Add SonarQube webhook integration.

Fixes #13395.
This commit is contained in:
tushar912 2021-03-10 08:17:04 +05:30 committed by Tim Abbott
parent 3a6d44b691
commit 83f6557f43
13 changed files with 486 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

View File

@ -444,6 +444,7 @@ WEBHOOK_INTEGRATIONS: List[WebhookIntegration] = [
),
WebhookIntegration("slack", ["communication"]),
WebhookIntegration("solano", ["continuous-integration"], display_name="Solano Labs"),
WebhookIntegration("sonarqube", ["continuous-integration"], display_name="SonarQube"),
WebhookIntegration("sonarr", ["entertainment"], display_name="Sonarr"),
WebhookIntegration("splunk", ["monitoring"], display_name="Splunk"),
WebhookIntegration("statuspage", ["customer-support"], display_name="Statuspage"),
@ -780,6 +781,7 @@ DOC_SCREENSHOT_CONFIG: Dict[str, List[BaseScreenshotConfig]] = {
],
"slack": [ScreenshotConfig("message_info.txt")],
"solano": [ScreenshotConfig("build_001.json")],
"sonarqube": [ScreenshotConfig("error.json")],
"sonarr": [ScreenshotConfig("sonarr_episode_grabbed.json")],
"splunk": [ScreenshotConfig("search_one_result.json")],
"statuspage": [ScreenshotConfig("incident_created.json")],

View File

View File

@ -0,0 +1,15 @@
Get Zulip notifications for your Sonarqube code analysis!
1. {!create-stream.md!}
1. {!create-bot-construct-url-indented.md!}
1. To configure webhooks for a specific SonarQube project, go to the project and select **Administration**. Select
**Webhooks** and click **Create**. **Note**: you can also configure webhooks globally by going to **Configurations** ->
**Webhooks** in SonarQube.
1. Set **Name** to a name for the webhook. Set **URL** to the URL constructed above and click **Create**.
{!congrats.md!}
![](/static/images/integrations/sonarqube/001.png)

View File

@ -0,0 +1,46 @@
{
"serverUrl": "http://localhost:9000",
"taskId": "AXgTFfXRZCzhMRNj54bo",
"status": "SUCCESS",
"analysedAt": "2021-03-08T18:25:04+0000",
"changedAt": "2021-03-08T18:25:04+0000",
"project": {
"key": "test-sonar",
"name": "test-sonar",
"url": "http://localhost:9000/dashboard?id=test-sonar"
},
"branch": {
"name": "master",
"type": "BRANCH",
"isMain": true,
"url": "http://localhost:9000/dashboard?id=test-sonar"
},
"qualityGate": {
"name": "Sonar way",
"status": "ERROR",
"conditions": [
{
"metric": "maintainability_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "coverage",
"operator": "LESS_THAN",
"value": "0.0",
"status": "ERROR",
"errorThreshold": "80"
},
{
"metric": "duplicated_lines_density",
"operator": "GREATER_THAN",
"value": "89.39828080229226",
"status": "ERROR",
"errorThreshold": "3"
}
]
},
"properties": {}
}

View File

@ -0,0 +1,40 @@
{
"serverUrl": "http://localhost:9000",
"taskId": "AXgTFfXRZCzhMRNj54bo",
"status": "SUCCESS",
"analysedAt": "2021-03-08T18:25:04+0000",
"changedAt": "2021-03-08T18:25:04+0000",
"project": {
"key": "test-sonar",
"name": "test-sonar",
"url": "http://localhost:9000/dashboard?id=test-sonar"
},
"qualityGate": {
"name": "Sonar way",
"status": "ERROR",
"conditions": [
{
"metric": "maintainability_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "coverage",
"operator": "LESS_THAN",
"value": "0.0",
"status": "ERROR",
"errorThreshold": "80"
},
{
"metric": "duplicated_lines_density",
"operator": "GREATER_THAN",
"value": "89.39828080229226",
"status": "ERROR",
"errorThreshold": "3"
}
]
},
"properties": {}
}

View File

@ -0,0 +1,45 @@
{
"serverUrl": "http://localhost:9000",
"taskId": "AXgTFfXRZCzhMRNj54bo",
"status": "SUCCESS",
"analysedAt": "2021-03-08T18:25:04+0000",
"changedAt": "2021-03-08T18:25:04+0000",
"project": {
"key": "test-sonar",
"name": "test-sonar",
"url": "http://localhost:9000/dashboard?id=test-sonar"
},
"branch": {
"name": "master",
"type": "BRANCH",
"isMain": true,
"url": "http://localhost:9000/dashboard?id=test-sonar"
},
"qualityGate": {
"name": "Sonar way",
"status": "ERROR",
"conditions": [
{
"metric": "maintainability_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "coverage",
"operator": "LESS_THAN",
"value": "0.0",
"status": "ERROR",
"errorThreshold": "80"
},
{
"metric": "duplicated_lines_density",
"operator": "GREATER_THAN",
"status": "ERROR",
"errorThreshold": "3"
}
]
},
"properties": {}
}

View File

@ -0,0 +1,64 @@
{
"serverUrl": "http://localhost:9000",
"taskId": "AXgTFfXRZCzhMRNj54bo",
"status": "SUCCESS",
"analysedAt": "2021-03-08T18:25:04+0000",
"changedAt": "2021-03-08T18:25:04+0000",
"project": {
"key": "test-sonar",
"name": "test-sonar",
"url": "http://localhost:9000/dashboard?id=test-sonar"
},
"branch": {
"name": "master",
"type": "BRANCH",
"isMain": true,
"url": "http://localhost:9000/dashboard?id=test-sonar"
},
"qualityGate": {
"name": "Sonar way",
"status": "OK",
"conditions": [
{
"metric": "new_reliability_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "new_security_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "new_maintainability_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "new_coverage",
"operator": "LESS_THAN",
"status": "NO_VALUE",
"errorThreshold": "80"
},
{
"metric": "new_duplicated_lines_density",
"operator": "GREATER_THAN",
"status": "NO_VALUE",
"errorThreshold": "3"
},
{
"metric": "new_security_hotspots_reviewed",
"operator": "LESS_THAN",
"status": "NO_VALUE",
"errorThreshold": "100"
}
]
},
"properties": {}
}

View File

@ -0,0 +1,58 @@
{
"serverUrl": "http://localhost:9000",
"taskId": "AXgTFfXRZCzhMRNj54bo",
"status": "SUCCESS",
"analysedAt": "2021-03-08T18:25:04+0000",
"changedAt": "2021-03-08T18:25:04+0000",
"project": {
"key": "test-sonar",
"name": "test-sonar",
"url": "http://localhost:9000/dashboard?id=test-sonar"
},
"qualityGate": {
"name": "Sonar way",
"status": "OK",
"conditions": [
{
"metric": "new_reliability_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "new_security_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "new_maintainability_rating",
"operator": "GREATER_THAN",
"value": "1",
"status": "OK",
"errorThreshold": "1"
},
{
"metric": "new_coverage",
"operator": "LESS_THAN",
"status": "NO_VALUE",
"errorThreshold": "80"
},
{
"metric": "new_duplicated_lines_density",
"operator": "GREATER_THAN",
"status": "NO_VALUE",
"errorThreshold": "3"
},
{
"metric": "new_security_hotspots_reviewed",
"operator": "LESS_THAN",
"status": "NO_VALUE",
"errorThreshold": "100"
}
]
},
"properties": {}
}

View File

@ -0,0 +1,84 @@
from zerver.lib.test_classes import WebhookTestCase
class SonarqubeHookTests(WebhookTestCase):
STREAM_NAME = "SonarQube"
URL_TEMPLATE = "/api/v1/external/sonarqube?api_key={api_key}&stream={stream}"
FIXTURE_DIR_NAME = "sonarqube"
WEBHOOK_DIR_NAME = "sonarqube"
def test_analysis_success(self) -> None:
expected_topic = "test-sonar / master"
expected_message = """
Project [test-sonar](http://localhost:9000/dashboard?id=test-sonar) analysis of branch master resulted in success.
""".strip()
self.check_webhook(
"success",
expected_topic,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_analysis_error(self) -> None:
expected_topic = "test-sonar / master"
expected_message = """
Project [test-sonar](http://localhost:9000/dashboard?id=test-sonar) analysis of branch master resulted in error:
* coverage: **error** 0.0 should be greater than or equal to 80.
* duplicated lines density: **error** 89.39828080229226 should be less than or equal to 3.
""".strip()
self.check_webhook(
"error",
expected_topic,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_analysis_error_no_value(self) -> None:
expected_topic = "test-sonar / master"
expected_message = """
Project [test-sonar](http://localhost:9000/dashboard?id=test-sonar) analysis of branch master resulted in error:
* coverage: **error** 0.0 should be greater than or equal to 80.
* duplicated lines density: **error**.
""".strip()
self.check_webhook(
"error_no_value",
expected_topic,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_analysis_success_no_branch(self) -> None:
expected_topic = "test-sonar"
expected_message = """
Project [test-sonar](http://localhost:9000/dashboard?id=test-sonar) analysis resulted in success.
""".strip()
self.check_webhook(
"success_no_branch",
expected_topic,
expected_message,
content_type="application/x-www-form-urlencoded",
)
def test_analysis_error_no_branch(self) -> None:
expected_topic = "test-sonar"
expected_message = """
Project [test-sonar](http://localhost:9000/dashboard?id=test-sonar) analysis resulted in error:
* coverage: **error** 0.0 should be greater than or equal to 80.
* duplicated lines density: **error** 89.39828080229226 should be less than or equal to 3.
""".strip()
self.check_webhook(
"error_no_branch",
expected_topic,
expected_message,
content_type="application/x-www-form-urlencoded",
)

View File

@ -0,0 +1,132 @@
# Webhooks for external integrations.
from typing import Any, Dict, List, Mapping
from django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view
from zerver.lib.request import REQ, has_request_variables
from zerver.lib.response import json_success
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("_"))
def parse_condition(condition: Mapping[str, Any]) -> str:
metric = condition["metric"]
metric_name = parse_metric_name(metric)
operator = condition["operator"]
operator = INVERSE_OPERATORS.get(operator, operator)
value = condition.get("value", "no value")
status = condition["status"].lower()
threshold = condition["errorThreshold"]
if value == "no value":
return TEMPLATES["no_value"].format(metric_name, status)
template = TEMPLATES["default"]
return template.format(metric_name, status, value, operator, threshold)
def parse_conditions(conditions: List[Mapping[str, Any]]) -> str:
return "\n".join(
[
parse_condition(condition)
for condition in conditions
if condition["status"].lower() != "ok" and condition["status"].lower() != "no_value"
]
)
def render_body_with_branch(payload: Mapping[str, Any]) -> str:
project_name = payload["project"]["name"]
project_url = payload["project"]["url"]
quality_gate_status = payload["qualityGate"]["status"].lower()
if quality_gate_status == "ok":
quality_gate_status = "success"
else:
quality_gate_status = "error"
branch = payload["branch"]["name"]
conditions = payload["qualityGate"]["conditions"]
conditions = parse_conditions(conditions)
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
def render_body_without_branch(payload: Mapping[str, Any]) -> str:
project_name = payload["project"]["name"]
project_url = payload["project"]["url"]
quality_gate_status = payload["qualityGate"]["status"].lower()
if quality_gate_status == "ok":
quality_gate_status = "success"
else:
quality_gate_status = "error"
conditions = payload["qualityGate"]["conditions"]
conditions = parse_conditions(conditions)
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")
@has_request_variables
def api_sonarqube_webhook(
request: HttpRequest,
user_profile: UserProfile,
payload: Dict[str, Any] = REQ(argument_type="body"),
) -> HttpResponse:
project = payload["project"]["name"]
branch = None
if "branch" in payload.keys():
branch = payload["branch"].get("name", None)
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)
return json_success()