Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tap_dumper: filter HOMEBREW_GITHUB_API_TOKEN. #1270

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/bundle/tap_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def reset!
def dump
taps.map do |tap|
remote = if tap.custom_remote? && (tap_remote = tap.remote)
if (api_token = ENV.fetch("HOMEBREW_GITHUB_API_TOKEN", false).presence)
# Replace the API token in the remote URL with interpolation.
# Rubocop's warning here is wrong; we intentionally want to not
# evaluate this string until the Brewfile is evaluated.
# rubocop:disable Lint/InterpolationCheck
tap_remote = tap_remote.gsub api_token, '#{ENV.fetch("HOMEBREW_GITHUB_API_TOKEN")}'
# rubocop:enable Lint/InterpolationCheck
end
", \"#{tap_remote}\""
end
"tap \"#{tap.name}\"#{remote}"
Expand Down
27 changes: 21 additions & 6 deletions spec/bundle/tap_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,40 @@
end
end

context "with `bitbucket/bar`, `homebrew/baz` and `homebrew/foo` taps" do
context "with taps" do
before do
described_class.reset!

bar = instance_double(Tap, name: "bitbucket/bar", custom_remote?: true,
remote: "https://bitbucket.org/bitbucket/bar.git")
baz = instance_double(Tap, name: "homebrew/baz", custom_remote?: false)
foo = instance_double(Tap, name: "homebrew/foo", custom_remote?: false)
allow(Tap).to receive(:each).and_return [bar, baz, foo]

ENV["HOMEBREW_GITHUB_API_TOKEN_BEFORE"] = ENV.fetch("HOMEBREW_GITHUB_API_TOKEN", nil)
ENV["HOMEBREW_GITHUB_API_TOKEN"] = "some-token"
private_tap = instance_double(Tap, name: "privatebrew/private", custom_remote?: true,
remote: "https://#{ENV.fetch("HOMEBREW_GITHUB_API_TOKEN")}@github.com/privatebrew/homebrew-private")

allow(Tap).to receive(:each).and_return [bar, baz, foo, private_tap]
end

after do
ENV["HOMEBREW_GITHUB_API_TOKEN"] = ENV.fetch("HOMEBREW_GITHUB_API_TOKEN_BEFORE", nil)
ENV.delete("HOMEBREW_GITHUB_API_TOKEN_BEFORE")
end

it "returns list of information" do
expect(dumper.tap_names).not_to be_empty
end

it "dumps output" do
expect(dumper.dump).to eql(
"tap \"bitbucket/bar\", \"https://bitbucket.org/bitbucket/bar.git\"\n" \
"tap \"homebrew/baz\"\ntap \"homebrew/foo\"",
)
expected_output = <<~EOS
tap "bitbucket/bar", "https://bitbucket.org/bitbucket/bar.git"
tap "homebrew/baz"
tap "homebrew/foo"
tap "privatebrew/private", "https://\#{ENV.fetch("HOMEBREW_GITHUB_API_TOKEN")}@github.com/privatebrew/homebrew-private"
EOS
expect(dumper.dump).to eql(expected_output.chomp)
end
end
end