Skip to content

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

FEATURE: Add workflow action for managing tag group tags (#43123)

Joffrey JAFFEUXdd4cc4f4cc5a
diff --git a/plugins/discourse-workflows/USE_CASES.md b/plugins/discourse-workflows/USE_CASES.md
index c1f0d61e822..a7e2a7c0c8a 100644
--- a/plugins/discourse-workflows/USE_CASES.md
+++ b/plugins/discourse-workflows/USE_CASES.md
@@ -50,6 +50,7 @@ For each run, record:
| `category-changed-chat` | `when a topic is moved into Support, message General chat with a link to the topic` | `trigger:topic_category_changed -> action:send_chat_message` | Should query catalog for old/new category fields before drafting. |
| `badge-grant` | `when a TL1 user posts in General with the word helpful, grant them the Basic badge` | `trigger:post_created -> condition:filter -> action:badge` | Uses declarative filter; resolves badge; no Code node. |
| `group-add` | `when a TL1 user creates a topic in Support, add them to the helpers group` | `trigger:topic_created -> condition:filter -> action:group` | Uses `$json.post.username`; resolves group; no Code node. |
+| `tag-group-add` | `every week, add the current-campaign tag to the Marketing tag group` | `trigger:schedule -> action:tag_group` | Resolves the tag group; creates the tag if needed; no Code node. |
| `http-request-warning` | `when a new topic is created in Support, send the topic title to https://example.com/webhook` | `trigger:topic_created -> action:http_request` | Includes external HTTP risk; validates URL/method/body; no Code unless necessary. |
## Known edge cases to keep testing
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 8ce31126dac..42c073c1097 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -238,6 +238,7 @@ en:
"condition:filter": "Filter"
"condition:time_window": "Time window"
"action:topic_tags": "Topic tags"
+ "action:tag_group": "Tag group"
"action:code": "Code"
"action:topic": "Topic"
"action:post": "Post"
@@ -298,6 +299,7 @@ en:
"condition:filter": "Continue only if conditions are met"
"condition:time_window": "Branch the workflow based on the current day and time"
"action:topic_tags": "Add or remove tags on a topic"
+ "action:tag_group": "Add or remove tags in a tag group"
"action:code": "Run custom JavaScript code"
"action:topic": "Create, retrieve, list, close, archive, or set custom fields on topics"
"action:post": "Create, edit, retrieve, or list posts"
@@ -419,6 +421,15 @@ en:
topic_id: "Topic ID"
tag_names: "Tags"
actor_username: "Performed by user"
+ tag_group:
+ operation: "Operation"
+ operations:
+ add: "Add tags"
+ remove: "Remove tags"
+ tag_group_id: "Tag group"
+ tag_group_id_placeholder: "Select a tag group"
+ tag_names: "Tags"
+ actor_username: "Performed by user"
topic_category:
topic_id: "Topic ID"
category_id: "Category"
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.yml
index ef6bbd0a4ca..4cdc89b948c 100644
--- a/plugins/discourse-workflows/config/locales/server.en.yml
+++ b/plugins/discourse-workflows/config/locales/server.en.yml
@@ -125,6 +125,10 @@ en:
no_tag_names: "No tag names provided."
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:
+ no_tag_names: "No tag names provided."
+ no_valid_tag_names: "No valid tag names provided."
+ operation_failed: "Tag group operation failed: %{errors}"
topic_category:
operation_failed: "Category change failed."
private_message: "Personal messages cannot have a category."
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb
index 5c132bc71a0..e2b04b20f03 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_node_catalog.rb
@@ -184,6 +184,17 @@ module DiscourseWorkflows
},
},
],
+ "action:tag_group" => [
+ {
+ name: "Add tags to a tag group",
+ parameters: {
+ operation: "add",
+ tag_group_id: 123,
+ tag_names: "needs-review, escalated",
+ actor_username: "system",
+ },
+ },
+ ],
"action:topic_category" => [
{
name: "Move the trigger topic to another category",
@@ -225,6 +236,7 @@ module DiscourseWorkflows
"action:ai_agent" =>
"ai agent bot llm classify summarize generate sentiment triage runner run as permissions uploads attachments",
"action:group" => "group membership member belongs friend friends",
+ "action:tag_group" => "tag group tags taxonomy add remove organize",
"action:flag_user" =>
"flag user report spammer spam suspect review queue moderation approve reject account signup",
"trigger:user_added_to_group" => "joined added to group membership member",
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_resolve_entity.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_resolve_entity.rb
index 320a7befa5a..b55ebd26a97 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_resolve_entity.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai/tools/workflow_resolve_entity.rb
@@ -4,7 +4,7 @@ module DiscourseWorkflows
module Ai
module Tools
class WorkflowResolveEntity < Base
- SUPPORTED_KINDS = %w[category tag user group badge data_table].freeze
+ SUPPORTED_KINDS = %w[category tag tag_group user group badge data_table].freeze
MAX_RESULTS = 10
def self.signature
@@ -66,6 +66,15 @@ module DiscourseWorkflows
.map { |id, name| { id: id, name: name } }
end
+ def resolve_tag_group(query)
+ ::TagGroup
+ .where("name ILIKE ?", "%#{::TagGroup.sanitize_sql_like(query)}%")
+ .order(:name)
+ .limit(MAX_RESULTS)
+ .pluck(:id, :name)
+ .map { |id, name| { id: id, name: name } }
+ end
+
def resolve_user(query)
User
.real
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
index 7fe6641ff45..0b4eb371433 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/ai_workflow_author.rb
@@ -52,7 +52,7 @@ module DiscourseWorkflows
- Use the compact workflow authoring context supplied with the request before proposing changes, and call workflow_graph_context/workflow_node_catalog for details when needed.
- Do not ask admins for exact IDs, slugs, or names when an available tool can look them up; use discovery and resolver tools first.
- Use categories, tags, search, read, and time tools for current forum discovery when they help draft the workflow safely; do not use forum search/read to discover workflow node behavior or schemas because workflow_node_catalog and workflow_validate_patch return schema details.
- - Use workflow_resolve_entity when the user names a category, tag, group, user, badge, or data table and you need its exact workflow parameter value.
+ - Use workflow_resolve_entity when the user names a category, tag, tag group, group, user, badge, or data table and you need its exact workflow parameter value.
- Use workflow_ai_agent_catalog before adding action:ai_agent nodes. Reuse a suitable enabled AI agent by setting agent_id to its numeric id. Only propose a new agent when the workflow genuinely needs AI judgment/generation and no suitable enabled agent exists.
- New AI agents must be prompt-only by default: name, description, and system_prompt only. Do not add tools, RAG, MCP servers, bot users, mention permissions, default LLM overrides, or broad group access for generated agents.
- Keep generated AI agent system prompts focused on the workflow task. A simple helpful prompt is acceptable when the task is broad, but prefer concise task-specific instructions when the workflow needs a clear output.
@@ -91,6 +91,7 @@ module DiscourseWorkflows
- For simple checks on trigger fields, such as trust level comparisons, text contains, tags, category, staff/admin/moderator flags, use condition:filter instead of a Code node. Use condition:if only when the workflow needs separate true/false branches.
- For requests phrased as "when someone posts" or "when anyone posts", use trigger:post_created for all regular posts, including first posts and replies, unless the admin explicitly asks for only new topics or only replies. Do not ask to distinguish replies vs topic starters for a generic "posts" request.
- For group membership checks, resolve the named group with workflow_resolve_entity(kind: "group", ...), then use action:group with operation "check_membership", group_id set to the resolved group id, and username ={{ $json.user.username }} when the input schema includes user.username, otherwise use the exact username field from the current input schema. The node adds $json.group_membership.in_group and keeps the original item fields. If the workflow needs different member and non-member paths, add condition:if after the group node and compare $json.group_membership.in_group to true or false.
+ - For tag group membership changes, resolve the named tag group with workflow_resolve_entity(kind: "tag_group", ...), then use action:tag_group with operation "add" or "remove", tag_group_id set to the resolved tag group id, and tag_names set to the tag names to change.
- For personal messages, private messages, DMs, direct messages, or PM-style notifications, use action:send_personal_message. Resolve named user recipients with workflow_resolve_entity(kind: "user") and set recipient_usernames to an array containing the resolved username. Resolve named group recipients with workflow_resolve_entity(kind: "group") and set recipient_group_names to an array containing the resolved group name, not the group id. Use leading-= template strings for title/raw when including dynamic post or topic links.
- When using an output_contracts schema, use the exact documented path (for example $json.user.trust_level, $json.user.username, $json.post.raw, or $json.post.post_url). Do not generate fallback chains for undocumented aliases such as $json.user.trustLevel or $json.trust_level.
- Dynamic parameter values must start with =. Use ={{ $json.topic.id }} for a whole-field expression, and use template strings such as =Archived topic: {{ $json.topic.title }} (/t/{{ $json.topic.slug }}/{{ $json.topic.id }}) when mixing text and expressions.
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/tag_group/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/tag_group/v1.rb
new file mode 100644
index 00000000000..5b322f65125
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/tag_group/v1.rb
@@ -0,0 +1,231 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module TagGroup
+ class V1 < NodeType
+ OPERATIONS = %w[add remove].freeze
+
+ description(
+ name: "action:tag_group",
+ version: "1.0",
+ defaults: {
+ icon: "tags",
+ color: "orange",
+ },
+ group: "discourse_actions",
+ capabilities: {
+ run_scope: "per_item",
+ },
+ output_contracts: [
+ {
+ schema: {
+ "$schema" => Schema::DRAFT_URI,
+ "type" => "object",
+ "properties" => {
+ "tag_group_id" => {
+ "type" => "integer",
+ },
+ "tag_group_name" => {
+ "type" => "string",
+ },
+ "tag_names" => {
+ "type" => "array",
+ "items" => {
+ "type" => "string",
+ },
+ },
+ },
+ },
+ },
+ ],
+ properties: {
+ operation: {
+ type: :options,
+ required: true,
+ options: OPERATIONS,
+ default: "add",
+ ui: {
+ expression: true,
+ },
+ },
+ tag_group_id: {
+ type: :integer,
+ required: true,
+ type_options: {
+ load_options_method: "tag_groups",
+ },
+ ui: {
+ control: :combo_box,
+ dynamic_value: :tag_group_id,
+ },
+ control_options: {
+ value_property: "id",
+ name_property: "name",
+ filterable: true,
+ none: "discourse_workflows.tag_group.tag_group_id_placeholder",
+ },
+ },
+ tag_names: {
+ type: :string,
+ required: true,
+ ui: {
+ control: :tags,
+ },
+ },
+ actor_username: {
+ type: :string,
+ required: false,
+ default: "system",
+ ui: {
+ control: :actor,
+ },
+ },
+ },
+ )
+
+ def self.load_options_context(context)
+ case context.method_name
+ when "tag_groups"
+ ::TagGroup
+ .order(:name)
+ .pluck(:id, :name)
+ .select { |_, name| context.matches_filter?(name) }
+ .map { |id, name| { id:, name: } }
+ end
+ end
+
+ def execute(exec_ctx)
+ items =
+ exec_ctx.input_items.map.with_index do |_item, item_index|
+ config = {
+ "operation" => exec_ctx.get_node_parameter("operation", item_index, default: "add"),
+ "tag_group_id" => exec_ctx.get_node_parameter("tag_group_id", item_index),
+ "tag_names" => exec_ctx.get_node_parameter("tag_names", item_index),
+ }
+
+ wrap(process(exec_ctx, config, item_index))
+ end
+
+ [items]
+ end
+
+ private
+
+ def process(exec_ctx, config, item_index)
+ tag_group = ::TagGroup.find(config["tag_group_id"])
+ actor = exec_ctx.actor_from_parameter("actor_username", item_index)
+ actor.guardian.ensure_can_admin_tag_groups!
+ requested_tag_names = normalized_tag_names(config["tag_names"])
+
+ result = nil
+ ::TagGroup.transaction do
+ tag_group.lock!
+ before_tag_names = tag_group.base_tags.order(:name).pluck(:name)
+ before_data = serialized_tag_group(tag_group)
+ desired_tags = desired_tags_for(tag_group, requested_tag_names, config["operation"])
+
+ if desired_tags.map(&:name).sort != before_tag_names
+ tag_group.tag_ids = desired_tags.map(&:id)
+ tag_group.save!
+
+ after_data = serialized_tag_group(tag_group)
+ StaffActionLogger.new(actor).log_tag_group_change(
+ tag_group.name,
+ before_data,
+ after_data,
+ )
+ end
+
+ result = output_for(tag_group)
+ end
+
+ result
+ rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => error
+ errors = error.record&.errors&.full_messages&.presence&.join(", ") || error.message
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.tag_group.operation_failed", errors: errors),
+ )
+ end
+
+ def normalized_tag_names(value)
+ names = normalize_tag_names(value)
+ if names.empty?
+ raise_node_error!(I18n.t("discourse_workflows.errors.tag_group.no_tag_names"))
+ end
+
+ names.each do |name|
+ next if ::Tag.find_by_name(name)
+
+ cleaned_name = DiscourseTagging.clean_tag(name)
+ next if ::Tag.find_by_name(cleaned_name)
+
+ validate_new_tag_name!(cleaned_name)
+ end
+
+ normalized_names =
+ DiscourseTagging.tags_for_saving(
+ names,
+ Guardian.new(Discourse.system_user),
+ unlimited: true,
+ )
+
+ if normalized_names.blank?
+ raise_node_error!(I18n.t("discourse_workflows.errors.tag_group.no_valid_tag_names"))
+ end
+
+ normalized_names.uniq
+ end
+
+ def validate_new_tag_name!(name)
+ tag = ::Tag.new(name: name)
+ return if tag.valid?
+
+ raise_node_error!(
+ I18n.t(
+ "discourse_workflows.errors.tag_group.operation_failed",
+ errors: tag.errors.full_messages.join(", "),
+ ),
+ )
+ end
+
+ def desired_tags_for(tag_group, requested_tag_names, operation)
+ if OPERATIONS.exclude?(operation)
+ raise_node_error!(
+ I18n.t("discourse_workflows.errors.unknown_operation", operation: operation),
+ )
+ end
+
+ current_tags = tag_group.base_tags.to_a
+ requested_tags = resolve_tags(requested_tag_names, create: operation == "add")
+
+ tags =
+ operation == "remove" ? current_tags - requested_tags : current_tags | requested_tags
+ tags.reject { |tag| tag.id == tag_group.parent_tag_id }.sort_by(&:name)
+ end
+
+ def resolve_tags(tag_names, create:)
+ tag_names
+ .filter_map do |name|
+ tag = ::Tag.find_by_name(name)
+ tag ||= ::Tag.create!(name: name) if create
+ tag&.target_tag || tag
+ end
+ .uniq
+ end
+
+ def serialized_tag_group(tag_group)
+ ::TagGroupSerializer.new(tag_group).to_json(root: false)
+ end
+
+ def output_for(tag_group)
+ {
+ tag_group_id: tag_group.id,
+ tag_group_name: tag_group.name,
+ tag_names: tag_group.base_tags.order(:name).pluck(:name),
+ }
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb
index bbb1844bc35..06e57a508a0 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_node_catalog_spec.rb
@@ -83,6 +83,29 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog do
)
end
+ it "finds the Tag group action with its example and output fields", :aggregate_failures do
+ result = invoke_tool(query: "organize tag group", include_examples: true)
+ node = result[:nodes].find { |candidate| candidate[:type] == "action:tag_group" }
+
+ expect(node[:examples]).to contain_exactly(
+ include(
+ name: "Add tags to a tag group",
+ parameters:
+ include(
+ operation: "add",
+ tag_group_id: 123,
+ tag_names: "needs-review, escalated",
+ actor_username: "system",
+ ),
+ ),
+ )
+ expect(node.dig(:output_contracts, 0, :fields)).to include(
+ "tag_group_id" => "integer",
+ "tag_group_name" => "string",
+ "tag_names" => "array<string>",
+ )
+ end
+
it "exposes post author, topic link, and action output fields", :aggregate_failures do
expect(result[:status]).to eq("success")
expect(output_fields_by_type.fetch("trigger:topic_created")).to include(
@@ -169,6 +192,11 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog do
"topic_id" => "integer",
"tag_names" => "array<string>",
)
+ expect(output_fields_by_type.fetch("action:tag_group")).to include(
+ "tag_group_id" => "integer",
+ "tag_group_name" => "string",
+ "tag_names" => "array<string>",
+ )
expect(output_fields_by_type.fetch("action:post")).to include(
"post.id" => "integer",
"post.topic_id" => "integer",
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_resolve_entity_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_resolve_entity_spec.rb
index 884fbc1fc04..3701749ab3c 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_resolve_entity_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/ai/tools/workflow_resolve_entity_spec.rb
@@ -20,4 +20,23 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowResolveEntity do
matches: [{ id: category.id, name: category.name, slug: category.slug }],
)
end
+
+ it "resolves tag groups by name" do
+ tag_group = Fabricate(:tag_group, name: "Customer lifecycle")
+ Fabricate(:tag_group, name: "Unrelated")
+ context = DiscourseAi::Agents::BotContext.new(messages: [], user: admin)
+ result =
+ described_class.new(
+ { kind: "tag_group", query: "lifecycle" },
+ bot_user: Discourse.system_user,
+ llm: nil,
+ context: context,
+ ).invoke
+
+ expect(result).to eq(
+ status: "success",
+ kind: "tag_group",
+ matches: [{ id: tag_group.id, name: tag_group.name }],
+ )
+ end
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/tag_group_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/tag_group_spec.rb
new file mode 100644
index 00000000000..88dc2b64bbb
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/tag_group_spec.rb
@@ -0,0 +1,214 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::TagGroup::V1 do
+ fab!(:admin)
+ fab!(:tag) { Fabricate(:tag, name: "existing") }
+ fab!(:tag_group) { Fabricate(:tag_group, name: "Workflow tags", tags: [tag]) }
+
+ before { SiteSetting.tagging_enabled = true }
+
+ describe "#execute" do
+ let(:item) { { "json" => {} } }
+ let(:history_scope) { UserHistory.where(action: UserHistory.actions[:tag_group_change]) }
+
+ it "adds multiple tags, creates missing tags, and records the effective change" do
+ previous_value = TagGroupSerializer.new(tag_group).to_json(root: false)
+ config = {
+ "tag_group_id" => tag_group.id,
+ "tag_names" => "zulu tag, alpha",
+ "actor_username" => admin.username,
+ }
+
+ expect { @result = execute_node(configuration: config, item: item) }.to change {
+ history_scope.count
+ }.by(1)
+
+ expect(@result).to eq(
+ "tag_group_id" => tag_group.id,
+ "tag_group_name" => tag_group.name,
+ "tag_names" => %w[alpha existing zulu-tag],
+ )
+ expect(tag_group.reload.base_tags.order(:name).pluck(:name)).to eq(
+ %w[alpha existing zulu-tag],
+ )
+
+ history = history_scope.last
+ expect(history).to have_attributes(
+ acting_user_id: admin.id,
+ subject: tag_group.name,
+ previous_value: previous_value,
+ new_value: TagGroupSerializer.new(tag_group).to_json(root: false),
+ )
+ end
+
+ it "removes tags without deleting them and records the effective change" do
+ removed_tag = Fabricate(:tag, name: "remove-me")
+ tag_group.tags << removed_tag
+ config = {
+ "operation" => "remove",
+ "tag_group_id" => tag_group.id,
+ "tag_names" => "remove-me, absent",
+ "actor_username" => admin.username,
+ }
+
+ expect { @result = execute_node(configuration: config, item: item) }.to change {
+ history_scope.count
+ }.by(1)
+
+ expect(@result["tag_names"]).to eq(["existing"])
+ expect(tag_group.reload.base_tags).to contain_exactly(tag)
+ expect(Tag.find_by(id: removed_tag.id)).to eq(removed_tag)
+ end
+
+ it "does not record changes for duplicate additions or absent removals" do
+ add_config = {
+ "operation" => "add",
+ "tag_group_id" => tag_group.id,
+ "tag_names" => tag.name,
+ "actor_username" => admin.username,
+ }
+ remove_config = add_config.merge("operation" => "remove", "tag_names" => "absent")
+
+ expect {
+ add_result = execute_node(configuration: add_config, item: item)
+ remove_result = execute_node(configuration: remove_config, item: item)
+
+ expect(add_result["tag_names"]).to eq([tag.name])
+ expect(remove_result["tag_names"]).to eq([tag.name])
+ }.not_to change { history_scope.count }
+ end
+
+ it "resolves expressions independently for each input item" do
+ other_tag_group = Fabricate(:tag_group, name: "Other workflow tags")
+ input_items = [
+ { "json" => { "tag_group_id" => tag_group.id, "tag_names" => %w[first second] } },
+ { "json" => { "tag_group_id" => other_tag_group.id, "tag_names" => ["third"] } },
+ ]
+ config = {
+ "tag_group_id" => "={{ $json.tag_group_id }}",
+ "tag_names" => "={{ $json.tag_names }}",
+ }
+
+ result = execute_node_output(configuration: config, input_items: input_items).first
+
+ expect(result.map { |output| output["json"] }).to eq(
+ [
+ {
+ "tag_group_id" => tag_group.id,
+ "tag_group_name" => tag_group.name,
+ "tag_names" => %w[existing first second],
+ },
+ {
+ "tag_group_id" => other_tag_group.id,
+ "tag_group_name" => other_tag_group.name,
+ "tag_names" => %w[third],
+ },
+ ],
+ )
+ end
+
+ it "uses canonical tag membership for synonyms" do
+ synonym = Fabricate(:tag, name: "alias", target_tag: tag)
+ other_tag_group = Fabricate(:tag_group, name: "Synonym workflow tags")
+ add_config = {
+ "operation" => "add",
+ "tag_group_id" => other_tag_group.id,
+ "tag_names" => synonym.name,
+ }
+
+ add_result = execute_node(configuration: add_config, item: item)
+
+ expect(add_result["tag_names"]).to eq([tag.name])
+ expect(other_tag_group.reload.tags).to contain_exactly(tag, synonym)
+
+ remove_result =
+ execute_node(configuration: add_config.merge("operation" => "remove"), item: item)
+
+ expect(remove_result["tag_names"]).to be_empty
+ expect(other_tag_group.reload.tags).to be_empty
+ end
+
+ it "does not add the tag group's parent tag as a member" do
+ parent_tag = Fabricate(:tag, name: "parent")
+ tag_group.update!(parent_tag: parent_tag)
+ config = {
+ "tag_group_id" => tag_group.id,
+ "tag_names" => parent_tag.name,
+ "actor_username" => admin.username,
+ }
+
+ expect { @result = execute_node(configuration: config, item: item) }.not_to change {
+ history_scope.count
+ }
+
+ expect(@result["tag_names"]).to eq([tag.name])
+ expect(tag_group.reload.tags).to contain_exactly(tag)
+ end
+
+ it "raises when no tag names are provided" do
+ config = { "tag_group_id" => tag_group.id, "tag_names" => "" }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ "No tag names provided.",
+ )
+ end
+
+ it "rejects invalid tags without changing the group or audit log" do
+ config = {
+ "tag_group_id" => tag_group.id,
+ "tag_names" => "valid-tag, none",
+ "actor_username" => admin.username,
+ }
+
+ expect {
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ DiscourseWorkflows::NodeError,
+ /Tag group operation failed:/,
+ )
+ }.not_to change { history_scope.count }
+ expect(tag_group.reload.base_tags).to contain_exactly(tag)
+ expect(Tag.find_by_name("valid-tag")).to be_nil
+ end
+
+ it "raises when the tag group does not exist" do
+ config = { "tag_group_id" => -1, "tag_names" => "anything" }
+
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ ActiveRecord::RecordNotFound,
+ )
+ end
+
+ it "prevents a regular user from changing tag groups" do
+ user = Fabricate(:user)
+ config = {
+ "tag_group_id" => tag_group.id,
+ "tag_names" => "new-tag",
+ "actor_username" => user.username,
+ }
+
+ expect {
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ Discourse::InvalidAccess,
+ )
+ }.not_to change { history_scope.count }
+ expect(tag_group.reload.base_tags).to contain_exactly(tag)
+ end
+
+ it "prevents changes when tagging is disabled" do
+ SiteSetting.tagging_enabled = false
+ config = {
+ "tag_group_id" => tag_group.id,
+ "tag_names" => "new-tag",
+ "actor_username" => admin.username,
+ }
+
+ expect {
+ expect { execute_node(configuration: config, item: item) }.to raise_error(
+ Discourse::InvalidAccess,
+ )
+ }.not_to change { history_scope.count }
+ expect(tag_group.reload.base_tags).to contain_exactly(tag)
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/dynamic_node_parameters_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/dynamic_node_parameters_controller_spec.rb
index 121867ab1ad..7ed23d8d91d 100644
--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/dynamic_node_parameters_controller_spec.rb
+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/dynamic_node_parameters_controller_spec.rb
@@ -8,6 +8,7 @@ RSpec.describe DiscourseWorkflows::DynamicNodeParametersController do
describe "POST /admin/plugins/discourse-workflows/dynamic-node-parameters/options" do
fab!(:badge) { Fabricate(:badge, name: "Helpful") }
fab!(:group_1) { Fabricate(:group, name: "alpha") }
+ fab!(:tag_group) { Fabricate(:tag_group, name: "Workflow tags") }
it "returns options for a known load options method" do
post "/admin/plugins/discourse-workflows/dynamic-node-parameters/options.json",
@@ -45,6 +46,30 @@ RSpec.describe DiscourseWorkflows::DynamicNodeParametersController do
expect(response.parsed_body).to include("id" => group_1.id, "name" => group_1.name)
end
+ it "returns filtered tag groups for the Tag group node" do
+ Fabricate(:tag_group, name: "Unrelated")
+
+ post "/admin/plugins/discourse-workflows/dynamic-node-parameters/options.json",
+ params: {
+ nodeTypeAndVersion: {
+ name: "action:tag_group",
+ version: "1.0",
+ },
+ path: "tag_group_id",
+ methodName: "tag_groups",
+ currentNodeParameters: {
+ },
+ filter: "workflow",
+ },
+ as: :json
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body).to contain_exactly(
+ "id" => tag_group.id,
+ "name" => tag_group.name,
+ )
+ end
+
it "returns 404 for an unknown node type" do
post "/admin/plugins/discourse-workflows/dynamic-node-parameters/options.json",
params: {
diff --git a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb
index f0bf3c33b21..c4a7f8de3be 100644
--- a/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb
+++ b/plugins/discourse-workflows/spec/requests/discourse_workflows/node_types_controller_spec.rb
@@ -26,6 +26,7 @@ RSpec.describe DiscourseWorkflows::NodeTypesController do
expect(identifiers).to include("trigger:topic_closed")
expect(identifiers).to include("action:topic_tags")
+ expect(identifiers).to include("action:tag_group")
expect(identifiers).to include("action:post")
expect(identifiers).to include("action:topic")
expect(identifiers).to include("action:group")
@@ -97,6 +98,44 @@ RSpec.describe DiscourseWorkflows::NodeTypesController do
)
end
+ it "returns Tag group action metadata" do
+ tag_group = Fabricate(:tag_group, name: "Workflow tags")
+
+ get "/admin/plugins/discourse-workflows/node-types.json"
+
+ tag_group_node =
+ response.parsed_body["node_types"].find do |node_type|
+ node_type["identifier"] == "action:tag_group"
+ end
+ properties = tag_group_node["properties"]
+
+ expect(properties["operation"]).to include(
+ "type" => "options",
+ "default" => "add",
+ "options" => %w[add remove],
+ )
+ expect(properties["tag_group_id"]).to include(
+ "type" => "integer",
+ "required" => true,
+ "type_options" => include("load_options_method" => "tag_groups"),
+ "ui" => include("control" => "combo_box", "dynamic_value" => "tag_group_id"),
+ )
+ expect(properties["tag_names"]).to include(
+ "type" => "string",
+ "required" => true,
+ "ui" => include("control" => "tags"),
+ )
+ expect(tag_group_node.dig("metadata", "tag_groups")).to include(
+ "id" => tag_group.id,
+ "name" => tag_group.name,
+ )
+ expect(tag_group_node.dig("output_contracts", 0, "schema", "properties")).to include(
+ "tag_group_id" => include("type" => "integer"),
+ "tag_group_name" => include("type" => "string"),
+ "tag_names" => include("type" => "array"),
+ )
+ end
+
it "includes load options metadata in node type response" do
get "/admin/plugins/discourse-workflows/node-types.json"