Set the default branch inside the event data (#354)
* set ${{github.event.repository.default_branch}} * change `ok == false` to `!ok` Co-authored-by: Casey Lee <cplee@nektos.com>
This commit is contained in:
parent
d521fa5bba
commit
7d443c6520
4 changed files with 40 additions and 0 deletions
|
@ -20,6 +20,7 @@ type Input struct {
|
||||||
noOutput bool
|
noOutput bool
|
||||||
envfile string
|
envfile string
|
||||||
secretfile string
|
secretfile string
|
||||||
|
defaultBranch string
|
||||||
privileged bool
|
privileged bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,6 +41,7 @@ func Execute(ctx context.Context, version string) {
|
||||||
rootCmd.Flags().BoolVarP(&input.bindWorkdir, "bind", "b", false, "bind working directory to container, rather than copy")
|
rootCmd.Flags().BoolVarP(&input.bindWorkdir, "bind", "b", false, "bind working directory to container, rather than copy")
|
||||||
rootCmd.Flags().BoolVarP(&input.forcePull, "pull", "p", false, "pull docker image(s) if already present")
|
rootCmd.Flags().BoolVarP(&input.forcePull, "pull", "p", false, "pull docker image(s) if already present")
|
||||||
rootCmd.Flags().StringVarP(&input.eventPath, "eventpath", "e", "", "path to event JSON file")
|
rootCmd.Flags().StringVarP(&input.eventPath, "eventpath", "e", "", "path to event JSON file")
|
||||||
|
rootCmd.Flags().StringVar(&input.defaultBranch, "defaultbranch", "", "the name of the main branch")
|
||||||
rootCmd.Flags().BoolVar(&input.privileged, "privileged", false, "use privileged mode")
|
rootCmd.Flags().BoolVar(&input.privileged, "privileged", false, "use privileged mode")
|
||||||
rootCmd.PersistentFlags().StringVarP(&input.actor, "actor", "a", "nektos/act", "user that triggered the event")
|
rootCmd.PersistentFlags().StringVarP(&input.actor, "actor", "a", "nektos/act", "user that triggered the event")
|
||||||
rootCmd.PersistentFlags().StringVarP(&input.workflowsPath, "workflows", "W", "./.github/workflows/", "path to workflow file(s)")
|
rootCmd.PersistentFlags().StringVarP(&input.workflowsPath, "workflows", "W", "./.github/workflows/", "path to workflow file(s)")
|
||||||
|
@ -156,11 +157,18 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
|
||||||
return drawGraph(plan)
|
return drawGraph(plan)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check to see if the main branch was defined
|
||||||
|
defaultbranch, err := cmd.Flags().GetString("defaultbranch")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// run the plan
|
// run the plan
|
||||||
config := &runner.Config{
|
config := &runner.Config{
|
||||||
Actor: input.actor,
|
Actor: input.actor,
|
||||||
EventName: eventName,
|
EventName: eventName,
|
||||||
EventPath: input.EventPath(),
|
EventPath: input.EventPath(),
|
||||||
|
DefaultBranch: defaultbranch,
|
||||||
ForcePull: input.forcePull,
|
ForcePull: input.forcePull,
|
||||||
ReuseContainers: input.reuseContainers,
|
ReuseContainers: input.reuseContainers,
|
||||||
Workdir: input.Workdir(),
|
Workdir: input.Workdir(),
|
||||||
|
|
|
@ -400,6 +400,13 @@ func (rc *RunContext) getGithubContext() *githubContext {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set the branch in the event data
|
||||||
|
if rc.Config.DefaultBranch != "" {
|
||||||
|
ghc.Event = withDefaultBranch(rc.Config.DefaultBranch, ghc.Event)
|
||||||
|
} else {
|
||||||
|
ghc.Event = withDefaultBranch("master", ghc.Event)
|
||||||
|
}
|
||||||
|
|
||||||
if ghc.EventName == "pull_request" {
|
if ghc.EventName == "pull_request" {
|
||||||
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"))
|
||||||
|
@ -452,6 +459,29 @@ func nestedMapLookup(m map[string]interface{}, ks ...string) (rval interface{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withDefaultBranch(b string, event map[string]interface{}) map[string]interface{} {
|
||||||
|
repoI, ok := event["repository"]
|
||||||
|
if !ok {
|
||||||
|
repoI = make(map[string]interface{})
|
||||||
|
}
|
||||||
|
|
||||||
|
repo, ok := repoI.(map[string]interface{})
|
||||||
|
if !ok {
|
||||||
|
log.Warnf("unable to set default branch to %v", b)
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
// if the branch is already there return with no changes
|
||||||
|
if _, ok = repo["default_branch"]; ok {
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
|
repo["default_branch"] = b
|
||||||
|
event["repository"] = repo
|
||||||
|
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
|
func (rc *RunContext) withGithubEnv(env map[string]string) map[string]string {
|
||||||
github := rc.getGithubContext()
|
github := rc.getGithubContext()
|
||||||
env["HOME"] = "/github/home"
|
env["HOME"] = "/github/home"
|
||||||
|
|
|
@ -22,6 +22,7 @@ type Config struct {
|
||||||
BindWorkdir bool // bind the workdir to the job container
|
BindWorkdir bool // bind the workdir to the job container
|
||||||
EventName string // name of event to run
|
EventName string // name of event to run
|
||||||
EventPath string // path to JSON file to use for event.json in containers
|
EventPath string // path to JSON file to use for event.json in containers
|
||||||
|
DefaultBranch string // name of the main branch for this repository
|
||||||
ReuseContainers bool // reuse containers to maintain state
|
ReuseContainers bool // reuse containers to maintain state
|
||||||
ForcePull bool // force pulling of the image, if already present
|
ForcePull bool // force pulling of the image, if already present
|
||||||
LogOutput bool // log the output from docker run
|
LogOutput bool // log the output from docker run
|
||||||
|
|
Loading…
Reference in a new issue