Skip to content

Commit

Permalink
Extract slug from GitHub Enterprise URLs
Browse files Browse the repository at this point in the history
Co-authored-by: Markus Wolf <[email protected]>
  • Loading branch information
ZauberNerd and KnisterPeter committed May 4, 2021
1 parent c15e4a7 commit fcf1bc5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions pkg/common/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,12 @@ func findGitPrettyRef(head, root, sub string) (string, error) {
}

// FindGithubRepo get the repo
func FindGithubRepo(file string) (string, error) {
func FindGithubRepo(file string, githubInstance string) (string, error) {
url, err := findGitRemoteURL(file)
if err != nil {
return "", err
}
_, slug, err := findGitSlug(url)
_, slug, err := findGitSlug(url, githubInstance)
return slug, err
}

Expand All @@ -174,7 +174,7 @@ func findGitRemoteURL(file string) (string, error) {
return url, nil
}

func findGitSlug(url string) (string, string, error) {
func findGitSlug(url string, githubInstance string) (string, string, error) {
if matches := codeCommitHTTPRegex.FindStringSubmatch(url); matches != nil {
return "CodeCommit", matches[2], nil
} else if matches := codeCommitSSHRegex.FindStringSubmatch(url); matches != nil {
Expand All @@ -183,6 +183,14 @@ func findGitSlug(url string) (string, string, error) {
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 {
return "GitHubEnterprise", fmt.Sprintf("%s/%s", matches[1], matches[2]), nil
}
}
return "", url, nil
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestFindGitSlug(t *testing.T) {
}

for _, tt := range slugTests {
provider, slug, err := findGitSlug(tt.url)
provider, slug, err := findGitSlug(tt.url, "github.com")

assert.NoError(err)
assert.Equal(tt.provider, provider)
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/run_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func (rc *RunContext) getGithubContext() *githubContext {
}

repoPath := rc.Config.Workdir
repo, err := common.FindGithubRepo(repoPath)
repo, err := common.FindGithubRepo(repoPath, rc.Config.GitHubInstance)
if err != nil {
log.Warningf("unable to get git repo: %v", err)
} else {
Expand Down Expand Up @@ -620,7 +620,7 @@ func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
env["GITHUB_SHA"] = github.Sha
env["GITHUB_REF"] = github.Ref
env["GITHUB_TOKEN"] = github.Token
if (rc.Config.GitHubInstance == "github.com") {
if rc.Config.GitHubInstance == "github.com" {
env["GITHUB_SERVER_URL"] = "https://github.com"
env["GITHUB_API_URL"] = "https://api.github.com"
env["GITHUB_GRAPHQL_URL"] = "https://api.github.com/graphql"
Expand Down

0 comments on commit fcf1bc5

Please sign in to comment.