Skip to content

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

FEATURE: add the ability to add tagging automation on topic closed (#38134)

Jeff Wong011f63dd77e5
diff --git a/plugins/automation/lib/discourse_automation/scripts/auto_tag_topic.rb b/plugins/automation/lib/discourse_automation/scripts/auto_tag_topic.rb
index 65a63f75905..1d009bb22f2 100644
--- a/plugins/automation/lib/discourse_automation/scripts/auto_tag_topic.rb
+++ b/plugins/automation/lib/discourse_automation/scripts/auto_tag_topic.rb
@@ -5,14 +5,19 @@ DiscourseAutomation::Scriptable.add(DiscourseAutomation::Scripts::AUTO_TAG_TOPIC
version 1
- triggerables %i[post_created_edited pm_created]
+ triggerables %i[post_created_edited pm_created topic_closed]
script do |context, fields|
- post = context["post"]
+ topic = nil
+ if context["topic"]
+ topic = context["topic"]
+ else
+ post = context["post"]
- next if !post.is_first_post?
- next if !post.topic
- next unless topic = Topic.find_by(id: post.topic.id)
+ next if !post.is_first_post?
+ next if !post.topic
+ next unless topic = Topic.find_by(id: post.topic.id)
+ end
tags = fields.dig("tags", "value")
diff --git a/plugins/automation/spec/scripts/auto_tag_topic_spec.rb b/plugins/automation/spec/scripts/auto_tag_topic_spec.rb
index 1b0862cf1b1..c6e10b6a8f4 100644
--- a/plugins/automation/spec/scripts/auto_tag_topic_spec.rb
+++ b/plugins/automation/spec/scripts/auto_tag_topic_spec.rb
@@ -75,4 +75,22 @@ describe "AutoTagTopic" do
end
end
end
+
+ context "with a topic" do
+ context "when tags list is empty" do
+ it "exits early with no error" do
+ expect { automation.trigger!("topic" => topic) }.to_not raise_error
+ end
+ end
+
+ context "when there are tags" do
+ before { automation.upsert_field!("tags", "tags", { value: %w[tag1 tag2] }) }
+
+ it "works" do
+ automation.trigger!("topic" => topic)
+
+ expect(topic.reload.tags.pluck(:name)).to match_array(%w[tag1 tag2])
+ end
+ end
+ end
end