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
219 lines (186 loc) · 5.05 KB
/
http.go
File metadata and controls
219 lines (186 loc) · 5.05 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
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
package list
import (
"fmt"
"net/http"
"strings"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/pkg/githubsearch"
"github.com/shurcooL/githubv4"
)
type RepositoryList struct {
Owner string
Repositories []api.Repository
TotalCount int
FromSearch bool
}
type FilterOptions struct {
Visibility string // private, public
Fork bool
Source bool
Language string
Topic string
Archived bool
NonArchived bool
Fields []string
}
func listRepos(client *http.Client, hostname string, limit int, owner string, filter FilterOptions) (*RepositoryList, error) {
if filter.Language != "" || filter.Archived || filter.NonArchived || filter.Topic != "" {
return searchRepos(client, hostname, limit, owner, filter)
}
perPage := limit
if perPage > 100 {
perPage = 100
}
variables := map[string]interface{}{
"perPage": githubv4.Int(perPage),
}
if filter.Visibility != "" {
variables["privacy"] = githubv4.RepositoryPrivacy(strings.ToUpper(filter.Visibility))
}
if filter.Fork {
variables["fork"] = githubv4.Boolean(true)
} else if filter.Source {
variables["fork"] = githubv4.Boolean(false)
}
inputs := []string{"$perPage:Int!", "$endCursor:String", "$privacy:RepositoryPrivacy", "$fork:Boolean"}
var ownerConnection string
if owner == "" {
ownerConnection = "repositoryOwner: viewer"
} else {
ownerConnection = "repositoryOwner(login: $owner)"
variables["owner"] = githubv4.String(owner)
inputs = append(inputs, "$owner:String!")
}
type result struct {
RepositoryOwner struct {
Login string
Repositories struct {
Nodes []api.Repository
TotalCount int
PageInfo struct {
HasNextPage bool
EndCursor string
}
}
}
}
query := fmt.Sprintf(`query RepositoryList(%s) {
%s {
login
repositories(first: $perPage, after: $endCursor, privacy: $privacy, isFork: $fork, ownerAffiliations: OWNER, orderBy: { field: PUSHED_AT, direction: DESC }) {
nodes{%s}
totalCount
pageInfo{hasNextPage,endCursor}
}
}
}`, strings.Join(inputs, ","), ownerConnection, api.RepositoryGraphQL(filter.Fields))
apiClient := api.NewClientFromHTTP(client)
listResult := RepositoryList{}
pagination:
for {
var res result
err := apiClient.GraphQL(hostname, query, variables, &res)
if err != nil {
return nil, err
}
owner := res.RepositoryOwner
listResult.TotalCount = owner.Repositories.TotalCount
listResult.Owner = owner.Login
for _, repo := range owner.Repositories.Nodes {
listResult.Repositories = append(listResult.Repositories, repo)
if len(listResult.Repositories) >= limit {
break pagination
}
}
if !owner.Repositories.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(owner.Repositories.PageInfo.EndCursor)
}
return &listResult, nil
}
func searchRepos(client *http.Client, hostname string, limit int, owner string, filter FilterOptions) (*RepositoryList, error) {
type result struct {
Search struct {
RepositoryCount int
Nodes []api.Repository
PageInfo struct {
HasNextPage bool
EndCursor string
}
}
}
query := fmt.Sprintf(`query RepositoryListSearch($query:String!,$perPage:Int!,$endCursor:String) {
search(type: REPOSITORY, query: $query, first: $perPage, after: $endCursor) {
repositoryCount
nodes{...on Repository{%s}}
pageInfo{hasNextPage,endCursor}
}
}`, api.RepositoryGraphQL(filter.Fields))
perPage := limit
if perPage > 100 {
perPage = 100
}
variables := map[string]interface{}{
"query": githubv4.String(searchQuery(owner, filter)),
"perPage": githubv4.Int(perPage),
}
apiClient := api.NewClientFromHTTP(client)
listResult := RepositoryList{FromSearch: true}
pagination:
for {
var result result
err := apiClient.GraphQL(hostname, query, variables, &result)
if err != nil {
return nil, err
}
listResult.TotalCount = result.Search.RepositoryCount
for _, repo := range result.Search.Nodes {
if listResult.Owner == "" && repo.NameWithOwner != "" {
idx := strings.IndexRune(repo.NameWithOwner, '/')
listResult.Owner = repo.NameWithOwner[:idx]
}
listResult.Repositories = append(listResult.Repositories, repo)
if len(listResult.Repositories) >= limit {
break pagination
}
}
if !result.Search.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(result.Search.PageInfo.EndCursor)
}
return &listResult, nil
}
func searchQuery(owner string, filter FilterOptions) string {
q := githubsearch.NewQuery()
q.SortBy(githubsearch.UpdatedAt, githubsearch.Desc)
if owner == "" {
q.OwnedBy("@me")
} else {
q.OwnedBy(owner)
}
if filter.Fork {
q.OnlyForks()
} else {
q.IncludeForks(!filter.Source)
}
if filter.Language != "" {
q.SetLanguage(filter.Language)
}
if filter.Topic != "" {
q.SetTopic(filter.Topic)
}
switch filter.Visibility {
case "public":
q.SetVisibility(githubsearch.Public)
case "private":
q.SetVisibility(githubsearch.Private)
}
if filter.Archived {
q.SetArchived(true)
} else if filter.NonArchived {
q.SetArchived(false)
}
return q.String()
}