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

feat: Add option to output issue creation result as Json #775

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
34 changes: 25 additions & 9 deletions internal/cmd/issue/create/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package create

import (
"encoding/json"
"fmt"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -36,23 +37,32 @@ $ 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

# 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`

flagRaw = "raw"
)

// 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(flagRaw, false, "Print output in JSON format")

return &cmd
}

// SetFlags sets flags supported by create command.
Expand Down Expand Up @@ -94,7 +104,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 +136,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(flagRaw)
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
Loading