-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo.go
193 lines (167 loc) · 3.89 KB
/
repo.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
package main
import (
"errors"
"time"
)
const ISSUE_LIMIT = 100
type Entity struct {
Name string `json:"login"`
}
type Pagination struct {
Cursor string `json:"endCursor"`
}
type Label struct {
Name string `json:"name"`
}
type TimelineItem struct {
Type string `json:"__typename"`
// IssueComment <https://developer.github.com/early-access/graphql/object/issuecomment/>
Author Entity `json:"author"`
Editor Entity `json:"editor"`
Body string `json:"body"`
CreatedAt time.Time `json:"createdAt"`
// ClosedEvent <https://developer.github.com/early-access/graphql/object/closedevent/>
Actor Entity `json:"actor"`
// +CreatedAt
// ReopenedEvent <https://developer.github.com/early-access/graphql/object/reopenedevent/>
// +Actor
// +CreatedAt
// LabeledEvent <https://developer.github.com/early-access/graphql/object/labeledevent/>
// +Actor
// +CreatedAt
Labels []Label `json:"label"`
}
type Timeline struct {
Timeline []TimelineItem `json:"nodes"`
NextPage Pagination `json:"pageInfo"`
}
type Issue struct {
Author Entity `json:"author"`
Editor Entity `json:"editor"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
Id string `json:"id"`
Number int `json:"number"`
Timeline Timeline `json:"timeline"`
}
type Issues struct {
Issues []Issue `json:"nodes"`
NextPage Pagination `json:"pageInfo"`
}
type Repository struct {
Owner Entity `json:"owner"`
Name string `json:"name"`
Issues Issues `json:"issues"`
}
var query = `
query ($owner: String!, $name: String!, $after: String) {
repository(owner: $owner, name: $name) {
owner {
login
}
name
issues(first: 100, after: $after) {
pageInfo {
endCursor
}
nodes {
id
body
createdAt
number
title
updatedAt
state
timeline(first:100) {
pageInfo {
endCursor
}
nodes {
__typename
... on IssueComment {
author {
login
}
body
createdAt
}
... on ClosedEvent {
actor {
login
}
createdAt
}
... on ReopenedEvent {
actor {
login
}
createdAt
}
... on LabeledEvent {
actor {
login
}
createdAt
}
}
}
editor {
login
}
author {
login
}
}
}
}
}
`
type Wrapper struct {
Message string `json:"message"`
Data struct {
Repository Repository `json:"repository"`
} `json:"data"`
}
func LoadRepo(owner, name string) (Repository, error) {
client, err := NewClient()
if err != nil {
return Repository{}, err
}
var repo Wrapper
err = client.Execute(query, map[string]interface{}{
"owner": owner,
"name": name,
}, &repo)
if err != nil {
return Repository{}, err
}
if len(repo.Message) > 0 {
return repo.Data.Repository, errors.New(repo.Message)
}
//TODO: Handle hitting the ratelimit gracefully
if len(repo.Data.Repository.Issues.NextPage.Cursor) > 0 {
after := repo.Data.Repository.Issues.NextPage.Cursor
for {
var repo2 Wrapper
err := client.Execute(query, map[string]interface{}{
"owner": owner,
"name": name,
"after": after,
}, &repo2)
if err != nil {
return Repository{}, err
}
if len(repo2.Message) > 0 {
return repo2.Data.Repository, errors.New(repo.Message)
}
repo.Data.Repository.Issues.Issues = append(repo.Data.Repository.Issues.Issues, repo2.Data.Repository.Issues.Issues...)
if len(repo2.Data.Repository.Issues.NextPage.Cursor) > 0 {
after = repo2.Data.Repository.Issues.NextPage.Cursor
} else {
break
}
}
}
return repo.Data.Repository, err
}