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 Redmine webhook integration.

harsh_0303c5b9285a89d9
diff --git a/static/images/integrations/redmine/001.png b/static/images/integrations/redmine/001.png
index 612a53a3332..bef74a38c31 100644
Binary files a/static/images/integrations/redmine/001.png and b/static/images/integrations/redmine/001.png differ
diff --git a/templates/zerver/integrations/redmine.md b/templates/zerver/integrations/redmine.md
deleted file mode 100644
index 2d1f712ae1e..00000000000
--- a/templates/zerver/integrations/redmine.md
+++ /dev/null
@@ -1,59 +0,0 @@
-Get information on new or updated Redmine issues right in
-Zulip with our Zulip Redmine plugin!
-
-_Note: this setup must be done by a Redmine Administrator._
-
-### Installing
-
-Following the [Redmine plugin installation guide][1]:
-
-1. Start by changing to the Redmine instance root directory:
- `cd /path/to/redmine/instance`
-
-1. Clone the [Zulip Redmine plugin repository][2] into the `plugins` subdirectory
- of your Redmine instance.
- `git clone https://github.com/zulip/zulip-redmine-plugin plugins/redmine_zulip`
-
-1. Update the Redmine database by running (for Rake 2.X, see
- the guide for instructions for older versions):
- `rake redmine:plugins:migrate RAILS_ENV=production`
-
-1. Restart your Redmine instance.
-
-The Zulip plugin is now registered with Redmine!
-
-### Global settings
-
-1. {!create-an-incoming-webhook.md!}
-
-2. Log in to your Redmine instance, click on **Administration** in the top-left
-corner, then click on **Plugins**.
-
-3. Find the **Redmine Zulip** plugin, and click **Configure**. Fill
-out the following fields:
-
- * Zulip URL (e.g `{{ zulip_url }}`)
- * Zulip Bot E-mail
- * Zulip Bot API key
- * Stream name __*__
- * Issue updates subject __*__
- * Version updates subject __*__
-
- _* You can use the following variables in these fields:_
-
- * ${issue_id}
- * ${issue_subject}
- * ${project_name}
- * ${version_name}
-
-### Project settings
-
-To override the global settings for a specific project, go to the
-project's **Settings** page, and select the **Zulip** tab.
-
-{!congrats.md!}
-
-![Redmine bot message](/static/images/integrations/redmine/001.png)
-
-[1]: https://www.redmine.org/projects/redmine/wiki/Plugins
-[2]: https://github.com/zulip/zulip-redmine-plugin
diff --git a/zerver/lib/integrations.py b/zerver/lib/integrations.py
index 984043ecb8d..063dd60d03b 100644
--- a/zerver/lib/integrations.py
+++ b/zerver/lib/integrations.py
@@ -82,7 +82,6 @@ FIXTURELESS_INTEGRATIONS_WITH_SCREENSHOTS: list[str] = [
"openshift",
"perforce",
"puppet",
- "redmine",
"rss",
"svn",
"trac",
@@ -857,6 +856,9 @@ INCOMING_WEBHOOK_INTEGRATIONS: list[IncomingWebhookIntegration] = [
IncomingWebhookIntegration(
"raygun", ["monitoring"], [WebhookScreenshotConfig("new_error.json")]
),
+ IncomingWebhookIntegration(
+ "redmine", ["project-management"], [WebhookScreenshotConfig("issue_opened.json")]
+ ),
IncomingWebhookIntegration(
"reviewboard",
["productivity"],
@@ -1029,7 +1031,6 @@ STANDALONE_REPO_INTEGRATIONS: list[Integration] = [
),
Integration("hubot", ["meta-integration", "bots"]),
Integration("puppet", ["deployment"]),
- Integration("redmine", ["project-management"]),
]
ZULIP_SEND_INTEGRATIONS: list[Integration] = [
diff --git a/zerver/webhooks/fixtureless_integrations.py b/zerver/webhooks/fixtureless_integrations.py
index 33699042fcd..196508b1d9d 100644
--- a/zerver/webhooks/fixtureless_integrations.py
+++ b/zerver/webhooks/fixtureless_integrations.py
@@ -168,22 +168,6 @@ PUPPET = ScreenshotContent(
content=f"""Puppet production run for web-server-01 completed at {DATETIME_GLOBAL}. [GitHub Gist]() | [Report URL]()""",
)
-REDMINE = ScreenshotContent(
- topic=TASK_TITLE,
- content=f"""{BO_NAME} **created** issue [{TICKET_NUMBER} {TASK_TITLE}]():
-
-~~~quote
-
-{TASK_DESCRIPTION}...
-
-~~~
-
-* **Assignee**: {KEVIN_NAME}
-* **Status**: New
-* **Target version**: {VERSION_NUMBER[1:]}
-* **Estimated hours**: 40""",
-)
-
RSS = MASTODON
SVN = ScreenshotContent(
diff --git a/zerver/webhooks/redmine/__init__.py b/zerver/webhooks/redmine/__init__.py
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/zerver/webhooks/redmine/doc.md b/zerver/webhooks/redmine/doc.md
new file mode 100644
index 00000000000..757a9719a45
--- /dev/null
+++ b/zerver/webhooks/redmine/doc.md
@@ -0,0 +1,39 @@
+# Zulip Redmine integration
+
+Get notifications for Redmine issues in Zulip!
+
+{start_tabs}
+
+1. {!create-an-incoming-webhook.md!}
+
+1. {!generate-webhook-url-basic.md!}
+
+1. Install the [`redmine_webhook` plugin](https://github.com/suer/redmine_webhook)
+ on your Redmine server.
+
+1. In your Redmine installation, go to **Projects** and select the project
+ you want to receive notifications for.
+
+1. From the project page, enable the **Webhooks** checkbox to
+ activate webhook functionality. Click on **Settings**, and navigate to
+ the **WebHook** tab.
+
+1. Add the URL generated above, select the
+ [events](#filtering-incoming-events) you would like to receive
+ notifications for, and save your webhook configuration.
+
+{end_tabs}
+
+{!congrats.md!}
+
+![](/static/images/integrations/redmine/001.png)
+
+{!event-filtering-additional-feature.md!}
+
+### Related documentation
+
+- [`redmine_webhook` plugin](https://github.com/suer/redmine_webhook)
+
+- [Redmine's plugin installation guide](https://www.redmine.org/projects/redmine/wiki/Plugins)
+
+{!webhooks-url-specification.md!}
diff --git a/zerver/webhooks/redmine/fixtures/issue_opened.json b/zerver/webhooks/redmine/fixtures/issue_opened.json
new file mode 100644
index 00000000000..97170bde403
--- /dev/null
+++ b/zerver/webhooks/redmine/fixtures/issue_opened.json
@@ -0,0 +1,60 @@
+{
+ "payload": {
+ "issue": {
+ "author": {
+ "icon_url": "http://www.gravatar.com/avatar/example",
+ "identity_url": null,
+ "lastname": "user",
+ "firstname": "test",
+ "mail": "[email protected]",
+ "login": "test",
+ "id": 3
+ },
+ "assignee": {
+ "icon_url": "http://www.gravatar.com/avatar/example",
+ "identity_url": null,
+ "lastname": "user",
+ "firstname": "test",
+ "mail": "[email protected]",
+ "login": "test",
+ "id": 3
+ },
+ "priority": {
+ "name": "normal",
+ "id": 2
+ },
+ "tracker": {
+ "name": "bug",
+ "id": 1
+ },
+ "parent_id": null,
+ "root_id": 191,
+ "closed_on": null,
+ "updated_on": "2014-03-01T15:17:48Z",
+ "created_on": "2014-03-01T15:17:48Z",
+ "description": "I'm having a problem with this.",
+ "subject": "Found a bug",
+ "id": 191,
+ "done_ratio": 0,
+ "start_date": "2014-03-02",
+ "due_date": null,
+ "estimated_hours": null,
+ "is_private": false,
+ "lock_version": 0,
+ "project": {
+ "homepage": "",
+ "created_on": "2013-01-12T11:50:26Z",
+ "description": "",
+ "name": "Test Project",
+ "identifier": "test",
+ "id": 4
+ },
+ "status": {
+ "name": "new",
+ "id": 1
+ }
+ },
+ "action": "opened",
+ "url": "https://example.com"
+ }
+}
diff --git a/zerver/webhooks/redmine/fixtures/issue_opened_without_assignee.json b/zerver/webhooks/redmine/fixtures/issue_opened_without_assignee.json
new file mode 100644
index 00000000000..831c972e41a
--- /dev/null
+++ b/zerver/webhooks/redmine/fixtures/issue_opened_without_assignee.json
@@ -0,0 +1,52 @@
+{
+ "payload": {
+ "issue": {
+ "author": {
+ "icon_url": "http://www.gravatar.com/avatar/example",
+ "identity_url": null,
+ "lastname": "user",
+ "firstname": "test",
+ "mail": "[email protected]",
+ "login": "test",
+ "id": 3
+ },
+ "assignee": null,
+ "priority": {
+ "name": "normal",
+ "id": 2
+ },
+ "tracker": {
+ "name": "bug",
+ "id": 1
+ },
+ "parent_id": null,
+ "root_id": 191,
+ "closed_on": null,
+ "updated_on": "2014-03-01T15:17:48Z",
+ "created_on": "2014-03-01T15:17:48Z",
+ "description": "I'm having a problem with this.",
+ "subject": "Found a bug",
+ "id": 191,
+ "done_ratio": 0,
+ "start_date": "2014-03-02",
+ "due_date": null,
+ "estimated_hours": null,
+ "is_private": false,
+ "lock_version": 0,
+ "project": {
+ "homepage": "",
+ "created_on": "2013-01-12T11:50:26Z",
+ "description": "",
+ "name": "Test Project",
+ "identifier": "test",
+ "id": 4
+ },
+ "status": {
+ "name": "new",
+ "id": 1
+ }
+ },
+ "action": "opened",
+ "url": "https://example.com"
+ }
+}
diff --git a/zerver/webhooks/redmine/fixtures/issue_updated.json b/zerver/webhooks/redmine/fixtures/issue_updated.json
new file mode 100644
index 00000000000..8768a00c6b1
--- /dev/null
+++ b/zerver/webhooks/redmine/fixtures/issue_updated.json
@@ -0,0 +1,74 @@
+{
+ "payload": {
+ "issue": {
+ "author": {
+ "icon_url": "http://www.gravatar.com/avatar/example",
+ "identity_url": null,
+ "lastname": "user",
+ "firstname": "test",
+ "mail": "[email protected]",
+ "login": "test",
+ "id": 3
+ },
+ "assignee": {
+ "icon_url": "http://www.gravatar.com/avatar/example",
+ "identity_url": null,
+ "lastname": "user",
+ "firstname": "test",
+ "mail": "[email protected]",
+ "login": "test",
+ "id": 3
+ },
+ "priority": {
+ "name": "normal",
+ "id": 2
+ },
+ "tracker": {
+ "name": "bug",
+ "id": 1
+ },
+ "parent_id": null,
+ "root_id": 191,
+ "closed_on": null,
+ "updated_on": "2014-03-01T16:17:48Z",
+ "created_on": "2014-03-01T15:17:48Z",
+ "description": "I'm having a problem with this.",
+ "subject": "Found a bug",
+ "id": 191,
+ "done_ratio": 0,
+ "start_date": "2014-03-02",
+ "due_date": null,
+ "estimated_hours": null,
+ "is_private": false,
+ "lock_version": 1,
+ "project": {
+ "homepage": "",
+ "created_on": "2013-01-12T11:50:26Z",
+ "description": "",
+ "name": "Test Project",
+ "identifier": "test",
+ "id": 4
+ },
+ "status": {
+ "name": "in progress",
+ "id": 2
+ }
+ },
+ "journal": {
+ "notes": "I've started working on this issue. The problem seems to be in the authentication module.",
+ "author": {
+ "icon_url": "http://www.gravatar.com/avatar/example",
+ "identity_url": null,
+ "lastname": "user",
+ "firstname": "test",
+ "mail": "[email protected]",
+ "login": "test",
+ "id": 3
+ },
+ "created_on": "2014-03-01T16:17:48Z",
+ "id": 1
+ },
+ "action": "updated",
+ "url": "https://example.com"
+ }
+}
diff --git a/zerver/webhooks/redmine/tests.py b/zerver/webhooks/redmine/tests.py
new file mode 100644
index 00000000000..e793742be25
--- /dev/null
+++ b/zerver/webhooks/redmine/tests.py
@@ -0,0 +1,32 @@
+from zerver.lib.test_classes import WebhookTestCase
+
+
+class RedmineHookTests(WebhookTestCase):
+ CHANNEL_NAME = "redmine"
+ URL_TEMPLATE = "/api/v1/external/redmine?&api_key={api_key}&stream={stream}"
+ WEBHOOK_DIR_NAME = "redmine"
+ TOPIC_NAME = "Issue #191 Found a bug"
+
+ def test_issue_opened(self) -> None:
+ expected_message = """**test user** opened [#191 Found a bug](https://example.com) for **test user**.
+
+~~~ quote
+I'm having a problem with this.
+~~~"""
+ self.check_webhook("issue_opened", self.TOPIC_NAME, expected_message)
+
+ def test_issue_opened_without_assignee(self) -> None:
+ expected_message = """**test user** opened [#191 Found a bug](https://example.com).
+
+~~~ quote
+I'm having a problem with this.
+~~~"""
+ self.check_webhook("issue_opened_without_assignee", self.TOPIC_NAME, expected_message)
+
+ def test_issue_updated(self) -> None:
+ expected_message = """**test user** updated [#191 Found a bug](https://example.com).
+
+~~~ quote
+I've started working on this issue. The problem seems to be in the authentication module.
+~~~"""
+ self.check_webhook("issue_updated", self.TOPIC_NAME, expected_message)
diff --git a/zerver/webhooks/redmine/view.py b/zerver/webhooks/redmine/view.py
new file mode 100644
index 00000000000..9c96510cf64
--- /dev/null
+++ b/zerver/webhooks/redmine/view.py
@@ -0,0 +1,113 @@
+from django.http import HttpRequest, HttpResponse
+
+from zerver.decorator import webhook_view
+from zerver.lib.exceptions import UnsupportedWebhookEventTypeError
+from zerver.lib.response import json_success
+from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
+from zerver.lib.validator import WildValue, check_int, check_string
+from zerver.lib.webhooks.common import check_send_webhook_message
+from zerver.models import UserProfile
+
+ISSUE_OPENED_MESSAGE_TEMPLATE = (
+ "**{author_name}** opened {issue_link}{assignee_info}.{issue_description}"
+)
+
+ISSUE_UPDATED_MESSAGE_TEMPLATE = "**{author_name}** updated {issue_link}.{journal_notes}"
+
+CONTENT_MESSAGE_TEMPLATE = "\n\n~~~ quote\n{message}\n~~~"
+
+
+def _extract_issue_label(issue: WildValue) -> str:
+ id = issue["id"].tame(check_int)
+ subject = issue["subject"].tame(check_string)
+ return f"#{id} {subject}"
+
+
+def get_issue_topic(payload: WildValue) -> str:
+ issue_label = _extract_issue_label(payload["issue"])
+ return f"Issue {issue_label}"
+
+
+def _get_issue_link(payload: WildValue) -> str:
+ url = payload["url"].tame(check_string)
+ issue = payload["issue"]
+ issue_label = _extract_issue_label(issue)
+ return f"[{issue_label}]({url})"
+
+
+def _get_user_name(user: WildValue) -> str:
+ return f"{user['firstname'].tame(check_string)} {user['lastname'].tame(check_string)}"
+
+
+def _get_assignee_string(issue: WildValue) -> str:
+ if assignee := issue.get("assignee"):
+ assignee_name = _get_user_name(assignee)
+ return f" for **{assignee_name}**"
+ return ""
+
+
+def handle_issue_opened(payload: WildValue) -> str:
+ issue = payload["issue"]
+ author_name = _get_user_name(issue["author"])
+ issue_link = _get_issue_link(payload)
+ assignee_info = _get_assignee_string(issue)
+
+ issue_description = ""
+ if issue.get("description") and (
+ description := issue["description"].tame(check_string).strip()
+ ):
+ issue_description = CONTENT_MESSAGE_TEMPLATE.format(message=description)
+
+ return ISSUE_OPENED_MESSAGE_TEMPLATE.format(
+ author_name=author_name,
+ issue_link=issue_link,
+ assignee_info=assignee_info,
+ issue_description=issue_description,
+ )
+
+
+def handle_issue_updated(payload: WildValue) -> str:
+ issue = payload["issue"]
+ author_name = _get_user_name(issue["author"])
+ issue_link = _get_issue_link(payload)
+
+ journal_notes = ""
+ if (
+ (journal := payload.get("journal"))
+ and (notes := journal.get("notes"))
+ and (tamed_notes := notes.tame(check_string).strip())
+ ):
+ journal_notes = CONTENT_MESSAGE_TEMPLATE.format(message=tamed_notes)
+
+ return ISSUE_UPDATED_MESSAGE_TEMPLATE.format(
+ author_name=author_name,
+ issue_link=issue_link,
+ journal_notes=journal_notes,
+ )
+
+
+REDMINE_EVENT_FUNCTION_MAPPER = {
+ "opened": handle_issue_opened,
+ "updated": handle_issue_updated,
+}
+
+ALL_EVENT_TYPES = list(REDMINE_EVENT_FUNCTION_MAPPER.keys())
+
+
+@webhook_view("Redmine", notify_bot_owner_on_invalid_json=True, all_event_types=ALL_EVENT_TYPES)
+@typed_endpoint
+def api_redmine_webhook(
+ request: HttpRequest, user_profile: UserProfile, *, payload: JsonBodyPayload[WildValue]
+) -> HttpResponse:
+ redmine_payload = payload["payload"]
+ event = redmine_payload["action"].tame(check_string)
+
+ if event not in REDMINE_EVENT_FUNCTION_MAPPER:
+ raise UnsupportedWebhookEventTypeError(event)
+
+ topic_name = get_issue_topic(redmine_payload)
+ content_func = REDMINE_EVENT_FUNCTION_MAPPER[event]
+ content = content_func(redmine_payload)
+ check_send_webhook_message(request, user_profile, topic_name, content)
+
+ return json_success(request)