A platform for community discussion. Free, open, simple.
FEATURE: Add PR status icon to inline oneboxes (#39649)
Jarek Radosz6db2c7696f2e
diff --git a/frontend/discourse-markdown-it/src/features/onebox.js b/frontend/discourse-markdown-it/src/features/onebox.jsindex cda5fe00eb4..c623b0542d9 100644--- a/frontend/discourse-markdown-it/src/features/onebox.js+++ b/frontend/discourse-markdown-it/src/features/onebox.js@@ -100,9 +100,12 @@ function applyOnebox(state, silent) {if (onebox && onebox.title) {text.content = onebox.title;- attrs.push(["class", "inline-onebox"]);+ child.attrJoin("class", "inline-onebox");+ if (onebox.css_class) {+ child.attrJoin("class", onebox.css_class);+ }} else if (!onebox) {- attrs.push(["class", "inline-onebox-loading"]);+ child.attrJoin("class", "inline-onebox-loading");}}}diff --git a/frontend/discourse/tests/unit/lib/pretty-text/inline-oneboxer-test.js b/frontend/discourse/tests/unit/lib/pretty-text/inline-oneboxer-test.jsindex 2573b7def87..27b2b49042b 100644--- a/frontend/discourse/tests/unit/lib/pretty-text/inline-oneboxer-test.js+++ b/frontend/discourse/tests/unit/lib/pretty-text/inline-oneboxer-test.js@@ -21,6 +21,29 @@ module("Unit | Pretty Text | Inline Oneboxer", function (hooks) {links = {};});+ test("applies the css_class returned by the server", async function (assert) {+ const url = "http://example.com/pr";+ const link = document.createElement("a");+ link.classList.add("inline-onebox-loading");+ link.href = url;++ pretender.get("/inline-onebox", async () =>+ response({+ "inline-oneboxes": [+ { url, title: "PR title", css_class: "--gh-status-merged" },+ ],+ })+ );++ applyInlineOneboxes({ [url]: [link] }, ajax);+ await settled();++ assert.dom(link).hasClass("inline-onebox");+ assert.dom(link).hasClass("--gh-status-merged");+ assert.dom(link).doesNotHaveClass("inline-onebox-loading");+ assert.dom(link).hasText("PR title");+ });+test("batches requests when oneboxing more than 10 urls", async function (assert) {const requestedUrls = [];let requestCount = 0;diff --git a/frontend/pretty-text/addon/allow-lister.js b/frontend/pretty-text/addon/allow-lister.jsindex bc0fb84bb0f..480c1246268 100644--- a/frontend/pretty-text/addon/allow-lister.js+++ b/frontend/pretty-text/addon/allow-lister.js@@ -121,6 +121,12 @@ export const DEFAULT_LIST = ["a.onebox",`a.inline-onebox`,`a.inline-onebox-loading`,+ "a[class=inline-onebox --gh-status-draft]",+ "a[class=inline-onebox --gh-status-open]",+ "a[class=inline-onebox --gh-status-approved]",+ "a[class=inline-onebox --gh-status-changes_requested]",+ "a[class=inline-onebox --gh-status-merged]",+ "a[class=inline-onebox --gh-status-closed]","a[data-bbcode]","a[data-word]","a[name]",diff --git a/frontend/pretty-text/addon/inline-oneboxer.js b/frontend/pretty-text/addon/inline-oneboxer.jsindex 8dbdb578895..c6c0b62fbf7 100644--- a/frontend/pretty-text/addon/inline-oneboxer.js+++ b/frontend/pretty-text/addon/inline-oneboxer.js@@ -34,6 +34,9 @@ export async function applyInlineOneboxes(inline, ajax, opts) {links.forEach((link) => {link.innerText = onebox.title;link.classList.add("inline-onebox");+ if (onebox.css_class) {+ link.classList.add(onebox.css_class);+ }link.classList.remove("inline-onebox-loading");});}diff --git a/lib/cooked_processor_mixin.rb b/lib/cooked_processor_mixin.rbindex b21e5843393..8db836d9520 100644--- a/lib/cooked_processor_mixin.rb+++ b/lib/cooked_processor_mixin.rb@@ -345,6 +345,9 @@ module CookedProcessorMixinif title = inline_onebox&.dig(:title)element.children = CGI.escapeHTML(title)element.add_class("inline-onebox")+ if css_class = inline_onebox[:css_class]+ element.add_class(css_class)+ endendremove_inline_onebox_loading_class(element)diff --git a/lib/inline_oneboxer.rb b/lib/inline_oneboxer.rbindex 22187606af8..2eb89218a7d 100644--- a/lib/inline_oneboxer.rb+++ b/lib/inline_oneboxer.rb@@ -58,6 +58,10 @@ class InlineOneboxerendend+ if data = Oneboxer.inline_data_for(url)+ return onebox_for(url, data[:title], opts, css_class: data[:css_class])+ end+always_allow = SiteSetting.enable_inline_onebox_on_all_domainsallowed_domains = SiteSetting.allowed_inline_onebox_domains&.split("|") unless always_allow@@ -105,7 +109,7 @@ class InlineOneboxerprivate- def self.onebox_for(url, title, opts)+ def self.onebox_for(url, title, opts, css_class: nil)title = title && Emoji.gsub_emoji_to_unicode(title)if title && opts[:post_number]title += " - "@@ -126,6 +130,7 @@ class InlineOneboxertitle = WordWatcher.censor_text(title) if title.present?onebox = { url: url, title: title }+ onebox[:css_class] = css_class if css_class.present?Discourse.cache.write(cache_key(url), onebox, expires_in: 1.day) if !opts[:skip_cache]oneboxdiff --git a/lib/onebox/engine/github_pull_request_onebox.rb b/lib/onebox/engine/github_pull_request_onebox.rbindex cbbfdb2f747..3e34bdbce50 100644--- a/lib/onebox/engine/github_pull_request_onebox.rb+++ b/lib/onebox/engine/github_pull_request_onebox.rb@@ -23,6 +23,22 @@ module Onebox"https://api.github.com/repos/#{match[:org]}/#{match[:repository]}/pulls/#{match[:number]}"end+ def inline_data+ return unless SiteSetting.github_pr_status_enabled++ pr_data = raw(github_auth_header(match[:org]))+ status = fetch_pr_status(pr_data)&.dig(:status)+ return unless status++ title =+ "#{pr_data["title"]} · Pull Request ##{match[:number]} · " \+ "#{match[:org]}/#{match[:repository]}"+ { title: title, css_class: "--gh-status-#{status}" }+ rescue StandardError => e+ Rails.logger.warn("Inline GitHub PR onebox error for #{@url}: #{e.message}")+ nil+ end+privatedef matchdiff --git a/lib/onebox/matcher.rb b/lib/onebox/matcher.rbindex 2f4b7fc3ffa..f47d0c38d5d 100644--- a/lib/onebox/matcher.rb+++ b/lib/onebox/matcher.rb@@ -17,7 +17,7 @@ module Oneboxenddef oneboxed- return if @uri.nil?+ return if @uri.nil? || @uri.host.nil?return if @uri.port && !Onebox.options.allowed_ports.include?(@uri.port)return if @uri.scheme && !Onebox.options.allowed_schemes.include?(@uri.scheme)diff --git a/lib/oneboxer.rb b/lib/oneboxer.rbindex db04b426ac9..3aa54acab02 100644--- a/lib/oneboxer.rb+++ b/lib/oneboxer.rb@@ -127,6 +127,13 @@ module OneboxerDiscourse.cache.delete(onebox_failed_cache_key(url))end+ def self.inline_data_for(url)+ engine_class = engine(url)+ return if engine_class.nil? || !engine_class.method_defined?(:inline_data)++ engine_class.new(url).inline_data+ end+# Parse URLs out of HTML, returning the document when finished.def self.each_onebox_link(doc, extra_paths: [])onebox_links = doc.css("a.#{ONEBOX_CSS_CLASS}", *extra_paths)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.rbindex 50e484f59d5..30503556d80 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@@ -8,8 +8,8 @@ module Jobsurl = args[:pr_url]return if url.blank?- # invalidate & refresh the onebox cache for this PR URLOneboxer.preview(url, invalidate_oneboxes: true)+ InlineOneboxer.invalidate(url)rebake_posts(url)rebake_chat_messages(url) if SiteSetting.chat_enabled@@ -22,7 +22,10 @@ module JobsPost.where(id: post_ids)- .where("cooked LIKE ?", "%githubpullrequest%#{url}%")+ .where(+ "cooked LIKE :pattern AND (cooked LIKE '%githubpullrequest%' OR cooked LIKE '%inline-onebox%')",+ pattern: "%#{url}%",+ ).find_each { |post| post.rebake!(priority: :low, skip_publish_rebaked_changes: true) }end@@ -35,7 +38,10 @@ module Jobs::Chat::Message.where(id: message_ids)- .where("cooked LIKE ?", "%githubpullrequest%#{url}%")+ .where(+ "cooked LIKE :pattern AND (cooked LIKE '%githubpullrequest%' OR cooked LIKE '%inline-onebox%')",+ pattern: "%#{url}%",+ ).find_each { |message| message.rebake!(priority: :low, skip_notifications: true) }endenddiff --git a/plugins/discourse-github/assets/stylesheets/common/github-pr-status.scss b/plugins/discourse-github/assets/stylesheets/common/github-pr-status.scssindex 974e0063038..bcc15544e96 100644--- a/plugins/discourse-github/assets/stylesheets/common/github-pr-status.scss+++ b/plugins/discourse-github/assets/stylesheets/common/github-pr-status.scss@@ -4,18 +4,46 @@ $icon-aspect-ratio: calc(12 / 16);$icon-width: 2.5em;$icon-width-mobile: 1.8em;-.onebox.githubpullrequest .github-row {+.onebox.githubpullrequest .github-row,+a.inline-onebox {--gh-icon-draft: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.25 1A2.25 2.25 0 0 1 4 5.372v5.256a2.251 2.251 0 1 1-1.5 0V5.372A2.251 2.251 0 0 1 3.25 1Zm9.5 14a2.25 2.25 0 1 1 0-4.5 2.25 2.25 0 0 1 0 4.5ZM2.5 3.25a.75.75 0 1 0 1.5 0 .75.75 0 0 0-1.5 0ZM3.25 12a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm9.5 0a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5ZM14 4.25a.75.75 0 0 1-.75.75h-2a.75.75 0 0 1 0-1.5h2a.75.75 0 0 1 .75.75Zm-.75 3.75a.75.75 0 0 0 0-1.5h-2a.75.75 0 0 0 0 1.5Z'/%3E%3C/svg%3E");--gh-icon-open: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M1.5 3.25a2.25 2.25 0 1 1 3 2.122v5.256a2.251 2.251 0 1 1-1.5 0V5.372A2.25 2.25 0 0 1 1.5 3.25Zm5.677-.177L9.573.677A.25.25 0 0 1 10 .854V2.5h1A2.5 2.5 0 0 1 13.5 5v5.628a2.251 2.251 0 1 1-1.5 0V5a1 1 0 0 0-1-1h-1v1.646a.25.25 0 0 1-.427.177L7.177 3.427a.25.25 0 0 1 0-.354ZM3.75 2.5a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm0 9.5a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm8.25.75a.75.75 0 1 0 1.5 0 .75.75 0 0 0-1.5 0Z'/%3E%3C/svg%3E");--gh-icon-merged: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M5.45 5.154A4.25 4.25 0 0 0 9.25 7.5h1.378a2.251 2.251 0 1 1 0 1.5H9.25A5.734 5.734 0 0 1 5 7.123v3.505a2.25 2.25 0 1 1-1.5 0V5.372a2.25 2.25 0 1 1 1.95-.218ZM4.25 13.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5Zm8.5-4.5a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5ZM5 3.25a.75.75 0 1 0 0 .005V3.25Z'/%3E%3C/svg%3E");--gh-icon-closed: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3.25 1A2.25 2.25 0 0 1 4 5.372v5.256a2.251 2.251 0 1 1-1.5 0V5.372A2.251 2.251 0 0 1 3.25 1Zm9.5 5.5a.75.75 0 0 1 .75.75v3.378a2.251 2.251 0 1 1-1.5 0V7.25a.75.75 0 0 1 .75-.75Zm-2.03-5.273a.75.75 0 0 1 1.06 0l.97.97.97-.97a.748.748 0 0 1 1.265.332.75.75 0 0 1-.205.729l-.97.97.97.97a.751.751 0 0 1-.018 1.042.751.751 0 0 1-1.042.018l-.97-.97-.97.97a.749.749 0 0 1-1.275-.326.749.749 0 0 1 .215-.734l.97-.97-.97-.97a.75.75 0 0 1 0-1.06ZM2.5 3.25a.75.75 0 1 0 1.5 0 .75.75 0 0 0-1.5 0ZM3.25 12a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Zm9.5 0a.75.75 0 1 0 0 1.5.75.75 0 0 0 0-1.5Z'/%3E%3C/svg%3E");- &.--gh-status-draft,- &.--gh-status-open,- &.--gh-status-approved,- &.--gh-status-changes_requested,- &.--gh-status-merged,+ &.--gh-status-draft {+ --gh-status-icon: var(--gh-icon-draft);+ --gh-status-color: #8b949e;+ }++ &.--gh-status-open {+ --gh-status-icon: var(--gh-icon-open);+ --gh-status-color: #8b949e;+ }++ &.--gh-status-approved {+ --gh-status-icon: var(--gh-icon-open);+ --gh-status-color: #3fb950;+ }++ &.--gh-status-changes_requested {+ --gh-status-icon: var(--gh-icon-open);+ --gh-status-color: #f79939;+ }++ &.--gh-status-merged {+ --gh-status-icon: var(--gh-icon-merged);+ --gh-status-color: #a371f7;+ }+&.--gh-status-closed {+ --gh-status-icon: var(--gh-icon-closed);+ --gh-status-color: #f85149;+ }+}++.onebox.githubpullrequest .github-row {+ &[class*="--gh-status-"] {.github-icon-container {display: block;grid-area: icon;@@ -41,6 +69,8 @@ $icon-width-mobile: 1.8em;mask-size: 100% 100%;mask-repeat: no-repeat;mask-position: center;+ mask-image: var(--gh-status-icon);+ background-color: var(--gh-status-color);@include viewport.until(sm) {width: $icon-width-mobile;@@ -48,34 +78,22 @@ $icon-width-mobile: 1.8em;}}}+}- &.--gh-status-draft .github-icon-container::before {- mask-image: var(--gh-icon-draft);- background-color: #8b949e;- }-- &.--gh-status-open .github-icon-container::before,- &.--gh-status-approved .github-icon-container::before,- &.--gh-status-changes_requested .github-icon-container::before {- mask-image: var(--gh-icon-open);- background-color: #8b949e;- }-- &.--gh-status-approved .github-icon-container::before {- background-color: #3fb950;- }-- &.--gh-status-changes_requested .github-icon-container::before {- background-color: #f79939;- }-- &.--gh-status-merged .github-icon-container::before {- mask-image: var(--gh-icon-merged);- background-color: #a371f7;- }+a.inline-onebox {+ --gh-icon-size: 1.2em;- &.--gh-status-closed .github-icon-container::before {- mask-image: var(--gh-icon-closed);- background-color: #f85149;+ &[class*="--gh-status-"]::before {+ content: "";+ display: inline-block;+ height: var(--gh-icon-size);+ width: var(--gh-icon-size);+ margin-inline-end: 0.2em;+ mask-size: 100% 100%;+ mask-repeat: no-repeat;+ mask-position: center;+ mask-image: var(--gh-status-icon);+ background-color: var(--gh-status-color);+ vertical-align: -0.2em;}}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.rbindex 060d31b14e6..6e3c500e1eb 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@@ -28,11 +28,27 @@ RSpec.describe Jobs::RebakeGithubPrPosts doexpect(Oneboxer).to have_received(:preview).with(pr_url, invalidate_oneboxes: true)end+ it "invalidates the inline onebox cache for the PR URL" do+ allow(InlineOneboxer).to receive(:invalidate)+ described_class.new.execute(pr_url:)++ expect(InlineOneboxer).to have_received(:invalidate).with(pr_url)+ end+it "rebakes posts with full GitHub PR oneboxes" docreate_post_with_link(<<~HTML)<aside class="onebox githubpullrequest"><a href="#{pr_url}">PR</a></aside>HTML+ expect_any_instance_of(Post).to receive(:rebake!).with(+ priority: :low,+ skip_publish_rebaked_changes: true,+ )++ described_class.new.execute(pr_url:)+ end+ it "rebakes posts with inline oneboxes for the PR URL" do+ create_post_with_link(%(<a href="#{pr_url}" class="inline-onebox">inline onebox</a>))expect_any_instance_of(Post).to receive(:rebake!).with(priority: :low,skip_publish_rebaked_changes: true,@@ -41,10 +57,15 @@ RSpec.describe Jobs::RebakeGithubPrPosts dodescribed_class.new.execute(pr_url:)end- it "does not rebake posts with plain links or inline oneboxes" do+ it "does not rebake posts with plain links" docreate_post_with_link(%(<a href="#{pr_url}">plain link</a>))- create_post_with_link(%(<a href="#{pr_url}" class="inline-onebox">inline onebox</a>))+ expect_any_instance_of(Post).not_to receive(:rebake!)++ described_class.new.execute(pr_url:)+ end+ it "does not rebake posts with an inline onebox for an unrelated URL" do+ create_post_with_link(%(<a href="https://example.com/other" class="inline-onebox">other</a>))expect_any_instance_of(Post).not_to receive(:rebake!)described_class.new.execute(pr_url:)@@ -99,6 +120,17 @@ RSpec.describe Jobs::RebakeGithubPrPosts dodescribed_class.new.execute(pr_url:)end++ it "rebakes chat messages with inline oneboxes for the PR URL" do+ create_chat_message_with_link(%(<a href="#{pr_url}" class="inline-onebox">inline</a>))++ expect_any_instance_of(Chat::Message).to receive(:rebake!).with(+ priority: :low,+ skip_notifications: true,+ )++ described_class.new.execute(pr_url:)+ endendendenddiff --git a/plugins/discourse-github/spec/lib/github_pr_onebox_status_spec.rb b/plugins/discourse-github/spec/lib/github_pr_onebox_status_spec.rbindex 1d28160adbc..1018a7b22e4 100644--- a/plugins/discourse-github/spec/lib/github_pr_onebox_status_spec.rb+++ b/plugins/discourse-github/spec/lib/github_pr_onebox_status_spec.rb@@ -219,6 +219,36 @@ RSpec.describe Onebox::Engine::GithubPullRequestOnebox doendend+ describe "#inline_data" do+ let(:onebox) { Onebox::Engine::GithubPullRequestOnebox.new(gh_link) }++ it "returns nil when github_pr_status_enabled is false" do+ SiteSetting.github_pr_status_enabled = false+ expect(onebox.inline_data).to be_nil+ end++ context "when github_pr_status_enabled is true" do+ before { SiteSetting.github_pr_status_enabled = true }++ it "returns a title with repo and PR number plus a status css class" do+ stub_request(:get, api_uri).to_return(status: 200, body: MultiJson.dump(open_pr_response))+ stub_request(:get, reviews_api_uri).to_return(status: 200, body: "[]")++ expect(onebox.inline_data).to eq(+ title: "#{open_pr_response["title"]} · Pull Request #1253 · discourse/discourse",+ css_class: "--gh-status-open",+ )+ end++ it "returns nil when the status fetch fails" do+ stub_request(:get, api_uri).to_return(status: 200, body: MultiJson.dump(open_pr_response))+ stub_request(:get, reviews_api_uri).to_return(status: 500, body: "error")++ expect(onebox.inline_data).to be_nil+ end+ end+ end+describe "#status_date_label" dolet(:onebox) { Onebox::Engine::GithubPullRequestOnebox.new(gh_link) }diff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rbindex 6255dc27273..750e5414b4a 100644--- a/spec/lib/cooked_post_processor_spec.rb+++ b/spec/lib/cooked_post_processor_spec.rb@@ -248,6 +248,45 @@ RSpec.describe CookedPostProcessor doexpect(cpp.html).to_not have_tag("a[rel='noopener nofollow ugc']")endend++ describe "engine-supplied css_class" do+ let(:url) { "https://example.com/foo" }+ let(:post) { Fabricate(:post, user: user_with_auto_groups, raw: "Look at #{url} today") }+ let(:cpp) { CookedPostProcessor.new(post, invalidate_oneboxes: true) }++ before do+ allow(Oneboxer).to receive(:inline_data_for).with(url).and_return(+ title: "engine title",+ css_class: "--gh-status-merged",+ )+ end++ after { InlineOneboxer.invalidate(url) }++ it "adds the css_class alongside inline-onebox" do+ cpp.post_process++ link = Nokogiri::HTML5.fragment(cpp.html).at_css(%(a[href="#{url}"]))+ expect(link["class"]).to eq("inline-onebox --gh-status-merged")+ expect(link.text).to eq("engine title")+ end++ it "escapes HTML in engine-supplied title and css_class" do+ allow(Oneboxer).to receive(:inline_data_for).with(url).and_return(+ title: %(<script>alert("xss")</script>),+ css_class: %(broken" onerror="alert(1)),+ )++ cpp.post_process++ doc = Nokogiri::HTML5.fragment(cpp.html)+ link = doc.at_css(%(a[href="#{url}"]))++ expect(link.text).to eq(%(<script>alert("xss")</script>))+ expect(link["onerror"]).to be_nil+ expect(doc.css("script")).to be_empty+ end+ endendcontext "when processing images" dodiff --git a/spec/lib/inline_oneboxer_spec.rb b/spec/lib/inline_oneboxer_spec.rbindex b09448324dd..188c35eb63e 100644--- a/spec/lib/inline_oneboxer_spec.rb+++ b/spec/lib/inline_oneboxer_spec.rb@@ -434,4 +434,18 @@ RSpec.describe InlineOneboxer doexpect(results[0][:title]).to eq("Custom Onebox for Wizard")endend++ it "returns engine-supplied title and css_class" do+ url = "https://example.com/foo"+ allow(Oneboxer).to receive(:inline_data_for).with(url).and_return(+ title: "Engine title",+ css_class: "--engine-class",+ )++ expect(InlineOneboxer.lookup(url, skip_cache: true)).to eq(+ url: url,+ title: "Engine title",+ css_class: "--engine-class",+ )+ endend