From 7721061057acb017e894cfcc5ac1a8e261d06408 Mon Sep 17 00:00:00 2001 From: Daniel Liszka Date: Thu, 20 Jul 2023 11:24:37 +0200 Subject: [PATCH 1/2] Robot Account automatically created during the workflow creation Signed-off-by: Daniel Liszka --- app/cli/cmd/workflow_create.go | 14 ++++++++++++++ app/cli/cmd/workflow_list.go | 5 +++++ app/cli/internal/action/workflow_list.go | 18 ++++++++++-------- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/cli/cmd/workflow_create.go b/app/cli/cmd/workflow_create.go index a73aaed49..c152ec659 100644 --- a/app/cli/cmd/workflow_create.go +++ b/app/cli/cmd/workflow_create.go @@ -25,6 +25,7 @@ import ( func newWorkflowCreateCmd() *cobra.Command { var workflowName, project, team, contract string + var skipRACreate bool cmd := &cobra.Command{ Use: "create", @@ -64,6 +65,15 @@ func newWorkflowCreateCmd() *cobra.Command { return err } + // If the robot account creation is not skipped via --skip-robot-account-create , create a new one + if !skipRACreate { + resRA, errRA := action.NewWorkflowRobotAccountCreate(actionOpts).Run(res.ID, "default") + if errRA != nil { + return errRA + } + res.RobotAccountID = resRA.ID + res.RobotAccountKey = resRA.Key + } return encodeOutput([]*action.WorkflowItem{res}, WorkflowListTableOutput) }, } @@ -79,6 +89,10 @@ func newWorkflowCreateCmd() *cobra.Command { cmd.Flags().StringVar(&team, "team", "", "team name") cmd.Flags().StringVar(&contract, "contract", "", "the ID of an existing contract or the path/URL to a contract file. If not provided an empty one will be created.") + cmd.Flags().BoolVarP(&skipRACreate, "skip-robot-account-create", "s", false, "Skip creating a Robot Account for this workflow.") + + cmd.Flags().SortFlags = false + return cmd } diff --git a/app/cli/cmd/workflow_list.go b/app/cli/cmd/workflow_list.go index 10e1601e7..5e05d362a 100644 --- a/app/cli/cmd/workflow_list.go +++ b/app/cli/cmd/workflow_list.go @@ -89,5 +89,10 @@ func WorkflowListTableOutput(workflows []*action.WorkflowItem) error { } t.Render() + // Print the Robot Account Token if there is only one workflow and it has a Robot Account Token + if len(workflows) == 1 && workflows[0].RobotAccountKey != "" { + fmt.Printf("\nThis is automatically generated Robot Account Token (ID: %s). Save the following token since it will not printed again: \n\n %s\n\n", workflows[0].RobotAccountID, workflows[0].RobotAccountKey) + } + return nil } diff --git a/app/cli/internal/action/workflow_list.go b/app/cli/internal/action/workflow_list.go index d7b309c34..f1cdd2616 100644 --- a/app/cli/internal/action/workflow_list.go +++ b/app/cli/internal/action/workflow_list.go @@ -28,14 +28,16 @@ type WorkflowList struct { } type WorkflowItem struct { - Name string `json:"name"` - ID string `json:"id"` - Team string `json:"team"` - Project string `json:"project,omitempty"` - CreatedAt *time.Time `json:"createdAt"` - RunsCount int32 `json:"runsCount"` - ContractID string `json:"contractID,omitempty"` - LastRun *WorkflowRunItem `json:"lastRun,omitempty"` + Name string `json:"name"` + ID string `json:"id"` + Team string `json:"team"` + Project string `json:"project,omitempty"` + CreatedAt *time.Time `json:"createdAt"` + RunsCount int32 `json:"runsCount"` + ContractID string `json:"contractID,omitempty"` + LastRun *WorkflowRunItem `json:"lastRun,omitempty"` + RobotAccountID string `json:"robotAccountID,omitempty"` + RobotAccountKey string `json:"robotAccountKey,omitempty"` } func NewWorkflowList(cfg *ActionsOpts) *WorkflowList { From 450c8ec2eb9cfdead878e42ed8747a8f04fe539d Mon Sep 17 00:00:00 2001 From: Daniel Liszka Date: Thu, 20 Jul 2023 15:24:27 +0200 Subject: [PATCH 2/2] create and use a new WorkflowItemWithRobotAccount struct Signed-off-by: Daniel Liszka --- app/cli/cmd/workflow_create.go | 30 ++++++++++++++++++++---- app/cli/cmd/workflow_list.go | 5 ---- app/cli/internal/action/workflow_list.go | 24 +++++++++++-------- 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/app/cli/cmd/workflow_create.go b/app/cli/cmd/workflow_create.go index c152ec659..d05491154 100644 --- a/app/cli/cmd/workflow_create.go +++ b/app/cli/cmd/workflow_create.go @@ -60,21 +60,41 @@ func newWorkflowCreateCmd() *cobra.Command { Name: workflowName, Team: team, Project: project, ContractID: contract, } - res, err := action.NewWorkflowCreate(actionOpts).Run(opts) + workflow, err := action.NewWorkflowCreate(actionOpts).Run(opts) if err != nil { return err } + workflowRA := &action.WorkflowItemWithRobotAccount{ + WorkflowItem: workflow, + } + // If the robot account creation is not skipped via --skip-robot-account-create , create a new one if !skipRACreate { - resRA, errRA := action.NewWorkflowRobotAccountCreate(actionOpts).Run(res.ID, "default") + ra, errRA := action.NewWorkflowRobotAccountCreate(actionOpts).Run(workflow.ID, "autogenerated") if errRA != nil { return errRA } - res.RobotAccountID = resRA.ID - res.RobotAccountKey = resRA.Key + workflowRA.RobotAccountKey = ra.Key + workflowRA.RobotAccountID = ra.ID + } + + // Print the workflow JSON + if flagOutputFormat == formatJSON { + return encodeJSON(workflowRA) + } + + // Print the workflow table + if err := encodeOutput([]*action.WorkflowItem{workflowRA.WorkflowItem}, WorkflowListTableOutput); err != nil { + return err } - return encodeOutput([]*action.WorkflowItem{res}, WorkflowListTableOutput) + + // Print the Robot Account Token + if workflowRA.RobotAccountID != "" { + fmt.Printf("\nThis is automatically generated Robot Account Token (ID: %s). Save the following token since it will not printed again: \n\n %s\n\n", workflowRA.RobotAccountID, workflowRA.RobotAccountKey) + } + + return nil }, } diff --git a/app/cli/cmd/workflow_list.go b/app/cli/cmd/workflow_list.go index 5e05d362a..10e1601e7 100644 --- a/app/cli/cmd/workflow_list.go +++ b/app/cli/cmd/workflow_list.go @@ -89,10 +89,5 @@ func WorkflowListTableOutput(workflows []*action.WorkflowItem) error { } t.Render() - // Print the Robot Account Token if there is only one workflow and it has a Robot Account Token - if len(workflows) == 1 && workflows[0].RobotAccountKey != "" { - fmt.Printf("\nThis is automatically generated Robot Account Token (ID: %s). Save the following token since it will not printed again: \n\n %s\n\n", workflows[0].RobotAccountID, workflows[0].RobotAccountKey) - } - return nil } diff --git a/app/cli/internal/action/workflow_list.go b/app/cli/internal/action/workflow_list.go index f1cdd2616..d235cae4c 100644 --- a/app/cli/internal/action/workflow_list.go +++ b/app/cli/internal/action/workflow_list.go @@ -28,16 +28,20 @@ type WorkflowList struct { } type WorkflowItem struct { - Name string `json:"name"` - ID string `json:"id"` - Team string `json:"team"` - Project string `json:"project,omitempty"` - CreatedAt *time.Time `json:"createdAt"` - RunsCount int32 `json:"runsCount"` - ContractID string `json:"contractID,omitempty"` - LastRun *WorkflowRunItem `json:"lastRun,omitempty"` - RobotAccountID string `json:"robotAccountID,omitempty"` - RobotAccountKey string `json:"robotAccountKey,omitempty"` + Name string `json:"name"` + ID string `json:"id"` + Team string `json:"team"` + Project string `json:"project,omitempty"` + CreatedAt *time.Time `json:"createdAt"` + RunsCount int32 `json:"runsCount"` + ContractID string `json:"contractID,omitempty"` + LastRun *WorkflowRunItem `json:"lastRun,omitempty"` +} + +type WorkflowItemWithRobotAccount struct { + *WorkflowItem + RobotAccountID string `json:"robotAccountID,omitempty"` + RobotAccountKey string `json:"robotAccountKey,omitempty"` } func NewWorkflowList(cfg *ActionsOpts) *WorkflowList {