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:
Markus Wolf
2022-09-27 00:49:19 +02:00
committed by GitHub
parent 7d0407fa4a
commit 97c083e902
4 changed files with 52 additions and 7 deletions

View File

@@ -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.HeadRef = asString(nestedMapLookup(ghc.Event, "pull_request", "head", "ref"))
}

View File

@@ -396,6 +396,48 @@ func TestGetGitHubContext(t *testing.T) {
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 {
rc := &RunContext{
Config: &Config{