Skip to content

Commit

Permalink
[match] Adding support for self-managed GitLab instances (#21274)
Browse files Browse the repository at this point in the history
* Adding support for self-managed GitLab instances

* Adding gitlab_host option to the match generator

* Fixing rubocop issues
  • Loading branch information
darbyfrey committed May 13, 2023
1 parent b3ae1b0 commit 7b3e224
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions match/lib/match/importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def import_cert(params, cert_path: nil, p12_path: nil, profile_path: nil)
s3_secret_access_key: params[:s3_secret_access_key],
s3_object_prefix: params[:s3_object_prefix],
gitlab_project: params[:gitlab_project],
gitlab_host: params[:gitlab_host],
readonly: params[:readonly],
username: params[:username],
team_id: params[:team_id],
Expand Down
1 change: 1 addition & 0 deletions match/lib/match/nuke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def run(params, type: nil)
s3_bucket: params[:s3_bucket].to_s,
s3_object_prefix: params[:s3_object_prefix].to_s,
gitlab_project: params[:gitlab_project],
gitlab_host: params[:gitlab_host],
team_id: params[:team_id] || Spaceship::ConnectAPI.client.portal_team_id
})
self.storage.download
Expand Down
5 changes: 5 additions & 0 deletions match/lib/match/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ def self.available_options
env_name: "MATCH_GITLAB_PROJECT",
description: "GitLab Project Path (i.e. 'gitlab-org/gitlab')",
optional: true),
FastlaneCore::ConfigItem.new(key: :gitlab_host,
env_name: "MATCH_GITLAB_HOST",
default_value: 'https://gitlab.com',
description: "GitLab Host (i.e. 'https://gitlab.com')",
optional: true),

