Skip to content

Commit

Permalink
feat(subtask): allow to pass inputs as jsson
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Bétrancourt <[email protected]>
  • Loading branch information
rclsilver committed Oct 28, 2022
1 parent d31b5cb commit 121f78a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
42 changes: 22 additions & 20 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,30 +1168,32 @@ func TestResolveSubTask(t *testing.T) {
require.NotNil(t, res)
assert.Equal(t, resolution.StateWaiting, res.State)

subtaskCreationOutput := res.Steps["subtaskCreation"].Output.(map[string]interface{})
subtaskPublicID := subtaskCreationOutput["id"].(string)
for _, subtaskName := range []string{"subtaskCreation", "jsonInputSubtask", "templatingJsonInputSubtask"} {
subtaskCreationOutput := res.Steps[subtaskName].Output.(map[string]interface{})
subtaskPublicID := subtaskCreationOutput["id"].(string)

subtask, err := task.LoadFromPublicID(dbp, subtaskPublicID)
require.Nil(t, err)
assert.Equal(t, task.StateTODO, subtask.State)
subtask, err := task.LoadFromPublicID(dbp, subtaskPublicID)
require.Nil(t, err)
assert.Equal(t, task.StateTODO, subtask.State)

subtaskResolution, err := resolution.Create(dbp, subtask, nil, "", false, nil)
require.Nil(t, err)
subtaskResolution, err := resolution.Create(dbp, subtask, nil, "", false, nil)
require.Nil(t, err)

subtaskResolution, err = runResolution(subtaskResolution)
require.Nil(t, err)
assert.Equal(t, task.StateDone, subtaskResolution.State)
for k, v := range subtaskResolution.Steps {
assert.Equal(t, step.StateDone, v.State, "not valid state for step %s", k)
}
subtaskResolution, err = runResolution(subtaskResolution)
require.Nil(t, err)
assert.Equal(t, task.StateDone, subtaskResolution.State)
for k, v := range subtaskResolution.Steps {
assert.Equal(t, step.StateDone, v.State, "not valid state for step %s", k)
}

subtask, err = task.LoadFromPublicID(dbp, subtaskPublicID)
require.Nil(t, err)
assert.Equal(t, task.StateDone, subtask.State)
parentTaskToResume, err := taskutils.ShouldResumeParentTask(dbp, subtask)
require.Nil(t, err)
require.NotNil(t, parentTaskToResume)
assert.Equal(t, res.TaskID, parentTaskToResume.ID)
subtask, err = task.LoadFromPublicID(dbp, subtaskPublicID)
require.Nil(t, err)
assert.Equal(t, task.StateDone, subtask.State)
parentTaskToResume, err := taskutils.ShouldResumeParentTask(dbp, subtask)
require.Nil(t, err)
require.NotNil(t, parentTaskToResume)
assert.Equal(t, res.TaskID, parentTaskToResume.ID)
}

// checking if the parent task is picked up after that the subtask is resolved.
// need to sleep a bit because the parent task is resumed asynchronously
Expand Down
23 changes: 23 additions & 0 deletions engine/templates_tests/subtask.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
name: subtaskTemplate
description: Template that spawns a subtask
title_format: "[test] subtask template test"
variables:
- name: quaranteDeux
value: '42'
steps:
subtaskCreation:
description: creating a subtask
Expand All @@ -15,5 +18,25 @@ steps:
configuration:
output:
foo: OK
jsonInputSubtask:
description: creating a subtask using json_input
action:
type: subtask
configuration:
template: input
json_input: |-
{
"quantity": 1337
}
templatingJsonInputSubtask:
description: creating a subtask using json_input
action:
type: subtask
configuration:
template: input
json_input: |-
{
"quantity": {{eval `quaranteDeux`}}
}
result_format:
foo: "{{.step.echoOK.output.foo}}"
3 changes: 3 additions & 0 deletions hack/template-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@
"input": {
"type": "object"
},
"json_input": {
"type": "string"
},
"tags": {
"$ref": "#/definitions/Tags"
},
Expand Down
1 change: 1 addition & 0 deletions pkg/plugins/builtin/subtask/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fully `DONE`.
|----------------------|-------------------------------------------------------------------------------------------------------------------|
| `template` | the name of a task template, as accepted through µTask's API |
| `input` | a map of named values, as accepted on µTask's API |
| `json_input` | a JSON string passed as input to the subtask template |
| `resolver_usernames` | a string containing a JSON array of additional resolver users for the subtask |
| `resolver_groups` | a string containing a JSON array of additional resolver groups for the subtask |
| `watcher_usernames` | a string containing a JSON array of additional watcher users for the subtask |
Expand Down
7 changes: 7 additions & 0 deletions pkg/plugins/builtin/subtask/subtask.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pluginsubtask

import (
"context"
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -33,6 +34,7 @@ var (
type SubtaskConfig struct {
Template string `json:"template"`
Input map[string]interface{} `json:"input"`
JsonInput string `json:"json_input,omitempty"`
ResolverUsernames string `json:"resolver_usernames"`
ResolverGroups string `json:"resolver_groups"`
WatcherUsernames string `json:"watcher_usernames"`
Expand Down Expand Up @@ -145,6 +147,11 @@ func exec(stepName string, config interface{}, ctx interface{}) (interface{}, in
return nil, nil, fmt.Errorf("can't convert JSON to row slice: %s", err)
}
}
if cfg.JsonInput != "" {
if err := json.Unmarshal([]byte(cfg.JsonInput), &cfg.Input); err != nil {
return nil, nil, fmt.Errorf("can't parse `json_input`: %s", err)
}
}

// TODO inherit watchers from parent task
ctx := auth.WithIdentity(context.Background(), stepContext.RequesterUsername)
Expand Down

0 comments on commit 121f78a

Please sign in to comment.