mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Allow oneboxing private GitHub repo URLs and add private indicator to HTML (#27947)
Followup 560e8aff75
The linked commit allowed oneboxing private GitHub PRs,
issues, commits, and so on, but it didn't actually allow
oneboxing the root repo e.g https://github.com/discourse/discourse-reactions
We didn't have an engine for this, we were relying on OpenGraph
tags on the HTML rendering of the page like we do with other
oneboxes.
To fix this, we needed a new github engine for repos specifically.
Also, this commit adds a `data-github-private-repo` attribute to
PR, issue, and repo onebox HTML so we have an indicator of
whether the repo was private, which can be used for theme components
and so on.
This commit is contained in:
@@ -2,12 +2,15 @@
|
||||
|
||||
RSpec.describe Onebox::Engine::GithubIssueOnebox do
|
||||
let(:issue_uri) { "https://api.github.com/repos/discourse/discourse/issues/1" }
|
||||
let(:repo_uri) { "https://api.github.com/repos/discourse/discourse" }
|
||||
let(:repo_response) { onebox_response("githubrepo") }
|
||||
|
||||
before do
|
||||
stub_request(:get, issue_uri).to_return(
|
||||
status: 200,
|
||||
body: onebox_response("github_issue_onebox"),
|
||||
)
|
||||
stub_request(:get, repo_uri).to_return(status: 200, body: repo_response)
|
||||
end
|
||||
|
||||
include_context "with engines" do
|
||||
@@ -23,6 +26,22 @@ RSpec.describe Onebox::Engine::GithubIssueOnebox do
|
||||
expect(html).to include(sanitized_label)
|
||||
end
|
||||
|
||||
it "sets the data-github-private-repo attr to false" do
|
||||
expect(html).to include("data-github-private-repo=\"false\"")
|
||||
end
|
||||
|
||||
context "when the PR is in a private repo" do
|
||||
let(:repo_response) do
|
||||
resp = MultiJson.load(onebox_response("githubrepo"))
|
||||
resp["private"] = true
|
||||
MultiJson.dump(resp)
|
||||
end
|
||||
|
||||
it "sets the data-github-private-repo attr to true" do
|
||||
expect(html).to include("data-github-private-repo=\"true\"")
|
||||
end
|
||||
end
|
||||
|
||||
context "when github_onebox_access_token is configured" do
|
||||
before { SiteSetting.github_onebox_access_tokens = "discourse|github_pat_1234" }
|
||||
|
||||
|
||||
@@ -3,13 +3,9 @@
|
||||
RSpec.describe Onebox::Engine::GithubPullRequestOnebox do
|
||||
let(:gh_link) { "https://github.com/discourse/discourse/pull/1253/" }
|
||||
let(:api_uri) { "https://api.github.com/repos/discourse/discourse/pulls/1253" }
|
||||
let(:response) { onebox_response(described_class.onebox_name) }
|
||||
|
||||
before do
|
||||
stub_request(:get, api_uri).to_return(
|
||||
status: 200,
|
||||
body: onebox_response(described_class.onebox_name),
|
||||
)
|
||||
end
|
||||
before { stub_request(:get, api_uri).to_return(status: 200, body: response) }
|
||||
|
||||
include_context "with engines" do
|
||||
let(:link) { gh_link }
|
||||
@@ -53,18 +49,37 @@ RSpec.describe Onebox::Engine::GithubPullRequestOnebox do
|
||||
expect(html).to include("http://meta.discourse.org/t/audio-html5-tag/8168")
|
||||
expect(html).not_to include("test comment")
|
||||
end
|
||||
|
||||
it "sets the data-github-private-repo attr to false" do
|
||||
expect(html).to include("data-github-private-repo=\"false\"")
|
||||
end
|
||||
|
||||
context "when the PR is in a private repo" do
|
||||
let(:response) do
|
||||
resp = MultiJson.load(onebox_response(described_class.onebox_name))
|
||||
resp["base"]["repo"]["private"] = true
|
||||
MultiJson.dump(resp)
|
||||
end
|
||||
|
||||
it "sets the data-github-private-repo attr to true" do
|
||||
expect(html).to include("data-github-private-repo=\"true\"")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with commit links" do
|
||||
let(:gh_link) do
|
||||
"https://github.com/discourse/discourse/pull/1253/commits/d7d3be1130c665cc7fab9f05dbf32335229137a6"
|
||||
end
|
||||
let(:pr_commit_url) do
|
||||
"https://api.github.com/repos/discourse/discourse/commits/d7d3be1130c665cc7fab9f05dbf32335229137a6"
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(
|
||||
:get,
|
||||
"https://api.github.com/repos/discourse/discourse/commits/d7d3be1130c665cc7fab9f05dbf32335229137a6",
|
||||
).to_return(status: 200, body: onebox_response(described_class.onebox_name + "_commit"))
|
||||
stub_request(:get, pr_commit_url).to_return(
|
||||
status: 200,
|
||||
body: onebox_response(described_class.onebox_name + "_commit"),
|
||||
)
|
||||
end
|
||||
|
||||
it "includes commit name" do
|
||||
@@ -74,21 +89,50 @@ RSpec.describe Onebox::Engine::GithubPullRequestOnebox do
|
||||
"http://meta.discourse.org/t/audio-html5-tag/8168",
|
||||
)
|
||||
end
|
||||
|
||||
context "when github_onebox_access_token is configured" do
|
||||
before { SiteSetting.github_onebox_access_tokens = "discourse|github_pat_1234" }
|
||||
|
||||
it "sends it as part of the request" do
|
||||
html
|
||||
expect(WebMock).to have_requested(:get, pr_commit_url).with(
|
||||
headers: {
|
||||
"Authorization" => "Bearer github_pat_1234",
|
||||
},
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "with comment links" do
|
||||
let(:gh_link) { "https://github.com/discourse/discourse/pull/1253/#issuecomment-21597425" }
|
||||
let(:comment_api_url) do
|
||||
"https://api.github.com/repos/discourse/discourse/issues/comments/21597425"
|
||||
end
|
||||
|
||||
before do
|
||||
stub_request(
|
||||
:get,
|
||||
"https://api.github.com/repos/discourse/discourse/issues/comments/21597425",
|
||||
).to_return(status: 200, body: onebox_response(described_class.onebox_name + "_comment"))
|
||||
stub_request(:get, comment_api_url).to_return(
|
||||
status: 200,
|
||||
body: onebox_response(described_class.onebox_name + "_comment"),
|
||||
)
|
||||
end
|
||||
|
||||
it "includes comment" do
|
||||
expect(html).to include("You've signed the CLA")
|
||||
end
|
||||
|
||||
context "when github_onebox_access_token is configured" do
|
||||
before { SiteSetting.github_onebox_access_tokens = "discourse|github_pat_1234" }
|
||||
|
||||
it "sends it as part of the request" do
|
||||
html
|
||||
expect(WebMock).to have_requested(:get, api_uri).with(
|
||||
headers: {
|
||||
"Authorization" => "Bearer github_pat_1234",
|
||||
},
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when github_onebox_access_token is configured" do
|
||||
|
||||
72
spec/lib/onebox/engine/github_repo_onebox_spec.rb
Normal file
72
spec/lib/onebox/engine/github_repo_onebox_spec.rb
Normal file
@@ -0,0 +1,72 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Onebox::Engine::GithubRepoOnebox do
|
||||
let(:gh_link) { "https://github.com/discourse/discourse" }
|
||||
let(:api_uri) { "https://api.github.com/repos/discourse/discourse" }
|
||||
let(:response) { onebox_response(described_class.onebox_name) }
|
||||
|
||||
before { stub_request(:get, api_uri).to_return(status: 200, body: response) }
|
||||
|
||||
include_context "with engines" do
|
||||
let(:link) { gh_link }
|
||||
end
|
||||
it_behaves_like "an engine"
|
||||
|
||||
describe "#to_html" do
|
||||
it "includes the description of the repo" do
|
||||
expect(html).to include("A platform for community discussion. Free, open, simple.")
|
||||
end
|
||||
|
||||
it "includes the name of the repo and truncated description for the title" do
|
||||
expect(html).to include(
|
||||
"GitHub - discourse/discourse: A platform for community discussion. Free, open,...",
|
||||
)
|
||||
end
|
||||
|
||||
it "includes a thumbnail url" do
|
||||
SecureRandom.stubs(:hex).returns("1234")
|
||||
expect(html).to include("https://opengraph.githubassets.com/1234/discourse/discourse")
|
||||
end
|
||||
|
||||
it "sets the data-github-private-repo attr to false" do
|
||||
expect(html).to include("data-github-private-repo=\"false\"")
|
||||
end
|
||||
|
||||
context "when the PR is in a private repo" do
|
||||
let(:response) do
|
||||
resp = MultiJson.load(onebox_response(described_class.onebox_name))
|
||||
resp["private"] = true
|
||||
MultiJson.dump(resp)
|
||||
end
|
||||
|
||||
it "sets the data-github-private-repo attr to true" do
|
||||
expect(html).to include("data-github-private-repo=\"true\"")
|
||||
end
|
||||
end
|
||||
|
||||
context "when the repo has no description" do
|
||||
let(:response) do
|
||||
resp = onebox_response(described_class.onebox_name)
|
||||
resp["description"] = ""
|
||||
resp
|
||||
end
|
||||
|
||||
it "includes a message about contributing to the repo" do
|
||||
expect(html).to include(I18n.t("onebox.github.no_description", repo: "discourse/discourse"))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when github_onebox_access_token is configured" do
|
||||
before { SiteSetting.github_onebox_access_tokens = "discourse|github_pat_1234" }
|
||||
|
||||
it "sends it as part of the request" do
|
||||
html
|
||||
expect(WebMock).to have_requested(:get, api_uri).with(
|
||||
headers: {
|
||||
"Authorization" => "Bearer github_pat_1234",
|
||||
},
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user