Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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