1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-09-15 18:57:01 +00:00

fix #134 - include base_ref and head_ref in github context from the event json

This commit is contained in:
Casey Lee 2020-03-06 14:17:57 -08:00
parent 8f6d32db71
commit cc19e1e341
5 changed files with 100 additions and 3 deletions

View file

@ -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"