diff --git a/pkg/common/git/git.go b/pkg/common/git/git.go index 706f9ca5802..911ef3c1578 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 { - return "GitHub", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil - } else if matches := githubSSHRegex.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 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 } } diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index b8f338eef96..a412aebc181 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -990,13 +990,18 @@ func nestedMapLookup(m map[string]interface{}, ks ...string) (rval interface{}) } if rval, ok = m[ks[0]]; !ok { return nil - } else if len(ks) == 1 { // we've reached the final key + } + + if len(ks) == 1 { // we've reached the final key return rval - } else if m, ok = rval.(map[string]interface{}); !ok { + } + + if m, ok = rval.(map[string]interface{}); !ok { return nil - } else { // 1+ more keys - return nestedMapLookup(m, ks[1:]...) } + + // 1+ more keys + return nestedMapLookup(m, ks[1:]...) } func (rc *RunContext) withGithubEnv(ctx context.Context, github *model.GithubContext, env map[string]string) map[string]string {