Skip to content

Commit 5515e0a

Browse files
author
tvizor
committed
fix: #479 allow nested repos for gitlab.com
1 parent 268c11c commit 5515e0a

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

detect_gitlab.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,29 @@ func (d *GitLabDetector) Detect(src, _ string) (string, bool, error) {
2323
}
2424

2525
func (d *GitLabDetector) detectHTTP(src string) (string, bool, error) {
26-
parts := strings.Split(src, "/")
27-
if len(parts) < 3 {
28-
return "", false, fmt.Errorf(
29-
"GitLab URLs should be gitlab.com/username/repo")
30-
}
31-
32-
urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
33-
repoUrl, err := url.Parse(urlStr)
26+
repoUrl, err := url.Parse(fmt.Sprintf("https://%s", src))
3427
if err != nil {
3528
return "", true, fmt.Errorf("error parsing GitLab URL: %s", err)
3629
}
3730

38-
if !strings.HasSuffix(repoUrl.Path, ".git") {
39-
repoUrl.Path += ".git"
31+
parts := strings.Split(repoUrl.Path, "//")
32+
33+
if len(strings.Split(parts[0], "/")) < 3 {
34+
return "", false, fmt.Errorf(
35+
"GitLab URLs should be gitlab.com/username/repo " +
36+
"or gitlab.com/organization/project/repo")
4037
}
4138

42-
if len(parts) > 3 {
43-
repoUrl.Path += "//" + strings.Join(parts[3:], "/")
39+
if len(parts) > 2 {
40+
return "", false, fmt.Errorf(
41+
"URL malformed: \"//\" can only used once in path")
42+
}
43+
44+
if !strings.HasSuffix(parts[0], ".git") {
45+
parts[0] += ".git"
4446
}
4547

48+
repoUrl.Path = fmt.Sprintf("%s", strings.Join(parts, "//"))
49+
4650
return "git::" + repoUrl.String(), true, nil
4751
}

detect_gitlab_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestGitLabDetector(t *testing.T) {
1414
{"gitlab.com/hashicorp/foo.git", "git::https://gitlab.com/hashicorp/foo.git"},
1515
{
1616
"gitlab.com/hashicorp/foo/bar",
17-
"git::https://gitlab.com/hashicorp/foo.git//bar",
17+
"git::https://gitlab.com/hashicorp/foo/bar.git",
1818
},
1919
{
2020
"gitlab.com/hashicorp/foo?foo=bar",
@@ -24,6 +24,26 @@ func TestGitLabDetector(t *testing.T) {
2424
"gitlab.com/hashicorp/foo.git?foo=bar",
2525
"git::https://gitlab.com/hashicorp/foo.git?foo=bar",
2626
},
27+
{
28+
"gitlab.com/hashicorp/foo/bar",
29+
"git::https://gitlab.com/hashicorp/foo/bar.git",
30+
},
31+
{
32+
"gitlab.com/hashicorp/foo/bar.git",
33+
"git::https://gitlab.com/hashicorp/foo/bar.git",
34+
},
35+
{
36+
"gitlab.com/hashicorp/foo/bar.git//baz",
37+
"git::https://gitlab.com/hashicorp/foo/bar.git//baz",
38+
},
39+
{
40+
"gitlab.com/hashicorp/foo/bar//baz",
41+
"git::https://gitlab.com/hashicorp/foo/bar.git//baz",
42+
},
43+
{
44+
"gitlab.com/hashicorp/foo/bar.git//baz?foo=bar",
45+
"git::https://gitlab.com/hashicorp/foo/bar.git//baz?foo=bar",
46+
},
2747
}
2848

2949
pwd := "/pwd"

0 commit comments

Comments
 (0)