Skip to content

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

FIX: Better handling of github_folder onebox w/o opengraph tags (#39142)

Jarek Radosz379304a9c3ef
diff --git a/lib/onebox/engine/github_folder_onebox.rb b/lib/onebox/engine/github_folder_onebox.rb
index 99c10aea283..99471e30163 100644
--- a/lib/onebox/engine/github_folder_onebox.rb
+++ b/lib/onebox/engine/github_folder_onebox.rb
@@ -21,10 +21,10 @@ module Onebox
max_length = 250
- display_path = extract_path(og.url, max_length)
+ display_path, fallback_title = extract_path(max_length)
display_description = clean_description(og.description, og.title, max_length)
- title = og.title
+ title = og.title || fallback_title
fragment = Addressable::URI.parse(url).fragment
if fragment
@@ -48,13 +48,14 @@ module Onebox
}
end
- def extract_path(root, max_length)
- path = url.split("#")[0].split("?")[0]
- path = path["#{root}/tree/".length..-1]
+ def extract_path(max_length)
+ clean_url = url.split("#")[0].split("?")[0]
+ match = clean_url.match(%r{github\.com/([\w\-]+/[\w\-]+)/tree/(.+)})
- return unless path
+ return unless match
- path.length > max_length ? path[-max_length..-1] : path
+ title, path = match.captures
+ [path.length > max_length ? path[-max_length..] : path, title]
end
def clean_description(description, title, max_length)
diff --git a/spec/fixtures/onebox/githubfolder-no-og.response b/spec/fixtures/onebox/githubfolder-no-og.response
new file mode 100644
index 00000000000..26ffed773db
--- /dev/null
+++ b/spec/fixtures/onebox/githubfolder-no-og.response
@@ -0,0 +1,8 @@
+<!DOCTYPE html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <title>organization/repo-foobar/tree/main/models</title>
+ </head>
+ <body></body>
+</html>
diff --git a/spec/lib/onebox/engine/github_folder_onebox_spec.rb b/spec/lib/onebox/engine/github_folder_onebox_spec.rb
index 0d23a104e98..65cdcbddb33 100644
--- a/spec/lib/onebox/engine/github_folder_onebox_spec.rb
+++ b/spec/lib/onebox/engine/github_folder_onebox_spec.rb
@@ -68,6 +68,22 @@ RSpec.describe Onebox::Engine::GithubFolderOnebox do
end
end
+ context "without opengraph tags" do
+ before do
+ @link = "https://github.com/organization/repo-foobar/tree/main/models"
+ @uri = "https://github.com/organization/repo-foobar/tree/main/models"
+
+ stub_request(:get, @uri).to_return(status: 200, body: onebox_response("githubfolder-no-og"))
+ @onebox = described_class.new(@link)
+ end
+
+ it "falls back to URL-derived title and path" do
+ html = @onebox.to_html
+ expect(html).to include("organization/repo-foobar")
+ expect(html).to include("main/models")
+ end
+ end
+
describe ".===" do
it "matches valid GitHub tree URL" do
valid_url = URI("https://github.com/username/repository/tree/main")