From 176b3c602eaf942af4488e76f6714e2a706f5383 Mon Sep 17 00:00:00 2001 From: York Chen Date: Tue, 11 Apr 2023 15:53:29 -0400 Subject: [PATCH] fix: downloading github url with / in branch name --- detect_github.go | 20 ++++++++++++++------ detect_github_test.go | 4 ++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/detect_github.go b/detect_github.go index 4bf4daf23..b6af9d23f 100644 --- a/detect_github.go +++ b/detect_github.go @@ -23,13 +23,17 @@ func (d *GitHubDetector) Detect(src, _ string) (string, bool, error) { } func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) { - parts := strings.Split(src, "/") - if len(parts) < 3 { + parts := strings.Split(src, "?") + if len(parts) > 2 { + return "", false, fmt.Errorf("there is more than 1 '?' in the URL") + } + hostAndPath := parts[0] + hostAndPathParts := strings.Split(hostAndPath, "/") + if len(hostAndPathParts) < 3 { return "", false, fmt.Errorf( "GitHub URLs should be github.com/username/repo") } - - urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/")) + urlStr := fmt.Sprintf("https://%s", strings.Join(hostAndPathParts[:3], "/")) url, err := url.Parse(urlStr) if err != nil { return "", true, fmt.Errorf("error parsing GitHub URL: %s", err) @@ -39,8 +43,12 @@ func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) { url.Path += ".git" } - if len(parts) > 3 { - url.Path += "//" + strings.Join(parts[3:], "/") + if len(hostAndPathParts) > 3 { + url.Path += "//" + strings.Join(hostAndPathParts[3:], "/") + } + + if len(parts) == 2 { + url.RawQuery = parts[1] } return "git::" + url.String(), true, nil diff --git a/detect_github_test.go b/detect_github_test.go index 70f1c8329..196c8dba4 100644 --- a/detect_github_test.go +++ b/detect_github_test.go @@ -24,6 +24,10 @@ func TestGitHubDetector(t *testing.T) { "github.com/hashicorp/foo.git?foo=bar", "git::https://github.com/hashicorp/foo.git?foo=bar", }, + { + "github.com/hashicorp/foo.git?foo=bar/foobar,barfoo=bar", + "git::https://github.com/hashicorp/foo.git?foo=bar/foobar,barfoo=bar", + }, } pwd := "/pwd"