package cli import ( "errors" "fmt" "strings" "github.com/spf13/cobra" "tangled.org/rockorager.dev/knit/internal/tangled" ) func newIssueCmd(app *App) *cobra.Command { cmd := &cobra.Command{ Use: "issue ", Short: "Manage issues", } addRepoFlag(app, cmd) cmd.AddCommand( newIssueCreateCmd(app), newTicketListCmd(app, issueKind), newIssueViewCmd(app), newTicketCommentCmd(app, issueKind), newTicketStateCmd(app, issueKind, "close", "Close an issue", tangled.StateClosed), newTicketStateCmd(app, issueKind, "reopen", "Reopen an issue", tangled.StateOpen), newTicketEditCmd(app, issueKind), ) return cmd } func newIssueCreateCmd(app *App) *cobra.Command { var ( title, body, bodyFile string labels []string web bool ) cmd := &cobra.Command{ Use: "create", Short: "Create an issue", Long: `Create an issue. Without --title, knit prompts for the title and body when run interactively.`, Example: ` knit issue create --title "Crash on startup" --body "Steps to reproduce..." knit issue create --label bug knit issue create --web`, Args: cobra.NoArgs, RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() repo, err := app.baseRepo(ctx) if err != nil { return err } c := app.Client() if web { return app.browse(c.WebURL(repo, "issues", "new")) } if err := app.requireAuth(ctx); err != nil { return err } text, given, err := app.readBody(cmd, body, bodyFile) if err != nil { return err } if title == "" { if !app.interactive() { return errors.New("--title is required when not running interactively") } app.Infof("Creating issue in %s\n\n", app.Bold(repo.FullName())) if err := app.promptInput("Title", "", &title); err != nil { return err } if !given { if text, err = app.promptBody(); err != nil { return err } } } if strings.TrimSpace(title) == "" { return errors.New("title can't be empty") } keys, err := app.labelKeys(ctx, repo, labels) if err != nil { return err } w, err := c.CreateTicket(ctx, repo, tangled.TicketInput{Title: title, Body: text, Labels: keys}) if err != nil { return err } app.settle(ctx, w) app.Printf("%s\n", app.ticketURL(repo, issueKind, tangled.RkeyFromURI(w.URI))) return nil }, } cmd.Flags().StringVarP(&title, "title", "t", "", "Title of the issue") cmd.Flags().StringVarP(&body, "body", "b", "", "Body of the issue") cmd.Flags().StringVarP(&bodyFile, "body-file", "F", "", "Read the body from `file` (use \"-\" for standard input)") cmd.Flags().StringSliceVarP(&labels, "label", "l", nil, "Add labels by name") cmd.Flags().BoolVarP(&web, "web", "w", false, "Open the browser to create an issue") return cmd } // promptBody asks whether to write a body in the editor. func (a *App) promptBody() (string, error) { ok, err := a.promptConfirm("Write a body in your editor?") if err != nil || !ok { return "", err } text, err := a.editText("", "BODY.md") return strings.TrimSpace(text), err } func newIssueViewCmd(app *App) *cobra.Command { var comments, web, asJSON bool cmd := &cobra.Command{ Use: "view ", Short: "View an issue", Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) error { ctx := cmd.Context() number, repo, err := app.ticketRepo(ctx, args[0]) if err != nil { return err } url := app.ticketURL(repo, issueKind, number) if web { return app.browse(url) } c := app.Client() t, err := c.GetTicket(ctx, repo.DID, number) if err != nil { return err } if t.IsPull() { return fmt.Errorf("#%s is a pull request; use `knit pr view %s`", number, number) } app.labelNames(ctx, repo, t) var cms []*tangled.Comment if comments || asJSON { if cms, err = c.ListComments(ctx, t.URI); err != nil { return err } } if asJSON { return app.PrintJSON(struct { *tangled.Ticket CommentList []*tangled.Comment `json:"commentList"` }{t, cms}) } app.printTicket(t, issueKind, nil) if comments { app.printComments(cms) } else if t.Comments > 0 { app.Printf("%s\n\n", app.Gray(fmt.Sprintf("Use --comments to view %s.", countNoun(t.Comments, "comment")))) } app.Printf("%s\n", app.Gray("View this issue on Tangled: "+url)) return nil }, } cmd.Flags().BoolVarP(&comments, "comments", "c", false, "Show comments") cmd.Flags().BoolVarP(&web, "web", "w", false, "Open the issue in the browser") cmd.Flags().BoolVar(&asJSON, "json", false, "Output JSON") return cmd } func countNoun(n int, noun string) string { if n == 1 { return "1 " + noun } return fmt.Sprintf("%d %ss", n, noun) }