forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries_bulk.go
More file actions
153 lines (130 loc) · 3.19 KB
/
queries_bulk.go
File metadata and controls
153 lines (130 loc) · 3.19 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
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
package api
import (
"fmt"
"strings"
"github.com/cli/cli/internal/ghrepo"
)
type BulkInput struct {
Number int
Repository ghrepo.Interface
Label string
RepositoryID string
}
type ResolveResult struct {
ID string
Issue struct {
ID string
} `json:"issueOrPullRequest"`
Label struct {
ID string
}
}
func ResolveNodeIDs(client *Client, inputs []BulkInput) ([]ResolveResult, error) {
var queries []string
for i, input := range inputs {
if input.Number > 0 {
queries = append(queries, fmt.Sprintf(`
r%03d: repository(owner:%q, name:%q) {
id
issueOrPullRequest(number:%d) {
...on Issue { id }
...on PullRequest { id }
}
}
`, i, input.Repository.RepoOwner(), input.Repository.RepoName(), input.Number))
} else if input.Label != "" {
queries = append(queries, fmt.Sprintf(`
r%03d: node(id:%q) {
...on Repository {
id
label(name:%q) { id }
}
}
`, i, input.RepositoryID, input.Label))
} else {
queries = append(queries, fmt.Sprintf(`
r%03d: repository(owner:%q, name:%q) { id }
`, i, input.Repository.RepoOwner(), input.Repository.RepoName()))
}
}
result := make(map[string]ResolveResult)
var ids []ResolveResult
err := client.GraphQL(fmt.Sprintf(`{%s}`, strings.Join(queries, "")), nil, &result)
if err != nil {
return ids, err
}
// NOTE: iteration is not ordered
for _, v := range result {
ids = append(ids, v)
}
return ids, nil
}
func BulkAddLabels(client *Client, inputs []BulkInput, labelNames []string) error {
ids, err := ResolveNodeIDs(client, inputs)
if err != nil {
return err
}
var mutations []string
cache := make(map[string][]ResolveResult)
for i, id := range ids {
labelIDs, ok := cache[id.ID]
if !ok {
var labelInputs []BulkInput
for _, l := range labelNames {
labelInputs = append(labelInputs, BulkInput{
RepositoryID: id.ID,
Label: l,
})
}
labelIDs, err = ResolveNodeIDs(client, labelInputs)
if err != nil {
return err
}
cache[id.ID] = labelIDs
}
var labelIDsSerialized []string
for _, l := range labelIDs {
labelIDsSerialized = append(labelIDsSerialized, fmt.Sprintf("%q", l.Label.ID))
}
mutations = append(mutations, fmt.Sprintf(`
m%03d: addLabelsToLabelable(input: {
labelableId: %q
labelIds: [%s]
}) { clientMutationId }
`, i, id.Issue.ID, strings.Join(labelIDsSerialized, ",")))
}
err = client.GraphQL(fmt.Sprintf(`mutation{%s}`, strings.Join(mutations, "")), nil, nil)
if err != nil {
return err
}
return nil
}
func BulkChangeState(client *Client, inputs []BulkInput, state string) error {
ids, err := ResolveNodeIDs(client, inputs)
if err != nil {
return err
}
var mutations []string
var newState string
switch state {
case "open":
newState = "OPEN"
case "close":
newState = "CLOSED"
default:
return fmt.Errorf("unrecognized state: %q", state)
}
for i, id := range ids {
mutations = append(mutations, fmt.Sprintf(`
m%03d: updateIssue(input: {
id: %q
state: %s
}) { clientMutationId }
`, i, id.Issue.ID, newState))
}
err = client.GraphQL(fmt.Sprintf(`mutation{%s}`, strings.Join(mutations, "")), nil, nil)
if err != nil {
return err
}
return nil
}