forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries_search.go
More file actions
127 lines (111 loc) · 2.6 KB
/
queries_search.go
File metadata and controls
127 lines (111 loc) · 2.6 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
package api
import (
"context"
"time"
"github.com/shurcooL/githubv4"
)
type SearchRepoResult struct {
NameWithOwner string
Description string
Stargazers struct {
TotalCount int
}
}
func SearchRepos(client *Client, q string, limit int) ([]SearchRepoResult, error) {
var query struct {
Search struct {
Nodes []struct {
Repository SearchRepoResult `graphql:"...on Repository"`
}
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"search(query: $query, type: REPOSITORY, first: $limit, after: $endCursor)"`
}
perPage := limit
if perPage > 100 {
perPage = 100
}
variables := map[string]interface{}{
"query": githubv4.String(q),
"limit": githubv4.Int(perPage),
"endCursor": (*githubv4.String)(nil),
}
v4 := githubv4.NewClient(client.http)
var results []SearchRepoResult
pagination:
for {
err := v4.Query(context.Background(), &query, variables)
if err != nil {
return nil, err
}
for _, n := range query.Search.Nodes {
results = append(results, n.Repository)
if len(results) == limit {
break pagination
}
}
if !query.Search.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Search.PageInfo.EndCursor)
}
return results, nil
}
type SearchIssueResult struct {
Title string
Number int
State string
CreatedAt time.Time
Repository struct {
NameWithOwner string
}
}
func SearchIssues(client *Client, q string, limit int) ([]SearchIssueResult, error) {
var query struct {
Search struct {
Nodes []struct {
Issue SearchIssueResult `graphql:"...on Issue"`
PullRequest SearchIssueResult `graphql:"...on PullRequest"`
}
PageInfo struct {
HasNextPage bool
EndCursor string
}
} `graphql:"search(query: $query, type: ISSUE, first: $limit, after: $endCursor)"`
}
perPage := limit
if perPage > 100 {
perPage = 100
}
variables := map[string]interface{}{
"query": githubv4.String(q),
"limit": githubv4.Int(perPage),
"endCursor": (*githubv4.String)(nil),
}
v4 := githubv4.NewClient(client.http)
var results []SearchIssueResult
pagination:
for {
err := v4.Query(context.Background(), &query, variables)
if err != nil {
return nil, err
}
for _, n := range query.Search.Nodes {
if n.Issue.Number > 0 {
results = append(results, n.Issue)
} else {
results = append(results, n.PullRequest)
}
if len(results) == limit {
break pagination
}
}
if !query.Search.PageInfo.HasNextPage {
break
}
variables["endCursor"] = githubv4.String(query.Search.PageInfo.EndCursor)
}
return results, nil
}