forked from genuinetools/audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphql.go
302 lines (273 loc) · 7.07 KB
/
graphql.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
// GQLRequest is the GraphQL request containing Query and Variables
type GQLRequest struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}
// GQLResponse is the response from GraphQL server
type GQLResponse struct {
Data *json.RawMessage `json:"data"`
Errors *json.RawMessage `json:"errors"`
}
// GQLError is a the GraphQL error from GitHub API
type GQLError struct {
Message string `json:"message"`
Locations []GQLErrorLocation `json:"locations"`
Type string `json:"type"`
Path []interface{} `json:"path"`
}
// Error returns the error message
func (e GQLError) Error() string {
return e.Message
}
// GQLErrorLocation is the location of error in the query string
type GQLErrorLocation struct {
Line int `json:"line"`
Column int `json:"column"`
}
// GQLClient can execute GraphQL queries against an endpoint
type GQLClient struct {
Endpoint string
Headers map[string]string
client *http.Client
}
// NewGQLClient returns a GQLClient for given endpoint and headers
func NewGQLClient(endpoint string, headers map[string]string) *GQLClient {
return &GQLClient{
Endpoint: endpoint,
Headers: headers,
client: &http.Client{},
}
}
// Execute executes the GQLRequest r using the GQLClient c and returns an error
// Response data and errors can be unmarshalled to the passed interfaces
func (c *GQLClient) Execute(r GQLRequest, data interface{}, errors interface{}) error {
payload, err := json.Marshal(r)
if err != nil {
return err
}
req, err := http.NewRequest("POST", c.Endpoint, bytes.NewBuffer(payload))
if err != nil {
return err
}
for k, v := range c.Headers {
req.Header.Set(k, v)
}
res, err := c.client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
var response GQLResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return err
}
err = json.Unmarshal(*response.Data, data)
if err != nil {
return err
}
if response.Errors != nil {
err = json.Unmarshal(*response.Errors, errors)
if err != nil {
return err
}
}
return nil
}
// queryGetLogin is the GraphQL query to get login name of the user
const queryGetLogin = `
query {
viewer {
login
}
}
`
// loginData is the response data for QUERY_GET_LOGIN
type loginData map[string]map[string]string
// buildGetReposQuery takes a param (user or organization) and returns the
// correct GraphQL query to fetch repositories under that resource
func buildGetReposQuery(param string) string {
return fmt.Sprintf(`
query getRepos(
$login: String!,
$affiliations: [RepositoryAffiliation]!,
$cursor: String
) {
%s (login: $login) {
repositories(
first: 100,
affiliations: $affiliations,
orderBy: {field: STARGAZERS, direction: DESC},
after: $cursor
) {
totalCount
pageInfo {
startCursor
endCursor
hasNextPage
hasPreviousPage
}
nodes {
owner {
login
}
name
nameWithOwner
stargazers {
totalCount
}
refs(first: 100, refPrefix: "refs/heads/") {
totalCount
nodes {
name
}
}
mergeCommitAllowed
rebaseMergeAllowed
squashMergeAllowed
defaultBranchRef {
name
}
branchProtectionRules(first: 100) {
totalCount
nodes {
pattern
}
}
deployKeys(first: 100) {
totalCount
nodes {
id
title
readOnly
}
}
collaborators(first: 100) {
totalCount
edges {
permission
node {
login
}
}
}
}
}
}
}
`, param)
}
// queryGetRepo is the GraphQL query to get details about a repository
const queryGetRepo = `
query getRepo($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
owner {
login
}
name
nameWithOwner
stargazers {
totalCount
}
mergeCommitAllowed
rebaseMergeAllowed
squashMergeAllowed
defaultBranchRef {
name
}
refs(first: 100, refPrefix: "refs/heads/") {
totalCount
nodes {
name
}
}
branchProtectionRules(first: 100) {
totalCount
nodes {
pattern
}
}
deployKeys(first: 100) {
totalCount
nodes {
id
title
readOnly
}
}
collaborators(first: 100) {
totalCount
edges {
permission
node {
login
}
}
}
}
}
`
type userReposResponse struct {
User repos `json:"user"`
}
type orgReposResponse struct {
Org repos `json:"organization"`
}
type repos struct {
Repositories repositoriesInfo `json:"repositories"`
}
type repositoriesInfo struct {
TotalCount int `json:"totalCount"`
PageInfo pageInfo `json:"pageInfo"`
Nodes []ghrepo `json:"nodes"`
}
type pageInfo struct {
StartCursor string `json:"startCursor"`
EndCursor string `json:"endCursor"`
HasNextPage bool `json:"hasNextPage"`
}
type repoResponse struct {
Repository ghrepo `json:"repository"`
}
type ghrepo struct {
Name string `json:"name"`
Owner collaboratorNode `json:"owner"`
NameWithOwner string `json:"nameWithOwner"`
Stargazers countNodeName `json:"stargazers"`
MergeCommitAllowed bool `json:"mergeCommitAllowed"`
RebaseMergeAllowed bool `json:"rebaseMergeAllowed"`
SquashMergeAllowed bool `json:"squashMergeAllowed"`
Refs countNodeName `json:"refs"`
BranchProtectionRules countNodeName `json:"branchProtectionRules"`
DeployKeys countNodeName `json:"deployKeys"`
Collaborators collaborators `json:"collaborators"`
}
type countNodeName struct {
TotalCount int `json:"totalCount"`
Nodes []nodeElement `json:"nodes"`
}
type nodeElement struct {
Name string `json:"name"`
Title string `json:"title"`
ReadOnly bool `json:"readOnly"`
ID string `json:"id"`
Pattern string `json:"pattern"`
}
type collaborators struct {
TotalCount int `json:"totalCount"`
Edges []collaboratorEdge `json:"edges"`
}
type collaboratorEdge struct {
Permission string `json:"permission"`
Node collaboratorNode `json:"node"`
}
type collaboratorNode struct {
Login string `json:"login"`
}