Skip to content

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

FEATURE: Add tag creation workflow trigger (#43122)

Joffrey JAFFEUXbd8b99b84bb3
diff --git a/plugins/discourse-workflows/config/locales/client.en.yml b/plugins/discourse-workflows/config/locales/client.en.yml
index 97e40769350..8ce31126dac 100644
--- a/plugins/discourse-workflows/config/locales/client.en.yml
+++ b/plugins/discourse-workflows/config/locales/client.en.yml
@@ -221,6 +221,7 @@ en:
"trigger:topic_created": "Topic created"
"trigger:topic_category_changed": "Topic category changed"
"trigger:topic_tag_changed": "Topic tag changed"
+ "trigger:tag_created": "Tag created"
"trigger:user_added_to_group": "User added to group"
"trigger:user_removed_from_group": "User removed from group"
"trigger:user_created": "User created"
@@ -280,6 +281,7 @@ en:
"trigger:topic_created": "Fires when a new topic is created"
"trigger:topic_category_changed": "Fires when a topic is moved to a different category"
"trigger:topic_tag_changed": "Fires when tags are added to or removed from a topic"
+ "trigger:tag_created": "Fires when a new tag is created"
"trigger:user_added_to_group": "Fires when a user is added to a selected group"
"trigger:user_removed_from_group": "Fires when a user is removed from a selected group"
"trigger:user_created": "Fires when a new user account is created"
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 e33c6f0e7ef..5c132bc71a0 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
@@ -234,6 +234,7 @@ module DiscourseWorkflows
"trigger:reviewable_created" =>
"review queue flag flagged spam moderation pending needs approval queued post akismet",
"trigger:badge_granted" => "badge award achievement medal granted earned",
+ "trigger:tag_created" => "taxonomy label keyword created new",
"action:user" =>
"user profile bio title trust level lock groups fields lookup edit update",
"action:flag_post" =>
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/nodes/tag_created/v1.rb b/plugins/discourse-workflows/lib/discourse_workflows/nodes/tag_created/v1.rb
new file mode 100644
index 00000000000..be9bb75b05f
--- /dev/null
+++ b/plugins/discourse-workflows/lib/discourse_workflows/nodes/tag_created/v1.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+module DiscourseWorkflows
+ module Nodes
+ module TagCreated
+ class V1 < NodeType
+ description(
+ name: "trigger:tag_created",
+ version: "1.0",
+ defaults: {
+ icon: "tag",
+ color: "deep-orange",
+ },
+ group: "discourse_triggers",
+ event: :tag_created,
+ output_contracts: [{ schema: Schema::TAG_SCHEMA }],
+ )
+
+ def initialize(tag, *)
+ super(parameters: {})
+ @tag = tag
+ end
+
+ def valid?
+ @tag.present?
+ end
+
+ def output
+ { tag: serialize_record(@tag, TagSerializer) }
+ end
+ end
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/schema.rb b/plugins/discourse-workflows/lib/discourse_workflows/schema.rb
index 46c7b1da62b..d35efd3d80b 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/schema.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/schema.rb
@@ -385,6 +385,20 @@ module DiscourseWorkflows
REVIEWABLE_EVENT_SCHEMA =
entity("reviewable", REVIEWABLE_PROPERTIES, "Review queue item payload")
+ TAG_PROPERTIES = JSON.parse(<<~JSON).freeze
+ {
+ "id": { "type": "integer" },
+ "name": { "type": "string" },
+ "slug": { "type": "string" },
+ "topic_count": { "type": "integer" },
+ "staff": { "type": "boolean" },
+ "description": { "type": ["string", "null"] },
+ "description_cooked": { "type": ["string", "null"] }
+ }
+ JSON
+
+ TAG_SCHEMA = entity("tag", TAG_PROPERTIES, "Tag created by Discourse")
+
BADGE_PROPERTIES = JSON.parse(<<~JSON).freeze
{
"id": { "type": "integer" },
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 20703b1c6c2..bbb1844bc35 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
@@ -130,6 +130,15 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog do
"topic.closed" => "boolean",
"topic.archived" => "boolean",
)
+ expect(output_fields_by_type.fetch("trigger:tag_created")).to include(
+ "tag.id" => "integer",
+ "tag.name" => "string",
+ "tag.slug" => "string",
+ "tag.topic_count" => "integer",
+ "tag.staff" => "boolean",
+ "tag.description" => "string|null",
+ "tag.description_cooked" => "string|null",
+ )
{
"trigger:user_added_to_group" => "\"added\"",
"trigger:user_removed_from_group" => "\"removed\"",
@@ -243,6 +252,12 @@ RSpec.describe DiscourseWorkflows::Ai::Tools::WorkflowNodeCatalog do
expect(output_fields_by_type.fetch("action:ai_agent")).to include("result" => "string")
end
+ it "finds the tag created trigger by taxonomy keywords" do
+ result = invoke_tool(query: "taxonomy keyword")
+
+ expect(result[:nodes].map { |node| node[:type] }).to include("trigger:tag_created")
+ end
+
it "matches broad multi-term catalog queries", :aggregate_failures do
result =
described_class.new(
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb
index eec948aca99..39bcbc60076 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/event_listener_spec.rb
@@ -46,6 +46,23 @@ RSpec.describe DiscourseWorkflows::EventListener do
expect(jobs).to be_empty
end
+ it "enqueues tag created workflows with the tag payload" do
+ create_published_workflow("tag-created-trigger", "trigger:tag_created")
+
+ created_tag = Fabricate(:tag, name: "new-workflow-tag")
+
+ expect(enqueued_trigger_node_ids).to contain_exactly("tag-created-trigger")
+ expect(trigger_data_for("tag-created-trigger")).to include(
+ "tag" =>
+ include(
+ "id" => created_tag.id,
+ "name" => created_tag.name,
+ "slug" => created_tag.slug,
+ "description" => created_tag.description,
+ ),
+ )
+ end
+
it "only enqueues topic closed workflows matching the category and tags" do
topic.tags << tag
create_published_workflow(
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/tag_created_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/tag_created_spec.rb
new file mode 100644
index 00000000000..5cf28c166b5
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/nodes/tag_created_spec.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Nodes::TagCreated::V1 do
+ fab!(:tag) { Fabricate(:tag, name: "workflow-tag") }
+
+ describe "#valid?" do
+ it "returns true when the tag exists" do
+ expect(described_class.new(tag)).to be_valid
+ end
+
+ it "returns false when the tag is missing" do
+ expect(described_class.new(nil)).not_to be_valid
+ end
+ end
+
+ describe "#output" do
+ it "returns the tag payload", :aggregate_failures do
+ tag.update_columns(description: "A workflow tag", description_cooked: "<p>A workflow tag</p>")
+
+ output = described_class.new(tag).output
+
+ expect(output[:tag]).to include(
+ id: tag.id,
+ name: tag.name,
+ slug: tag.slug,
+ topic_count: tag.staff_topic_count,
+ staff: false,
+ description: tag.description,
+ description_cooked: tag.description_cooked,
+ )
+ expect(output).to match_node_output_schema(described_class)
+ end
+ end
+end