Skip to content

Commit

Permalink
fix: handle multiple nested includes (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
decleaver authored Dec 18, 2023
1 parent fc90e5f commit 2db7f6e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/pkg/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,33 @@ func Run(tasksFile types.TasksFile, taskName string, setVariables map[string]str
return err
}

// only process includes if the task requires them
runner.processIncludes(task, tasksFile)

if err = runner.checkForTaskLoops(task, tasksFile); err != nil {
return err
}

err = runner.executeTask(task)
return err
}

func (r *Runner) processIncludes(task types.Task, tasksFile types.TasksFile) error {
for _, a := range task.Actions {
if strings.Contains(a.TaskReference, ":") {
taskReferenceName := strings.Split(a.TaskReference, ":")[0]
for _, include := range tasksFile.Includes {
if include[taskReferenceName] != "" {
referencedIncludes := []map[string]string{include}
err = runner.importTasks(referencedIncludes, config.TaskFileLocation)
err := r.importTasks(referencedIncludes, config.TaskFileLocation)
if err != nil {
return err
}
break
}
if err != nil {
return err
}
}
}
}

if err = runner.checkForTaskLoops(task); err != nil {
return err
}

err = runner.executeTask(task)
return err
return nil
}

func (r *Runner) importTasks(includes []map[string]string, dir string) error {
Expand Down Expand Up @@ -302,7 +306,7 @@ func (r *Runner) performAction(action types.Action) error {
return nil
}

func (r *Runner) checkForTaskLoops(task types.Task) error {
func (r *Runner) checkForTaskLoops(task types.Task, tasksFile types.TasksFile) error {
// Filtering unique task actions allows for rerunning tasks in the same execution
uniqueTaskActions := getUniqueTaskActions(task.Actions)
for _, action := range uniqueTaskActions {
Expand All @@ -316,7 +320,9 @@ func (r *Runner) checkForTaskLoops(task types.Task) error {
if err != nil {
return err
}
if err = r.checkForTaskLoops(newTask); err != nil {
// check new task includes
r.processIncludes(newTask, tasksFile)
if err = r.checkForTaskLoops(newTask, tasksFile); err != nil {
return err
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/e2e/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,20 @@ func TestUseCLI(t *testing.T) {
require.Contains(t, stdErr, "echo bar")
require.Contains(t, stdErr, "defenseunicorns is a pretty ok company")
})

t.Run("test action with multiple nested include tasks", func(t *testing.T) {
t.Parallel()
gitRev, err := e2e.GetGitRevision()
if err != nil {
return
}
setVar := fmt.Sprintf("GIT_REVISION=%s", gitRev)

stdOut, stdErr, err := e2e.RunTasksWithFile("run", "extra-foobar", "--set", setVar)
require.NoError(t, err, stdOut, stdErr)
require.Contains(t, stdErr, "echo foo")
require.Contains(t, stdErr, "echo bar")
require.Contains(t, stdErr, "defenseunicorns")
require.Contains(t, stdErr, "defenseunicorns is a pretty ok company")
})
}
3 changes: 3 additions & 0 deletions src/test/tasks/tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ tasks:
actions:
- task: foo:foobar
- task: remote:echo-var
- name: extra-foobar
actions:
- task: more-foobar

0 comments on commit 2db7f6e

Please sign in to comment.