-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-scrape.go
183 lines (168 loc) · 4.11 KB
/
github-scrape.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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/machinebox/graphql"
)
type IssueQueryResponse struct {
Node struct {
Title string
Assignees struct {
Nodes []struct {
Login string
}
}
Author struct {
Login string
}
BodyText string
Labels struct {
Nodes []struct {
Name string
}
}
Number int
URL string
State string
UpdatedAt string
}
}
type PullRequestQueryResponse struct {
Node struct {
Title string
BaseRefName string
ChangedFiles int
ChecksURL string
UpdatedAt string
Number int
Permalink string
State string
Author struct {
Login string
}
}
}
func determineQueryType(event interface{}, eventType string) {
if eventType == "push" {
MessageString := constructPushMessage(event.(PushResponse))
// fmt.Printf("%s", MessageString)
taskQueue <- Task{message: MessageString}
} else if eventType == "pull request" {
queryResult := QueryGitHubGraphQL(pullRequestQuery(event.(PullRequestResponse)))
MessageString := constructPullRequestMessage(queryResult.(PullRequestQueryResponse), event.(PullRequestResponse))
// fmt.Printf("%s", MessageString)
taskQueue <- Task{message: MessageString}
} else if eventType == "issue" {
queryResult := QueryGitHubGraphQL(issueQuery(event.(IssueResponse)))
MessageString := constructIssueMessage(queryResult.(IssueQueryResponse), event.(IssueResponse))
// fmt.Printf("%s", MessageString)
taskQueue <- Task{message: MessageString}
} else if eventType == "create" {
MessageString := constructCreateMessage(event.(CreateResponse))
// fmt.Printf("%s", MessageString)
taskQueue <- Task{message: MessageString}
} else if eventType == "delete" {
MessageString := constructDeleteMessage(event.(DeleteResponse))
// fmt.Printf("%s", MessageString)
taskQueue <- Task{message: MessageString}
} else {
fmt.Printf("Unknown event type %s:", eventType)
}
}
func issueQuery(event IssueResponse) string {
return fmt.Sprintf(`
query {
node(id: "%s") {
... on Issue {
title
assignees(first: 10) {
nodes {
login
}
}
author {
login
}
body
labels(first: 10) {
nodes {
name
}
}
number
url
state
updatedAt
}
}
}`, string(event.Issue.NodeID))
}
func pullRequestQuery(event PullRequestResponse) string {
return fmt.Sprintf(`
query {
node(id: "%s") {
... on PullRequest {
title
baseRefName
changedFiles
checksUrl
updatedAt
number
permalink
state
author {
login
}
}
}
}
`, string(event.PullRequest.NodeID))
}
type CustomTransport struct {
Transport http.RoundTripper
}
func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
return t.Transport.RoundTrip(req)
}
func QueryGitHubGraphQL(query string) interface{} {
token := config.GithubOAuthToken
client := graphql.NewClient("https://api.github.com/graphql", graphql.WithHTTPClient(&http.Client{
Transport: &CustomTransport{
Transport: http.DefaultTransport,
},
}))
request := graphql.NewRequest(query)
request.Header.Set("Authorization", "Bearer "+token)
var rawResponse json.RawMessage
err := client.Run(context.Background(), request, &rawResponse)
if err != nil {
fmt.Println("Error querying GitHub GraphQL API:", err)
return nil
}
// fmt.Printf("Raw JSON Response: %s\n", rawResponse)
var result interface{}
if queryContains(query, "Issue") {
var issueData IssueQueryResponse
err = json.Unmarshal(rawResponse, &issueData)
if err != nil {
fmt.Println("Error unmarshalling Issue data:", err)
return nil
}
result = iterateIssueFields(issueData)
} else if queryContains(query, "PullRequest") {
var pullRequestData PullRequestQueryResponse
err = json.Unmarshal(rawResponse, &pullRequestData)
if err != nil {
fmt.Println("Error unmarshalling PullRequest data:", err)
return nil
}
result = iteratePRFields(pullRequestData)
}
return result
}
func queryContains(query string, substr string) bool {
return bytes.Contains([]byte(query), []byte(substr))
}