-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
363 lines (313 loc) · 10.3 KB
/
main.go
File metadata and controls
363 lines (313 loc) · 10.3 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Package main provides a comparison utility for REST vs GraphQL PR fetching.
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"reflect"
"sort"
"strings"
"github.com/codeGROOVE-dev/prx/pkg/prx"
"github.com/codeGROOVE-dev/prx/pkg/prx/github"
"github.com/codeGROOVE-dev/prx/pkg/prx/types"
)
const (
defaultPRNumber = 1359
truncateDisplayLength = 50
)
func main() {
var token string
var owner string
var repo string
var prNumber int
flag.StringVar(&token, "token", os.Getenv("GITHUB_TOKEN"), "GitHub token")
flag.StringVar(&owner, "owner", "oxidecomputer", "Repository owner")
flag.StringVar(&repo, "repo", "dropshot", "Repository name")
flag.IntVar(&prNumber, "pr", defaultPRNumber, "Pull request number")
flag.Parse()
if token == "" {
log.Fatal("GitHub token required (set GITHUB_TOKEN or use -token)")
}
// Both now use GraphQL, but we'll compare two fetches to ensure consistency
fmt.Println("Fetching first time...")
restClient := prx.NewClientWithPlatform(github.NewPlatform(token))
restData, err := restClient.PullRequest(context.TODO(), owner, repo, prNumber)
if err != nil {
log.Fatalf("First fetch failed: %v", err)
}
// Fetch again to compare consistency
fmt.Println("Fetching second time...")
graphqlClient := prx.NewClientWithPlatform(github.NewPlatform(token))
graphqlData, err := graphqlClient.PullRequest(context.TODO(), owner, repo, prNumber)
if err != nil {
log.Fatalf("Second fetch failed: %v", err)
}
// Compare and report differences
fmt.Println("\n=== COMPARISON RESULTS ===")
comparePullRequestData(restData, graphqlData)
// Save to files for detailed inspection
saveJSON("rest_output.json", restData)
saveJSON("graphql_output.json", graphqlData)
fmt.Println("\nFull data saved to rest_output.json and graphql_output.json")
}
func comparePullRequestData(rest, graphql *types.PullRequestData) {
// Compare PullRequest fields
fmt.Println("=== Pull Request Metadata ===")
comparePullRequest(&rest.PullRequest, &graphql.PullRequest)
// Compare Events
fmt.Println("\n=== Events Comparison ===")
compareEvents(rest.Events, graphql.Events)
}
func comparePullRequest(rest, graphql *types.PullRequest) {
differences, matches := compareFields(rest, graphql)
if len(differences) > 0 {
fmt.Println("Differences found:")
for _, diff := range differences {
fmt.Println(diff)
}
}
fmt.Printf("\nMatching fields: %s\n", strings.Join(matches, ", "))
}
func compareFields(rest, graphql *types.PullRequest) (differences, matches []string) {
restVal := reflect.ValueOf(*rest)
graphqlVal := reflect.ValueOf(*graphql)
restType := restVal.Type()
for i := range restVal.NumField() {
field := restType.Field(i)
restField := restVal.Field(i)
graphqlField := graphqlVal.Field(i)
// Special handling for pointer fields
if restField.Kind() == reflect.Ptr {
if diff := comparePointerField(field.Name, restField, graphqlField); diff != "" {
differences = append(differences, diff)
continue
}
}
// Compare values
if !reflect.DeepEqual(restField.Interface(), graphqlField.Interface()) {
if field.Name == "CheckSummary" {
compareCheckSummary(rest, graphql)
} else {
differences = append(differences, fmt.Sprintf(" %s: REST=%v, GraphQL=%v",
field.Name, restField.Interface(), graphqlField.Interface()))
}
} else {
matches = append(matches, field.Name)
}
}
return differences, matches
}
func comparePointerField(name string, restField, graphqlField reflect.Value) string {
if restField.IsNil() != graphqlField.IsNil() {
return fmt.Sprintf(" %s: REST=%v, GraphQL=%v", name, restField.IsNil(), graphqlField.IsNil())
}
if !restField.IsNil() {
restField.Elem()
graphqlField.Elem()
}
return ""
}
func compareCheckSummary(rest, graphql *types.PullRequest) {
if rest.CheckSummary == nil || graphql.CheckSummary == nil {
return
}
fmt.Println(" CheckSummary:")
fmt.Printf(" REST: Success=%d, Failing=%d, Pending=%d, Cancelled=%d, Skipped=%d, Stale=%d, Neutral=%d\n",
len(rest.CheckSummary.Success), len(rest.CheckSummary.Failing),
len(rest.CheckSummary.Pending), len(rest.CheckSummary.Cancelled),
len(rest.CheckSummary.Skipped), len(rest.CheckSummary.Stale), len(rest.CheckSummary.Neutral))
fmt.Printf(" GraphQL: Success=%d, Failing=%d, Pending=%d, Cancelled=%d, Skipped=%d, Stale=%d, Neutral=%d\n",
len(graphql.CheckSummary.Success), len(graphql.CheckSummary.Failing),
len(graphql.CheckSummary.Pending), len(graphql.CheckSummary.Cancelled),
len(graphql.CheckSummary.Skipped), len(graphql.CheckSummary.Stale), len(graphql.CheckSummary.Neutral))
compareCheckSummaryMaps(rest.CheckSummary, graphql.CheckSummary)
}
func compareCheckSummaryMaps(rest, graphql *types.CheckSummary) {
compareSummaryMap("Success", rest.Success, graphql.Success)
compareSummaryMap("Failing", rest.Failing, graphql.Failing)
compareSummaryMap("Pending", rest.Pending, graphql.Pending)
compareSummaryMap("Cancelled", rest.Cancelled, graphql.Cancelled)
compareSummaryMap("Skipped", rest.Skipped, graphql.Skipped)
compareSummaryMap("Stale", rest.Stale, graphql.Stale)
compareSummaryMap("Neutral", rest.Neutral, graphql.Neutral)
}
func compareSummaryMap(name string, rest, graphql map[string]string) {
if len(rest) > 0 || len(graphql) > 0 {
fmt.Printf(" %s:\n", name)
compareStatusMaps(rest, graphql)
}
}
func compareStatusMaps(rest, graphql map[string]string) {
allKeys := make(map[string]bool)
for k := range rest {
allKeys[k] = true
}
for k := range graphql {
allKeys[k] = true
}
for k := range allKeys {
restVal := rest[k]
graphqlVal := graphql[k]
if restVal != graphqlVal {
fmt.Printf(" %s:\n", k)
fmt.Printf(" REST: %q\n", restVal)
fmt.Printf(" GraphQL: %q\n", graphqlVal)
}
}
}
func compareEvents(restEvents, graphqlEvents []types.Event) {
// Count events by type
restCounts := countEventsByType(restEvents)
graphqlCounts := countEventsByType(graphqlEvents)
fmt.Println("Event counts by type:")
allTypes := make(map[string]bool)
for k := range restCounts {
allTypes[k] = true
}
for k := range graphqlCounts {
allTypes[k] = true
}
var eventTypes []string
for t := range allTypes {
eventTypes = append(eventTypes, t)
}
sort.Strings(eventTypes)
for _, eventType := range eventTypes {
restCount := restCounts[eventType]
graphqlCount := graphqlCounts[eventType]
if restCount != graphqlCount {
fmt.Printf(" %s: REST=%d, GraphQL=%d ❌\n", eventType, restCount, graphqlCount)
} else {
fmt.Printf(" %s: %d ✓\n", eventType, restCount)
}
}
// Total events
fmt.Printf("\nTotal events: REST=%d, GraphQL=%d\n", len(restEvents), len(graphqlEvents))
// Check for missing events
fmt.Println("\n=== Event Details Comparison ===")
// Group events by type for detailed comparison
restByType := groupEventsByType(restEvents)
graphqlByType := groupEventsByType(graphqlEvents)
for _, eventType := range eventTypes {
restTypeEvents := restByType[eventType]
graphqlTypeEvents := graphqlByType[eventType]
if len(restTypeEvents) != len(graphqlTypeEvents) {
fmt.Printf("\n%s events differ:\n", eventType)
fmt.Printf(" REST has %d events\n", len(restTypeEvents))
fmt.Printf(" GraphQL has %d events\n", len(graphqlTypeEvents))
// Show first few differences
maxShow := 3
if len(restTypeEvents) > 0 && len(restTypeEvents) <= maxShow {
fmt.Println(" REST events:")
for i := range restTypeEvents {
if i >= maxShow {
break
}
e := &restTypeEvents[i]
fmt.Printf(" - %s by %s: %s\n", e.Timestamp.Format("2006-01-02 15:04"), e.Actor, truncate(e.Body, truncateDisplayLength))
}
}
if len(graphqlTypeEvents) > 0 && len(graphqlTypeEvents) <= maxShow {
fmt.Println(" GraphQL events:")
for i := range graphqlTypeEvents {
if i >= maxShow {
break
}
e := &graphqlTypeEvents[i]
fmt.Printf(" - %s by %s: %s\n", e.Timestamp.Format("2006-01-02 15:04"), e.Actor, truncate(e.Body, truncateDisplayLength))
}
}
}
}
// Check WriteAccess preservation
fmt.Println("\n=== Write Access Comparison ===")
restWriteAccess := extractWriteAccess(restEvents)
graphqlWriteAccess := extractWriteAccess(graphqlEvents)
for actor, restAccess := range restWriteAccess {
graphqlAccess := graphqlWriteAccess[actor]
if restAccess != graphqlAccess {
fmt.Printf(" %s: REST=%d, GraphQL=%d\n", actor, restAccess, graphqlAccess)
}
}
// Check Bot detection
fmt.Println("\n=== Bot Detection Comparison ===")
restBots := extractBots(restEvents)
graphqlBots := extractBots(graphqlEvents)
allBotActors := make(map[string]bool)
for actor := range restBots {
allBotActors[actor] = true
}
for actor := range graphqlBots {
allBotActors[actor] = true
}
for actor := range allBotActors {
restIsBot := restBots[actor]
graphqlIsBot := graphqlBots[actor]
if restIsBot != graphqlIsBot {
fmt.Printf(" %s: REST=%v, GraphQL=%v\n", actor, restIsBot, graphqlIsBot)
}
}
}
func countEventsByType(events []types.Event) map[string]int {
counts := make(map[string]int)
for i := range events {
counts[events[i].Kind]++
}
return counts
}
func groupEventsByType(events []types.Event) map[string][]types.Event {
grouped := make(map[string][]types.Event)
for i := range events {
grouped[events[i].Kind] = append(grouped[events[i].Kind], events[i])
}
return grouped
}
func extractWriteAccess(events []types.Event) map[string]int {
access := make(map[string]int)
for i := range events {
e := &events[i]
if e.Actor != "" && e.WriteAccess != 0 {
// Keep the highest access level seen
if current, exists := access[e.Actor]; !exists || e.WriteAccess > current {
access[e.Actor] = e.WriteAccess
}
}
}
return access
}
func extractBots(events []types.Event) map[string]bool {
bots := make(map[string]bool)
for i := range events {
e := &events[i]
if e.Actor != "" {
bots[e.Actor] = e.Bot
}
}
return bots
}
func truncate(s string, maxLen int) string {
if len(s) <= maxLen {
return s
}
return s[:maxLen] + "..."
}
func saveJSON(filename string, data any) {
file, err := os.Create(filename)
if err != nil {
log.Printf("Failed to create %s: %v", filename, err)
return
}
defer func() {
if err := file.Close(); err != nil {
log.Printf("Failed to close %s: %v", filename, err)
}
}()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
if err := encoder.Encode(data); err != nil {
log.Printf("Failed to encode %s: %v", filename, err)
}
}