Skip to content

Commit

Permalink
Merge pull request #1 from tvizor/allow-nested-repos-gitlab-com-allow…
Browse files Browse the repository at this point in the history
…-subpath-query

fix: #479 allow nested repos for gitlab.com
  • Loading branch information
tvizor authored Jun 28, 2024
2 parents 268c11c + 5515e0a commit e484031
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 e484031

Please sign in to comment.