From acaf888537b7da5296e75c5038584f770ff28b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20B=2E?= Date: Mon, 16 Sep 2024 10:23:36 +0200 Subject: [PATCH 1/5] feat: Add option to output issue creation result as Json --- internal/cmd/issue/create/create.go | 36 +++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/internal/cmd/issue/create/create.go b/internal/cmd/issue/create/create.go index 5e418826..31d6e63f 100644 --- a/internal/cmd/issue/create/create.go +++ b/internal/cmd/issue/create/create.go @@ -1,12 +1,10 @@ package create import ( + "encoding/json" "fmt" "github.com/AlecAivazis/survey/v2" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "github.com/ankitpokhrel/jira-cli/api" "github.com/ankitpokhrel/jira-cli/internal/cmdcommon" "github.com/ankitpokhrel/jira-cli/internal/cmdutil" @@ -14,6 +12,8 @@ import ( "github.com/ankitpokhrel/jira-cli/pkg/jira" "github.com/ankitpokhrel/jira-cli/pkg/surveyext" "github.com/ankitpokhrel/jira-cli/pkg/tui" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) const ( @@ -42,17 +42,23 @@ $ echo "Description from stdin" | jira issue create -s"Summary" -tTask # For issue description, the flag --body/-b takes precedence over the --template flag # The example below will add "Body from flag" as an issue description $ jira issue create -tTask -sSummary -b"Body from flag" --template /path/to/template.tpl` + + flagJson = "json" ) // NewCmdCreate is a create command. func NewCmdCreate() *cobra.Command { - return &cobra.Command{ + cmd := cobra.Command{ Use: "create", Short: "Create an issue in a project", Long: helpText, Example: examples, Run: create, } + + cmd.Flags().Bool(flagJson, false, "Print output in JSON format") + + return &cmd } // SetFlags sets flags supported by create command. @@ -94,7 +100,7 @@ func create(cmd *cobra.Command, _ []string) { params.Reporter = cmdcommon.GetRelevantUser(client, project, params.Reporter) params.Assignee = cmdcommon.GetRelevantUser(client, project, params.Assignee) - key, err := func() (string, error) { + issue, err := func() (*jira.CreateResponse, error) { s := cmdutil.Info("Creating an issue...") defer s.Stop() @@ -126,18 +132,24 @@ func create(cmd *cobra.Command, _ []string) { cr.SubtaskField = handle } - resp, err := client.CreateV2(&cr) - if err != nil { - return "", err - } - return resp.Key, nil + return client.CreateV2(&cr) }() cmdutil.ExitIfError(err) - cmdutil.Success("Issue created\n%s", cmdutil.GenerateServerBrowseURL(server, key)) + + jsonFlag, err := cmd.Flags().GetBool(flagJson) + cmdutil.ExitIfError(err) + if jsonFlag { + jsonData, err := json.Marshal(issue) + cmdutil.ExitIfError(err) + fmt.Println(string(jsonData)) + return + } + + cmdutil.Success("Issue created\n%s", cmdutil.GenerateServerBrowseURL(server, issue.Key)) if web, _ := cmd.Flags().GetBool("web"); web { - err := cmdutil.Navigate(server, key) + err := cmdutil.Navigate(server, issue.Key) cmdutil.ExitIfError(err) } } From 6ee2847b15901ccb7230445a591c3b1726917f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20B=2E?= Date: Mon, 16 Sep 2024 10:23:36 +0200 Subject: [PATCH 2/5] feat: Add option to output issue creation result as Json --- internal/cmd/issue/create/create.go | 36 +++++++++++++++++++---------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/internal/cmd/issue/create/create.go b/internal/cmd/issue/create/create.go index 5e418826..31d6e63f 100644 --- a/internal/cmd/issue/create/create.go +++ b/internal/cmd/issue/create/create.go @@ -1,12 +1,10 @@ package create import ( + "encoding/json" "fmt" "github.com/AlecAivazis/survey/v2" - "github.com/spf13/cobra" - "github.com/spf13/viper" - "github.com/ankitpokhrel/jira-cli/api" "github.com/ankitpokhrel/jira-cli/internal/cmdcommon" "github.com/ankitpokhrel/jira-cli/internal/cmdutil" @@ -14,6 +12,8 @@ import ( "github.com/ankitpokhrel/jira-cli/pkg/jira" "github.com/ankitpokhrel/jira-cli/pkg/surveyext" "github.com/ankitpokhrel/jira-cli/pkg/tui" + "github.com/spf13/cobra" + "github.com/spf13/viper" ) const ( @@ -42,17 +42,23 @@ $ echo "Description from stdin" | jira issue create -s"Summary" -tTask # For issue description, the flag --body/-b takes precedence over the --template flag # The example below will add "Body from flag" as an issue description $ jira issue create -tTask -sSummary -b"Body from flag" --template /path/to/template.tpl` + + flagJson = "json" ) // NewCmdCreate is a create command. func NewCmdCreate() *cobra.Command { - return &cobra.Command{ + cmd := cobra.Command{ Use: "create", Short: "Create an issue in a project", Long: helpText, Example: examples, Run: create, } + + cmd.Flags().Bool(flagJson, false, "Print output in JSON format") + + return &cmd } // SetFlags sets flags supported by create command. @@ -94,7 +100,7 @@ func create(cmd *cobra.Command, _ []string) { params.Reporter = cmdcommon.GetRelevantUser(client, project, params.Reporter) params.Assignee = cmdcommon.GetRelevantUser(client, project, params.Assignee) - key, err := func() (string, error) { + issue, err := func() (*jira.CreateResponse, error) { s := cmdutil.Info("Creating an issue...") defer s.Stop() @@ -126,18 +132,24 @@ func create(cmd *cobra.Command, _ []string) { cr.SubtaskField = handle } - resp, err := client.CreateV2(&cr) - if err != nil { - return "", err - } - return resp.Key, nil + return client.CreateV2(&cr) }() cmdutil.ExitIfError(err) - cmdutil.Success("Issue created\n%s", cmdutil.GenerateServerBrowseURL(server, key)) + + jsonFlag, err := cmd.Flags().GetBool(flagJson) + cmdutil.ExitIfError(err) + if jsonFlag { + jsonData, err := json.Marshal(issue) + cmdutil.ExitIfError(err) + fmt.Println(string(jsonData)) + return + } + + cmdutil.Success("Issue created\n%s", cmdutil.GenerateServerBrowseURL(server, issue.Key)) if web, _ := cmd.Flags().GetBool("web"); web { - err := cmdutil.Navigate(server, key) + err := cmdutil.Navigate(server, issue.Key) cmdutil.ExitIfError(err) } } From 1c06591c5faea85d49a392ab2fe649eb4500eade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20B=2E?= Date: Mon, 20 Jan 2025 12:34:05 +0100 Subject: [PATCH 3/5] feat: Add --raw flag for JSON output in issue creation command --- internal/cmd/issue/create/create.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/cmd/issue/create/create.go b/internal/cmd/issue/create/create.go index 31d6e63f..84bc8224 100644 --- a/internal/cmd/issue/create/create.go +++ b/internal/cmd/issue/create/create.go @@ -36,6 +36,9 @@ $ jira issue create --template /path/to/template.tmpl # Get description from standard input $ jira issue create --template - +# Create issue in the configured project with JSON output +$ jira issue create --raw + # Or, use pipe to read input directly from standard input $ echo "Description from stdin" | jira issue create -s"Summary" -tTask @@ -43,7 +46,7 @@ $ echo "Description from stdin" | jira issue create -s"Summary" -tTask # The example below will add "Body from flag" as an issue description $ jira issue create -tTask -sSummary -b"Body from flag" --template /path/to/template.tpl` - flagJson = "json" + flagRaw = "raw" ) // NewCmdCreate is a create command. @@ -56,7 +59,7 @@ func NewCmdCreate() *cobra.Command { Run: create, } - cmd.Flags().Bool(flagJson, false, "Print output in JSON format") + cmd.Flags().Bool(flagRaw, false, "Print output in JSON format") return &cmd } @@ -137,7 +140,7 @@ func create(cmd *cobra.Command, _ []string) { cmdutil.ExitIfError(err) - jsonFlag, err := cmd.Flags().GetBool(flagJson) + jsonFlag, err := cmd.Flags().GetBool(flagRaw) cmdutil.ExitIfError(err) if jsonFlag { jsonData, err := json.Marshal(issue) From 88f26d9c6fae9e96c3dd5ad20b9c1fcdbfa30f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien?= <5331611+sebbbastien@users.noreply.github.com> Date: Mon, 20 Jan 2025 11:47:56 +0000 Subject: [PATCH 4/5] Fix imports --- internal/cmd/issue/create/create.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/cmd/issue/create/create.go b/internal/cmd/issue/create/create.go index 84bc8224..eb85ff2d 100644 --- a/internal/cmd/issue/create/create.go +++ b/internal/cmd/issue/create/create.go @@ -5,6 +5,9 @@ import ( "fmt" "github.com/AlecAivazis/survey/v2" + "github.com/spf13/cobra" + "github.com/spf13/viper" + "github.com/ankitpokhrel/jira-cli/api" "github.com/ankitpokhrel/jira-cli/internal/cmdcommon" "github.com/ankitpokhrel/jira-cli/internal/cmdutil" @@ -12,8 +15,6 @@ import ( "github.com/ankitpokhrel/jira-cli/pkg/jira" "github.com/ankitpokhrel/jira-cli/pkg/surveyext" "github.com/ankitpokhrel/jira-cli/pkg/tui" - "github.com/spf13/cobra" - "github.com/spf13/viper" ) const ( From 7e1ccb79b046cf371f3a504aef07940de0ebbcdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien?= <5331611+sebbbastien@users.noreply.github.com> Date: Mon, 20 Jan 2025 11:48:52 +0000 Subject: [PATCH 5/5] whitespace --- internal/cmd/issue/create/create.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/issue/create/create.go b/internal/cmd/issue/create/create.go index eb85ff2d..78e48662 100644 --- a/internal/cmd/issue/create/create.go +++ b/internal/cmd/issue/create/create.go @@ -7,7 +7,7 @@ import ( "github.com/AlecAivazis/survey/v2" "github.com/spf13/cobra" "github.com/spf13/viper" - + "github.com/ankitpokhrel/jira-cli/api" "github.com/ankitpokhrel/jira-cli/internal/cmdcommon" "github.com/ankitpokhrel/jira-cli/internal/cmdutil"