# Keychain
FastlaneCore::ConfigItem.new(key: :keychain_name,
Expand Down
1 change: 1 addition & 0 deletions match/lib/match/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def run(params)
s3_bucket: params[:s3_bucket],
s3_object_prefix: params[:s3_object_prefix],
gitlab_project: params[:gitlab_project],
gitlab_host: params[:gitlab_host],
readonly: params[:readonly],
username: params[:readonly] ? nil : params[:username], # only pass username if not readonly
team_id: params[:team_id],
Expand Down
14 changes: 10 additions & 4 deletions match/lib/match/storage/gitlab_secure_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ class GitLabSecureFiles < Interface
attr_reader :team_name
attr_reader :api_key_path
attr_reader :api_key
attr_reader :api_v4_url

def self.configure(params)
api_v4_url = params[:api_v4_url] || ENV['CI_API_V4_URL'] || 'https://gitlab.com/api/v4'
api_v4_url = ENV['CI_API_V4_URL'] || "#{params[:gitlab_host]}/api/v4"
project_id = params[:gitlab_project] || ENV['GITLAB_PROJECT'] || ENV['CI_PROJECT_ID']
job_token = params[:job_token] || ENV['CI_JOB_TOKEN']
private_token = params[:private_token] || ENV['PRIVATE_TOKEN']
Expand Down Expand Up @@ -72,9 +73,9 @@ def initialize(api_v4_url: nil,
@private_token = private_token
@api_v4_url = api_v4_url
@project_id = project_id
@gitlab_client = GitLab::Client.new(job_token: job_token, private_token: private_token, project_id: project_id, api_v4_url: api_v4_url)
@gitlab_client = GitLab::Client.new(job_token: @job_token, private_token: @private_token, project_id: @project_id, api_v4_url: @api_v4_url)

UI.message("Initializing match for GitLab project #{@project_id}")
UI.message("Initializing match for GitLab project #{@project_id} on #{@gitlab_host}")
end

# To make debugging easier, we have a custom exception here
Expand Down Expand Up @@ -174,8 +175,13 @@ def list_files(file_name: "", file_ext: "")
# that should be generated
def generate_matchfile_content(template: nil)
project = UI.input("What is your GitLab Project (i.e. gitlab-org/gitlab): ")
host = UI.input("What is your GitLab Host (i.e. https://gitlab.example.com, skip to default to https://gitlab.com): ")

return "gitlab_project(\"#{project}\")"
content = "gitlab_project(\"#{project}\")"

content += "\ngitlab_host(\"#{host}\")" if host

return content
end
end
end
Expand Down
1 change: 1 addition & 0 deletions match/spec/importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def setup_fake_storage(repo_dir, config)
s3_secret_access_key: nil,
s3_object_prefix: nil,
gitlab_project: nil,
gitlab_host: 'https://gitlab.com',
readonly: false,
username: config[:username],
team_id: nil,
Expand Down
1 change: 1 addition & 0 deletions match/spec/nuke_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
s3_bucket: "",
s3_object_prefix: "",
gitlab_project: nil,
gitlab_host: 'https://gitlab.com',
team_id: nil
).and_return(fake_storage)

Expand Down
4 changes: 4 additions & 0 deletions match/spec/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
s3_bucket: nil,
s3_object_prefix: nil,
gitlab_project: nil,
gitlab_host: 'https://gitlab.com',
readonly: false,
username: values[:username],
team_id: nil,
Expand Down Expand Up @@ -154,6 +155,7 @@
s3_bucket: nil,
s3_object_prefix: nil,
gitlab_project: nil,
gitlab_host: 'https://gitlab.com',
readonly: false,
username: values[:username],
team_id: nil,
Expand Down Expand Up @@ -244,6 +246,7 @@
s3_bucket: nil,
s3_object_prefix: nil,
gitlab_project: nil,
gitlab_host: 'https://gitlab.com',
readonly: false,
username: values[:username],
team_id: nil,
Expand Down Expand Up @@ -316,6 +319,7 @@
s3_bucket: nil,
s3_object_prefix: nil,
gitlab_project: nil,
gitlab_host: 'https://gitlab.com',
readonly: false,
username: values[:username],
team_id: nil,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
describe Match do
describe Match::Storage::GitLabSecureFiles do
subject { described_class.new(api_v4_url: 'https://example.com/api', private_token: 'abc123', project_id: 'fake-project') }
subject { described_class.new(private_token: 'abc123', project_id: 'fake-project') }
let(:working_directory) { '/fake/path/to/files' }

before do
allow(subject).to receive(:working_directory).and_return(working_directory)
end

describe '.configure' do
describe 'api_v4_url' do
it 'sets the value to CI_API_V4_URL when supplied' do
stub_const('ENV', ENV.to_hash.merge('CI_API_V4_URL' => 'https://gitlab.com/api/v4'))

storage = described_class.configure({})

expect(storage.api_v4_url).to eq('https://gitlab.com/api/v4')
end

it 'sets the value based on the gitlab_host param' do
storage = described_class.configure(gitlab_host: 'http://gitlab.foo.com')

expect(storage.api_v4_url).to eq('http://gitlab.foo.com/api/v4')
end
end
end

describe '#upload_files' do
let(:files_to_upload) do
[
Expand Down Expand Up @@ -96,11 +114,19 @@
end

describe '#generate_matchfile_content' do
it 'returns the correct match file contents for the configured storage mode' do
it 'returns the correct match file contents for the configured storage mode and project path' do
expect(FastlaneCore::UI).to receive(:input).once.and_return("fake-project")
expect(FastlaneCore::UI).to receive(:input).once.and_return(nil)

expect(subject.generate_matchfile_content).to eq('gitlab_project("fake-project")')
end

it 'returns the correct match file contents for the configured storage mode and project path and gitlab host' do
expect(FastlaneCore::UI).to receive(:input).once.and_return("fake-project")
expect(FastlaneCore::UI).to receive(:input).once.and_return("https://gitlab.example.com")

expect(subject.generate_matchfile_content).to eq("gitlab_project(\"fake-project\")\ngitlab_host(\"https://gitlab.example.com\")")
end
end
end
end

0 comments on commit 7b3e224

Please sign in to comment.