diff --git a/config/module/detect_github.go b/config/module/detect_github.go index e4854cf42..7e7deb653 100644 --- a/config/module/detect_github.go +++ b/config/module/detect_github.go @@ -25,7 +25,13 @@ func (d *GitHubDetector) Detect(src, _ string) (string, bool, error) { } func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) { - urlStr := fmt.Sprintf("https://%s", src) + parts := strings.Split(src, "/") + if len(parts) < 3 { + return "", false, fmt.Errorf( + "GitHub URLs should be github.com/username/repo") + } + + urlStr := fmt.Sprintf("https://%s", strings.Join(parts[:3], "/")) url, err := url.Parse(urlStr) if err != nil { return "", true, fmt.Errorf("error parsing GitHub URL: %s", err) @@ -35,6 +41,10 @@ func (d *GitHubDetector) detectHTTP(src string) (string, bool, error) { url.Path += ".git" } + if len(parts) > 3 { + url.Path += "//" + strings.Join(parts[3:], "/") + } + return "git::" + url.String(), true, nil } diff --git a/config/module/detect_github_test.go b/config/module/detect_github_test.go index 37fac84c8..822e1806d 100644 --- a/config/module/detect_github_test.go +++ b/config/module/detect_github_test.go @@ -12,6 +12,10 @@ func TestGitHubDetector(t *testing.T) { // HTTP {"github.com/hashicorp/foo", "git::https://github.com/hashicorp/foo.git"}, {"github.com/hashicorp/foo.git", "git::https://github.com/hashicorp/foo.git"}, + { + "github.com/hashicorp/foo/bar", + "git::https://github.com/hashicorp/foo.git//bar", + }, { "github.com/hashicorp/foo?foo=bar", "git::https://github.com/hashicorp/foo.git?foo=bar", @@ -23,6 +27,10 @@ func TestGitHubDetector(t *testing.T) { // SSH {"git@github.com:hashicorp/foo.git", "git::ssh://git@github.com/hashicorp/foo.git"}, + { + "git@github.com:hashicorp/foo.git//bar", + "git::ssh://git@github.com/hashicorp/foo.git//bar", + }, { "git@github.com:hashicorp/foo.git?foo=bar", "git::ssh://git@github.com/hashicorp/foo.git?foo=bar", diff --git a/config/module/tree.go b/config/module/tree.go index 27fdecf6e..b7cfc5108 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -146,6 +146,12 @@ func (t *Tree) Load(s Storage, mode GetMode) error { return fmt.Errorf("module %s: %s", m.Name, err) } + // Check if the detector introduced something new. + source, subDir2 := getDirSubdir(source) + if subDir2 != "" { + subDir = filepath.Join(subDir2, subDir) + } + // Get the directory where this module is so we can load it dir, ok, err := getStorage(s, source, mode) if err != nil {