Skip to content

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

FEATURE: Change workflow topic tags without intermediate states

Régis Hanolc38c82309880
diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index d83712c3cf4..1c5ca1ee7ff 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -259,14 +259,16 @@ module DiscourseTagging
end
return false
end
-
- topic.tags = tags
else
return false unless validate_min_required_tags_for_category(guardian, topic, category)
return false unless validate_required_tags_from_group(guardian, topic, category)
- topic.tags = []
+ tags = []
end
+
+ yield(tags) if block_given?
+
+ topic.tags = tags
topic.tags_changed = true
DiscourseEvent.trigger(
diff --git a/plugins/discourse-workflows/USE_CASES.md b/plugins/discourse-workflows/USE_CASES.md
index 0e0537647d5..8c4f35459d0 100644
--- a/plugins/discourse-workflows/USE_CASES.md
+++ b/plugins/discourse-workflows/USE_CASES.md
@@ -58,6 +58,7 @@ For each run, record:
## Known edge cases to keep testing
+- `action:topic_tags` version `2.0` defaults to `mode: modify`: `add_tag_names` and `remove_tag_names` apply together, preserving unrelated tags and allowing implicit parent tags. `mode: replace` sets the complete tag set from `replace_tag_names`; an empty value clears all tags. Replacement uses the current topic state under a lock and rejects category or parent-tag rules that change the requested final set. Only the selected mode's fields are evaluated; invalid names and expression errors stop the action before any tag changes. Both modes enforce the actor’s permission to edit the topic’s tags and return the final `tag_names`; rejected changes neither persist nor emit tag-change events. Existing tag names remain usable after the maximum tag length is reduced. Version `1.0` retains `operation` and `tag_names`.
- Topic-only triggers that need author/post fields should use `action:topic` get before filtering or messaging.
- Generic prompts like "when someone posts" should use `trigger:post_created` for all regular posts; do not ask whether to include replies unless the prompt explicitly narrows the scope.
- Actions that replace item JSON require downstream nodes to use the action output schema, not the original trigger schema.
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 3a62c8b0a33..7caaea2975b 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -418,12 +418,19 @@ en:
"true": "is true"
"false": "is false"
topic_tags:
+ mode: "Mode"
+ mode_modify: "Modify"
+ mode_replace: "Replace all"
operation: "Operation"
operations:
add: "Add tag"
remove: "Remove tag"
topic_id: "Topic ID"
tag_names: "Tags"
+ add_tag_names: "Add tags"
+ remove_tag_names: "Remove tags"
+ replace_tag_names: "Tags"
+ replace_tag_names_description: "Leave empty to remove all tags."
actor_username: "Performed by user"
tag_group:
operation: "Operation"
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.yml
index 752c668cb84..5c2e99911ee 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -127,7 +127,10 @@ en:
no_target_user: "No target user is available for the modal."
user_not_found: "User '%{username}' not found."
topic_tags:
+ unknown_mode: "Unknown tag mode: %{mode}."
no_tag_names: "No tag names provided."
+ overlapping_tag_names: "The same tags cannot be added and removed: %{tag_names}."
+ unapplied_tag_names: "Tag changes could not be applied: %{tag_names}."
operation_failed: "Tag operation failed: %{errors}"
personal_message_not_allowed: "'%{username}' cannot tag personal messages. Add one of their groups to the 'pm_tags_allowed_for_groups' site setting."
tag_group:
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
index c08005be7ee..1d6b3e791b2 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/node_execution_context.rb
@@ -278,6 +278,13 @@ module DiscourseWorkflows
parameter_resolver.resolve(path, item_index, default:, options:)
end
+ def ensure_no_expression_errors!
+ error = @resolver.expression_errors.first
+ return unless error
+
+ raise NodeError, "#{error[:expression]}: #{error[:error]}"
+ end
+
def helpers
@helpers ||=
ContextHelpers.new(
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tags/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tags/v1.rb
index 3cff539c6e7..ed30f330f0a 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tags/v1.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tags/v1.rb
@@ -121,8 +121,10 @@ module DiscourseWorkflows
)
end
- def tag_topic!(topic, guardian, tag_names, append: false)
- unless DiscourseTagging.tag_topic_by_names(topic, guardian, tag_names, append:)
+ def tag_topic!(topic, guardian, tag_names, append: false, &)
+ guardian.ensure_can_edit_tags!(topic)
+
+ unless DiscourseTagging.tag_topic_by_names(topic, guardian, tag_names, append:, &)
raise_node_error!(
I18n.t(
"discourse_workflows.errors.topic_tags.operation_failed",
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tags/v2.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tags/v2.rb
new file mode 100644
index 00000000000..5d044676026
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/topic_tags/v2.rb
@@ -0,0 +1,147 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module TopicTags
+ class V2 < V1
+ MODIFY = "modify"
+ REPLACE = "replace"
+ MODES = [MODIFY, REPLACE].freeze
+
+ description(
+ V1.description.merge(
+ version: "2.0",
+ properties: {
+ mode: {
+ type: :options,
+ options: MODES,
+ default: MODIFY,
+ no_data_expression: true,
+ },
+ topic_id: V1.properties[:topic_id],
+ add_tag_names:
+ V1.properties[:tag_names].merge(display_options: { hide: { mode: [REPLACE] } }),
+ remove_tag_names:
+ V1.properties[:tag_names].merge(display_options: { hide: { mode: [REPLACE] } }),
+ replace_tag_names:
+ V1.properties[:tag_names].merge(display_options: { show: { mode: [REPLACE] } }),
+ actor_username: V1.properties[:actor_username],
+ },
+ ),
+ )
+
+ def execute(exec_ctx)
+ items =
+ exec_ctx.input_items.map.with_index do |_item, item_index|
+ wrap(process(exec_ctx, item_index))
+ end
+
+ [items]
+ end
+
+ private
+
+ def process(exec_ctx, item_index)
+ mode = exec_ctx.get_node_parameter("mode", item_index, default: MODIFY)
+ if MODES.exclude?(mode)
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.topic_tags.unknown_mode", mode: mode),
+ )
+ end
+
+ topic = ::Topic.find(exec_ctx.get_node_parameter("topic_id", item_index))
+ tag_names, removed_names = configured_tags(exec_ctx, item_index, mode)
+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)
+ exec_ctx.ensure_no_expression_errors!
+ ensure_can_tag_personal_message!(topic, actor)
+
+ topic.with_lock do
+ desired_names =
+ mode == REPLACE ? tag_names : (topic.tags.pluck(:name) - removed_names) | tag_names
+ if desired_names.size > SiteSetting.max_tags_per_topic
+ raise_node_error!(
+ I18n.t("tags.too_many_tags_for_topic", count: SiteSetting.max_tags_per_topic),
+ )
+ end
+
+ tag_topic!(topic, actor.guardian, desired_names) do |tags|
+ resolved_names = tags.map(&:name)
+ unexpected_names =
+ mode == REPLACE ? resolved_names - desired_names : removed_names & resolved_names
+ unapplied_names = (desired_names - resolved_names) | unexpected_names
+ if unapplied_names.present?
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.topic_tags.unapplied_tag_names",
+ tag_names: unapplied_names.sort.join(", "),
+ ),
+ )
+ end
+ end
+
+ { topic_id: topic.id, tag_names: topic.tags.map(&:name).sort }
+ end
+ end
+
+ def configured_tags(exec_ctx, item_index, mode)
+ if mode == REPLACE
+ return [
+ canonical_tag_names(exec_ctx.get_node_parameter("replace_tag_names", item_index)),
+ []
+ ]
+ end
+
+ added_names =
+ canonical_tag_names(exec_ctx.get_node_parameter("add_tag_names", item_index))
+ removed_names =
+ canonical_tag_names(exec_ctx.get_node_parameter("remove_tag_names", item_index))
+ if added_names.empty? && removed_names.empty?
+ raise_node_error!(I18n.t("discourse_workflows.errors.topic_tags.no_tag_names"))
+ end
+
+ overlapping_names = added_names.map(&:downcase) & removed_names.map(&:downcase)
+ if overlapping_names.present?
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.topic_tags.overlapping_tag_names",
+ tag_names: overlapping_names.sort.join(", "),
+ ),
+ )
+ end
+
+ [added_names, removed_names]
+ end
+
+ def canonical_tag_names(value)
+ names =
+ normalize_tag_names(value).to_h { |name| [name, DiscourseTagging.clean_tag(name)] }
+ return [] if names.empty?
+
+ existing_names =
+ ::Tag
+ .where_name(names.keys | names.values)
+ .includes(:target_tag)
+ .to_h { |tag| [tag.name.downcase, tag.target_tag&.name || tag.name] }
+
+ names
+ .map do |name, cleaned_name|
+ canonical_name =
+ existing_names[name.downcase] || existing_names[cleaned_name.downcase] ||
+ cleaned_name.presence
+ if canonical_name.nil?
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.topic_tags.unapplied_tag_names",
+ tag_names: name,
+ ),
+ )
+ end
+
+ canonical_name
+ end
+ .uniq(&:downcase)
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_tags/v2_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_tags/v2_spec.rb
new file mode 100644
index 00000000000..24cca6ba4d0
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_tags/v2_spec.rb
@@ -0,0 +1,387 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::TopicTags::V2 do
+ fab!(:topic)
+ fab!(:existing_tag) { Fabricate(:tag, name: "existing") }
+ fab!(:removed_tag) { Fabricate(:tag, name: "submitted") }
+ fab!(:added_tag) { Fabricate(:tag, name: "scheduled") }
+
+ before do
+ SiteSetting.tagging_enabled = true
+ topic.tags = [existing_tag, removed_tag]
+ end
+
+ describe "#execute" do
+ it "uses existing tag names after the maximum tag length is reduced" do
+ SiteSetting.max_tag_length = existing_tag.name.length
+ config = {
+ "topic_id" => topic.id,
+ "add_tag_names" => added_tag.name.upcase,
+ "remove_tag_names" => removed_tag.name.upcase,
+ }
+
+ execute_node(configuration: config)
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, added_tag)
+ end
+
+ it "replaces mutually exclusive tags together, preserving unrelated tags" do
+ Fabricate(:tag_group, tags: [removed_tag, added_tag], one_per_topic: true)
+ config = {
+ "topic_id" => topic.id,
+ "add_tag_names" => added_tag.name,
+ "remove_tag_names" => removed_tag.name,
+ }
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ @result = execute_node(configuration: config)
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, added_tag)
+ expect(@result).to eq(
+ "topic_id" => topic.id,
+ "tag_names" => [existing_tag.name, added_tag.name],
+ )
+ expect(@result).to match_node_output_schema(described_class)
+ expect(events).to contain_exactly(
+ a_hash_including(
+ params: [
+ topic,
+ {
+ old_tag_names: contain_exactly(existing_tag.name, removed_tag.name),
+ new_tag_names: contain_exactly(existing_tag.name, added_tag.name),
+ user: Discourse.system_user,
+ },
+ ],
+ ),
+ )
+ end
+
+ it "resolves expressions and canonical tag names for each input item" do
+ synonym = Fabricate(:tag, name: "alias", target_tag: removed_tag)
+ other_topic = Fabricate(:topic, tags: [existing_tag, removed_tag])
+ items = [
+ { "json" => { "topic_id" => topic.id, "add" => ["new tag, another"], "remove" => [] } },
+ {
+ "json" => {
+ "topic_id" => other_topic.id,
+ "add" => [],
+ "remove" => "#{synonym.name.upcase}, #{existing_tag.name.upcase}",
+ },
+ },
+ ]
+ config = {
+ "topic_id" => "={{ $json.topic_id }}",
+ "add_tag_names" => "={{ $json.add }}",
+ "remove_tag_names" => "={{ $json.remove }}",
+ "replace_tag_names" => "={{ globalThis.unusedTagsEvaluated = true }}",
+ "actor_username" => "={{ globalThis.unusedTagsEvaluated ? '' : 'system' }}",
+ }
+
+ execute_node_output(configuration: config, input_items: items)
+
+ expect(topic.reload.tags.pluck(:name)).to contain_exactly(
+ existing_tag.name,
+ removed_tag.name,
+ "new-tag",
+ "another",
+ )
+ expect(other_topic.reload.tags).to be_empty
+ end
+
+ it "replaces live tags with canonical expression results and ignores modify fields" do
+ synonym = Fabricate(:tag, name: "alias", target_tag: added_tag)
+ item = {
+ "json" => {
+ "tags" => [removed_tag.name],
+ "replacement" => [synonym.name.upcase, added_tag.name],
+ },
+ }
+ config = {
+ "mode" => "replace",
+ "topic_id" => topic.id,
+ "replace_tag_names" => "={{ $json.replacement }}",
+ "add_tag_names" => "={{ globalThis.unusedTagsEvaluated = true }}",
+ "remove_tag_names" => "={{ globalThis.unusedTagsEvaluated = true }}",
+ "actor_username" => "={{ globalThis.unusedTagsEvaluated ? '' : 'system' }}",
+ }
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ @result = execute_node(configuration: config, item: item)
+ end
+
+ expect(topic.reload.tags).to contain_exactly(added_tag)
+ expect(@result).to eq("topic_id" => topic.id, "tag_names" => [added_tag.name])
+ expect(events).to contain_exactly(
+ a_hash_including(
+ params: [
+ topic,
+ {
+ old_tag_names: contain_exactly(existing_tag.name, removed_tag.name),
+ new_tag_names: [added_tag.name],
+ user: Discourse.system_user,
+ },
+ ],
+ ),
+ )
+ end
+
+ it "clears all tags with an empty replacement and leaves an already empty topic unchanged" do
+ config = { "mode" => "replace", "topic_id" => topic.id, "replace_tag_names" => [] }
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ @result = execute_node(configuration: config)
+ end
+
+ expect(topic.reload.tags).to be_empty
+ expect(@result).to eq("topic_id" => topic.id, "tag_names" => [])
+ expect(events.map { |event| event[:params].last[:new_tag_names] }).to eq([[]])
+
+ expect(execute_node(configuration: config)).to eq(@result)
+ expect(topic.reload.tags).to be_empty
+ end
+
+ it "rejects failed replacement expressions without changing tags or emitting events",
+ :aggregate_failures do
+ config = {
+ "mode" => "replace",
+ "topic_id" => topic.id,
+ "replace_tag_names" => "={{ $json.missing.deep }}",
+ }
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ )
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(events).to be_empty
+ end
+
+ it "rejects nonblank replacement names that normalize to empty" do
+ config = { "mode" => "replace", "topic_id" => topic.id, "replace_tag_names" => ["..."] }
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ I18n.t("discourse_workflows.errors.topic_tags.unapplied_tag_names", tag_names: "..."),
+ )
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(events).to be_empty
+ end
+
+ it "rejects native changes to the requested replacement without changing tags or emitting events" do
+ category = Fabricate(:category, allow_global_tags: false, tags: [existing_tag, removed_tag])
+ topic.update!(category: category)
+ config = {
+ "mode" => "replace",
+ "topic_id" => topic.id,
+ "replace_tag_names" => [existing_tag.name, added_tag.name],
+ }
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ )
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(events).to be_empty
+
+ category.update!(allow_global_tags: true)
+ parent_tag = Fabricate(:tag)
+ Fabricate(:tag_group, tags: [added_tag], parent_tag: parent_tag)
+ config["replace_tag_names"] = [added_tag.name]
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ )
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(events).to be_empty
+
+ execute_node(configuration: { "topic_id" => topic.id, "add_tag_names" => added_tag.name })
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag, added_tag, parent_tag)
+ end
+
+ it "rejects tag names present in both lists after normalization" do
+ SiteSetting.force_lowercase_tags = false
+ synonym = Fabricate(:tag, name: "alias", target_tag: removed_tag)
+ config = {
+ "topic_id" => topic.id,
+ "add_tag_names" => "#{synonym.name.upcase}, New tag",
+ "remove_tag_names" => "#{removed_tag.name}, new-tag",
+ }
+
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ I18n.t(
+ "discourse_workflows.errors.topic_tags.overlapping_tag_names",
+ tag_names: "new-tag, #{removed_tag.name}",
+ ),
+ )
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(Tag.find_by_name("new-tag")).to be_nil
+ end
+
+ it "requires nonempty modify input and a supported mode" do
+ config = { "topic_id" => topic.id, "add_tag_names" => [" ", ","], "remove_tag_names" => "" }
+
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "No tag names provided.",
+ )
+
+ config = { "topic_id" => topic.id, "mode" => "unknown", "replace_tag_names" => [] }
+
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ I18n.t("discourse_workflows.errors.topic_tags.unknown_mode", mode: config["mode"]),
+ )
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ end
+
+ it "rolls back all changes when the final tag set is invalid" do
+ Fabricate(:tag_group, tags: [removed_tag, added_tag], one_per_topic: true)
+ config = {
+ "topic_id" => topic.id,
+ "add_tag_names" => [added_tag.name, "new-tag"],
+ "remove_tag_names" => existing_tag.name,
+ }
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /Tag operation failed:/,
+ )
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(Tag.find_by_name("new-tag")).to be_nil
+ expect(events).to be_empty
+ end
+
+ it "rejects changes exceeding the tag limit instead of dropping unrelated tags" do
+ SiteSetting.max_tags_per_topic = 1
+ config = { "topic_id" => topic.id, "add_tag_names" => added_tag.name }
+
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ I18n.t("tags.too_many_tags_for_topic", count: SiteSetting.max_tags_per_topic),
+ )
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ end
+
+ it "rejects partially applied tag changes without changing the topic or emitting events" do
+ category = Fabricate(:category, allow_global_tags: false, tags: [existing_tag, removed_tag])
+ topic.update!(category: category)
+ config = {
+ "topic_id" => topic.id,
+ "add_tag_names" => added_tag.name,
+ "remove_tag_names" => removed_tag.name,
+ }
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ )
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(events).to be_empty
+
+ category.update!(allow_global_tags: true)
+ Fabricate(:tag_group, tags: [removed_tag], parent_tag: existing_tag)
+ config["remove_tag_names"] = existing_tag.name
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ )
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(events).to be_empty
+ end
+
+ it "rejects actors who can use tags but cannot access the topic" do
+ actor = Fabricate(:trust_level_1)
+ SiteSetting.tag_topic_allowed_groups = Group::AUTO_GROUPS[:trust_level_0].to_s
+ topic.update!(category: Fabricate(:private_category, group: Group[:staff]))
+ config = {
+ "topic_id" => topic.id,
+ "actor_username" => actor.username,
+ "add_tag_names" => added_tag.name,
+ "remove_tag_names" => removed_tag.name,
+ }
+
+ expect(actor.guardian.can_tag?(topic)).to eq(true)
+ expect(actor.guardian.can_see_topic?(topic)).to eq(false)
+
+ events =
+ DiscourseEvent.track_events(:topic_tags_changed) do
+ expect { execute_node(configuration: config) }.to raise_error(Discourse::InvalidAccess)
+ end
+
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ expect(events).to be_empty
+ end
+
+ it "preserves all tags when the actor cannot add a restricted tag" do
+ actor = Fabricate(:trust_level_1)
+ topic.update!(user: actor)
+ Fabricate(
+ :tag_group,
+ tags: [added_tag],
+ permissions: {
+ "staff" => TagGroupPermission.permission_types[:full],
+ },
+ )
+ config = {
+ "topic_id" => topic.id,
+ "actor_username" => actor.username,
+ "add_tag_names" => added_tag.name,
+ "remove_tag_names" => removed_tag.name,
+ }
+
+ expect { execute_node(configuration: config) }.to raise_error(DiscourseWorkflows::NodeError)
+ expect(topic.reload.tags).to contain_exactly(existing_tag, removed_tag)
+ end
+
+ it "requires the actor's permission to tag personal messages" do
+ actor = Fabricate(:admin)
+ personal_message = Fabricate(:private_message_topic)
+ SiteSetting.pm_tags_allowed_for_groups = ""
+ config = {
+ "topic_id" => personal_message.id,
+ "actor_username" => actor.username,
+ "add_tag_names" => added_tag.name,
+ }
+
+ expect { execute_node(configuration: config) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ I18n.t(
+ "discourse_workflows.errors.topic_tags.personal_message_not_allowed",
+ username: actor.username,
+ ),
+ )
+ expect(personal_message.reload.tags).to be_empty
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_tags_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_tags_spec.rb
index f3486872e78..a911657f134 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_tags_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/topic_tags_spec.rb
@@ -126,6 +126,24 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicTags::V1 do
)
end
+ it "preserves operation expressions in version one" do
+ topic.tags << tag
+ config = {
+ "operation" => "={{ $json.operation }}",
+ "topic_id" => topic.id,
+ "tag_names" => "={{ $json.tags }}",
+ }
+ input_items = [
+ { "json" => { "operation" => "add", "tags" => [tag_2.name] } },
+ { "json" => { "operation" => "remove", "tags" => [tag.name] } },
+ ]
+
+ result = execute_node_output(configuration: config, input_items: input_items).first
+
+ expect(result.map { |output| output["json"]["tag_names"] }).to eq([[tag_2.name], [tag.name]])
+ expect(topic.reload.tags).to contain_exactly(tag_2)
+ end
+
it "raises when no tag names are provided" do
admin = Fabricate(:admin)
personal_message = Fabricate(:private_message_topic)
@@ -162,7 +180,7 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicTags::V1 do
end
context "with restricted tags" do
- fab!(:regular_user, :user)
+ fab!(:regular_user, :trust_level_1)
fab!(:admin)
fab!(:staff_tag_group) do
Fabricate(
@@ -179,6 +197,7 @@ RSpec.describe DiscourseWorkflows::Nodes::TopicTags::V1 do
end
it "prevents adding staff-only tags when actor_username is a regular user" do
+ topic.update!(user: regular_user)
config = {
"operation" => "add",
"topic_id" => topic.id.to_s,
diff --git a/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb b/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb
index ae25c9058b9..07eb95e3c09 100644
--- a/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb
+++ b/plugins/discourse-workflows/spec/system/page_objects/pages/workflow_editor.rb
@@ -63,6 +63,57 @@ module PageObjects
page.has_no_css?(".workflows-configurator-modal")
end
+ def has_combined_topic_tag_fields?
+ page.has_css?("[data-name='add_tag_names'] .mini-tag-chooser") &&
+ page.has_css?("[data-name='remove_tag_names'] .mini-tag-chooser") &&
+ page.has_no_css?("[data-name='replace_tag_names']") &&
+ page.has_no_css?("[data-name='operation']")
+ end
+
+ def select_topic_tag_mode(mode)
+ find("[data-name='mode'] select").select(mode)
+ self
+ end
+
+ def set_tagged_topic(topic)
+ find("[data-name='topic_id'] input[type='text']").fill_in(with: topic.id)
+ self
+ end
+
+ def add_replacement_tag(tag)
+ selector =
+ PageObjects::Components::SelectKit.new(
+ "[data-name='replace_tag_names'] .mini-tag-chooser",
+ )
+ selector.expand
+ selector.search(tag.name)
+ selector.select_row_by_value(tag.id)
+ self
+ end
+
+ def has_replacement_topic_tag_fields?
+ page.has_css?("[data-name='mode'] option:checked", text: "Replace all", visible: :all) &&
+ page.has_css?("[data-name='replace_tag_names'] .mini-tag-chooser") &&
+ page.has_css?(
+ "[data-name='replace_tag_names']",
+ text: I18n.t("js.discourse_workflows.topic_tags.replace_tag_names_description"),
+ ) && page.has_no_css?("[data-name='add_tag_names']") &&
+ page.has_no_css?("[data-name='remove_tag_names']")
+ end
+
+ def has_replacement_tag?(tag)
+ page.has_css?("[data-name='replace_tag_names'] .select-kit-header", text: tag.name)
+ end
+
+ def has_legacy_topic_tag_fields?
+ page.has_css?(
+ "[data-name='operation'] .cm-wf-reference-pill__path",
+ exact_text: "operation",
+ ) && page.has_css?("[data-name='tag_names'] .mini-tag-chooser") &&
+ page.has_no_css?("[data-name='add_tag_names']") &&
+ page.has_no_css?("[data-name='remove_tag_names']")
+ end
+
def has_saved_node_configuration?
page.has_css?(".workflows-configurator-modal__save-status--saved")
end
@@ -134,10 +185,6 @@ module PageObjects
"grant" => "Grant badge",
"revoke" => "Revoke badge",
},
- "action:topic_tags" => {
- "add" => "Add",
- "remove" => "Remove",
- },
"action:group" => {
"add" => "Add to group",
"remove" => "Remove from group",
diff --git a/plugins/discourse-workflows/spec/system/workflows_spec.rb b/plugins/discourse-workflows/spec/system/workflows_spec.rb
index 85237404cf9..9e4a3837c88 100644
--- a/plugins/discourse-workflows/spec/system/workflows_spec.rb
+++ b/plugins/discourse-workflows/spec/system/workflows_spec.rb
@@ -11,17 +11,66 @@ RSpec.describe "Discourse Workflows" do
sign_in(admin)
end
- it "creates a workflow with trigger and action" do
+ it "creates a workflow and switches between modifying and replacing topic tags" do
+ topic = Fabricate(:topic)
+ tag = Fabricate(:tag)
+
editor_page.visit_new
editor_page.click_empty_state_add_node
editor_page.select_node_type("trigger:topic_closed")
editor_page.click_add_node
- editor_page.select_node_type("action:topic_tags", operation: "add")
+ editor_page.select_node_type("action:topic_tags")
+ editor_page.double_click_node(1)
+
+ expect(editor_page).to have_combined_topic_tag_fields
+
+ editor_page.set_tagged_topic(topic)
+ editor_page.select_topic_tag_mode("Replace all")
+ expect(editor_page).to have_replacement_topic_tag_fields
+ editor_page.add_replacement_tag(tag)
+ expect(editor_page).to have_saved_node_configuration
+
+ page.refresh
+ expect(editor_page).to have_replacement_topic_tag_fields
+ expect(editor_page).to have_replacement_tag(tag)
+
+ editor_page.select_topic_tag_mode("Modify")
+ expect(editor_page).to have_combined_topic_tag_fields
+
+ editor_page.close_node_configurator
workflows_page.visit_index
expect(workflows_page).to have_workflow("My workflow")
end
+ it "keeps the operation expression and tag picker for existing topic tag nodes" do
+ node_id = "topic-tags"
+ topic = Fabricate(:topic)
+ tag = Fabricate(:tag)
+ workflow =
+ Fabricate(
+ :discourse_workflows_workflow,
+ created_by: admin,
+ nodes: [
+ {
+ "id" => node_id,
+ "type" => "action:topic_tags",
+ "typeVersion" => "1.0",
+ "name" => "Legacy topic tags",
+ "parameters" => {
+ "operation" => "={{ $json.operation }}",
+ "topic_id" => topic.id.to_s,
+ "tag_names" => [tag.name],
+ },
+ },
+ ],
+ )
+
+ editor_page.visit_node(workflow, node_id)
+
+ expect(editor_page).to have_legacy_topic_tag_fields
+ end
+
it "creates a workflow with condition node" do
editor_page.visit_new
editor_page.click_empty_state_add_node