From 5515e0a1ab1760180d1f785e7310572365aab7b8 Mon Sep 17 00:00:00 2001 From: tvizor Date: Mon, 3 Jun 2024 13:09:46 -0700 Subject: [PATCH] fix: #479 allow nested repos for gitlab.com --- detect_gitlab.go | 28 ++++++++++++++++------------ detect_gitlab_test.go | 22 +++++++++++++++++++++- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/detect_gitlab.go b/detect_gitlab.go index 9d1e8d83c..4379e139a 100644 --- a/detect_gitlab.go +++ b/detect_gitlab.go @@ -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 } diff --git a/detect_gitlab_test.go b/detect_gitlab_test.go index f1f85af3c..8734bcb2f 100644 --- a/detect_gitlab_test.go +++ b/detect_gitlab_test.go @@ -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", @@ -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"