forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk.go
More file actions
123 lines (103 loc) · 2.87 KB
/
bulk.go
File metadata and controls
123 lines (103 loc) · 2.87 KB
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
package command
import (
"bufio"
"fmt"
"os"
"regexp"
"strconv"
"strings"
"github.com/cli/cli/api"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
func init() {
RootCmd.AddCommand(bulkCmd)
bulkCmd.Flags().StringSliceP("label", "l", nil, "Add label")
}
var bulkCmd = &cobra.Command{
Use: "bulk {add|open|close|browse}",
Args: cobra.ExactArgs(1),
Short: "Perform operation on a set of objects",
Long: `Perform bulk operation on a set of objects passed in via standard input.
The first word of every line of input is interpreted like so:
- number, e.g. "123": an issue number in the current repository
- repo with number, e.g. "owner/repo#123": an issue in a specific repository
- repo, e.g. "owner/repo": a specific repository
Valid operations are:
- "add": add labels
- "close": set issue state to closed
- "open": set issue state to open
- "browse": open items in the web browser
Examples:
$ gh issue list -L 10 | gh bulk add --label "triage"`,
RunE: bulk,
}
func bulk(cmd *cobra.Command, args []string) error {
ctx := contextForCommand(cmd)
baseRepo, err := ctx.BaseRepo()
if err != nil {
return err
}
client, err := apiClientForContext(ctx)
if err != nil {
return err
}
input := bufio.NewScanner(os.Stdin)
var inputItems []api.BulkInput
repoWithIssueRE := regexp.MustCompile(`^([^/]+/[^/]+)#(\d+)$`)
repoNameRE := regexp.MustCompile(`^([^/]+/[^/]+)$`)
for input.Scan() {
line := input.Text()
tokens := strings.Fields(line)
if len(tokens) < 1 {
continue
}
if issueNum, err := strconv.Atoi(tokens[0]); err == nil {
inputItems = append(inputItems, api.BulkInput{
Number: issueNum,
Repository: baseRepo,
})
continue
}
if m := repoWithIssueRE.FindAllStringSubmatch(tokens[0], 1); len(m) > 0 {
repo := ghrepo.FromFullName(m[0][1])
issueNum, _ := strconv.Atoi(m[0][2])
inputItems = append(inputItems, api.BulkInput{
Number: issueNum,
Repository: repo,
})
continue
}
if m := repoNameRE.FindAllStringSubmatch(tokens[0], 1); len(m) > 0 {
repo := ghrepo.FromFullName(m[0][1])
inputItems = append(inputItems, api.BulkInput{
Repository: repo,
})
continue
}
return fmt.Errorf("unrecognized input format: %q", tokens[0])
}
labels, err := cmd.Flags().GetStringSlice("label")
if err != nil {
return err
}
operation := args[0]
switch operation {
case "add":
return api.BulkAddLabels(client, inputItems, labels)
case "open", "close":
return api.BulkChangeState(client, inputItems, operation)
case "browse":
for _, item := range inputItems {
url := fmt.Sprintf("https://github.com/%s/%s", item.Repository.RepoOwner(), item.Repository.RepoName())
if item.Number > 0 {
url += fmt.Sprintf("/issues/%d", item.Number)
}
_ = utils.OpenInBrowser(url)
}
return nil
default:
return fmt.Errorf("unrecognized operation: %q", operation)
}
}