Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXCDT-320: Fix management of action triggers #597

Merged
merged 18 commits into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
253f880
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Dec 5, 2022
4cea50f
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Dec 13, 2022
69917ab
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Dec 15, 2022
2737584
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Dec 19, 2022
d58729f
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Dec 20, 2022
6986baa
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Dec 22, 2022
8b97922
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Dec 22, 2022
494cbf3
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Jan 3, 2023
7222ab4
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Jan 5, 2023
d257b34
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Jan 9, 2023
e5bffc9
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Jan 10, 2023
1940202
Merge branch 'v1' of https://github.com/auth0/auth0-cli into v1
willvedd Jan 10, 2023
be79eb1
Disable updating action trigger
willvedd Jan 11, 2023
486e280
Correcting error message and consolidating logic
willvedd Jan 11, 2023
f139698
Updating docs
willvedd Jan 11, 2023
85f2736
Merge branch 'v1' of https://github.com/auth0/auth0-cli into DXCDT-32…
willvedd Jan 11, 2023
c3e12bd
Fix typos in docs
sergiught Jan 11, 2023
85065e1
Merge branch 'v1' into DXCDT-320-fix-actions-create-list-all-triggers
sergiught Jan 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions docs/auth0_actions_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Update an action.

To update interactively, use `auth0 actions update` with no arguments.

To update non-interactively, supply the action id, name, trigger, secrets and dependencies through the flags.
To update non-interactively, supply the action id, name, code, secrets and dependencies through the flags.

## Usage
```
Expand All @@ -19,11 +19,10 @@ auth0 actions update [flags]
```
auth0 actions update <action-id>
auth0 actions update <action-id> --name myaction
auth0 actions update <action-id> --name myaction --trigger post-login
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --dependency "uuid=9.0.0" --secret "API_KEY=value" --secret "SECRET=value"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --dependency "uuid=9.0.0" --secret "API_KEY=value" --secret "SECRET=value"
auth0 actions update <action-id> -n myaction -t post-login -c "$(cat path/to/code.js)" -d "lodash=4.0.0" -d "uuid=9.0.0" -s "API_KEY=value" -s "SECRET=value" --json
```

Expand All @@ -36,7 +35,6 @@ auth0 actions update [flags]
--json Output in json format.
-n, --name string Name of the action.
-s, --secret stringToString Secrets to be used in the action. (default [])
-t, --trigger string Trigger of the action. At this time, an action can only target a single trigger at a time.
```


Expand Down
88 changes: 29 additions & 59 deletions internal/cli/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func showActionCmd(cli *cli) *cobra.Command {
action, err = cli.api.Action.Read(inputs.ID)
return err
}); err != nil {
return fmt.Errorf("Unable to get an action with Id '%s': %w", inputs.ID, err)
return fmt.Errorf("Unable to get an action with ID '%s': %w", inputs.ID, err)
}

cli.renderer.ActionShow(action)
Expand Down Expand Up @@ -190,12 +190,17 @@ func createActionCmd(cli *cli) *cobra.Command {
return err
}

triggers, version, err := latestActionTriggers(cli)
triggers, err := getCurrentTriggers(cli)
if err != nil {
return err
}

