Commit bc2739a5 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Adopt Git style URI

parent 75984534
require 'cgi'
require 'uri' require 'uri'
module QA module QA
...@@ -5,6 +6,19 @@ 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 }
......
...@@ -45,7 +45,7 @@ module QA ...@@ -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
......
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment