fix #134 - include base_ref and head_ref in github context from the event json
This commit is contained in:
parent
87392c2ed7
commit
15fb58bf43
5 changed files with 100 additions and 3 deletions
|
@ -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"
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
10
pkg/runner/testdata/pull-request/event.json
vendored
Normal file
10
pkg/runner/testdata/pull-request/event.json
vendored
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"pull_request": {
|
||||
"head": {
|
||||
"ref": "sample-head-ref"
|
||||
},
|
||||
"base": {
|
||||
"ref": "sample-base-ref"
|
||||
}
|
||||
}
|
||||
}
|
10
pkg/runner/testdata/pull-request/main.yaml
vendored
Normal file
10
pkg/runner/testdata/pull-request/main.yaml
vendored
Normal file
|
@ -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
|
4
pkg/runner/testdata/secrets/.actrc
vendored
Normal file
4
pkg/runner/testdata/secrets/.actrc
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
-W .
|
||||
-s MY_SECRET
|
||||
-s MULTILINE_SECRET
|
||||
-s JSON_SECRET
|
Loading…
Reference in a new issue