github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/cmd/issue/status/status.go (about) 1 package status 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "github.com/andrewhsu/cli/v2/api" 8 "github.com/andrewhsu/cli/v2/internal/config" 9 "github.com/andrewhsu/cli/v2/internal/ghrepo" 10 issueShared "github.com/andrewhsu/cli/v2/pkg/cmd/issue/shared" 11 prShared "github.com/andrewhsu/cli/v2/pkg/cmd/pr/shared" 12 "github.com/andrewhsu/cli/v2/pkg/cmdutil" 13 "github.com/andrewhsu/cli/v2/pkg/iostreams" 14 "github.com/spf13/cobra" 15 ) 16 17 type StatusOptions struct { 18 HttpClient func() (*http.Client, error) 19 Config func() (config.Config, error) 20 IO *iostreams.IOStreams 21 BaseRepo func() (ghrepo.Interface, error) 22 23 Exporter cmdutil.Exporter 24 } 25 26 func NewCmdStatus(f *cmdutil.Factory, runF func(*StatusOptions) error) *cobra.Command { 27 opts := &StatusOptions{ 28 IO: f.IOStreams, 29 HttpClient: f.HttpClient, 30 Config: f.Config, 31 } 32 33 cmd := &cobra.Command{ 34 Use: "status", 35 Short: "Show status of relevant issues", 36 Args: cmdutil.NoArgsQuoteReminder, 37 RunE: func(cmd *cobra.Command, args []string) error { 38 // support `-R, --repo` override 39 opts.BaseRepo = f.BaseRepo 40 41 if runF != nil { 42 return runF(opts) 43 } 44 return statusRun(opts) 45 }, 46 } 47 48 cmdutil.AddJSONFlags(cmd, &opts.Exporter, api.IssueFields) 49 50 return cmd 51 } 52 53 var defaultFields = []string{ 54 "number", 55 "title", 56 "url", 57 "state", 58 "updatedAt", 59 "labels", 60 } 61 62 func statusRun(opts *StatusOptions) error { 63 httpClient, err := opts.HttpClient() 64 if err != nil { 65 return err 66 } 67 apiClient := api.NewClientFromHTTP(httpClient) 68 69 baseRepo, err := opts.BaseRepo() 70 if err != nil { 71 return err 72 } 73 74 currentUser, err := api.CurrentLoginName(apiClient, baseRepo.RepoHost()) 75 if err != nil { 76 return err 77 } 78 79 options := api.IssueStatusOptions{ 80 Username: currentUser, 81 Fields: defaultFields, 82 } 83 if opts.Exporter != nil { 84 options.Fields = opts.Exporter.Fields() 85 } 86 issuePayload, err := api.IssueStatus(apiClient, baseRepo, options) 87 if err != nil { 88 return err 89 } 90 91 err = opts.IO.StartPager() 92 if err != nil { 93 fmt.Fprintf(opts.IO.ErrOut, "error starting pager: %v\n", err) 94 } 95 defer opts.IO.StopPager() 96 97 if opts.Exporter != nil { 98 data := map[string]interface{}{ 99 "createdBy": issuePayload.Authored.Issues, 100 "assigned": issuePayload.Assigned.Issues, 101 "mentioned": issuePayload.Mentioned.Issues, 102 } 103 return opts.Exporter.Write(opts.IO, data) 104 } 105 106 out := opts.IO.Out 107 108 fmt.Fprintln(out, "") 109 fmt.Fprintf(out, "Relevant issues in %s\n", ghrepo.FullName(baseRepo)) 110 fmt.Fprintln(out, "") 111 112 prShared.PrintHeader(opts.IO, "Issues assigned to you") 113 if issuePayload.Assigned.TotalCount > 0 { 114 issueShared.PrintIssues(opts.IO, " ", issuePayload.Assigned.TotalCount, issuePayload.Assigned.Issues) 115 } else { 116 message := " There are no issues assigned to you" 117 prShared.PrintMessage(opts.IO, message) 118 } 119 fmt.Fprintln(out) 120 121 prShared.PrintHeader(opts.IO, "Issues mentioning you") 122 if issuePayload.Mentioned.TotalCount > 0 { 123 issueShared.PrintIssues(opts.IO, " ", issuePayload.Mentioned.TotalCount, issuePayload.Mentioned.Issues) 124 } else { 125 prShared.PrintMessage(opts.IO, " There are no issues mentioning you") 126 } 127 fmt.Fprintln(out) 128 129 prShared.PrintHeader(opts.IO, "Issues opened by you") 130 if issuePayload.Authored.TotalCount > 0 { 131 issueShared.PrintIssues(opts.IO, " ", issuePayload.Authored.TotalCount, issuePayload.Authored.Issues) 132 } else { 133 prShared.PrintMessage(opts.IO, " There are no issues opened by you") 134 } 135 fmt.Fprintln(out) 136 137 return nil 138 }