1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
|
package cmd
import (
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
)
var (
issueRepo string
issueState string
issueTitle string
issueBody string
issueLabels string
issueMilestone string
issueAssignee string
issueRemove bool
)
func init() {
issueCmd := &cobra.Command{
Use: "issue",
Aliases: []string{"issues"},
Short: "Work with issues (and labels)",
}
issueCmd.PersistentFlags().StringVarP(&issueRepo, "repo", "R", "", "target repo as owner/repo (default: current git remote)")
listCmd := &cobra.Command{
Use: "list",
Short: "List issues",
Args: cobra.NoArgs,
RunE: runIssueList,
}
listCmd.Flags().StringVar(&issueState, "state", "open", "open | closed | all")
addPaging(listCmd)
viewCmd := &cobra.Command{
Use: "view <number>",
Short: "Show an issue with its comments",
Args: cobra.ExactArgs(1),
RunE: runIssueView,
}
createCmd := &cobra.Command{
Use: "create",
Short: "Open an issue",
Args: cobra.NoArgs,
RunE: runIssueCreate,
}
createCmd.Flags().StringVarP(&issueTitle, "title", "t", "", "title (required)")
createCmd.Flags().StringVarP(&issueBody, "body", "b", "", "description body ('-' reads stdin)")
closeCmd := &cobra.Command{
Use: "close <number>",
Short: "Close an issue",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { return runIssueState(cmd, args, "closed") },
}
reopenCmd := &cobra.Command{
Use: "reopen <number>",
Short: "Reopen a closed issue",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { return runIssueState(cmd, args, "open") },
}
commentCmd := &cobra.Command{
Use: "comment <number>",
Short: "Comment on an issue",
Args: cobra.ExactArgs(1),
RunE: runIssueComment,
}
commentCmd.Flags().StringVarP(&issueBody, "body", "b", "", "comment body ('-' reads stdin)")
labelCmd := &cobra.Command{
Use: "label <number>",
Short: "Replace an issue's labels (by name, comma-separated; --clear empties)",
Args: cobra.ExactArgs(1),
RunE: runIssueLabel,
}
labelCmd.Flags().StringVar(&issueLabels, "labels", "", "comma-separated label names")
labelCmd.Flags().BoolVar(&issueRemove, "clear", false, "remove all labels")
milestoneCmd := &cobra.Command{
Use: "milestone <number>",
Short: "Assign an issue to a milestone (by title or id; --clear removes)",
Args: cobra.ExactArgs(1),
RunE: runIssueMilestone,
}
milestoneCmd.Flags().StringVar(&issueMilestone, "milestone", "", "milestone title or id")
milestoneCmd.Flags().BoolVar(&issueRemove, "clear", false, "remove the milestone")
assignCmd := &cobra.Command{
Use: "assign <number>",
Short: "Add or remove an assignee (--remove)",
Args: cobra.ExactArgs(1),
RunE: runIssueAssign,
}
assignCmd.Flags().StringVar(&issueAssignee, "user", "", "user handle")
assignCmd.Flags().BoolVar(&issueRemove, "remove", false, "remove instead of add")
labelsCmd := &cobra.Command{
Use: "labels",
Short: "List the repo's labels",
Args: cobra.NoArgs,
RunE: runIssueLabelsList,
}
issueCmd.AddCommand(listCmd, viewCmd, createCmd, closeCmd, reopenCmd, commentCmd, labelCmd, milestoneCmd, assignCmd, labelsCmd)
rootCmd.AddCommand(issueCmd)
}
// issueNumber parses a positive issue number argument.
func issueNumber(arg string) (int, error) {
n, err := strconv.Atoi(arg)
if err != nil || n <= 0 {
return 0, fmt.Errorf("invalid issue number %q", arg)
}
return n, nil
}
// labelNames splits a comma-separated --labels value.
func labelNames(s string) []string {
if strings.TrimSpace(s) == "" {
return []string{}
}
parts := strings.Split(s, ",")
out := make([]string, 0, len(parts))
for _, p := range parts {
if name := strings.TrimSpace(p); name != "" {
out = append(out, name)
}
}
return out
}
func runIssueList(cmd *cobra.Command, _ []string) error {
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
page, err := client.ListIssues(cmd.Context(), owner, repo, issueState, pageFlag, perPageFlag)
if err != nil {
return err
}
if flagJSON {
return printJSON(cmd.OutOrStdout(), page)
}
if len(page.Items) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No issues.")
return nil
}
tw := newTabw(cmd.OutOrStdout())
fmt.Fprintln(tw, "#\tSTATE\tLABELS\tMILESTONE\tTITLE")
for _, i := range page.Items {
var labels, milestone string
if len(i.Labels) > 0 {
names := make([]string, len(i.Labels))
for j, l := range i.Labels {
names[j] = l.Name
}
labels = strings.Join(names, ",")
}
if i.Milestone != nil {
milestone = i.Milestone.Title
}
fmt.Fprintf(tw, "%d\t%s\t%s\t%s\t%s\n", i.Number, i.State, dash(labels), dash(milestone), i.Title)
}
tw.Flush()
printPageFooter(cmd, page.Page)
return nil
}
func runIssueView(cmd *cobra.Command, args []string) error {
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
n, err := issueNumber(args[0])
if err != nil {
return err
}
i, err := client.GetIssue(cmd.Context(), owner, repo, n)
if err != nil {
return err
}
if flagJSON {
return printJSON(cmd.OutOrStdout(), i)
}
out := cmd.OutOrStdout()
state := i.State
if i.Milestone != nil {
state += " · " + i.Milestone.Title
}
fmt.Fprintf(out, "Issue #%d %s [%s]\n", i.Number, i.Title, state)
fmt.Fprintf(out, "by %s · %s\n", dash(i.Author), humanTime(i.CreatedAt))
if len(i.Labels) > 0 {
names := make([]string, len(i.Labels))
for j, l := range i.Labels {
names[j] = l.Name
}
fmt.Fprintf(out, "labels: %s\n", strings.Join(names, ", "))
}
if len(i.Assignees) > 0 {
handles := make([]string, len(i.Assignees))
for j, a := range i.Assignees {
handles[j] = a.Handle
}
fmt.Fprintf(out, "assignees: %s\n", strings.Join(handles, ", "))
}
fmt.Fprintln(out)
if i.Body != "" {
fmt.Fprintln(out, i.Body)
}
for _, c := range i.Comments {
fmt.Fprintf(out, "\n--- %s (%s) ---\n%s\n", c.Author, humanTime(c.CreatedAt), c.Body)
}
return nil
}
// bodyOrStdin resolves a --body flag value, reading stdin when it is "-".
func bodyOrStdin(cmd *cobra.Command, body string) (string, error) {
if body != "-" {
return body, nil
}
var sb strings.Builder
buf := make([]byte, 32*1024)
in := cmd.InOrStdin()
for {
n, err := in.Read(buf)
sb.Write(buf[:n])
if err != nil {
break
}
}
return strings.TrimRight(sb.String(), "\n"), nil
}
func runIssueCreate(cmd *cobra.Command, _ []string) error {
if issueTitle == "" {
return fmt.Errorf("--title is required")
}
body, err := bodyOrStdin(cmd, issueBody)
if err != nil {
return err
}
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
i, err := client.CreateIssue(cmd.Context(), owner, repo, issueTitle, body)
if err != nil {
return err
}
if flagJSON {
return printJSON(cmd.OutOrStdout(), i)
}
fmt.Fprintf(cmd.OutOrStdout(), "Opened issue #%d: %s\n", i.Number, i.Title)
return nil
}
func runIssueState(cmd *cobra.Command, args []string, state string) error {
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
n, err := issueNumber(args[0])
if err != nil {
return err
}
i, err := client.SetIssueState(cmd.Context(), owner, repo, n, state)
if err != nil {
return err
}
if flagJSON {
return printJSON(cmd.OutOrStdout(), i)
}
fmt.Fprintf(cmd.OutOrStdout(), "Issue #%d is now %s.\n", i.Number, i.State)
return nil
}
func runIssueComment(cmd *cobra.Command, args []string) error {
body, err := bodyOrStdin(cmd, issueBody)
if err != nil {
return err
}
if strings.TrimSpace(body) == "" {
return fmt.Errorf("--body is required")
}
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
n, err := issueNumber(args[0])
if err != nil {
return err
}
c, err := client.CommentIssue(cmd.Context(), owner, repo, n, body)
if err != nil {
return err
}
if flagJSON {
return printJSON(cmd.OutOrStdout(), c)
}
fmt.Fprintf(cmd.OutOrStdout(), "Commented on issue #%d.\n", n)
return nil
}
func runIssueLabel(cmd *cobra.Command, args []string) error {
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
n, err := issueNumber(args[0])
if err != nil {
return err
}
names := []string{}
if !issueRemove {
names = labelNames(issueLabels)
}
if err := client.SetIssueLabels(cmd.Context(), owner, repo, n, names); err != nil {
return err
}
if len(names) == 0 {
fmt.Fprintf(cmd.OutOrStdout(), "Cleared labels on issue #%d.\n", n)
} else {
fmt.Fprintf(cmd.OutOrStdout(), "Set labels on issue #%d: %s\n", n, strings.Join(names, ", "))
}
return nil
}
func runIssueMilestone(cmd *cobra.Command, args []string) error {
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
n, err := issueNumber(args[0])
if err != nil {
return err
}
ref := issueMilestone
if issueRemove {
ref = ""
}
if err := client.SetIssueMilestone(cmd.Context(), owner, repo, n, ref); err != nil {
return err
}
if ref == "" {
fmt.Fprintf(cmd.OutOrStdout(), "Removed the milestone from issue #%d.\n", n)
} else {
fmt.Fprintf(cmd.OutOrStdout(), "Set issue #%d's milestone to %s.\n", n, ref)
}
return nil
}
func runIssueAssign(cmd *cobra.Command, args []string) error {
if issueAssignee == "" {
return fmt.Errorf("--user is required")
}
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
n, err := issueNumber(args[0])
if err != nil {
return err
}
op := "add"
if issueRemove {
op = "remove"
}
if err := client.SetIssueAssignee(cmd.Context(), owner, repo, n, op, issueAssignee); err != nil {
return err
}
verb := "Assigned"
if issueRemove {
verb = "Unassigned"
}
fmt.Fprintf(cmd.OutOrStdout(), "%s %s on issue #%d.\n", verb, issueAssignee, n)
return nil
}
func runIssueLabelsList(cmd *cobra.Command, _ []string) error {
client, err := newClient()
if err != nil {
return err
}
owner, repo, err := resolveRepo(issueRepo)
if err != nil {
return err
}
labels, err := client.ListLabels(cmd.Context(), owner, repo)
if err != nil {
return err
}
if flagJSON {
return printJSON(cmd.OutOrStdout(), labels)
}
if len(labels) == 0 {
fmt.Fprintln(cmd.OutOrStdout(), "No labels.")
return nil
}
tw := newTabw(cmd.OutOrStdout())
fmt.Fprintln(tw, "NAME\tCOLOR")
for _, l := range labels {
fmt.Fprintf(tw, "%s\t#%s\n", l.Name, l.Color)
}
tw.Flush()
return nil
}
|