fix: align github.ref to GitHub Action (#1362)
* fix: align github.ref to GitHub Action The `github.ref` value should be `refs/heads/branch` in most cases. There are a few exceptions handled by the code. This change prefixes the default case with `refs/heads` and adds tests for this and the excpetional cases. * fix: correct existing assertions
This commit is contained in:
parent
7d0407fa4a
commit
97c083e902
4 changed files with 52 additions and 7 deletions
|
@ -95,7 +95,7 @@ func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string
|
||||||
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
// https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads
|
||||||
switch ghc.EventName {
|
switch ghc.EventName {
|
||||||
case "pull_request_target":
|
case "pull_request_target":
|
||||||
ghc.Ref = ghc.BaseRef
|
ghc.Ref = fmt.Sprintf("refs/heads/%s", ghc.BaseRef)
|
||||||
ghc.Sha = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
|
ghc.Sha = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "sha"))
|
||||||
case "pull_request", "pull_request_review", "pull_request_review_comment":
|
case "pull_request", "pull_request_review", "pull_request_review_comment":
|
||||||
ghc.Ref = fmt.Sprintf("refs/pull/%.0f/merge", ghc.Event["number"])
|
ghc.Ref = fmt.Sprintf("refs/pull/%.0f/merge", ghc.Event["number"])
|
||||||
|
@ -110,7 +110,10 @@ func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string
|
||||||
ghc.Sha = asString(ghc.Event["after"])
|
ghc.Sha = asString(ghc.Event["after"])
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
ghc.Ref = asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
defaultBranch := asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
||||||
|
if defaultBranch != "" {
|
||||||
|
ghc.Ref = fmt.Sprintf("refs/heads/%s", defaultBranch)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ghc.Ref == "" {
|
if ghc.Ref == "" {
|
||||||
|
@ -130,7 +133,7 @@ func (ghc *GithubContext) SetRefAndSha(ctx context.Context, defaultBranch string
|
||||||
}
|
}
|
||||||
|
|
||||||
if ghc.Ref == "" {
|
if ghc.Ref == "" {
|
||||||
ghc.Ref = asString(nestedMapLookup(ghc.Event, "repository", "default_branch"))
|
ghc.Ref = fmt.Sprintf("refs/heads/%s", asString(nestedMapLookup(ghc.Event, "repository", "default_branch")))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ func TestSetRefAndSha(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
ref: "master",
|
ref: "refs/heads/master",
|
||||||
sha: "pr-base-sha",
|
sha: "pr-base-sha",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -89,7 +89,7 @@ func TestSetRefAndSha(t *testing.T) {
|
||||||
"default_branch": "main",
|
"default_branch": "main",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
ref: "main",
|
ref: "refs/heads/main",
|
||||||
sha: "1234fakesha",
|
sha: "1234fakesha",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -127,7 +127,7 @@ func TestSetRefAndSha(t *testing.T) {
|
||||||
|
|
||||||
ghc.SetRefAndSha(context.Background(), "", "/some/dir")
|
ghc.SetRefAndSha(context.Background(), "", "/some/dir")
|
||||||
|
|
||||||
assert.Equal(t, "master", ghc.Ref)
|
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
||||||
assert.Equal(t, "1234fakesha", ghc.Sha)
|
assert.Equal(t, "1234fakesha", ghc.Sha)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -486,7 +486,7 @@ func (rc *RunContext) getGithubContext(ctx context.Context) *model.GithubContext
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ghc.EventName == "pull_request" {
|
if ghc.EventName == "pull_request" || ghc.EventName == "pull_request_target" {
|
||||||
ghc.BaseRef = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "ref"))
|
ghc.BaseRef = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "ref"))
|
||||||
ghc.HeadRef = asString(nestedMapLookup(ghc.Event, "pull_request", "head", "ref"))
|
ghc.HeadRef = asString(nestedMapLookup(ghc.Event, "pull_request", "head", "ref"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -396,6 +396,48 @@ func TestGetGitHubContext(t *testing.T) {
|
||||||
assert.Equal(t, ghc.Token, rc.Config.Secrets["GITHUB_TOKEN"])
|
assert.Equal(t, ghc.Token, rc.Config.Secrets["GITHUB_TOKEN"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetGithubContextRef(t *testing.T) {
|
||||||
|
table := []struct {
|
||||||
|
event string
|
||||||
|
json string
|
||||||
|
ref string
|
||||||
|
}{
|
||||||
|
{event: "push", json: `{"ref":"0000000000000000000000000000000000000000"}`, ref: "0000000000000000000000000000000000000000"},
|
||||||
|
{event: "create", json: `{"ref":"0000000000000000000000000000000000000000"}`, ref: "0000000000000000000000000000000000000000"},
|
||||||
|
{event: "workflow_dispatch", json: `{"ref":"0000000000000000000000000000000000000000"}`, ref: "0000000000000000000000000000000000000000"},
|
||||||
|
{event: "delete", json: `{"repository":{"default_branch": "main"}}`, ref: "refs/heads/main"},
|
||||||
|
{event: "pull_request", json: `{"number":123}`, ref: "refs/pull/123/merge"},
|
||||||
|
{event: "pull_request_review", json: `{"number":123}`, ref: "refs/pull/123/merge"},
|
||||||
|
{event: "pull_request_review_comment", json: `{"number":123}`, ref: "refs/pull/123/merge"},
|
||||||
|
{event: "pull_request_target", json: `{"pull_request":{"base":{"ref": "main"}}}`, ref: "refs/heads/main"},
|
||||||
|
{event: "deployment", json: `{"deployment": {"ref": "tag-name"}}`, ref: "tag-name"},
|
||||||
|
{event: "deployment_status", json: `{"deployment": {"ref": "tag-name"}}`, ref: "tag-name"},
|
||||||
|
{event: "release", json: `{"release": {"tag_name": "tag-name"}}`, ref: "tag-name"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, data := range table {
|
||||||
|
data := data
|
||||||
|
t.Run(data.event, func(t *testing.T) {
|
||||||
|
rc := &RunContext{
|
||||||
|
EventJSON: data.json,
|
||||||
|
Config: &Config{
|
||||||
|
EventName: data.event,
|
||||||
|
Workdir: "",
|
||||||
|
},
|
||||||
|
Run: &model.Run{
|
||||||
|
Workflow: &model.Workflow{
|
||||||
|
Name: "GitHubContextTest",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ghc := rc.getGithubContext(context.Background())
|
||||||
|
|
||||||
|
assert.Equal(t, data.ref, ghc.Ref)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func createIfTestRunContext(jobs map[string]*model.Job) *RunContext {
|
func createIfTestRunContext(jobs map[string]*model.Job) *RunContext {
|
||||||
rc := &RunContext{
|
rc := &RunContext{
|
||||||
Config: &Config{
|
Config: &Config{
|
||||||
|
|
Loading…
Reference in a new issue