Skip to content

Commit

Permalink
feat: Add option to output issue creation result as Json
Browse files Browse the repository at this point in the history
  • Loading branch information
sebbbastien committed Sep 16, 2024
1 parent 2c9e6db commit acaf888
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions internal/cmd/issue/create/create.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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"
"github.com/ankitpokhrel/jira-cli/internal/query"
"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 (
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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)
}
}
Expand Down

0 comments on commit acaf888

Please sign in to comment.