Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
integrations: Add Rundeck webhook integration.
Satyam Bansal338436dfbd33
diff --git a/static/images/integrations/bot_avatars/rundeck.png b/static/images/integrations/bot_avatars/rundeck.pngnew file mode 100644index 00000000000..b9996e045fbBinary files /dev/null and b/static/images/integrations/bot_avatars/rundeck.png differdiff --git a/static/images/integrations/logos/rundeck.svg b/static/images/integrations/logos/rundeck.svgnew file mode 100644index 00000000000..9a361539a9f--- /dev/null+++ b/static/images/integrations/logos/rundeck.svg@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg" width="64" height="64" viewBox="45.359 114.637 60.122 58.576"><path fill="#f91629" d="m46.83 113.864 7.608 12.01H92.5l-7.543-12.01zm15.26 23.98 3.684 5.754-3.968 6.32h38.4l3.815-6.017-3.815-5.907h-38.04zm-7.826 24.13-7.455 11.77v.24h38.148l7.564-12.012z"/></svg>\ No newline at end of filediff --git a/static/images/integrations/rundeck/001.png b/static/images/integrations/rundeck/001.pngnew file mode 100644index 00000000000..4513c028ff3Binary files /dev/null and b/static/images/integrations/rundeck/001.png differdiff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.pyindex 23579e85c58..dfe8244795e 100644--- a/zerver/lib/integrations.py+++ b/zerver/lib/integrations.py@@ -445,6 +445,7 @@ WEBHOOK_INTEGRATIONS: List[WebhookIntegration] = [WebhookIntegration("raygun", ["monitoring"], display_name="Raygun"),WebhookIntegration("reviewboard", ["version-control"], display_name="Review Board"),WebhookIntegration("rhodecode", ["version-control"], display_name="RhodeCode"),+ WebhookIntegration("rundeck", ["deployment"], display_name="Rundeck"),WebhookIntegration("semaphore", ["continuous-integration", "deployment"]),WebhookIntegration("sentry", ["monitoring"]),WebhookIntegration(@@ -789,6 +790,7 @@ DOC_SCREENSHOT_CONFIG: Dict[str, List[BaseScreenshotConfig]] = {"raygun": [ScreenshotConfig("new_error.json")],"reviewboard": [ScreenshotConfig("review_request_published.json")],"rhodecode": [ScreenshotConfig("push.json")],+ "rundeck": [ScreenshotConfig("start.json")],"semaphore": [ScreenshotConfig("pull_request.json")],"sentry": [ScreenshotConfig("event_for_exception_python.json"),diff --git a/zerver/webhooks/rundeck/__init__.py b/zerver/webhooks/rundeck/__init__.pynew file mode 100644index 00000000000..e69de29bb2ddiff --git a/zerver/webhooks/rundeck/doc.md b/zerver/webhooks/rundeck/doc.mdnew file mode 100644index 00000000000..a3808397be7--- /dev/null+++ b/zerver/webhooks/rundeck/doc.md@@ -0,0 +1,18 @@+Receive Job Notifications in Zulip!++1. {!create-stream.md!}++1. {!create-bot-construct-url.md!}++1. Go to your Rundeck web interface and click on the desired job.+ Click on **Actions** and then select **Edit this Job...**.+ Go to the **Notifications** tab.++1. Next to the desired event, click **Add Notification**. Select+ **Send Webhook** as the Notification Type. Enter the URL constructed+ above. Ensure payload format is **JSON** and Method is **POST**.+ Click **Save**.++{!congrats.md!}++diff --git a/zerver/webhooks/rundeck/fixtures/duration.json b/zerver/webhooks/rundeck/fixtures/duration.jsonnew file mode 100644index 00000000000..fe3939e6c18--- /dev/null+++ b/zerver/webhooks/rundeck/fixtures/duration.json@@ -0,0 +1,31 @@+{+ "trigger": "avgduration",+ "status": "running",+ "executionId": 6,+ "execution": {+ "id": 6,+ "href": "http://localhost:4440/project/welcome-project-community/execution/show/6",+ "permalink": null,+ "status": "running",+ "project": "welcome-project-community",+ "executionType": "user",+ "user": "admin",+ "date-started": {+ "unixtime": 1680848069313,+ "date": "2023-04-07T06:14:29Z"+ },+ "job": {+ "id": "a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "averageDuration": 2408,+ "name": "Global Log Filter Usage",+ "group": "Basic Examples/Basic Workflows",+ "project": "welcome-project-community",+ "description": "Global Log Filter basic example.\r\n\r\nGlobal Log Filter allows you to capture information from the whole job. This example shows how to capture env command data and use it later in the next step as [key-value](https://docs.rundeck.com/docs/manual/log-filters/key-value-data.html#key-value-data) data.\r\n\r\nMore information [here](https://docs.rundeck.com/docs/manual/log-filters/#log-filters).",+ "href": "http://localhost:4440/api/42/job/a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "permalink": "http://localhost:4440/project/welcome-project-community/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85"+ },+ "description": "env ('Using env command we can extract a lot of keys/values :-)') [... 3 steps]",+ "argstring": null,+ "serverUUID": "a14bc3e6-75e8-4fe4-a90d-a16dcc976bf6"+ }+}diff --git a/zerver/webhooks/rundeck/fixtures/failure.json b/zerver/webhooks/rundeck/fixtures/failure.jsonnew file mode 100644index 00000000000..3f3a432d57a--- /dev/null+++ b/zerver/webhooks/rundeck/fixtures/failure.json@@ -0,0 +1,31 @@+{+ "trigger": "failure",+ "status": "failed",+ "executionId": 7,+ "execution": {+ "id": 7,+ "href": "http://localhost:4440/project/welcome-project-community/execution/show/7",+ "permalink": null,+ "status": "failed",+ "project": "welcome-project-community",+ "executionType": "user",+ "user": "admin",+ "date-started": {+ "unixtime": 1680848123396,+ "date": "2023-04-07T06:15:23Z"+ },+ "job": {+ "id": "a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "averageDuration": 2659,+ "name": "Global Log Filter Usage",+ "group": "Basic Examples/Basic Workflows",+ "project": "welcome-project-community",+ "description": "Global Log Filter basic example.\r\n\r\nGlobal Log Filter allows you to capture information from the whole job. This example shows how to capture env command data and use it later in the next step as [key-value](https://docs.rundeck.com/docs/manual/log-filters/key-value-data.html#key-value-data) data.\r\n\r\nMore information [here](https://docs.rundeck.com/docs/manual/log-filters/#log-filters).",+ "href": "http://localhost:4440/api/42/job/a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "permalink": "http://localhost:4440/project/welcome-project-community/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85"+ },+ "description": "env ('Using env command we can extract a lot of keys/values :-)') [... 4 steps]",+ "argstring": null,+ "serverUUID": "a14bc3e6-75e8-4fe4-a90d-a16dcc976bf6"+ }+}diff --git a/zerver/webhooks/rundeck/fixtures/scheduled_start.json b/zerver/webhooks/rundeck/fixtures/scheduled_start.jsonnew file mode 100644index 00000000000..f6d29e361c4--- /dev/null+++ b/zerver/webhooks/rundeck/fixtures/scheduled_start.json@@ -0,0 +1,23 @@+{+ "trigger": "start",+ "status": "scheduled",+ "executionId": 12,+ "execution": {+ "id": 12,+ "href": "https://rundeck.com/project/myproject/execution/follow/12",+ "permalink": null,+ "status": "scheduled",+ "project": "myproject",+ "executionType": "user",+ "user": "guest",+ "job": {+ "id": "a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "name": "Global Log Filter Usage",+ "group": "Basic Examples/Basic Workflows",+ "project": "MyProject",+ "description": "Global Log Filter basic example. \n\nMore information: https://docs.rundeck.com/docs/manual/log-filters/#log-filters",+ "href": "https://rundeck.com/api/1/job/a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "permalink": "https://rundeck.com/project/myproject/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85"+ }+ }+ }diff --git a/zerver/webhooks/rundeck/fixtures/start.json b/zerver/webhooks/rundeck/fixtures/start.jsonnew file mode 100644index 00000000000..9899d1811d6--- /dev/null+++ b/zerver/webhooks/rundeck/fixtures/start.json@@ -0,0 +1,31 @@+{+ "trigger": "start",+ "status": "running",+ "executionId": 3,+ "execution": {+ "id": 3,+ "href": "http://localhost:4440/project/welcome-project-community/execution/show/3",+ "permalink": null,+ "status": "running",+ "project": "welcome-project-community",+ "executionType": "user",+ "user": "admin",+ "date-started": {+ "unixtime": 1680847933368,+ "date": "2023-04-07T06:12:13Z"+ },+ "job": {+ "id": "a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "averageDuration": 2354,+ "name": "Global Log Filter Usage",+ "group": "Basic Examples/Basic Workflows",+ "project": "welcome-project-community",+ "description": "Global Log Filter basic example.\r\n\r\nGlobal Log Filter allows you to capture information from the whole job. This example shows how to capture env command data and use it later in the next step as [key-value](https://docs.rundeck.com/docs/manual/log-filters/key-value-data.html#key-value-data) data.\r\n\r\nMore information [here](https://docs.rundeck.com/docs/manual/log-filters/#log-filters).",+ "href": "http://localhost:4440/api/42/job/a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "permalink": "http://localhost:4440/project/welcome-project-community/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85"+ },+ "description": "env ('Using env command we can extract a lot of keys/values :-)') [... 2 steps]",+ "argstring": null,+ "serverUUID": "a14bc3e6-75e8-4fe4-a90d-a16dcc976bf6"+ }+}diff --git a/zerver/webhooks/rundeck/fixtures/success.json b/zerver/webhooks/rundeck/fixtures/success.jsonnew file mode 100644index 00000000000..88ef85fb2d7--- /dev/null+++ b/zerver/webhooks/rundeck/fixtures/success.json@@ -0,0 +1,31 @@+{+ "trigger": "success",+ "status": "succeeded",+ "executionId": 3,+ "execution": {+ "id": 3,+ "href": "http://localhost:4440/project/welcome-project-community/execution/show/3",+ "permalink": null,+ "status": "succeeded",+ "project": "welcome-project-community",+ "executionType": "user",+ "user": "admin",+ "date-started": {+ "unixtime": 1680847933368,+ "date": "2023-04-07T06:12:13Z"+ },+ "job": {+ "id": "a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "averageDuration": 2354,+ "name": "Global Log Filter Usage",+ "group": "Basic Examples/Basic Workflows",+ "project": "welcome-project-community",+ "description": "Global Log Filter basic example.\r\n\r\nGlobal Log Filter allows you to capture information from the whole job. This example shows how to capture env command data and use it later in the next step as [key-value](https://docs.rundeck.com/docs/manual/log-filters/key-value-data.html#key-value-data) data.\r\n\r\nMore information [here](https://docs.rundeck.com/docs/manual/log-filters/#log-filters).",+ "href": "http://localhost:4440/api/42/job/a0296d93-4b10-48d7-8b7d-86ad3f603b85",+ "permalink": "http://localhost:4440/project/welcome-project-community/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85"+ },+ "description": "env ('Using env command we can extract a lot of keys/values :-)') [... 2 steps]",+ "argstring": null,+ "serverUUID": "a14bc3e6-75e8-4fe4-a90d-a16dcc976bf6"+ }+}diff --git a/zerver/webhooks/rundeck/tests.py b/zerver/webhooks/rundeck/tests.pynew file mode 100644index 00000000000..43ab9680a7a--- /dev/null+++ b/zerver/webhooks/rundeck/tests.py@@ -0,0 +1,58 @@+from zerver.lib.test_classes import WebhookTestCase+++class RundeckHookTests(WebhookTestCase):+ STREAM_NAME = "Rundeck"+ TOPIC_NAME = "Global Log Filter Usage"+ URL_TEMPLATE = "/api/v1/external/rundeck?&api_key={api_key}&stream={stream}"+ WEBHOOK_DIR_NAME = "rundeck"++ def test_start_message(self) -> None:+ expected_message = "[Global Log Filter Usage](http://localhost:4440/project/welcome-project-community/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85) execution [#3](http://localhost:4440/project/welcome-project-community/execution/show/3) for welcome-project-community has started. :running:"++ self.check_webhook(+ "start",+ RundeckHookTests.TOPIC_NAME,+ expected_message,+ content_type="application/x-www-form-urlencoded",+ )++ def test_success_message(self) -> None:+ expected_message = "[Global Log Filter Usage](http://localhost:4440/project/welcome-project-community/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85) execution [#3](http://localhost:4440/project/welcome-project-community/execution/show/3) for welcome-project-community has succeeded. :check:"++ self.check_webhook(+ "success",+ RundeckHookTests.TOPIC_NAME,+ expected_message,+ content_type="application/x-www-form-urlencoded",+ )++ def test_failure_message(self) -> None:+ expected_message = "[Global Log Filter Usage](http://localhost:4440/project/welcome-project-community/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85) execution [#7](http://localhost:4440/project/welcome-project-community/execution/show/7) for welcome-project-community has failed. :cross_mark:"++ self.check_webhook(+ "failure",+ RundeckHookTests.TOPIC_NAME,+ expected_message,+ content_type="application/x-www-form-urlencoded",+ )++ def test_duration_message(self) -> None:+ expected_message = "[Global Log Filter Usage](http://localhost:4440/project/welcome-project-community/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85) execution [#6](http://localhost:4440/project/welcome-project-community/execution/show/6) for welcome-project-community is running long. :time_ticking:"++ self.check_webhook(+ "duration",+ RundeckHookTests.TOPIC_NAME,+ expected_message,+ content_type="application/x-www-form-urlencoded",+ )++ def test_scheduled_start_message(self) -> None:+ expected_message = "[Global Log Filter Usage](https://rundeck.com/project/myproject/job/show/a0296d93-4b10-48d7-8b7d-86ad3f603b85) execution [#12](https://rundeck.com/project/myproject/execution/follow/12) for myproject has started. :running:"++ self.check_webhook(+ "scheduled_start",+ RundeckHookTests.TOPIC_NAME,+ expected_message,+ content_type="application/x-www-form-urlencoded",+ )diff --git a/zerver/webhooks/rundeck/view.py b/zerver/webhooks/rundeck/view.pynew file mode 100644index 00000000000..a9b0c1d58f5--- /dev/null+++ b/zerver/webhooks/rundeck/view.py@@ -0,0 +1,65 @@+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.validator import WildValue, check_int, check_string, to_wild_value+from zerver.lib.webhooks.common import check_send_webhook_message+from zerver.models import UserProfile++RUNDECK_MESSAGE_TEMPLATE = "[{job_name}]({job_link}) execution [#{execution_id}]({execution_link}) for {project_name} {status}. :{emoji}:"+RUNDECK_TOPIC_TEMPLATE = "{job_name}"+++@webhook_view("Rundeck")+@has_request_variables+def api_rundeck_webhook(+ request: HttpRequest,+ user_profile: UserProfile,+ payload: WildValue = REQ(argument_type="body", converter=to_wild_value),+) -> HttpResponse:+ subject = get_topic(payload)+ body = get_body(payload)++ check_send_webhook_message(request, user_profile, subject, body)+ return json_success(request)+++def get_topic(payload: WildValue) -> str:+ return RUNDECK_TOPIC_TEMPLATE.format(+ job_name=payload["execution"]["job"]["name"].tame(check_string)+ )+++def get_body(payload: WildValue) -> str:+ message_data = {+ "job_name": payload["execution"]["job"]["name"].tame(check_string),+ "job_link": payload["execution"]["job"]["permalink"].tame(check_string),+ "execution_id": payload["execution"]["id"].tame(check_int),+ "execution_link": payload["execution"]["href"].tame(check_string),+ "project_name": payload["execution"]["project"].tame(check_string),+ "status": payload["execution"]["status"].tame(check_string),+ }+ status = payload["execution"]["status"].tame(check_string)++ if status == "failed":+ message_data["status"] = "has failed"+ message_data["emoji"] = "cross_mark"++ if status == "succeeded":+ message_data["status"] = "has succeeded"+ message_data["emoji"] = "check"++ if status == "running":+ if payload["trigger"].tame(check_string) == "avgduration":+ message_data["status"] = "is running long"+ message_data["emoji"] = "time_ticking"+ else:+ message_data["status"] = "has started"+ message_data["emoji"] = "running"++ if status == "scheduled":+ message_data["status"] = "has started"+ message_data["emoji"] = "running"++ return RUNDECK_MESSAGE_TEMPLATE.format(**message_data)