if err := actionTrigger.Select(cmd, &inputs.Trigger, triggers, nil); err != nil {
triggerIds := make([]string, 0)
for _, t := range triggers {
triggerIds = append(triggerIds, t.GetID())
}

if err := actionTrigger.Select(cmd, &inputs.Trigger, triggerIds, nil); err != nil {
return err
}

Expand All @@ -212,6 +217,14 @@ func createActionCmd(cli *cli) *cobra.Command {
return err
}

var version string
for _, t := range triggers {
if t.GetID() == inputs.Trigger {
version = t.GetVersion()
break
}
}

action := &management.Action{
Name: &inputs.Name,
SupportedTriggers: []management.ActionTrigger{
Expand Down Expand Up @@ -250,7 +263,6 @@ func updateActionCmd(cli *cli) *cobra.Command {
var inputs struct {
ID string
Name string
Trigger string
Code string
Dependencies map[string]string
Secrets map[string]string
Expand All @@ -262,15 +274,14 @@ func updateActionCmd(cli *cli) *cobra.Command {
Short: "Update an action",
Long: "Update an action.\n\n" +
"To update interactively, use `auth0 actions update` with no arguments.\n\n" +
"To update non-interactively, supply the action id, name, trigger, secrets and " +
"To update non-interactively, supply the action id, name, code, secrets and " +
"dependencies through the flags.",
Example: ` auth0 actions update <action-id>
auth0 actions update <action-id> --name myaction
auth0 actions update <action-id> --name myaction --trigger post-login
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value"
auth0 actions update <action-id> --name myaction --trigger post-login --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --dependency "uuid=9.0.0" --secret "API_KEY=value" --secret "SECRET=value"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --secret "SECRET=value"
auth0 actions update <action-id> --name myaction --code "$(cat path/to/code.js)" --dependency "lodash=4.0.0" --dependency "uuid=9.0.0" --secret "API_KEY=value" --secret "SECRET=value"
auth0 actions update <action-id> -n myaction -t post-login -c "$(cat path/to/code.js)" -d "lodash=4.0.0" -d "uuid=9.0.0" -s "API_KEY=value" -s "SECRET=value" --json`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
Expand All @@ -296,20 +307,6 @@ func updateActionCmd(cli *cli) *cobra.Command {
return err
}

triggers, version, err := latestActionTriggers(cli)
if err != nil {
return err
}

var currentTriggerId = ""
if len(current.SupportedTriggers) > 0 {
currentTriggerId = current.SupportedTriggers[0].GetID()
}

if err := actionTrigger.SelectU(cmd, &inputs.Trigger, triggers, &currentTriggerId); err != nil {
return err
}

// TODO(cyx): we can re-think this once we have
// `--stdin` based commands. For now we don't have
// those yet, so keeping this simple.
Expand All @@ -330,10 +327,6 @@ func updateActionCmd(cli *cli) *cobra.Command {
inputs.Name = current.GetName()
}

if inputs.Trigger == "" && currentTriggerId != "" {
inputs.Trigger = currentTriggerId
}

if inputs.Code == "" {
inputs.Code = current.GetCode()
}
Expand All @@ -342,14 +335,9 @@ func updateActionCmd(cli *cli) *cobra.Command {
// re-hydrated by the SDK, which we'll use below during
// display.
action := &management.Action{
Name: &inputs.Name,
SupportedTriggers: []management.ActionTrigger{
{
ID: &inputs.Trigger,
Version: &version,
},
},
Code: &inputs.Code,
Name: &inputs.Name,
SupportedTriggers: current.SupportedTriggers,
Code: &inputs.Code,
}

if len(inputs.Dependencies) == 0 {
Expand Down Expand Up @@ -377,7 +365,6 @@ func updateActionCmd(cli *cli) *cobra.Command {

cmd.Flags().BoolVar(&cli.json, "json", false, "Output in json format.")
actionName.RegisterStringU(cmd, &inputs.Name, "")
actionTrigger.RegisterStringU(cmd, &inputs.Trigger, "")
actionCode.RegisterStringU(cmd, &inputs.Code, "")
actionDependency.RegisterStringMapU(cmd, &inputs.Dependencies, nil)
actionSecret.RegisterStringMapU(cmd, &inputs.Secrets, nil)
Expand Down Expand Up @@ -547,27 +534,17 @@ func formatActionDetailsPath(id string) string {
return fmt.Sprintf("actions/library/details/%s", id)
}

func latestActionTriggerVersion(list []*management.ActionTrigger) string {
latestVersion := "v1"
for _, t := range list {
if t.GetVersion() > latestVersion {
latestVersion = t.GetVersion()
}
}
return latestVersion
}

func filterActionTriggersByVersion(list []*management.ActionTrigger, version string) []*management.ActionTrigger {
func filterDeprecatedActionTriggers(list []*management.ActionTrigger) []*management.ActionTrigger {
res := []*management.ActionTrigger{}
for _, t := range list {
if t.GetVersion() == version && t.GetStatus() == "CURRENT" {
if t.GetStatus() == "CURRENT" {
res = append(res, t)
}
}
return res
}

func latestActionTriggers(cli *cli) ([]string, string, error) {
func getCurrentTriggers(cli *cli) ([]*management.ActionTrigger, error) {
var triggers []*management.ActionTrigger
if err := ansi.Waiting(func() error {
list, err := cli.api.Action.Triggers()
Expand All @@ -577,17 +554,10 @@ func latestActionTriggers(cli *cli) ([]string, string, error) {
triggers = list.Triggers
return nil
}); err != nil {
return nil, "", err
return nil, err
}

latestTriggerVersion := latestActionTriggerVersion(triggers)
triggers = filterActionTriggersByVersion(triggers, latestTriggerVersion)
var triggerIds []string

for _, t := range triggers {
triggerIds = append(triggerIds, t.GetID())
}
return triggerIds, latestTriggerVersion, nil
return filterDeprecatedActionTriggers(triggers), nil
}

func actionTemplate(key string) string {
Expand Down