Skip to content

Commit

Permalink
fix: downloading github url with / in branch name
Browse files Browse the repository at this point in the history
  • Loading branch information
York Chen committed Apr 11, 2023
1 parent c12e42f commit 176b3c6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 14 additions & 6 deletions detect_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions detect_github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 176b3c6

Please sign in to comment.