Skip to content

Commit

Permalink
GitService: Improve hostname extraction
Browse files Browse the repository at this point in the history
This commit adds support for more repository notations and provides unit
tests.
  • Loading branch information
neomilium committed Oct 5, 2021
1 parent a476c95 commit ce795d5
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 6 deletions.
33 changes: 29 additions & 4 deletions lib/modulesync/git_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ def self.endpoint_for(sourcecode:, type:)
end

# This method attempts to guess the git service endpoint based on remote and type
# FIXME: It only supports "git@hostname:repo_path" scheme
def self.guess_endpoint_from(remote:, type:)
pattern = /^git@(.*):(.*)(\.git)*$/
return nil unless remote.match?(pattern)
hostname = extract_hostname(remote)
return nil if hostname.nil?

endpoint = remote.sub(pattern, 'https://\1')
endpoint = "https://#{hostname}"
endpoint += '/api/v4' if type == :gitlab
endpoint
end
Expand All @@ -121,5 +120,31 @@ def self.token_for(sourcecode:, type:)

token
end

# This method extracts hostname from URL like:
#
# - ssh://[user@]host.xz[:port]/path/to/repo.git/
# - git://host.xz[:port]/path/to/repo.git/
# - [user@]host.xz:path/to/repo.git/
# - http[s]://host.xz[:port]/path/to/repo.git/
# - ftp[s]://host.xz[:port]/path/to/repo.git/
#
# Returns nil if
# - /path/to/repo.git/
# - file:///path/to/repo.git/
# - any invalid URL
def self.extract_hostname(url)
#pattern = /^((?<user>.*)@)*(?<hostname>.*):(?<path>[a-zA-Z].*)*$/
return nil if url.start_with?('/') || url.start_with?('file://') # local path (e.g. file:///path/to/repo)

unless url.start_with?(/[a-z]+:\/\//) # SSH notation does not contain protocol (e.g. user@server:path/to/repo/)
pattern = /^(.*@)?(?<hostname>[\w|.]*):(.*)$/ # SSH path (e.g. user@server:repo)
return url.match(pattern)[:hostname] if url.match?(pattern)
end

return URI.parse(url).host
rescue URI::InvalidURIError => e
return nil
end
end
end
10 changes: 8 additions & 2 deletions lib/modulesync/source_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ def path(*parts)
File.join(working_directory, *parts)
end

def git_service
@git_service ||= GitService.instantiate(**git_service_configuration)
end

def git_service_configuration
@git_service_configuration ||= GitService.configuration_for(sourcecode: self)
end

def open_pull_request
git_service_options = GitService.configuration_for(sourcecode: self)
git_service = GitService.instantiate(**git_service_options)
git_service.open_pull_request(
repo_path: repository_path,
namespace: repository_namespace,
Expand Down
40 changes: 40 additions & 0 deletions spec/unit/modulesync/git_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'modulesync'
require 'modulesync/git_service'

describe ModuleSync::GitService do
Expand Down Expand Up @@ -164,4 +165,43 @@
end
end
end

RSpec.shared_examples 'hostname_extractor' do |url, hostname|
context "with '#{url}' URL" do
subject { ModuleSync::GitService.extract_hostname(url) }
it "should extract '#{hostname}' as hostname" do
expect(subject).to eq(hostname)
end
end
end

context '#extract_hostname' do
[
%w[ssh://[email protected]:4444/path/to/repo.git/ host.xz],
%w[ssh://[email protected]:/path/to/repo.git/ host.xz],
%w[ssh://host.xz/path/to/repo.git/ host.xz],

%w[git://host.xz/path/to/repo.git/ host.xz],
%w[git://host.xz/path/to/repo/ host.xz],
%w[git://host.xz/path/to/repo host.xz],

%w[[email protected]:path/to/repo.git/ host.xz],
%w[[email protected]:path/to/repo.git host.xz],
%w[[email protected]:path/to/repo host.xz],
%w[host.xz:path/to/repo.git/ host.xz],

%w[https://host.xz:8443/path/to/repo.git/ host.xz],
%w[https://host.xz/path/to/repo.git/ host.xz],

%w[ftp://host.xz/path/to/repo/ host.xz],

['/path/to/repo.git/', nil],

['file:///path/to/repo.git/', nil],

['something-invalid', nil],
].each do |url, hostname|
it_should_behave_like 'hostname_extractor', url, hostname
end
end
end

0 comments on commit ce795d5

Please sign in to comment.