Skip to content

Commit

Permalink
fix: hashicorp#479 allow nested repos for gitlab.com
Browse files Browse the repository at this point in the history
  • Loading branch information
tvizor committed Jun 3, 2024
1 parent 268c11c commit 5515e0a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
28 changes: 16 additions & 12 deletions detect_gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,29 @@ func (d *GitLabDetector) Detect(src, _ string) (string, bool, error) {
}

func (d *GitLabDetector) detectHTTP(src string) (string, bool, error) {
parts := strings.Split(src, "/")
if len(parts) < 3 {
return "", false, fmt.Errorf(
"GitLab URLs should be gitlab.com/username/repo")
}

urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/"))
repoUrl, err := url.Parse(urlStr)
repoUrl, err := url.Parse(fmt.Sprintf("https://%s", src))
if err != nil {
return "", true, fmt.Errorf("error parsing GitLab URL: %s", err)
}

if !strings.HasSuffix(repoUrl.Path, ".git") {
repoUrl.Path += ".git"
parts := strings.Split(repoUrl.Path, "//")

if len(strings.Split(parts[0], "/")) < 3 {
return "", false, fmt.Errorf(
"GitLab URLs should be gitlab.com/username/repo " +
"or gitlab.com/organization/project/repo")
}

if len(parts) > 3 {
repoUrl.Path += "//" + strings.Join(parts[3:], "/")
if len(parts) > 2 {
return "", false, fmt.Errorf(
"URL malformed: \"//\" can only used once in path")
}

if !strings.HasSuffix(parts[0], ".git") {
parts[0] += ".git"
}

repoUrl.Path = fmt.Sprintf("%s", strings.Join(parts, "//"))

return "git::" + repoUrl.String(), true, nil
}
22 changes: 21 additions & 1 deletion detect_gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGitLabDetector(t *testing.T) {
{"gitlab.com/hashicorp/foo.git", "git::https://gitlab.com/hashicorp/foo.git"},
{
"gitlab.com/hashicorp/foo/bar",
"git::https://gitlab.com/hashicorp/foo.git//bar",
"git::https://gitlab.com/hashicorp/foo/bar.git",
},
{
"gitlab.com/hashicorp/foo?foo=bar",
Expand All @@ -24,6 +24,26 @@ func TestGitLabDetector(t *testing.T) {
"gitlab.com/hashicorp/foo.git?foo=bar",
"git::https://gitlab.com/hashicorp/foo.git?foo=bar",
},
{
"gitlab.com/hashicorp/foo/bar",
"git::https://gitlab.com/hashicorp/foo/bar.git",
},
{
"gitlab.com/hashicorp/foo/bar.git",
"git::https://gitlab.com/hashicorp/foo/bar.git",
},
{
"gitlab.com/hashicorp/foo/bar.git//baz",
"git::https://gitlab.com/hashicorp/foo/bar.git//baz",
},
{
"gitlab.com/hashicorp/foo/bar//baz",
"git::https://gitlab.com/hashicorp/foo/bar.git//baz",
},
{
"gitlab.com/hashicorp/foo/bar.git//baz?foo=bar",
"git::https://gitlab.com/hashicorp/foo/bar.git//baz?foo=bar",
},
}

pwd := "/pwd"
Expand Down

0 comments on commit 5515e0a

Please sign in to comment.