Skip to content

Commit dd1ca0f

Browse files
authored
change (#276)
add url parse validator. add gitlab as one of prefix
1 parent 21c1789 commit dd1ca0f

File tree

2 files changed

+83
-9
lines changed

2 files changed

+83
-9
lines changed

cmd/locations.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"context"
5+
"net/url"
56
"path/filepath"
67
"regexp"
78
"strings"
@@ -83,11 +84,12 @@ func maybeCloneGitUrl(ctx context.Context, repoManager cloner, repoRefreshDurati
8384
return path, nil
8485
}
8586

86-
func isGitURL(str string) bool {
87-
if IsURL(str) && urlPathWithFragmentSuffix.MatchString(str) {
87+
func isGitURL(url string) bool {
88+
str := strings.ToLower(url)
89+
if isValidURL(str) && urlPathWithFragmentSuffix.MatchString(str) {
8890
return true
8991
}
90-
for _, prefix := range []string{"git://", "github.com/", "git@"} {
92+
for _, prefix := range []string{"git://", "github.com/", "gitlab.com/", "git@"} {
9193
if strings.HasPrefix(str, prefix) {
9294
return true
9395
}
@@ -99,9 +101,8 @@ func isGitURL(str string) bool {
99101
// context from the Git repository. See IsGitURL for details.
100102
var urlPathWithFragmentSuffix = regexp.MustCompile(`\.git(?:#.+)?$`)
101103

102-
// IsURL returns true if the provided str is an HTTP(S) URL by checking if it
103-
// has a http:// or https:// scheme. No validation is performed to verify if the
104-
// URL is well-formed.
105-
func IsURL(str string) bool {
106-
return strings.HasPrefix(str, "https://") || strings.HasPrefix(str, "http://")
104+
// isValidURL returns true if the provided str is a well-formed HTTP(S) URL.
105+
func isValidURL(str string) bool {
106+
u, err := url.Parse(str)
107+
return err == nil && (u.Scheme == "http" || u.Scheme == "https")
107108
}

cmd/locations_test.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type fakeCloner struct {
1919
err error
2020
}
2121

22-
func (f *fakeCloner) Clone(ctx context.Context, cloneUrl, branchName string) (*git.Repo, error) {
22+
func (f *fakeCloner) Clone(_ context.Context, cloneUrl, branchName string) (*git.Repo, error) {
2323
f.cloneUrl = cloneUrl
2424
f.branchName = branchName
2525
return f.result, f.err
@@ -199,3 +199,76 @@ func TestMaybeCloneGitUrl_CloneError(t *testing.T) {
199199
})
200200
}
201201
}
202+
203+
func Test_isGitURL(t *testing.T) {
204+
type args struct {
205+
str string
206+
}
207+
tests := []struct {
208+
name string
209+
args args
210+
want bool
211+
}{
212+
{
213+
name: "git url 1",
214+
args: args{
215+
str: "https://gitlab.com/org/team/project.git",
216+
},
217+
want: true,
218+
},
219+
{
220+
name: "git url 2",
221+
args: args{
222+
str: "git://github.com/org/team/project.git",
223+
},
224+
want: true,
225+
},
226+
{
227+
name: "git url 3",
228+
args: args{
229+
str: "http://github.com/org/team/project.git",
230+
},
231+
want: true,
232+
},
233+
{
234+
name: "git url 4",
235+
args: args{
236+
str: "git://test.local/org/team/project.git",
237+
},
238+
want: true,
239+
},
240+
{
241+
name: "git url invalid 1",
242+
args: args{
243+
str: "scp://whatever.com/org/team/project.git",
244+
},
245+
want: false,
246+
},
247+
{
248+
name: "git url invalid 2",
249+
args: args{
250+
str: "ftp://github.com/org/team/project.git",
251+
},
252+
want: false,
253+
},
254+
{
255+
name: "git url invalid 3",
256+
args: args{
257+
str: "thisisnoturl",
258+
},
259+
want: false,
260+
},
261+
{
262+
name: "git url invalid 4",
263+
args: args{
264+
str: "http://zapier.com",
265+
},
266+
want: false,
267+
},
268+
}
269+
for _, tt := range tests {
270+
t.Run(tt.name, func(t *testing.T) {
271+
assert.Equalf(t, tt.want, isGitURL(tt.args.str), "isGitURL(%v)", tt.args.str)
272+
})
273+
}
274+
}

0 commit comments

Comments
 (0)