From 6f224d66a5210b090c095946fd20b9d33868695f Mon Sep 17 00:00:00 2001 From: Jason Garber Date: Wed, 31 Jan 2024 22:04:50 -0500 Subject: [PATCH] [rubygems/rubygems] Add `gitlab:` Git source shorthand 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. https://github.com/rubygems/rubygems/commit/f4399018c0 --- lib/bundler/dsl.rb | 15 +++++++++ spec/bundler/bundler/dsl_spec.rb | 55 ++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/lib/bundler/dsl.rb b/lib/bundler/dsl.rb index 7b342c594a..6af80fb31f 100644 --- a/lib/bundler/dsl.rb +++ b/lib/bundler/dsl.rb @@ -19,6 +19,7 @@ module Bundler 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 @@ -308,6 +309,20 @@ module Bundler 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) diff --git a/spec/bundler/bundler/dsl_spec.rb b/spec/bundler/bundler/dsl_spec.rb index e2e17bd357..3c3b6c26c3 100644 --- a/spec/bundler/bundler/dsl_spec.rb +++ b/spec/bundler/bundler/dsl_spec.rb @@ -32,6 +32,13 @@ RSpec.describe Bundler::Dsl do 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") @@ -55,6 +62,29 @@ RSpec.describe Bundler::Dsl do ) 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") @@ -64,6 +94,15 @@ RSpec.describe Bundler::Dsl do ) 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") @@ -77,6 +116,18 @@ RSpec.describe Bundler::Dsl do 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" @@ -103,8 +154,8 @@ RSpec.describe Bundler::Dsl do 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