Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

integrations: Add SonarQube webhook integration.

tushar91283f6557f4349
diff --git a/static/images/integrations/bot_avatars/sonarqube.png b/static/images/integrations/bot_avatars/sonarqube.png
new file mode 100644
index 00000000000..bb0d9e1be7a
Binary files /dev/null and b/static/images/integrations/bot_avatars/sonarqube.png differ
diff --git a/static/images/integrations/logos/sonarqube.svg b/static/images/integrations/logos/sonarqube.svg
new file mode 100644
index 00000000000..b943121b43d
--- /dev/null
+++ b/static/images/integrations/logos/sonarqube.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="51" height="51" xml:space="preserve"><image width="51" height="51" xlink:href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADMAAAAzCAYAAAA6oTAqAAAABGdBTUEAALGPC/xhBQAAACBjSFJN AAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAABmJLR0QA/wD/AP+gvaeTAAAI cklEQVRo3u2aa2xcxRXHfzP33l3vw971Y3dtxyF24sTGgQBRQgMqASFoIxyFoj5QVKmV2lRtWpV+ qNTSfmnaquoDFVGpAoRE1UckEImEUiiICEpwqJoggpo4QBLn4STEdhw79np3vd7HnemHe3e9GzuO U+WxRTnSSnfvjufOb/7nnDkz10JrrfmUmLzeA7gBcwPm/8zMK9GJ1hohBKW5pPS7EKKsbeGeKlxf 0F9p+8sxcSWzmdaa0s5ECdRcA1RaT7edBe6awlxsALZyujakKMKWtv9gIEXAMuiM+sqBLgF/MZsR M1rr6YdqjSr56As+xQG63zN5m6feHeTVQ2OMpHIYUmBIUfz7AnJhsNt6z7Nh62FeOjCKFAIhwFaq CHS58zwjZkpnRAgxy4xrlALlyIoU04MbTuZ54cAI/eNZOhuq+PzSEBu66rgl5gdAKQ1iOl5+tLaJ er/J42+cYs/pJL98sIWAxyBva0yjHHw+NsPNCn4vhWBi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rvieZ1h4HyxbAr 5sNvSQ6dm7rsA/SKgCm8ygBYFPYSq7Y4Hc8wls5fMh1XHAxMvxIJWpJowOLcZJ6RVN4J+HmKU1Ew QgiqTEkkYDKZVYykLk+ZK/Ia8EqYBtAayxDU+kxspUlmbdzb87KKUaawgdOuTw2ncsQzdinqJa1i lJnOWpruzjDRgMXymA9gzlW/rI8b/25yFWy2d6nzKWEqEuZC+19en99ws0q1GzCVap8qmP8CUixg 6Y5XCSIAAAAldEVYdGRhdGU6Y3JlYXRlADIwMjEtMDMtMDhUMjM6MDU6MTErMDM6MDAJFNw3AAAA JXRFWHRkYXRlOm1vZGlmeQAyMDIxLTAzLTA4VDIzOjA1OjExKzAzOjAweElkiwAAABl0RVh0U29m dHdhcmUAZ25vbWUtc2NyZWVuc2hvdO8Dvz4AAAAASUVORK5CYII="/></svg>
\ No newline at end of file
diff --git a/static/images/integrations/sonarqube/001.png b/static/images/integrations/sonarqube/001.png
new file mode 100644
index 00000000000..28b13066426
Binary files /dev/null and b/static/images/integrations/sonarqube/001.png differ
diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py
index ed09408d233..ac63bd609cc 100644
--- a/zerver/lib/integrations.py
+++ b/zerver/lib/integrations.py
@@ -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")],
diff --git a/zerver/webhooks/sonarqube/__init__.py b/zerver/webhooks/sonarqube/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/zerver/webhooks/sonarqube/doc.md b/zerver/webhooks/sonarqube/doc.md
new file mode 100644
index 00000000000..63526ba2e55
--- /dev/null
+++ b/zerver/webhooks/sonarqube/doc.md
@@ -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)
diff --git a/zerver/webhooks/sonarqube/fixtures/error.json b/zerver/webhooks/sonarqube/fixtures/error.json
new file mode 100644
index 00000000000..0a8d6ef5fc2
--- /dev/null
+++ b/zerver/webhooks/sonarqube/fixtures/error.json
@@ -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": {}
+ }
diff --git a/zerver/webhooks/sonarqube/fixtures/error_no_branch.json b/zerver/webhooks/sonarqube/fixtures/error_no_branch.json
new file mode 100644
index 00000000000..7c2099d27c0
--- /dev/null
+++ b/zerver/webhooks/sonarqube/fixtures/error_no_branch.json
@@ -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": {}
+ }
diff --git a/zerver/webhooks/sonarqube/fixtures/error_no_value.json b/zerver/webhooks/sonarqube/fixtures/error_no_value.json
new file mode 100644
index 00000000000..31b981ea45a
--- /dev/null
+++ b/zerver/webhooks/sonarqube/fixtures/error_no_value.json
@@ -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": {}
+ }
diff --git a/zerver/webhooks/sonarqube/fixtures/success.json b/zerver/webhooks/sonarqube/fixtures/success.json
new file mode 100644
index 00000000000..fe69733d637
--- /dev/null
+++ b/zerver/webhooks/sonarqube/fixtures/success.json
@@ -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": {}
+}
diff --git a/zerver/webhooks/sonarqube/fixtures/success_no_branch.json b/zerver/webhooks/sonarqube/fixtures/success_no_branch.json
new file mode 100644
index 00000000000..d9b6a449a18
--- /dev/null
+++ b/zerver/webhooks/sonarqube/fixtures/success_no_branch.json
@@ -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": {}
+ }
diff --git a/zerver/webhooks/sonarqube/tests.py b/zerver/webhooks/sonarqube/tests.py
new file mode 100644
index 00000000000..6a3fe5e4d7a
--- /dev/null
+++ b/zerver/webhooks/sonarqube/tests.py
@@ -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",
+ )
diff --git a/zerver/webhooks/sonarqube/view.py b/zerver/webhooks/sonarqube/view.py
new file mode 100644
index 00000000000..839c0f4d61f
--- /dev/null
+++ b/zerver/webhooks/sonarqube/view.py
@@ -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()