forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries_projects_v2.go
331 lines (294 loc) · 10.6 KB
/
queries_projects_v2.go
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
package api
import (
"fmt"
"strings"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/shurcooL/githubv4"
)
const (
errorProjectsV2ReadScope = "field requires one of the following scopes: ['read:project']"
errorProjectsV2UserField = "Field 'projectsV2' doesn't exist on type 'User'"
errorProjectsV2RepositoryField = "Field 'projectsV2' doesn't exist on type 'Repository'"
errorProjectsV2OrganizationField = "Field 'projectsV2' doesn't exist on type 'Organization'"
errorProjectsV2IssueField = "Field 'projectItems' doesn't exist on type 'Issue'"
errorProjectsV2PullRequestField = "Field 'projectItems' doesn't exist on type 'PullRequest'"
)
type ProjectV2 struct {
ID string `json:"id"`
Title string `json:"title"`
Number int `json:"number"`
ResourcePath string `json:"resourcePath"`
Closed bool `json:"closed"`
URL string `json:"url"`
}
// UpdateProjectV2Items uses the addProjectV2ItemById and the deleteProjectV2Item mutations
// to add and delete items from projects. The addProjectItems and deleteProjectItems arguments are
// mappings between a project and an item. This function can be used across multiple projects
// and items. Note that the deleteProjectV2Item mutation requires the item id from the project not
// the global id.
func UpdateProjectV2Items(client *Client, repo ghrepo.Interface, addProjectItems, deleteProjectItems map[string]string) error {
l := len(addProjectItems) + len(deleteProjectItems)
if l == 0 {
return nil
}
inputs := make([]string, 0, l)
mutations := make([]string, 0, l)
variables := make(map[string]interface{}, l)
var i int
for project, item := range addProjectItems {
inputs = append(inputs, fmt.Sprintf("$input_%03d: AddProjectV2ItemByIdInput!", i))
mutations = append(mutations, fmt.Sprintf("add_%03d: addProjectV2ItemById(input: $input_%03d) { item { id } }", i, i))
variables[fmt.Sprintf("input_%03d", i)] = map[string]interface{}{"contentId": item, "projectId": project}
i++
}
for project, item := range deleteProjectItems {
inputs = append(inputs, fmt.Sprintf("$input_%03d: DeleteProjectV2ItemInput!", i))
mutations = append(mutations, fmt.Sprintf("delete_%03d: deleteProjectV2Item(input: $input_%03d) { deletedItemId }", i, i))
variables[fmt.Sprintf("input_%03d", i)] = map[string]interface{}{"itemId": item, "projectId": project}
i++
}
query := fmt.Sprintf(`mutation UpdateProjectV2Items(%s) {%s}`, strings.Join(inputs, " "), strings.Join(mutations, " "))
return client.GraphQL(repo.RepoHost(), query, variables, nil)
}
// ProjectsV2ItemsForIssue fetches all ProjectItems for an issue.
func ProjectsV2ItemsForIssue(client *Client, repo ghrepo.Interface, issue *Issue) error {
type projectV2ItemStatus struct {
StatusFragment struct {
OptionID string `json:"optionId"`
Name string `json:"name"`
} `graphql:"... on ProjectV2ItemFieldSingleSelectValue"`
}
type projectV2Item struct {
ID string `json:"id"`
Project struct {
ID string `json:"id"`
Title string `json:"title"`
}
Status projectV2ItemStatus `graphql:"status:fieldValueByName(name: \"Status\")"`
}
type response struct {
Repository struct {
Issue struct {
ProjectItems struct {
Nodes []*projectV2Item
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"projectItems(first: 100, after: $endCursor)"`
} `graphql:"issue(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"name": githubv4.String(repo.RepoName()),
"number": githubv4.Int(issue.Number),
"endCursor": (*githubv4.String)(nil),
}
var items ProjectItems
for {
var query response
err := client.Query(repo.RepoHost(), "IssueProjectItems", &query, variables)
if err != nil {
return err
}
for _, projectItemNode := range query.Repository.Issue.ProjectItems.Nodes {
items.Nodes = append(items.Nodes, &ProjectV2Item{
ID: projectItemNode.ID,
Project: ProjectV2ItemProject{
ID: projectItemNode.Project.ID,
Title: projectItemNode.Project.Title,
},
Status: ProjectV2ItemStatus{
OptionID: projectItemNode.Status.StatusFragment.OptionID,
Name: projectItemNode.Status.StatusFragment.Name,
},
})
}
if !query.Repository.Issue.ProjectItems.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Repository.Issue.ProjectItems.PageInfo.EndCursor)
}
issue.ProjectItems = items
return nil
}
// ProjectsV2ItemsForPullRequest fetches all ProjectItems for a pull request.
func ProjectsV2ItemsForPullRequest(client *Client, repo ghrepo.Interface, pr *PullRequest) error {
type projectV2ItemStatus struct {
StatusFragment struct {
OptionID string `json:"optionId"`
Name string `json:"name"`
} `graphql:"... on ProjectV2ItemFieldSingleSelectValue"`
}
type projectV2Item struct {
ID string `json:"id"`
Project struct {
ID string `json:"id"`
Title string `json:"title"`
}
Status projectV2ItemStatus `graphql:"status:fieldValueByName(name: \"Status\")"`
}
type response struct {
Repository struct {
PullRequest struct {
ProjectItems struct {
Nodes []*projectV2Item
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"projectItems(first: 100, after: $endCursor)"`
} `graphql:"pullRequest(number: $number)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"name": githubv4.String(repo.RepoName()),
"number": githubv4.Int(pr.Number),
"endCursor": (*githubv4.String)(nil),
}
var items ProjectItems
for {
var query response
err := client.Query(repo.RepoHost(), "PullRequestProjectItems", &query, variables)
if err != nil {
return err
}
for _, projectItemNode := range query.Repository.PullRequest.ProjectItems.Nodes {
items.Nodes = append(items.Nodes, &ProjectV2Item{
ID: projectItemNode.ID,
Project: ProjectV2ItemProject{
ID: projectItemNode.Project.ID,
Title: projectItemNode.Project.Title,
},
Status: ProjectV2ItemStatus{
OptionID: projectItemNode.Status.StatusFragment.OptionID,
Name: projectItemNode.Status.StatusFragment.Name,
},
})
}
if !query.Repository.PullRequest.ProjectItems.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Repository.PullRequest.ProjectItems.PageInfo.EndCursor)
}
pr.ProjectItems = items
return nil
}
// OrganizationProjectsV2 fetches all open projectsV2 for an organization.
func OrganizationProjectsV2(client *Client, repo ghrepo.Interface) ([]ProjectV2, error) {
type responseData struct {
Organization struct {
ProjectsV2 struct {
Nodes []ProjectV2
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"projectsV2(first: 100, orderBy: {field: TITLE, direction: ASC}, after: $endCursor, query: $query)"`
} `graphql:"organization(login: $owner)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"endCursor": (*githubv4.String)(nil),
"query": githubv4.String("is:open"),
}
var projectsV2 []ProjectV2
for {
var query responseData
err := client.Query(repo.RepoHost(), "OrganizationProjectV2List", &query, variables)
if err != nil {
return nil, err
}
projectsV2 = append(projectsV2, query.Organization.ProjectsV2.Nodes...)
if !query.Organization.ProjectsV2.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Organization.ProjectsV2.PageInfo.EndCursor)
}
return projectsV2, nil
}
// RepoProjectsV2 fetches all open projectsV2 for a repository.
func RepoProjectsV2(client *Client, repo ghrepo.Interface) ([]ProjectV2, error) {
type responseData struct {
Repository struct {
ProjectsV2 struct {
Nodes []ProjectV2
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"projectsV2(first: 100, orderBy: {field: TITLE, direction: ASC}, after: $endCursor, query: $query)"`
} `graphql:"repository(owner: $owner, name: $name)"`
}
variables := map[string]interface{}{
"owner": githubv4.String(repo.RepoOwner()),
"name": githubv4.String(repo.RepoName()),
"endCursor": (*githubv4.String)(nil),
"query": githubv4.String("is:open"),
}
var projectsV2 []ProjectV2
for {
var query responseData
err := client.Query(repo.RepoHost(), "RepositoryProjectV2List", &query, variables)
if err != nil {
return nil, err
}
projectsV2 = append(projectsV2, query.Repository.ProjectsV2.Nodes...)
if !query.Repository.ProjectsV2.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Repository.ProjectsV2.PageInfo.EndCursor)
}
return projectsV2, nil
}
// CurrentUserProjectsV2 fetches all open projectsV2 for current user.
func CurrentUserProjectsV2(client *Client, hostname string) ([]ProjectV2, error) {
type responseData struct {
Viewer struct {
ProjectsV2 struct {
Nodes []ProjectV2
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"projectsV2(first: 100, orderBy: {field: TITLE, direction: ASC}, after: $endCursor, query: $query)"`
} `graphql:"viewer"`
}
variables := map[string]interface{}{
"endCursor": (*githubv4.String)(nil),
"query": githubv4.String("is:open"),
}
var projectsV2 []ProjectV2
for {
var query responseData
err := client.Query(hostname, "UserProjectV2List", &query, variables)
if err != nil {
return nil, err
}
projectsV2 = append(projectsV2, query.Viewer.ProjectsV2.Nodes...)
if !query.Viewer.ProjectsV2.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Viewer.ProjectsV2.PageInfo.EndCursor)
}
return projectsV2, nil
}
// When querying ProjectsV2 fields we generally dont want to show the user
// scope errors and field does not exist errors. ProjectsV2IgnorableError
// checks against known error strings to see if an error can be safely ignored.
// Due to the fact that the GraphQLClient can return multiple types of errors
// this uses brittle string comparison to check against the known error strings.
func ProjectsV2IgnorableError(err error) bool {
msg := err.Error()
if strings.Contains(msg, errorProjectsV2ReadScope) ||
strings.Contains(msg, errorProjectsV2UserField) ||
strings.Contains(msg, errorProjectsV2RepositoryField) ||
strings.Contains(msg, errorProjectsV2OrganizationField) ||
strings.Contains(msg, errorProjectsV2IssueField) ||
strings.Contains(msg, errorProjectsV2PullRequestField) {
return true
}
return false
}