forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
88 lines (76 loc) · 1.69 KB
/
http.go
File metadata and controls
88 lines (76 loc) · 1.69 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
package list
import (
"net/http"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/cmd/label/shared"
)
func listLabels(client *http.Client, repo ghrepo.Interface, limit int) ([]shared.Label, int, error) {
apiClient := api.NewClientFromHTTP(client)
query := `
query LabelList($owner: String!,$repo: String!, $limit: Int!, $endCursor: String) {
repository(owner: $owner, name: $repo) {
labels(first: $limit, after: $endCursor) {
totalCount,
nodes {
name,
description,
color
}
pageInfo {
hasNextPage
endCursor
}
}
}
}`
variables := map[string]interface{}{
"owner": repo.RepoOwner(),
"repo": repo.RepoName(),
}
type responseData struct {
Repository struct {
Labels struct {
TotalCount int
Nodes []shared.Label
PageInfo struct {
HasNextPage bool
EndCursor string
}
}
HasIssuesEnabled bool
}
}
var labels []shared.Label
var totalCount int
pageLimit := min(limit, 100)
loop:
for {
var response responseData
variables["limit"] = pageLimit
err := apiClient.GraphQL(repo.RepoHost(), query, variables, &response)
if err != nil {
return nil, 0, err
}
totalCount = response.Repository.Labels.TotalCount
for _, label := range response.Repository.Labels.Nodes {
labels = append(labels, label)
if len(labels) == limit {
break loop
}
}
if response.Repository.Labels.PageInfo.HasNextPage {
variables["endCursor"] = response.Repository.Labels.PageInfo.EndCursor
pageLimit = min(pageLimit, limit-len(labels))
} else {
break
}
}
return labels, totalCount, nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}