Skip to content

Commit

Permalink
Merge pull request #7449 from jgarber623/add-new-git-source-shorthands
Browse files Browse the repository at this point in the history
(cherry picked from commit 6572919)
  • Loading branch information
martinemde authored and deivid-rodriguez committed Mar 20, 2024
1 parent 06e2edb commit d3222bf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
15 changes: 15 additions & 0 deletions bundler/lib/bundler/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def self.evaluate(gemfile, lockfile, unlock)
platform platforms type source install_if gemfile force_ruby_platform].freeze

GITHUB_PULL_REQUEST_URL = %r{\Ahttps://github\.com/([A-Za-z0-9_\-\.]+/[A-Za-z0-9_\-\.]+)/pull/(\d+)\z}
GITLAB_MERGE_REQUEST_URL = %r{\Ahttps://gitlab\.com/([A-Za-z0-9_\-\./]+)/-/merge_requests/(\d+)\z}

attr_reader :gemspecs, :gemfile
attr_accessor :dependencies
Expand Down Expand Up @@ -308,6 +309,20 @@ def add_git_sources
repo_name ||= user_name
"https://#{user_name}@bitbucket.org/#{user_name}/#{repo_name}.git"
end

git_source(:gitlab) do |repo_name|
if repo_name =~ GITLAB_MERGE_REQUEST_URL
{
"git" => "https://gitlab.com/#{$1}.git",
"branch" => nil,
"ref" => "refs/merge-requests/#{$2}/head",
"tag" => nil,
}
else
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://gitlab.com/#{repo_name}.git"
end
end
end

def with_source(source)
Expand Down
55 changes: 53 additions & 2 deletions bundler/spec/bundler/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
expect(subject.dependencies.first.source.ref).to eq("refs/pull/5/head")
end

it "converts :gitlab PR to URI using https" do
subject.gem("sparks", gitlab: "https://gitlab.com/indirect/sparks/-/merge_requests/5")
gitlab_uri = "https://gitlab.com/indirect/sparks.git"
expect(subject.dependencies.first.source.uri).to eq(gitlab_uri)
expect(subject.dependencies.first.source.ref).to eq("refs/merge-requests/5/head")
end

it "rejects :github PR URI with a branch, ref or tag" do
expect do
subject.gem("sparks", github: "https://github.com/indirect/sparks/pull/5", branch: "foo")
Expand All @@ -55,6 +62,29 @@
)
end

it "rejects :gitlab PR URI with a branch, ref or tag" do
expect do
subject.gem("sparks", gitlab: "https://gitlab.com/indirect/sparks/-/merge_requests/5", branch: "foo")
end.to raise_error(
Bundler::GemfileError,
%(The :branch option can't be used with `gitlab: "https://gitlab.com/indirect/sparks/-/merge_requests/5"`),
)

expect do
subject.gem("sparks", gitlab: "https://gitlab.com/indirect/sparks/-/merge_requests/5", ref: "foo")
end.to raise_error(
Bundler::GemfileError,
%(The :ref option can't be used with `gitlab: "https://gitlab.com/indirect/sparks/-/merge_requests/5"`),
)

expect do
subject.gem("sparks", gitlab: "https://gitlab.com/indirect/sparks/-/merge_requests/5", tag: "foo")
end.to raise_error(
Bundler::GemfileError,
%(The :tag option can't be used with `gitlab: "https://gitlab.com/indirect/sparks/-/merge_requests/5"`),
)
end

it "rejects :github with :git" do
expect do
subject.gem("sparks", github: "indirect/sparks", git: "https://github.com/indirect/sparks.git")
Expand All @@ -64,6 +94,15 @@
)
end

it "rejects :gitlab with :git" do
expect do
subject.gem("sparks", gitlab: "indirect/sparks", git: "https://gitlab.com/indirect/sparks.git")
end.to raise_error(
Bundler::GemfileError,
%(The :git option can't be used with `gitlab: "indirect/sparks"`),
)
end

context "default hosts", bundler: "< 3" do
it "converts :github to URI using https" do
subject.gem("sparks", github: "indirect/sparks")
Expand All @@ -77,6 +116,18 @@
expect(subject.dependencies.first.source.uri).to eq(github_uri)
end

it "converts :gitlab to URI using https" do
subject.gem("sparks", gitlab: "indirect/sparks")
gitlab_uri = "https://gitlab.com/indirect/sparks.git"
expect(subject.dependencies.first.source.uri).to eq(gitlab_uri)
end

it "converts :gitlab shortcut to URI using https" do
subject.gem("sparks", gitlab: "rails")
gitlab_uri = "https://gitlab.com/rails/rails.git"
expect(subject.dependencies.first.source.uri).to eq(gitlab_uri)
end

it "converts numeric :gist to :git" do
subject.gem("not-really-a-gem", gist: 2_859_988)
github_uri = "https://gist.github.com/2859988.git"
Expand All @@ -103,8 +154,8 @@
end

context "default git sources" do
it "has bitbucket, gist, and github" do
expect(subject.instance_variable_get(:@git_sources).keys.sort).to eq(%w[bitbucket gist github])
it "has bitbucket, gist, github, and gitlab" do
expect(subject.instance_variable_get(:@git_sources).keys.sort).to eq(%w[bitbucket gist github gitlab])
end
end
end
Expand Down

0 comments on commit d3222bf

Please sign in to comment.