FEATURE: Allow oneboxing private GitHub URLs (#27705)

This commit adds the ability to onebox private GitHub
commits, pull requests, issues, blobs, and actions using
a new `github_onebox_access_token` site setting. The token
must be set up in correctly to have access to the repos needed.

To do this successfully with the Oneboxer, we need to skip
redirects on the github.com host, otherwise we get a 404
on the URL before it is translated into a GitHub API URL
and has the appropriate headers added.
This commit is contained in:
Martin Brennan
2024-07-10 09:39:31 +10:00
committed by GitHub
parent 8c038d9498
commit 560e8aff75
17 changed files with 187 additions and 39 deletions

View File

@ -4,16 +4,18 @@ RSpec.describe Onebox::Engine::GithubActionsOnebox do
describe "PR check run" do
before do
@link = "https://github.com/discourse/discourse/pull/13128/checks?check_run_id=2660861130"
@pr_run_uri = "https://api.github.com/repos/discourse/discourse/pulls/13128"
@run_uri = "https://api.github.com/repos/discourse/discourse/check-runs/2660861130"
stub_request(:get, "https://api.github.com/repos/discourse/discourse/pulls/13128").to_return(
stub_request(:get, @pr_run_uri).to_return(
status: 200,
body: onebox_response("githubactions_pr"),
)
stub_request(
:get,
"https://api.github.com/repos/discourse/discourse/check-runs/2660861130",
).to_return(status: 200, body: onebox_response("githubactions_pr_run"))
stub_request(:get, @run_uri).to_return(
status: 200,
body: onebox_response("githubactions_pr_run"),
)
end
include_context "with engines"
@ -28,16 +30,30 @@ RSpec.describe Onebox::Engine::GithubActionsOnebox do
expect(html).to include("simplify post and topic deletion language")
end
end
context "when github_onebox_access_token is configured" do
before { SiteSetting.github_onebox_access_token = "1234" }
it "sends it as part of the request" do
html
expect(WebMock).to have_requested(:get, @run_uri).with(
headers: {
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
},
)
end
end
end
describe "GitHub Actions run" do
before do
@link = "https://github.com/discourse/discourse/actions/runs/873214216"
@pr_run_uri = "https://api.github.com/repos/discourse/discourse/actions/runs/873214216"
stub_request(
:get,
"https://api.github.com/repos/discourse/discourse/actions/runs/873214216",
).to_return(status: 200, body: onebox_response("githubactions_actions_run"))
stub_request(:get, @pr_run_uri).to_return(
status: 200,
body: onebox_response("githubactions_actions_run"),
)
end
include_context "with engines"
@ -56,5 +72,18 @@ RSpec.describe Onebox::Engine::GithubActionsOnebox do
expect(html).to include("Linting")
end
end
context "when github_onebox_access_token is configured" do
before { SiteSetting.github_onebox_access_token = "1234" }
it "sends it as part of the request" do
html
expect(WebMock).to have_requested(:get, @pr_run_uri).with(
headers: {
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
},
)
end
end
end
end

View File

@ -5,10 +5,12 @@ RSpec.describe Onebox::Engine::GithubBlobOnebox do
@link =
"https://github.com/discourse/onebox/blob/master/lib/onebox/engine/github_blob_onebox.rb"
@uri = URI.parse(@link)
stub_request(
:get,
"https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb",
).to_return(status: 200, body: onebox_response(described_class.onebox_name))
@raw_uri =
"https://raw.githubusercontent.com/discourse/onebox/master/lib/onebox/engine/github_blob_onebox.rb"
stub_request(:get, @raw_uri).to_return(
status: 200,
body: onebox_response(described_class.onebox_name),
)
end
include_context "with engines"
@ -38,5 +40,18 @@ RSpec.describe Onebox::Engine::GithubBlobOnebox do
expect(html).not_to include("/Pages")
expect(html).to include("This file is binary.")
end
context "when github_onebox_access_token is configured" do
before { SiteSetting.github_onebox_access_token = "1234" }
it "sends it as part of the request" do
html
expect(WebMock).to have_requested(:get, @raw_uri).with(
headers: {
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
},
)
end
end
end
end

View File

@ -51,6 +51,19 @@ RSpec.describe Onebox::Engine::GithubCommitOnebox do
expect(html).to include("2 deletions")
end
end
context "when github_onebox_access_token is configured" do
before { SiteSetting.github_onebox_access_token = "1234" }
it "sends it as part of the request" do
html
expect(WebMock).to have_requested(:get, @uri).with(
headers: {
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
},
)
end
end
end
describe "PR with commit URL" do
@ -58,12 +71,9 @@ RSpec.describe Onebox::Engine::GithubCommitOnebox do
@link =
"https://github.com/discourse/discourse/pull/4662/commit/803d023e2307309f8b776ab3b8b7e38ba91c0919"
@uri =
"https://api.github.com/repos/discourse/discourse/commit/803d023e2307309f8b776ab3b8b7e38ba91c0919"
"https://api.github.com/repos/discourse/discourse/commits/803d023e2307309f8b776ab3b8b7e38ba91c0919"
stub_request(
:get,
"https://api.github.com/repos/discourse/discourse/commits/803d023e2307309f8b776ab3b8b7e38ba91c0919",
).to_return(status: 200, body: onebox_response("githubcommit"))
stub_request(:get, @uri).to_return(status: 200, body: onebox_response("githubcommit"))
end
include_context "with engines"
@ -107,5 +117,18 @@ RSpec.describe Onebox::Engine::GithubCommitOnebox do
expect(html).to include("2 deletions")
end
end
context "when github_onebox_access_token is configured" do
before { SiteSetting.github_onebox_access_token = "1234" }
it "sends it as part of the request" do
html
expect(WebMock).to have_requested(:get, @uri).with(
headers: {
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
},
)
end
end
end
end

View File

@ -3,8 +3,9 @@
RSpec.describe Onebox::Engine::GithubIssueOnebox do
before do
@link = "https://github.com/discourse/discourse/issues/1"
@issue_uri = "https://api.github.com/repos/discourse/discourse/issues/1"
stub_request(:get, "https://api.github.com/repos/discourse/discourse/issues/1").to_return(
stub_request(:get, @issue_uri).to_return(
status: 200,
body: onebox_response("github_issue_onebox"),
)
@ -20,5 +21,18 @@ RSpec.describe Onebox::Engine::GithubIssueOnebox do
expect(html).to include(sanitized_label)
end
context "when github_onebox_access_token is configured" do
before { SiteSetting.github_onebox_access_token = "1234" }
it "sends it as part of the request" do
html
expect(WebMock).to have_requested(:get, @issue_uri).with(
headers: {
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
},
)
end
end
end
end

View File

@ -90,4 +90,17 @@ RSpec.describe Onebox::Engine::GithubPullRequestOnebox do
expect(html).to include("You've signed the CLA")
end
end
context "when github_onebox_access_token is configured" do
before { SiteSetting.github_onebox_access_token = "1234" }
it "sends it as part of the request" do
html
expect(WebMock).to have_requested(:get, @uri).with(
headers: {
"Authorization" => "Bearer #{SiteSetting.github_onebox_access_token}",
},
)
end
end
end