Skip to content

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

FIX: Skip rebaking posts with inline GitHub PR oneboxes (#36576)

Régis Hanol2ea603cd2193
diff --git a/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb b/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb
index 527536cbace..a01f0bc813c 100644
--- a/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb
+++ b/plugins/discourse-github/app/jobs/regular/rebake_github_pr_posts.rb
@@ -46,7 +46,7 @@ module Jobs
def has_github_pr_onebox?(cooked, pr_url)
# quick & dirty check to avoid doing unnecessary rebakes
- cooked.present? && cooked.include?("onebox") && cooked.include?(pr_url)
+ cooked.present? && cooked.include?("githubpullrequest") && cooked.include?(pr_url)
end
end
end
diff --git a/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb b/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb
index fd0fa444f9d..a67ab65cb75 100644
--- a/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb
+++ b/plugins/discourse-github/spec/jobs/rebake_github_pr_posts_spec.rb
@@ -4,94 +4,57 @@ RSpec.describe Jobs::RebakeGithubPrPosts do
fab!(:user)
fab!(:topic)
let(:pr_url) { "https://github.com/discourse/discourse/pull/123" }
+ let(:domain) { "github.com" }
before { enable_current_plugin }
+ def create_post_with_link(cooked)
+ Fabricate(:post, topic:, user:, cooked:).tap do |post|
+ TopicLink.create!(topic:, post:, user:, url: pr_url, domain:)
+ end
+ end
+
describe "#execute" do
- it "does nothing with blank pr_url" do
+ it "does nothing with blank or missing pr_url" do
expect { described_class.new.execute(pr_url: nil) }.not_to raise_error
expect { described_class.new.execute(pr_url: "") }.not_to raise_error
end
- context "with oneboxed PR link" do
- fab!(:post) { Fabricate(:post, topic: topic, user: user) }
-
- before do
- post.update!(cooked: <<~HTML)
- <p>Check out this PR:</p>
- <aside class="onebox githubpullrequest" data-onebox-src="#{pr_url}">
- <header class="source"><a href="#{pr_url}">github.com</a></header>
- <article class="onebox-body">
- <div class="github-row">
- <span class="github-pr-title">Test PR</span>
- </div>
- </article>
- </aside>
- HTML
+ it "rebakes posts with full GitHub PR oneboxes" do
+ create_post_with_link(<<~HTML)
+ <aside class="onebox githubpullrequest"><a href="#{pr_url}">PR</a></aside>
+ HTML
- TopicLink.create!(
- topic_id: topic.id,
- post_id: post.id,
- user_id: user.id,
- url: pr_url,
- domain: "github.com",
- )
- end
+ expect_any_instance_of(Post).to receive(:rebake!).with(
+ invalidate_oneboxes: true,
+ priority: :low,
+ )
- it "rebakes posts with oneboxed PR links" do
- expect_any_instance_of(Post).to receive(:rebake!).with(
- invalidate_oneboxes: true,
- priority: :low,
- )
- described_class.new.execute(pr_url: pr_url)
- end
+ described_class.new.execute(pr_url:)
+ end
- it "matches URLs with path suffixes" do
- TopicLink.create!(
- topic_id: topic.id,
- post_id: post.id,
- user_id: user.id,
- url: "#{pr_url}/files",
- domain: "github.com",
- )
+ it "does not rebake posts with plain links or inline oneboxes" do
+ create_post_with_link(%(<a href="#{pr_url}">plain link</a>))
+ create_post_with_link(%(<a href="#{pr_url}" class="inline-onebox">inline onebox</a>))
- post_ids =
- TopicLink
- .where(url: pr_url)
- .or(TopicLink.where("url LIKE ?", "#{pr_url}%"))
- .pluck(:post_id)
+ expect_any_instance_of(Post).not_to receive(:rebake!)
- expect(post_ids).to include(post.id)
- end
+ described_class.new.execute(pr_url:)
end
- context "with inline PR link (not oneboxed)" do
- fab!(:post) { Fabricate(:post, topic: topic, user: user) }
-
- before do
- post.update!(cooked: <<~HTML)
- <p>Check out <a href="#{pr_url}">this PR</a> for details.</p>
- HTML
+ it "matches PR URLs with path suffixes like /files or /commits" do
+ post = create_post_with_link(<<~HTML)
+ <aside class="onebox githubpullrequest"><a href="#{pr_url}">PR</a></aside>
+ HTML
- TopicLink.create!(
- topic_id: topic.id,
- post_id: post.id,
- user_id: user.id,
- url: pr_url,
- domain: "github.com",
- )
- end
+ TopicLink.create!(topic:, post:, user:, url: "#{pr_url}/files", domain:)
- it "does not rebake posts with only inline links" do
- expect_any_instance_of(Post).not_to receive(:rebake!)
- described_class.new.execute(pr_url: pr_url)
- end
- end
+ expect_any_instance_of(Post).to receive(:rebake!).with(
+ invalidate_oneboxes: true,
+ priority: :low,
+ )
- context "with no matching posts" do
- it "completes without error" do
- expect { described_class.new.execute(pr_url: pr_url) }.not_to raise_error
- end
+ described_class.new.execute(pr_url:)
end
end
end