diff --git a/pkg/common/git/git.go b/pkg/common/git/git.go index d1a5a0ef..d1802a98 100644 --- a/pkg/common/git/git.go +++ b/pkg/common/git/git.go @@ -198,21 +198,34 @@ func findGitRemoteURL(_ context.Context, file, remoteName string) (string, error return remote.Config().URLs[0], nil } +type findStringSubmatcher interface { + FindStringSubmatch(string) []string +} + +func matchesRegex(url string, matchers ...findStringSubmatcher) []string { + for _, regex := range matchers { + if matches := regex.FindStringSubmatch(url); matches != nil { + return matches + } + } + + return nil +} + func findGitSlug(url string, githubInstance string) (string, string, error) { - if matches := codeCommitHTTPRegex.FindStringSubmatch(url); matches != nil { + if matches := matchesRegex(url, codeCommitHTTPRegex, codeCommitSSHRegex); matches != nil { return "CodeCommit", matches[2], nil - } else if matches := codeCommitSSHRegex.FindStringSubmatch(url); matches != nil { - return "CodeCommit", matches[2], nil - } else if matches := githubHTTPRegex.FindStringSubmatch(url); matches != nil { + } + + if matches := matchesRegex(url, githubHTTPRegex, githubSSHRegex); matches != nil { return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil - } else if matches := githubSSHRegex.FindStringSubmatch(url); matches != nil { - return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil - } else if githubInstance != "github.com" { - gheHTTPRegex := regexp.MustCompile(fmt.Sprintf(`^https?://%s/(.+)/(.+?)(?:.git)?$`, githubInstance)) - gheSSHRegex := regexp.MustCompile(fmt.Sprintf(`%s[:/](.+)/(.+?)(?:.git)?$`, githubInstance)) - if matches := gheHTTPRegex.FindStringSubmatch(url); matches != nil { - return "GitHubEnterprise", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil - } else if matches := gheSSHRegex.FindStringSubmatch(url); matches != nil { + } + + if githubInstance != "github.com" { + if matches := matchesRegex(url, + regexp.MustCompile(fmt.Sprintf(`^https?://%s/(.+)/(.+?)(?:.git)?$`, githubInstance)), + regexp.MustCompile(fmt.Sprintf(`%s[:/](.+)/(.+?)(?:.git)?$`, githubInstance)), + ); matches != nil { return "GitHubEnterprise", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil } }