Skip to content

Commit

Permalink
fix: Adding GitHub Enterprise support for catalog (#3678)
Browse files Browse the repository at this point in the history
  • Loading branch information
yhakbar authored Dec 18, 2024
1 parent d2a0285 commit 0397ec4
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 17 deletions.
43 changes: 26 additions & 17 deletions cli/commands/catalog/module/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
)

const (
githubHost = "github.com"
gitlabHost = "gitlab.com"
azuredevHost = "dev.azure.com"
bitbucketHost = "bitbucket.org"
githubHost = "github.com"
githubEnterpriseRegex = `^(github\.(.+))$`
gitlabHost = "gitlab.com"
azuredevHost = "dev.azure.com"
bitbucketHost = "bitbucket.org"
)

var (
Expand All @@ -40,8 +41,8 @@ type Repo struct {
cloneURL string
path string

remoteURL string
branchName string
RemoteURL string
BranchName string

walkWithSymlinks bool
}
Expand Down Expand Up @@ -123,29 +124,37 @@ func (repo *Repo) FindModules(ctx context.Context) (Modules, error) {
return modules, nil
}

var githubEnterprisePatternReg = regexp.MustCompile(githubEnterpriseRegex)

// ModuleURL returns the URL of the module in this repository. `moduleDir` is the path from the repository root.
func (repo *Repo) ModuleURL(moduleDir string) (string, error) {
if repo.remoteURL == "" {
if repo.RemoteURL == "" {
return filepath.Join(repo.path, moduleDir), nil
}

remote, err := vcsurl.Parse(repo.remoteURL)
remote, err := vcsurl.Parse(repo.RemoteURL)
if err != nil {
return "", errors.New(err)
}

// Simple, predictable hosts
switch remote.Host {
case githubHost:
return fmt.Sprintf("https://%s/%s/tree/%s/%s", remote.Host, remote.FullName, repo.branchName, moduleDir), nil
return fmt.Sprintf("https://%s/%s/tree/%s/%s", remote.Host, remote.FullName, repo.BranchName, moduleDir), nil
case gitlabHost:
return fmt.Sprintf("https://%s/%s/-/tree/%s/%s", remote.Host, remote.FullName, repo.branchName, moduleDir), nil
return fmt.Sprintf("https://%s/%s/-/tree/%s/%s", remote.Host, remote.FullName, repo.BranchName, moduleDir), nil
case bitbucketHost:
return fmt.Sprintf("https://%s/%s/browse/%s?at=%s", remote.Host, remote.FullName, moduleDir, repo.branchName), nil
return fmt.Sprintf("https://%s/%s/browse/%s?at=%s", remote.Host, remote.FullName, moduleDir, repo.BranchName), nil
case azuredevHost:
return fmt.Sprintf("https://%s/_git/%s?path=%s&version=GB%s", remote.Host, remote.FullName, moduleDir, repo.branchName), nil
default:
return "", errors.Errorf("hosting: %q is not supported yet", remote.Host)
return fmt.Sprintf("https://%s/_git/%s?path=%s&version=GB%s", remote.Host, remote.FullName, moduleDir, repo.BranchName), nil
}

// // Hosts that require special handling
if githubEnterprisePatternReg.MatchString(string(remote.Host)) {
return fmt.Sprintf("https://%s/%s/tree/%s/%s", remote.Host, remote.FullName, repo.BranchName, moduleDir), nil
}

return "", errors.Errorf("hosting: %q is not supported yet", remote.Host)
}

// clone clones the repository to a temporary directory if the repoPath is URL
Expand Down Expand Up @@ -251,8 +260,8 @@ func (repo *Repo) parseRemoteURL() error {
return nil
}

repo.remoteURL = inidata.Section(sectionName).Key("url").String()
repo.logger.Debugf("Remote url: %q for repo: %q", repo.remoteURL, repo.path)
repo.RemoteURL = inidata.Section(sectionName).Key("url").String()
repo.logger.Debugf("Remote url: %q for repo: %q", repo.RemoteURL, repo.path)

return nil
}
Expand All @@ -269,7 +278,7 @@ func (repo *Repo) parseBranchName() error {
}

if match := gitHeadBranchNameReg.FindStringSubmatch(data); len(match) > 0 {
repo.branchName = strings.TrimSpace(match[1])
repo.BranchName = strings.TrimSpace(match[1])
return nil
}

Expand Down
81 changes: 81 additions & 0 deletions cli/commands/catalog/module/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/gruntwork-io/terragrunt/cli/commands/catalog/module"
"github.com/gruntwork-io/terragrunt/internal/errors"
"github.com/gruntwork-io/terragrunt/pkg/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -86,3 +87,83 @@ func TestFindModules(t *testing.T) {
}

}

func TestModuleURL(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
repo *module.Repo
moduleDir string
expectedURL string
expectedErr error
}{
{
"github",
newRepo(t, "https://github.com/acme/terraform-aws-modules"),
".",
"https://github.com/acme/terraform-aws-modules/tree/main/.",
nil,
},
{
"github enterprise",
newRepo(t, "https://github.acme.com/acme/terraform-aws-modules"),
".",
"https://github.acme.com/acme/terraform-aws-modules/tree/main/.",
nil,
},
{
"gitlab",
newRepo(t, "https://gitlab.com/acme/terraform-aws-modules"),
".",
"https://gitlab.com/acme/terraform-aws-modules/-/tree/main/.",
nil,
},
{
"bitbucket",
newRepo(t, "https://bitbucket.org/acme/terraform-aws-modules"),
".",
"https://bitbucket.org/acme/terraform-aws-modules/browse/.?at=main",
nil,
},
{
"azuredev",
newRepo(t, "https://dev.azure.com/acme/terraform-aws-modules"),
".",
"https://dev.azure.com/_git/acme/terraform-aws-modules?path=.&version=GBmain",
nil,
},
{
"unsupported",
newRepo(t, "https://fake.com/acme/terraform-aws-modules"),
".",
"",
errors.Errorf("hosting: %q is not supported yet", "fake.com"),
},
}

for _, testCase := range testCases {
testCase := testCase

t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

url, err := testCase.repo.ModuleURL(testCase.moduleDir)
assert.Equal(t, testCase.expectedURL, url)
if testCase.expectedErr != nil {
assert.EqualError(t, err, testCase.expectedErr.Error())
} else {
assert.NoError(t, err)
}
})
}
}

func newRepo(t *testing.T, url string) *module.Repo {
t.Helper()

return &module.Repo{
RemoteURL: url,
BranchName: "main",
}
}

0 comments on commit 0397ec4

Please sign in to comment.