Skip to content

Commit d20a8de

Browse files
author
Peter Degen-Portnoy
committed
Merge pull request #12 from blackducksw/OTWO-3048
Change Git Validation to convert https and http protocols for git and web urls to git protocol
2 parents 5f5ef92 + 09d419f commit d20a8de

File tree

2 files changed

+42
-17
lines changed

2 files changed

+42
-17
lines changed

lib/scm/adapters/git/validation.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ def self.public_url_regex
1010

1111
def normalize
1212
super
13-
@url = read_only_url
13+
@url = normalize_url
1414
@branch_name = 'master' if @branch_name.to_s == ''
1515
self
1616
end
1717

18-
# Given a Github read-write URL, return a read-only URL.
19-
# Otherwise, return the original URL.
20-
def read_only_url
18+
# Given a Github read-write URL, return a git protocol read-only URL
19+
# Given a Github web URL, return a git protocol read-only URL
20+
# Given a Git read-write protocol URL, return a git protocol read-only URL
21+
# Else, return the URL
22+
def normalize_url
2123
case @url
22-
when /^https:\/\/\w+@github.com\/(.+)\.git$/
24+
when /^https?:\/\/\w+@github.com\/(.+)\.git$/
2325
"git://github.com/#{$1}.git"
26+
when /^https?:\/\/github.com\/(.+)/
27+
"git://github.com/#{$1}"
2428
when /^git@github.com:(.+)\.git$/
2529
"git://github.com/#{$1}.git"
2630
else

test/unit/git_validation_test.rb

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ def test_accepted_urls
2323
"https://kernel.org/pub/scm/git/git.git",
2424
"https://kernel.org:8080/pub/scm/git/git.git",
2525
"git://kernel.org/~foo/git.git",
26-
"http://git.onerussian.com/pub/deb/impose+.git"
26+
"http://git.onerussian.com/pub/deb/impose+.git",
27+
"https://Person@github.com/Person/some_repo.git",
28+
"http://Person@github.com/Person/some_repo.git",
29+
"https://github.com/Person/some_repo",
30+
"http://github.com/Person/some_repo"
2731
].each do |url|
2832
git = GitAdapter.new(:url => url)
2933
assert !git.validate_url
@@ -41,32 +45,49 @@ def test_guess_forge
4145
assert_equal 'kernel.org', git.guess_forge
4246
end
4347

44-
def test_read_only_url
45-
assert_equal nil, GitAdapter.new(:url => nil).read_only_url
46-
assert_equal '', GitAdapter.new(:url => '').read_only_url
47-
assert_equal 'foo', GitAdapter.new(:url => 'foo').read_only_url
48+
def test_normalize_url
49+
assert_equal nil, GitAdapter.new(:url => nil).normalize_url
50+
assert_equal '', GitAdapter.new(:url => '').normalize_url
51+
assert_equal 'foo', GitAdapter.new(:url => 'foo').normalize_url
4852

4953
# A non-Github URL: no change
5054
assert_equal 'git://kernel.org/pub/scm/git/git.git',
51-
GitAdapter.new(:url => 'git://kernel.org/pub/scm/git/git.git').read_only_url
55+
GitAdapter.new(:url => 'git://kernel.org/pub/scm/git/git.git').normalize_url
5256

5357
# A Github read-write URL: converted to read-only
5458
assert_equal 'git://github.com/robinluckey/ohcount.git',
55-
GitAdapter.new(:url => 'https://robinluckey@github.com/robinluckey/ohcount.git').read_only_url
59+
GitAdapter.new(:url => 'https://robinluckey@github.com/robinluckey/ohcount.git').normalize_url
5660

5761
# A Github read-write URL: converted to read-only
5862
assert_equal 'git://github.com/robinluckey/ohcount.git',
59-
GitAdapter.new(:url => 'git@github.com:robinluckey/ohcount.git').read_only_url
63+
GitAdapter.new(:url => 'git@github.com:robinluckey/ohcount.git').normalize_url
6064

6165
# A Github read-only URL: no change
6266
assert_equal 'git://github.com/robinluckey/ohcount.git',
63-
GitAdapter.new(:url => 'git@github.com:robinluckey/ohcount.git').read_only_url
67+
GitAdapter.new(:url => 'git@github.com:robinluckey/ohcount.git').normalize_url
6468
end
6569

6670
def test_normalize_converts_to_read_only
67-
git = GitAdapter.new(:url => 'https://robinluckey@github.com/robinluckey/ohcount.git')
68-
git.normalize
69-
assert_equal 'git://github.com/robinluckey/ohcount.git', git.url
71+
normalize_url_test('https://robinluckey@github.com/robinluckey/ohcount.git', 'git://github.com/robinluckey/ohcount.git')
72+
end
73+
74+
def test_normalize_handles_https_with_user_at_github_format
75+
normalize_url_test('http://Person@github.com/Person/something.git', 'git://github.com/Person/something.git')
76+
end
77+
78+
def test_normalize_handles_https_web_url
79+
normalize_url_test('https://github.com/Person/something', 'git://github.com/Person/something')
7080
end
81+
82+
def test_normalize_handles_http_web_url
83+
normalize_url_test('http://github.com/Person/something', 'git://github.com/Person/something')
84+
end
85+
86+
private
87+
def normalize_url_test(url, result_url)
88+
git = GitAdapter.new(:url => url)
89+
git.normalize
90+
assert_equal result_url, git.url
91+
end
7192
end
7293
end

0 commit comments

Comments
 (0)