diff --git a/pkg/runner/run_context.go b/pkg/runner/run_context.go index 1b5b8b9..260af16 100644 --- a/pkg/runner/run_context.go +++ b/pkg/runner/run_context.go @@ -194,7 +194,7 @@ func (rc *RunContext) newStepExecutor(step *model.Step) common.Executor { Outputs: make(map[string]string), } - sc.setupEnv()(ctx) + _ = sc.setupEnv()(ctx) rc.ExprEval = sc.NewExpressionEvaluator() if !rc.EvalBool(sc.Step.If) { @@ -327,6 +327,11 @@ type githubContext struct { } func (rc *RunContext) getGithubContext() *githubContext { + token, ok := rc.Config.Secrets["GITHUB_TOKEN"] + if !ok { + token = os.Getenv("GITHUB_TOKEN") + } + ghc := &githubContext{ Event: make(map[string]interface{}), EventPath: "/github/workflow/event.json", @@ -334,9 +339,8 @@ func (rc *RunContext) getGithubContext() *githubContext { RunID: "1", RunNumber: "1", Actor: "nektos/act", - EventName: rc.Config.EventName, - Token: os.Getenv("GITHUB_TOKEN"), + Token: token, Workspace: "/github/workspace", Action: rc.CurrentStep, } @@ -367,9 +371,41 @@ func (rc *RunContext) getGithubContext() *githubContext { if err != nil { logrus.Error(err) } + + if ghc.EventName == "pull_request" { + ghc.BaseRef = asString(nestedMapLookup(ghc.Event, "pull_request", "base", "ref")) + ghc.HeadRef = asString(nestedMapLookup(ghc.Event, "pull_request", "head", "ref")) + } + return ghc } +func asString(v interface{}) string { + if v == nil { + return "" + } else if s, ok := v.(string); ok { + return s + } + return "" +} + +func nestedMapLookup(m map[string]interface{}, ks ...string) (rval interface{}) { + var ok bool + + if len(ks) == 0 { // degenerate input + return nil + } + if rval, ok = m[ks[0]]; !ok { + return nil + } else if len(ks) == 1 { // we've reached the final key + return rval + } else if m, ok = rval.(map[string]interface{}); !ok { + return nil + } else { // 1+ more keys + return nestedMapLookup(m, ks[1:]...) + } +} + func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string { github := rc.getGithubContext() env["HOME"] = "/github/home" diff --git a/pkg/runner/runner_test.go b/pkg/runner/runner_test.go index 2d38832..9e6ee91 100644 --- a/pkg/runner/runner_test.go +++ b/pkg/runner/runner_test.go @@ -140,3 +140,40 @@ func TestRunEventSecrets(t *testing.T) { err = runner.NewPlanExecutor(plan)(ctx) assert.NilError(t, err, workflowPath) } + +func TestRunEventPullRequest(t *testing.T) { + if testing.Short() { + t.Skip("skipping integration test") + } + + log.SetLevel(log.DebugLevel) + ctx := context.Background() + + platforms := map[string]string{ + "ubuntu-latest": "node:12.6-buster-slim", + } + + workflowPath := "pull-request" + eventName := "pull_request" + + workdir, err := filepath.Abs("testdata") + assert.NilError(t, err, workflowPath) + + runnerConfig := &Config{ + Workdir: workdir, + EventName: eventName, + EventPath: filepath.Join(workdir, workflowPath, "event.json"), + Platforms: platforms, + ReuseContainers: false, + } + runner, err := New(runnerConfig) + assert.NilError(t, err, workflowPath) + + planner, err := model.NewWorkflowPlanner(fmt.Sprintf("testdata/%s", workflowPath)) + assert.NilError(t, err, workflowPath) + + plan := planner.PlanEvent(eventName) + + err = runner.NewPlanExecutor(plan)(ctx) + assert.NilError(t, err, workflowPath) +} diff --git a/pkg/runner/testdata/pull-request/event.json b/pkg/runner/testdata/pull-request/event.json new file mode 100644 index 0000000..87320dc --- /dev/null +++ b/pkg/runner/testdata/pull-request/event.json @@ -0,0 +1,10 @@ +{ + "pull_request": { + "head": { + "ref": "sample-head-ref" + }, + "base": { + "ref": "sample-base-ref" + } + } +} diff --git a/pkg/runner/testdata/pull-request/main.yaml b/pkg/runner/testdata/pull-request/main.yaml new file mode 100644 index 0000000..b9946b9 --- /dev/null +++ b/pkg/runner/testdata/pull-request/main.yaml @@ -0,0 +1,10 @@ +name: basic +on: pull_request + +jobs: + build: + runs-on: ubuntu-latest + steps: + - run: echo '${{github.ref}}' + - run: echo '${{github.head_ref}}' | grep sample-head-ref + - run: echo '${{github.base_ref}}' | grep sample-base-ref diff --git a/pkg/runner/testdata/secrets/.actrc b/pkg/runner/testdata/secrets/.actrc new file mode 100644 index 0000000..7679ca6 --- /dev/null +++ b/pkg/runner/testdata/secrets/.actrc @@ -0,0 +1,4 @@ +-W . +-s MY_SECRET +-s MULTILINE_SECRET +-s JSON_SECRET