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

Topic closed target is (obviously) a topic.
Auto tag topic currently targets a (first) post's topic.

Adds support to also pass a topic directly to auto_tag_topic automation.

In turn, allows adding `topic_closed` triggerable to the auto_tag_topic
automation.
This commit is contained in:
Jeff Wong
2026-03-02 14:46:44 -08:00
committed by GitHub
parent 7045ac189d
commit 011f63dd77
2 changed files with 28 additions and 5 deletions
@@ -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")
@@ -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