Skip to content

Commit

Permalink
Merge pull request #1270 from MikeMcQuaid/filter_github_api_token
Browse files Browse the repository at this point in the history
tap_dumper: filter HOMEBREW_GITHUB_API_TOKEN.
  • Loading branch information
MikeMcQuaid authored Dec 13, 2023
2 parents ea1f4fa + e0621c1 commit 3f43dfc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
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

0 comments on commit 3f43dfc

Please sign in to comment.