Skip to content

Commit

Permalink
Add gitlab: Git source shorthand
Browse files Browse the repository at this point in the history
This new shorthand, similar to the existing `github:` shorthand, adds
support for Gitlab repositories with a notable difference. Gitlab
projects may be organized into projects and subprojects. An example
Ruby gem exists at:

https://gitlab.com/gitlab-org/analytics-section/product-analytics/gl-application-sdk-rb

With the new shorthand, a user may install this gem from its repository
by adding:

```ruby
gem "gitlab-sdk", gitlab: "gitlab-org/analytics-section/product-analytics/gl-application-sdk-rb"
```

As with the `github:` shorthand, a supplied string with no `/` will be
interpreted as `example/example`.

Also in keeping with the utility of the `github:` shorthand, the new
`gitlab:` shorthand also supports Merge Request URLs.

```ruby
gem "gitlab-sdk", gitlab: "https://gitlab.com/gitlab-org/analytics-section/product-analytics/gl-application-sdk-rb/-/merge_requests/27"
```

The `gitlab:` gem source shortcut is modeled on the existing `github:`
shortcut, so the new specs mimic the existing examples.
  • Loading branch information
jgarber623 committed Feb 6, 2024
1 parent 2e28234 commit e52dfa2
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 @@ -31,6 +31,13 @@
expect(subject.dependencies.first.source.uri).to eq(github_uri)
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
Expand All @@ -54,6 +61,29 @@
%(The :tag option can't be used with `github: "https://github.com/indirect/sparks/pull/5"`),
)
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
Expand All @@ -63,6 +93,15 @@
%(The :git option can't be used with `github: "indirect/sparks"`),
)
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
Expand All @@ -76,6 +115,18 @@
github_uri = "https://github.com/rails/rails.git"
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)
Expand Down Expand Up @@ -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 e52dfa2

Please sign in to comment.