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 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
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