Skip to content

Commit dd655f1

Browse files
ducksZogStriP
andauthored
FEATURE: Add category ignore list for GitHub linkbacks (discourse#35921)
Sometimes you have categories where posts mention GitHub URLs but you don't actually want linkback comments posted to GitHub — think AI-generated summaries or automated reports that reference commits but would just create noise on the issue tracker. This adds a `github_linkback_ignored_categories` site setting that lets admins specify which categories should be skipped entirely when processing linkbacks. Posts in those categories won't trigger any GitHub comments, keeping things clean. To make this work nicely, the setting uses the `category_list` type which gives you a proper category picker in the admin UI rather than having to type raw IDs. This required extending core's `_map` metaprogramming helper (which previously only worked with `group_list` settings) to also handle `category_list` — so now any `category_list` setting automatically gets a `_map` variant that returns an array of integers, just like group settings do. Screenshot of new setting: <img width="774" height="116" alt="discourse-github-ignore-cat" src="https://github.com/user-attachments/assets/dd9a3c85-7f01-47c6-b1b7-fe6ef120ab8b" /> Ref: /t/141863 --------- Co-authored-by: zogstrip <regis@hanol.fr>
1 parent 004a536 commit dd655f1

5 files changed

Lines changed: 59 additions & 4 deletions

File tree

lib/site_setting_extension.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -963,10 +963,10 @@ def setup_methods(name)
963963
end
964964
end
965965

966-
# Any group_list setting, e.g. personal_message_enabled_groups, will have
967-
# a getter defined with _map on the end, e.g. personal_message_enabled_groups_map,
968-
# to avoid having to manually split and convert to integer for these settings.
969-
if type_supervisor.get_type(name) == :group_list
966+
# Any group_list or category_list setting will have a getter defined with _map
967+
# on the end, e.g. personal_message_enabled_groups_map, to avoid having to
968+
# manually split and convert to integer for these settings.
969+
if %i[group_list category_list].include?(type_supervisor.get_type(name))
970970
define_singleton_method("#{clean_name}_map") do
971971
self.public_send(clean_name).to_s.split("|").map(&:to_i)
972972
end

plugins/discourse-github/app/lib/github_linkback.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ def initialize(post)
2020
end
2121

2222
def should_enqueue?
23+
return false if ignored_category?
24+
2325
!!(
2426
SiteSetting.github_linkback_enabled? && SiteSetting.enable_discourse_github_plugin? &&
2527
@post.present? && @post.post_type == Post.types[:regular] && @post.raw =~ /github\.com/ &&
@@ -125,6 +127,12 @@ def self.field_for(url)
125127

126128
private
127129

130+
def ignored_category?
131+
return false if @post&.topic&.category_id.blank?
132+
133+
SiteSetting.github_linkback_ignored_categories_map.include?(@post.topic.category_id)
134+
end
135+
128136
def post_pr_or_issue(link, type)
129137
pr_or_issue_number = link.pr_number || link.issue_number
130138
github_url =

plugins/discourse-github/config/settings.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ discourse_github:
1313
github_linkback_maximum_links:
1414
default: 25
1515
min: 1
16+
github_linkback_ignored_categories:
17+
default: ""
18+
type: category_list
1619
github_badges_enabled:
1720
default: false
1821
github_badges_repos:

plugins/discourse-github/spec/lib/github_linkback_spec.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,45 @@
109109
expect(GithubLinkback.new(unlisted_post).should_enqueue?).to eq(false)
110110
end
111111
end
112+
113+
describe "ignored categories" do
114+
fab!(:category)
115+
fab!(:post_in_category) do
116+
topic = Fabricate(:topic, category:)
117+
Fabricate(:post, topic:, raw: "https://github.com/discourse/discourse/commit/abc123")
118+
end
119+
120+
before { SiteSetting.github_linkback_enabled = true }
121+
122+
it "doesn't enqueue posts in ignored categories" do
123+
SiteSetting.github_linkback_ignored_categories = category.id.to_s
124+
expect(GithubLinkback.new(post_in_category).should_enqueue?).to eq(false)
125+
end
126+
127+
it "enqueues posts in non-ignored categories" do
128+
other_category = Fabricate(:category)
129+
SiteSetting.github_linkback_ignored_categories = other_category.id.to_s
130+
expect(GithubLinkback.new(post_in_category).should_enqueue?).to eq(true)
131+
end
132+
133+
it "handles multiple ignored categories" do
134+
other_category = Fabricate(:category)
135+
SiteSetting.github_linkback_ignored_categories = "#{category.id}|#{other_category.id}"
136+
expect(GithubLinkback.new(post_in_category).should_enqueue?).to eq(false)
137+
end
138+
139+
it "enqueues when ignored categories setting is empty" do
140+
SiteSetting.github_linkback_ignored_categories = ""
141+
expect(GithubLinkback.new(post_in_category).should_enqueue?).to eq(true)
142+
end
143+
144+
it "enqueues posts without a category" do
145+
SiteSetting.github_linkback_ignored_categories = category.id.to_s
146+
uncategorized_post =
147+
Fabricate(:post, raw: "https://github.com/discourse/discourse/commit/abc123")
148+
expect(GithubLinkback.new(uncategorized_post).should_enqueue?).to eq(true)
149+
end
150+
end
112151
end
113152

114153
describe "#github_links" do

spec/lib/site_setting_extension_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,11 @@ def self.translate_names?
13921392
expect(SiteSetting.personal_message_enabled_groups_map).to eq([1, 2])
13931393
end
13941394

1395+
it "handles splitting category_list settings" do
1396+
SiteSetting.digest_suppress_categories = "3|4"
1397+
expect(SiteSetting.digest_suppress_categories_map).to eq([3, 4])
1398+
end
1399+
13951400
it "handles splitting compact list settings" do
13961401
SiteSetting.markdown_linkify_tlds = "com|net"
13971402
expect(SiteSetting.markdown_linkify_tlds_map).to eq(%w[com net])

0 commit comments

Comments
 (0)