Adopt Git style URI

This commit is contained in:
Lin Jen-Shin 2018-02-12 16:43:32 +08:00
parent 75984534f8
commit bc2739a52f
3 changed files with 67 additions and 1 deletions

View file

@ -1,3 +1,4 @@
require 'cgi'
require 'uri' require 'uri'
module QA module QA
@ -5,6 +6,19 @@ module QA
class Repository class Repository
include Scenario::Actable include Scenario::Actable
# See: config/initializers/1_settings.rb
# Settings#build_gitlab_shell_ssh_path_prefix
def self.parse_uri(git_uri)
if git_uri.start_with?('ssh://')
URI.parse(git_uri)
else
*rest, path = git_uri.split(':')
# Host cannot have : so we'll need to escape it
user_host = rest.join('%3A').sub(/\A\[(.+)\]\z/, '\1')
URI.parse("ssh://#{user_host}/#{path}")
end
end
def self.perform(*args) def self.perform(*args)
Dir.mktmpdir do |dir| Dir.mktmpdir do |dir|
Dir.chdir(dir) { super } Dir.chdir(dir) { super }

View file

@ -45,7 +45,7 @@ module QA
repository_location repository_location
end end
repository_uri = URI.parse(repository_url) repository_uri = Git::Repository.parse_uri(repository_url)
gitlab_ci = gitlab_ci =
<<~YAML <<~YAML

View file

@ -0,0 +1,52 @@
describe QA::Git::Repository do
describe '.parse_uri' do
context 'when URI starts with ssh://' do
context 'when URI has port' do
it 'parses correctly' do
uri = described_class
.parse_uri('ssh://git@qa.test:2222/sandbox/qa/repo.git')
expect(uri.user).to eq('git')
expect(uri.host).to eq('qa.test')
expect(uri.port).to eq(2222)
expect(uri.path).to eq('/sandbox/qa/repo.git')
end
end
context 'when URI does not have port' do
it 'parses correctly' do
uri = described_class
.parse_uri('ssh://git@qa.test/sandbox/qa/repo.git')
expect(uri.user).to eq('git')
expect(uri.host).to eq('qa.test')
expect(uri.path).to eq('/sandbox/qa/repo.git')
end
end
end
context 'when URI does not start with ssh://' do
context 'when host does not have colons' do
it 'parses correctly' do
uri = described_class
.parse_uri('git@qa.test:sandbox/qa/repo.git')
expect(uri.user).to eq('git')
expect(uri.host).to eq('qa.test')
expect(uri.path).to eq('/sandbox/qa/repo.git')
end
end
context 'when host has a colon' do
it 'parses correctly' do
uri = described_class
.parse_uri('[git@qa:test]:sandbox/qa/repo.git')
expect(uri.user).to eq('git')
expect(uri.host).to eq('qa%3Atest')
expect(uri.path).to eq('/sandbox/qa/repo.git')
end
end
end
end
end