Skip to content

A platform for community discussion. Free, open, simple.

FIX: Allow selecting tags when creating or editing webhooks (#37942)

Natalie Tay69af7d62ae87
diff --git a/app/controllers/admin/web_hooks_controller.rb b/app/controllers/admin/web_hooks_controller.rb
index b834b44798b..41d3d6e7b22 100644
--- a/app/controllers/admin/web_hooks_controller.rb
+++ b/app/controllers/admin/web_hooks_controller.rb
@@ -179,18 +179,33 @@ class Admin::WebHooksController < Admin::AdminController
private
def web_hook_params
- params.require(:web_hook).permit(
- :payload_url,
- :content_type,
- :secret,
- :wildcard_web_hook,
- :active,
- :verify_certificate,
- web_hook_event_type_ids: [],
- group_ids: [],
- tag_names: [],
- category_ids: [],
- )
+ permitted =
+ params.require(:web_hook).permit(
+ :payload_url,
+ :content_type,
+ :secret,
+ :wildcard_web_hook,
+ :active,
+ :verify_certificate,
+ web_hook_event_type_ids: [],
+ group_ids: [],
+ tag_names: [],
+ tag_ids: [],
+ category_ids: [],
+ )
+
+ if permitted[:tag_names].present?
+ Discourse.deprecate(
+ "The `tag_names` param for webhooks is deprecated, use `tag_ids` instead.",
+ since: "3.5.0.beta1",
+ drop_from: "3.6.0.beta1",
+ )
+ permitted.delete(:tag_ids)
+ else
+ permitted.delete(:tag_names)
+ end
+
+ permitted
end
def fetch_web_hook
diff --git a/app/models/web_hook.rb b/app/models/web_hook.rb
index d9b9c27181e..caa24b698ee 100644
--- a/app/models/web_hook.rb
+++ b/app/models/web_hook.rb
@@ -25,6 +25,10 @@ class WebHook < ActiveRecord::Base
DiscourseTagging.add_or_create_tags_by_name(self, tag_names_arg, unlimited: true)
end
+ def tag_ids=(ids)
+ self.tags = Tag.where(id: ids).to_a
+ end
+
def self.content_types
@content_types ||= Enum.new("application/json" => 1, "application/x-www-form-urlencoded" => 2)
end
diff --git a/app/serializers/admin_web_hook_serializer.rb b/app/serializers/admin_web_hook_serializer.rb
index e853028a6f2..95b3e58e80f 100644
--- a/app/serializers/admin_web_hook_serializer.rb
+++ b/app/serializers/admin_web_hook_serializer.rb
@@ -11,12 +11,7 @@ class AdminWebHookSerializer < ApplicationSerializer
:active
has_many :categories, serializer: BasicCategorySerializer, embed: :ids, include: true
- has_many :tags,
- key: :tag_names,
- serializer: TagSerializer,
- embed: :ids,
- embed_key: :name,
- include: false
+ has_many :tags, serializer: TagSerializer, embed: :objects
has_many :groups, serializer: BasicGroupSerializer, embed: :ids, include: false
has_many :web_hook_event_types,
serializer: WebHookEventTypeSerializer,
diff --git a/frontend/discourse/admin/components/admin-config-areas/webhooks-form.gjs b/frontend/discourse/admin/components/admin-config-areas/webhooks-form.gjs
index 7339f63951f..6ba98b8feca 100644
--- a/frontend/discourse/admin/components/admin-config-areas/webhooks-form.gjs
+++ b/frontend/discourse/admin/components/admin-config-areas/webhooks-form.gjs
@@ -46,7 +46,7 @@ export default class AdminConfigAreasWebhookForm extends Component {
secret: this.webhook.secret,
categories: this.webhook.categories,
group_names: this.webhook.group_names,
- tag_names: this.webhook.tag_names,
+ tags: this.webhook.tags,
wildcard: this.webhook.wildcard,
web_hook_event_types: this.webhook.web_hook_event_types,
verify_certificate: this.webhook.verify_certificate,
@@ -222,7 +222,7 @@ export default class AdminConfigAreasWebhookForm extends Component {
{{#if this.showTagsFilter}}
<form.Field
- @name="tag_names"
+ @name="tags"
@title={{i18n "admin.web_hooks.tags_filter"}}
@description={{i18n
"admin.web_hooks.tags_filter_instructions"
diff --git a/frontend/discourse/admin/models/web-hook.js b/frontend/discourse/admin/models/web-hook.js
index c4dd0a95bbd..77da15a3383 100644
--- a/frontend/discourse/admin/models/web-hook.js
+++ b/frontend/discourse/admin/models/web-hook.js
@@ -107,7 +107,7 @@ export default class WebHook extends RestModel {
createProperties() {
const types = this.web_hook_event_types;
const categoryIds = this.categories.map((c) => c.id);
- const tagNames = this.tag_names;
+ const tags = this.tags;
// Hack as {{group-selector}} accepts a comma-separated string as data source, but
// we use an array to populate the datasource above.
@@ -126,7 +126,9 @@ export default class WebHook extends RestModel {
? [null]
: types.map((type) => type.id),
category_ids: isEmpty(categoryIds) ? [null] : categoryIds,
- tag_names: isEmpty(tagNames) ? [null] : tagNames,
+ tag_ids: isEmpty(tags)
+ ? [null]
+ : tags.map((t) => (typeof t === "object" ? t.id : t)),
group_ids:
isEmpty(groupNames) || isEmpty(groupNames[0])
? [null]
diff --git a/spec/requests/admin/web_hooks_controller_spec.rb b/spec/requests/admin/web_hooks_controller_spec.rb
index 2a339a5e90e..fcbeb482816 100644
--- a/spec/requests/admin/web_hooks_controller_spec.rb
+++ b/spec/requests/admin/web_hooks_controller_spec.rb
@@ -38,6 +38,82 @@ RSpec.describe Admin::WebHooksController do
).to eq(1)
end
+ it "creates a webhook with tag_ids" do
+ tag1 = Fabricate(:tag)
+ tag2 = Fabricate(:tag)
+
+ post "/admin/api/web_hooks.json",
+ params: {
+ web_hook: {
+ payload_url: "https://meta.discourse.org/",
+ content_type: 1,
+ secret: "a_secret_for_webhooks",
+ wildcard_web_hook: false,
+ active: true,
+ verify_certificate: true,
+ web_hook_event_type_ids: [WebHookEventType::TYPES[:topic_created]],
+ tag_ids: [tag1.id, tag2.id],
+ group_ids: [],
+ category_ids: [],
+ },
+ }
+
+ expect(response.status).to eq(200)
+ webhook = WebHook.find(response.parsed_body["web_hook"]["id"])
+ expect(webhook.tags).to contain_exactly(tag1, tag2)
+ end
+
+ it "creates a webhook with deprecated tag_names param" do
+ tag1 = Fabricate(:tag, name: "alpha")
+ tag2 = Fabricate(:tag, name: "beta")
+
+ post "/admin/api/web_hooks.json",
+ params: {
+ web_hook: {
+ payload_url: "https://meta.discourse.org/",
+ content_type: 1,
+ secret: "a_secret_for_webhooks",
+ wildcard_web_hook: false,
+ active: true,
+ verify_certificate: true,
+ web_hook_event_type_ids: [WebHookEventType::TYPES[:topic_created]],
+ tag_names: %w[alpha beta],
+ group_ids: [],
+ category_ids: [],
+ },
+ }
+
+ expect(response.status).to eq(200)
+ webhook = WebHook.find(response.parsed_body["web_hook"]["id"])
+ expect(webhook.tags).to contain_exactly(tag1, tag2)
+ end
+
+ it "ignores tag_ids when tag_names is also provided" do
+ tag1 = Fabricate(:tag, name: "alpha")
+ tag2 = Fabricate(:tag)
+
+ post "/admin/api/web_hooks.json",
+ params: {
+ web_hook: {
+ payload_url: "https://meta.discourse.org/",
+ content_type: 1,
+ secret: "a_secret_for_webhooks",
+ wildcard_web_hook: false,
+ active: true,
+ verify_certificate: true,
+ web_hook_event_type_ids: [WebHookEventType::TYPES[:topic_created]],
+ tag_names: %w[alpha],
+ tag_ids: [tag2.id],
+ group_ids: [],
+ category_ids: [],
+ },
+ }
+
+ expect(response.status).to eq(200)
+ webhook = WebHook.find(response.parsed_body["web_hook"]["id"])
+ expect(webhook.tags).to contain_exactly(tag1)
+ end
+
it "returns error when field is not filled correctly" do
post "/admin/api/web_hooks.json",
params: {
diff --git a/spec/system/admin_webhooks_spec.rb b/spec/system/admin_webhooks_spec.rb
index 82de7fe8c7e..622691f8473 100644
--- a/spec/system/admin_webhooks_spec.rb
+++ b/spec/system/admin_webhooks_spec.rb
@@ -55,6 +55,30 @@ describe "Admin Webhooks Page", type: :system do
webhooks_page.click_save
expect(webhooks_page).to have_webhook_details("https://www.example.com/4")
+
+ webhook = WebHook.find_by(payload_url: "https://www.example.com/4")
+ expect(webhook.tags).to contain_exactly(tag)
+ end
+
+ it "persists tag filters when re-editing a webhook" do
+ tag_chooser = PageObjects::Components::SelectKit.new(".tag-chooser")
+
+ webhooks_page.visit_page
+ webhooks_page.click_add
+
+ tag_chooser.expand
+ tag_chooser.search(tag.name)
+ tag_chooser.select_row_by_name(tag.name)
+
+ webhooks_page.fill_payload_url("https://www.example.com/5")
+ webhooks_page.click_save
+
+ expect(webhooks_page).to have_webhook_details("https://www.example.com/5")
+
+ webhooks_page.click_back
+ webhooks_page.click_edit("https://www.example.com/5")
+
+ expect(tag_chooser).to have_selected_name(tag.name)
end
it "can delete webhooks" do