From 8c355d9e99414639174a73a7aeb6d8f887c25c10 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Wed, 18 Oct 2023 14:24:14 +1100 Subject: [PATCH] FIX: log for CleanUpTags job (#23964) In previous [PR](https://github.com/discourse/discourse/pull/23864) we introduced setting to automatically delete unused tags. This action should be logged. --- app/jobs/scheduled/clean_up_tags.rb | 9 ++++++++- spec/jobs/clean_up_tags_spec.rb | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/app/jobs/scheduled/clean_up_tags.rb b/app/jobs/scheduled/clean_up_tags.rb index 835ae8d689b..68cff5a4541 100644 --- a/app/jobs/scheduled/clean_up_tags.rb +++ b/app/jobs/scheduled/clean_up_tags.rb @@ -8,7 +8,14 @@ module Jobs def execute(args) return unless SiteSetting.automatically_clean_unused_tags - Tag.unused.where("tags.created_at < ?", GRACE_PERIOD_MINUTES.minutes.ago).destroy_all + Tag.transaction do + tags = Tag.unused.where("tags.created_at < ?", GRACE_PERIOD_MINUTES.minutes.ago) + StaffActionLogger.new(Discourse.system_user).log_custom( + "deleted_unused_tags", + tags: tags.pluck(:name), + ) + tags.destroy_all + end end end end diff --git a/spec/jobs/clean_up_tags_spec.rb b/spec/jobs/clean_up_tags_spec.rb index 34f48ef29b3..ac6dc9b8d1e 100644 --- a/spec/jobs/clean_up_tags_spec.rb +++ b/spec/jobs/clean_up_tags_spec.rb @@ -43,6 +43,16 @@ describe Jobs::CleanUpTags do created_at: 10.minutes.ago, ) end + fab!(:unused_tag_2) do + Fabricate( + :tag, + name: "unused2", + staff_topic_count: 0, + public_topic_count: 0, + pm_topic_count: 0, + created_at: 10.minutes.ago, + ) + end fab!(:tag_in_group) do Fabricate( @@ -57,8 +67,17 @@ describe Jobs::CleanUpTags do it "deletes unused tags" do SiteSetting.automatically_clean_unused_tags = true - expect { job.execute({}) }.to change { Tag.count }.by(-1) + expect { job.execute({}) }.to change { Tag.count }.by(-2) expect { unused_tag.reload }.to raise_error(ActiveRecord::RecordNotFound) + expect { unused_tag_2.reload }.to raise_error(ActiveRecord::RecordNotFound) + end + + it "logs action" do + SiteSetting.automatically_clean_unused_tags = true + expect { job.execute({}) }.to change { UserHistory.count }.by(1) + log = UserHistory.last + expect(log.acting_user_id).to eq(-1) + expect(log.details).to eq('tags: ["unused1", "unused2"]') end it "does nothing when site setting is disabled by default" do