-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjenkins-pr-collector.go
More file actions
996 lines (873 loc) · 28.5 KB
/
jenkins-pr-collector.go
File metadata and controls
996 lines (873 loc) · 28.5 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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
package main
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
"strings"
"sync"
"time"
"golang.org/x/oauth2"
"golang.org/x/time/rate"
)
func init() {
// Seed the random number generator for more unpredictable jitter
rand.Seed(time.Now().UnixNano())
}
// PullRequest represents a GitHub pull request
type PullRequest struct {
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
URL string `json:"url"`
Repository struct {
Name string `json:"name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
} `json:"repository"`
Author struct {
Login string `json:"login"`
} `json:"author"`
BodyText string `json:"bodyText"`
Labels struct {
Nodes []struct {
Name string `json:"name"`
} `json:"nodes"`
} `json:"labels"`
Commits struct {
Nodes []struct {
Commit struct {
StatusCheckRollup struct {
State string `json:"state"`
} `json:"statusCheckRollup"`
} `json:"commit"`
} `json:"nodes"`
} `json:"commits"`
}
// GraphQLSearchResponse represents the response structure for the search query
// Update GraphQLSearchResponse struct
type GraphQLSearchResponse struct {
Search struct {
PageInfo struct {
HasNextPage bool `json:"hasNextPage"`
EndCursor string `json:"endCursor"`
} `json:"pageInfo"`
Nodes []struct {
// Remove the nested PullRequest struct and flatten the fields
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
URL string `json:"url"`
Repository struct {
Name string `json:"name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
} `json:"repository"`
Author struct {
Login string `json:"login"`
} `json:"author"`
BodyText string `json:"bodyText"`
Labels struct {
Nodes []struct {
Name string `json:"name"`
} `json:"nodes"`
} `json:"labels"`
Commits struct {
Nodes []struct {
Commit struct {
StatusCheckRollup struct {
State string `json:"state"`
} `json:"statusCheckRollup"`
} `json:"commit"`
} `json:"nodes"`
} `json:"commits"`
} `json:"nodes"`
} `json:"search"`
Errors []struct {
Message string `json:"message"`
} `json:"errors,omitempty"`
}
// PullRequestData represents the data we want to collect about PRs
type PullRequestData struct {
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
User string `json:"user"`
Repository string `json:"repository"`
PluginName string `json:"pluginName"`
Labels []string `json:"labels"`
URL string `json:"url"`
Description string `json:"description,omitempty"`
CheckStatus string `json:"checkStatus,omitempty"`
}
// PluginInfo represents the information we need from the plugins.json file
type PluginInfo struct {
Name string `json:"name"`
SCM string `json:"scm"`
}
// UpdateCenter represents the structure of the update-center.json file
type UpdateCenter struct {
Plugins map[string]struct {
Name string `json:"name"`
SCM string `json:"scm"`
} `json:"plugins"`
}
// Config holds the application configuration
type Config struct {
GithubToken string
StartDate time.Time
EndDate time.Time
OutputFile string
FoundPullRequestsFile string
UpdateCenterURL string
RateLimit rate.Limit
}
// GraphQLClient represents a simple GitHub GraphQL API client
type GraphQLClient struct {
httpClient *http.Client
endpoint string
}
// GraphQLRequest represents a GitHub GraphQL API request
type GraphQLRequest struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}
// GraphQLResponse represents a GitHub GraphQL API response
type GraphQLResponse struct {
Data json.RawMessage `json:"data"`
Errors []GraphQLError `json:"errors,omitempty"`
}
// GraphQLError represents a GitHub GraphQL API error
type GraphQLError struct {
Message string `json:"message"`
Type string `json:"type"`
Path []string `json:"path,omitempty"`
}
// Add these new types for partial data saving
type PartialData struct {
LastCursor string `json:"last_cursor"`
PRs []PullRequest `json:"prs"`
Timestamp time.Time `json:"timestamp"`
}
var allFoundPRs []PullRequestData
// Add these new types and constants
const (
maxRetries = 5
baseDelay = 2 * time.Second
maxDelay = 5 * time.Minute
)
type RetryableError struct {
Err error
ShouldLog bool
}
func (e *RetryableError) Error() string {
return e.Err.Error()
}
func calculateBackoffDuration(attempt int) time.Duration {
// Calculate exponential backoff with jitter
delay := baseDelay * time.Duration(1<<uint(attempt))
if delay > maxDelay {
delay = maxDelay
}
// Add jitter (±10%)
jitter := time.Duration(rand.Float64()*0.2-0.1) * delay
return delay + jitter
}
func isRateLimitError(err error) bool {
if err == nil {
return false
}
errStr := strings.ToLower(err.Error())
return strings.Contains(errStr, "rate limit") ||
strings.Contains(errStr, "rate_limit") ||
strings.Contains(errStr, "secondary rate limit")
}
func isTransientError(err error) bool {
if err == nil {
return false
}
errStr := strings.ToLower(err.Error())
return strings.Contains(errStr, "timeout") ||
strings.Contains(errStr, "temporary") ||
strings.Contains(errStr, "500") ||
strings.Contains(errStr, "502") ||
strings.Contains(errStr, "503") ||
strings.Contains(errStr, "504") ||
strings.Contains(errStr, "connection") ||
isRateLimitError(err)
}
func main() {
// Parse command line arguments
githubToken := flag.String("token", os.Getenv("GITHUB_TOKEN"), "GitHub API token")
startDateFlag := flag.String("start", "", "Start date in YYYY-MM-DD format")
endDateFlag := flag.String("end", "", "End date in YYYY-MM-DD format")
outputFileFlag := flag.String("output", "jenkins_prs.json", "Output file name")
foundPRsFileFlag := flag.String("found-prs", "found_prs.json", "File to write found PRs")
updateCenterURLFlag := flag.String("update-center", "https://updates.jenkins.io/current/update-center.actual.json", "Jenkins update center URL")
flag.Parse()
// Validate required parameters
if *githubToken == "" {
log.Fatal("GitHub token is required. Set GITHUB_TOKEN environment variable or use -token flag.")
}
// Parse dates
startDate, err := time.Parse("2006-01-02", *startDateFlag)
if err != nil {
log.Fatalf("Invalid start date format. Expected YYYY-MM-DD: %v", err)
}
log.Printf("Parsed start date: %s", startDate.Format("2006-01-02"))
endDate, err := time.Parse("2006-01-02", *endDateFlag)
if err != nil {
log.Fatalf("Invalid end date format. Expected YYYY-MM-DD: %v", err)
}
log.Printf("Parsed end date: %s", endDate.Format("2006-01-02"))
// Make sure endDate is inclusive by setting it to the end of the day
endDate = endDate.Add(24*time.Hour - 1*time.Second)
// Create configuration
config := Config{
GithubToken: *githubToken,
StartDate: startDate,
EndDate: endDate,
OutputFile: *outputFileFlag,
FoundPullRequestsFile: *foundPRsFileFlag,
UpdateCenterURL: *updateCenterURLFlag,
RateLimit: rate.Limit(1), // 1 request per second is conservative
}
// Initialize GraphQL client
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.GithubToken},
)
tc := oauth2.NewClient(ctx, ts)
graphqlClient := &GraphQLClient{
httpClient: tc,
endpoint: "https://api.github.com/graphql",
}
// Create a rate limiter
limiter := rate.NewLimiter(config.RateLimit, 1)
// Fetch Jenkins plugin repositories from update center
log.Println("Fetching Jenkins plugin information from update center...")
pluginRepos, err := fetchJenkinsPluginInfo(config.UpdateCenterURL)
if err != nil {
log.Fatalf("Failed to fetch plugin information: %v", err)
}
log.Printf("Found %d plugins in the update center", len(pluginRepos))
// Fetch PRs using GraphQL
log.Println("Fetching pull requests using GraphQL...")
pullRequests, err := fetchPullRequestsGraphQL(ctx, graphqlClient, limiter, config, pluginRepos)
if err != nil {
log.Fatalf("Failed to fetch pull requests: %v", err)
}
log.Printf("Found %d pull requests", len(pullRequests))
// Write results to file
log.Printf("Writing results to %s...", config.OutputFile)
err = writeJSONFile(config.OutputFile, pullRequests)
if err != nil {
log.Fatalf("Failed to write output file: %v", err)
}
// Write found PRs to another file if any PRs were found
if len(allFoundPRs) > 0 {
log.Printf("Writing all found PRs to %s...", config.FoundPullRequestsFile)
err = writeJSONFile(config.FoundPullRequestsFile, allFoundPRs)
if err != nil {
log.Fatalf("Failed to write found PRs file: %v", err)
}
} else {
log.Printf("No pull requests found, not writing to %s", config.FoundPullRequestsFile)
}
log.Println("Done!")
}
// fetchJenkinsPluginInfo fetches plugin information from the Jenkins update center
func fetchJenkinsPluginInfo(updateCenterURL string) (map[string]PluginInfo, error) {
// Create an HTTP client that follows redirects
client := &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return nil
},
}
var resp *http.Response
var err error
// Implement retry logic with exponential backoff
for attempt := 0; attempt < 5; attempt++ {
// Make HTTP request to update center
resp, err = client.Get(updateCenterURL)
if err == nil && resp.StatusCode == http.StatusOK {
break
}
if err != nil {
log.Printf("Failed to fetch update center data (attempt %d/5): %v", attempt+1, err)
} else {
log.Printf("HTTP error (attempt %d/5): %d", attempt+1, resp.StatusCode)
}
// Wait before retry
waitTime := time.Duration(attempt+1) * time.Second * 2
time.Sleep(waitTime)
}
if err != nil {
return nil, fmt.Errorf("failed to fetch update center data after retries: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to fetch update center data: HTTP %d", resp.StatusCode)
}
// Read response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read update center data: %v", err)
}
// The update-center.json starts with submitUpdateCenter(...) and ends with );
// We need to extract the JSON part
jsonStr := string(body)
if strings.HasPrefix(jsonStr, "updateCenter.post(") {
// Find the first {
startIdx := strings.Index(jsonStr, "{")
// Find the last }
endIdx := strings.LastIndex(jsonStr, "}")
if startIdx >= 0 && endIdx >= 0 && endIdx > startIdx {
jsonStr = jsonStr[startIdx : endIdx+1]
} else {
return nil, fmt.Errorf("invalid update center JSON format")
}
}
// Parse JSON
var updateCenter UpdateCenter
err = json.Unmarshal([]byte(jsonStr), &updateCenter)
if err != nil {
return nil, fmt.Errorf("failed to parse update center data: %v", err)
}
// Extract plugin information
pluginRepos := make(map[string]PluginInfo)
for name, plugin := range updateCenter.Plugins {
if plugin.SCM != "" {
// Extract repository name from SCM URL
// SCM URL format: https://github.com/jenkinsci/repo-name
repoURL := plugin.SCM
// Make sure it's a GitHub repository in jenkinsci organization
if strings.Contains(repoURL, "github.com/jenkinsci/") {
parts := strings.Split(repoURL, "github.com/jenkinsci/")
if len(parts) > 1 {
repoName := strings.TrimSuffix(parts[1], ".git")
repoName = strings.TrimSuffix(repoName, "/")
pluginRepos[repoName] = PluginInfo{
Name: name,
SCM: repoURL,
}
}
}
}
}
return pluginRepos, nil
}
// Modify ExecuteGraphQL to handle retries internally
func (c *GraphQLClient) ExecuteGraphQL(ctx context.Context, req *GraphQLRequest, result interface{}) error {
var lastErr error
for attempt := 0; attempt < maxRetries; attempt++ {
if attempt > 0 {
waitTime := calculateBackoffDuration(attempt - 1)
log.Printf("Retrying request (attempt %d/%d) after %v...", attempt+1, maxRetries, waitTime)
time.Sleep(waitTime)
}
err := c.executeGraphQLRequest(ctx, req, result)
if err == nil {
return nil
}
lastErr = err
// Check if it's a retryable error
if !isTransientError(err) {
log.Printf("Non-retryable error encountered: %v", err)
return err
}
// Handle rate limit specifically
if isRateLimitError(err) {
log.Printf("Rate limit exceeded on attempt %d/%d", attempt+1, maxRetries)
// Use a longer backoff for rate limits
waitTime := calculateBackoffDuration(attempt) * 2
log.Printf("Rate limit exceeded, waiting %v before retry...", waitTime)
time.Sleep(waitTime)
continue
}
log.Printf("Retryable error encountered on attempt %d/%d: %v", attempt+1, maxRetries, err)
}
return fmt.Errorf("failed after %d retries, last error: %v", maxRetries, lastErr)
}
func (c *GraphQLClient) executeGraphQLRequest(ctx context.Context, req *GraphQLRequest, result interface{}) error {
// Convert request to JSON
reqBody, err := json.Marshal(req)
if err != nil {
return fmt.Errorf("failed to marshal request: %v", err)
}
// Create HTTP request
httpReq, err := http.NewRequestWithContext(ctx, "POST", c.endpoint, bytes.NewBuffer(reqBody))
if err != nil {
return fmt.Errorf("failed to create request: %v", err)
}
httpReq.Header.Set("Content-Type", "application/json")
// Execute request
resp, err := c.httpClient.Do(httpReq)
if err != nil {
return &RetryableError{Err: fmt.Errorf("failed to execute request: %v", err), ShouldLog: true}
}
defer resp.Body.Close()
// Check response status
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
err := fmt.Errorf("unexpected status code %d: %s", resp.StatusCode, string(body))
// Check for specific status codes
switch resp.StatusCode {
case http.StatusUnauthorized, http.StatusForbidden:
return fmt.Errorf("authentication error: %v", err)
case http.StatusNotFound:
return fmt.Errorf("resource not found: %v", err)
case http.StatusTooManyRequests:
return &RetryableError{Err: fmt.Errorf("rate limit exceeded: %v", err), ShouldLog: true}
case http.StatusInternalServerError,
http.StatusBadGateway,
http.StatusServiceUnavailable,
http.StatusGatewayTimeout:
return &RetryableError{Err: fmt.Errorf("server error: %v", err), ShouldLog: true}
default:
return fmt.Errorf("request failed: %v", err)
}
}
// Parse response
var graphqlResp GraphQLResponse
if err := json.NewDecoder(resp.Body).Decode(&graphqlResp); err != nil {
return fmt.Errorf("failed to decode response: %v", err)
}
// Check for GraphQL errors
if len(graphqlResp.Errors) > 0 {
// Check if any of the errors are rate limit related
for _, gqlErr := range graphqlResp.Errors {
if isRateLimitError(fmt.Errorf(gqlErr.Message)) {
return &RetryableError{
Err: fmt.Errorf("graphql rate limit error: %q", gqlErr.Message),
ShouldLog: true,
}
}
}
// If we get here, these are other GraphQL errors
var errMsgs []string
for _, err := range graphqlResp.Errors {
errMsgs = append(errMsgs, err.Message)
}
return fmt.Errorf("graphql errors: %s", strings.Join(errMsgs, "; "))
}
// Decode the actual result
if err := json.Unmarshal(graphqlResp.Data, result); err != nil {
return fmt.Errorf("failed to unmarshal data: %v", err)
}
return nil
}
func getCommitStatus(commits struct {
Nodes []struct {
Commit struct {
StatusCheckRollup struct {
State string `json:"state"`
} `json:"statusCheckRollup"`
} `json:"commit"`
} `json:"nodes"`
}) string {
if len(commits.Nodes) == 0 {
return "UNKNOWN"
}
if commits.Nodes[0].Commit.StatusCheckRollup.State == "" {
return "UNKNOWN"
}
return commits.Nodes[0].Commit.StatusCheckRollup.State
}
// Add function to save partial data
func savePartialData(prs []PullRequest, cursor string, outputFile string) error {
partial := PartialData{
LastCursor: cursor,
PRs: prs,
Timestamp: time.Now(),
}
// Create temp file
tmpFile := outputFile + ".tmp"
data, err := json.MarshalIndent(partial, "", " ")
if err != nil {
return fmt.Errorf("error marshaling partial data: %v", err)
}
if err := os.WriteFile(tmpFile, data, 0644); err != nil {
return fmt.Errorf("error writing partial data: %v", err)
}
// Atomic rename
if err := os.Rename(tmpFile, outputFile+".partial"); err != nil {
return fmt.Errorf("error saving partial data: %v", err)
}
return nil
}
// Add function to load partial data
func loadPartialData(outputFile string) (*PartialData, error) {
partialFile := outputFile + ".partial"
data, err := os.ReadFile(partialFile)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, fmt.Errorf("error reading partial data: %v", err)
}
var partial PartialData
if err := json.Unmarshal(data, &partial); err != nil {
return nil, fmt.Errorf("error parsing partial data: %v", err)
}
// Check if partial data is too old (> 24 hours)
if time.Since(partial.Timestamp) > 24*time.Hour {
os.Remove(partialFile)
return nil, nil
}
return &partial, nil
}
// Modify executeGraphQLQuery to use exponential backoff
func executeGraphQLQuery(client *http.Client, query string, variables map[string]interface{}) (*GraphQLSearchResponse, error) {
var lastErr error
maxAttempts := 10 // Increase max attempts
for attempt := 0; attempt < maxAttempts; attempt++ {
resp, err := sendGraphQLRequest(client, query, variables)
if err == nil {
// Check for specific error types in the response
if resp.Search.Nodes == nil && len(resp.Errors) > 0 {
// Handle specific GitHub API errors
if strings.Contains(resp.Errors[0].Message, "rate limit") {
waitTime := calculateBackoffDuration(attempt)
log.Printf("Rate limit hit. Waiting %v before retry...", waitTime)
time.Sleep(waitTime)
continue
}
if strings.Contains(resp.Errors[0].Message, "Something went wrong") {
waitTime := calculateBackoffDuration(attempt)
log.Printf("GitHub API error. Waiting %v before retry...", waitTime)
time.Sleep(waitTime)
continue
}
}
return resp, nil
}
lastErr = err
waitTime := calculateBackoffDuration(attempt)
log.Printf("Error executing query (attempt %d/%d): %v. Waiting %v...",
attempt+1, maxAttempts, err, waitTime)
time.Sleep(waitTime)
}
return nil, fmt.Errorf("failed after %d attempts: %v", maxAttempts, lastErr)
}
// Modify fetchPullRequests to use partial data
func fetchPullRequests(client *http.Client, startDate, endDate string, outputFile string) error {
var allPRs []PullRequest
var cursor string
// Try to load partial data
partial, err := loadPartialData(outputFile)
if err != nil {
log.Printf("Warning: Could not load partial data: %v", err)
} else if partial != nil {
log.Printf("Resuming from cursor: %s with %d PRs", partial.LastCursor, len(partial.PRs))
allPRs = partial.PRs
cursor = partial.LastCursor
}
for {
variables := map[string]interface{}{
"queryString": buildSearchQuery(startDate, endDate),
"cursor": cursor,
}
resp, err := executeGraphQLQuery(client, searchQuery, variables)
if err != nil {
return fmt.Errorf("error executing query: %v", err)
}
// Process the page of results
for _, node := range resp.Search.Nodes {
pr := convertNodeToPR(node)
allPRs = append(allPRs, pr)
}
// Save partial data after each successful page
if err := savePartialData(allPRs, cursor, outputFile); err != nil {
log.Printf("Warning: Could not save partial data: %v", err)
}
if !resp.Search.PageInfo.HasNextPage {
break
}
cursor = resp.Search.PageInfo.EndCursor
log.Printf("Fetched %d PRs so far...", len(allPRs))
}
// Save final data
finalData, err := json.MarshalIndent(allPRs, "", " ")
if err != nil {
return fmt.Errorf("error marshaling final data: %v", err)
}
if err := os.WriteFile(outputFile, finalData, 0644); err != nil {
return fmt.Errorf("error writing final data: %v", err)
}
// Clean up partial file
os.Remove(outputFile + ".partial")
return nil
}
// writeJSONFile writes data to a JSON file
func writeJSONFile(filename string, data interface{}) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
encoder.SetIndent("", " ")
return encoder.Encode(data)
}
// Add these helper functions
func sendGraphQLRequest(client *http.Client, query string, variables map[string]interface{}) (*GraphQLSearchResponse, error) {
// Prepare the request body
requestBody := map[string]interface{}{
"query": query,
"variables": variables,
}
jsonBody, err := json.Marshal(requestBody)
if err != nil {
return nil, fmt.Errorf("error marshaling request: %v", err)
}
req, err := http.NewRequest("POST", "https://api.github.com/graphql", bytes.NewBuffer(jsonBody))
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}
// Add GitHub token header
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return nil, fmt.Errorf("GITHUB_TOKEN environment variable not set")
}
req.Header.Set("Authorization", "Bearer "+token)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("HTTP error: %d, Body: %s", resp.StatusCode, string(body))
}
var response GraphQLSearchResponse
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, fmt.Errorf("error decoding response: %v", err)
}
return &response, nil
}
func buildSearchQuery(startDate, endDate string) string {
return fmt.Sprintf("org:jenkinsci is:pr created:%s..%s", startDate, endDate)
}
// GraphQL query for searching PRs
const searchQuery = `
query SearchPullRequests($queryString: String!, $cursor: String) {
search(query: $queryString, type: ISSUE, first: 100, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
nodes {
... on PullRequest {
number
title
state
createdAt
updatedAt
url
repository {
name
owner {
login
}
}
author {
login
}
bodyText
labels(first: 100) {
nodes {
name
}
}
commits(last: 1) {
nodes {
commit {
statusCheckRollup {
state
}
}
}
}
}
}
}
}`
func convertNodeToPR(node struct {
Number int `json:"number"`
Title string `json:"title"`
State string `json:"state"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
URL string `json:"url"`
Repository struct {
Name string `json:"name"`
Owner struct {
Login string `json:"login"`
} `json:"owner"`
} `json:"repository"`
Author struct {
Login string `json:"login"`
} `json:"author"`
BodyText string `json:"bodyText"`
Labels struct {
Nodes []struct {
Name string `json:"name"`
} `json:"nodes"`
} `json:"labels"`
Commits struct {
Nodes []struct {
Commit struct {
StatusCheckRollup struct {
State string `json:"state"`
} `json:"statusCheckRollup"`
} `json:"commit"`
} `json:"nodes"`
} `json:"commits"`
}) PullRequest {
pr := PullRequest{
Number: node.Number,
Title: node.Title,
State: node.State,
CreatedAt: node.CreatedAt,
UpdatedAt: node.UpdatedAt,
URL: node.URL,
Repository: node.Repository,
Author: node.Author,
BodyText: node.BodyText,
Labels: node.Labels,
Commits: node.Commits,
}
return pr
}
// Add fetchPullRequestsGraphQL function
func fetchPullRequestsGraphQL(ctx context.Context, client *GraphQLClient, limiter *rate.Limiter, config Config, pluginRepos map[string]PluginInfo) ([]PullRequestData, error) {
var allPRs []PullRequestData
// mutex protects concurrent access to shared data structures:
// - allPRs: The slice containing all collected PRs that match our criteria
// - allFoundPRs: The global slice containing all PRs found during collection
// This mutex is necessary because multiple goroutines may be processing PRs concurrently
// when handling paginated GraphQL responses, and we need to ensure thread-safe
// appending to these slices to prevent race conditions and data corruption.
var mutex sync.Mutex
var lastError error
// Split the date range into monthly chunks
startDate := config.StartDate
endDate := config.EndDate
for startDate.Before(endDate) {
// Calculate the end of the current month
currentEndDate := startDate.AddDate(0, 1, -startDate.Day())
if currentEndDate.After(endDate) {
currentEndDate = endDate
}
// GitHub search query format for PRs
queryString := fmt.Sprintf("org:jenkinsci is:pr created:%s..%s",
startDate.Format("2006-01-02"),
currentEndDate.Format("2006-01-02"))
// Variables for the GraphQL query
variables := map[string]interface{}{
"queryString": queryString,
"cursor": nil,
}
hasNextPage := true
for hasNextPage {
// Respect rate limit
if err := limiter.Wait(ctx); err != nil {
log.Printf("Warning: Rate limit error: %v", err)
time.Sleep(5 * time.Second)
continue
}
var response GraphQLSearchResponse
err := client.ExecuteGraphQL(ctx, &GraphQLRequest{
Query: searchQuery,
Variables: variables,
}, &response)
if err != nil {
log.Printf("Warning: GraphQL query error: %v", err)
lastError = err
// Save what we have so far before continuing
if len(allPRs) > 0 {
if err := writeJSONFile(config.OutputFile+".partial", allPRs); err != nil {
log.Printf("Warning: Failed to save partial results: %v", err)
}
}
time.Sleep(5 * time.Second)
continue
}
// Process search results
for _, pr := range response.Search.Nodes {
repoName := pr.Repository.Name
pluginInfo, isPlugin := pluginRepos[repoName]
// Add all found PRs to the global array
prData := PullRequestData{
Number: pr.Number,
Title: pr.Title,
State: pr.State,
CreatedAt: pr.CreatedAt,
UpdatedAt: pr.UpdatedAt,
User: pr.Author.Login,
Repository: fmt.Sprintf("%s/%s", pr.Repository.Owner.Login, pr.Repository.Name),
PluginName: pluginInfo.Name,
Labels: []string{},
URL: pr.URL,
Description: pr.BodyText,
CheckStatus: getCommitStatus(pr.Commits),
}
mutex.Lock()
allFoundPRs = append(allFoundPRs, prData)
mutex.Unlock()
// Only process plugin repositories
if !isPlugin {
continue
}
// Filter out PRs created by Dependabot and Renovate
if pr.Author.Login == "dependabot" || pr.Author.Login == "renovate" {
continue
}
// Check if "odernizer" can be found in the PR body
if strings.Contains(pr.BodyText, "odernizer") || strings.Contains(pr.BodyText, "recipe") {
// Collect labels
var labels []string
for _, label := range pr.Labels.Nodes {
labels = append(labels, label.Name)
}
prData.Labels = labels
mutex.Lock()
allPRs = append(allPRs, prData)
mutex.Unlock()
}
}
// Check if there are more pages
hasNextPage = response.Search.PageInfo.HasNextPage
if hasNextPage {
variables["cursor"] = response.Search.PageInfo.EndCursor
}
}
// Move to the next month
startDate = currentEndDate.AddDate(0, 0, 1)
}
// If we have any results but also had errors, return what we have
if len(allPRs) > 0 && lastError != nil {
log.Printf("Warning: Completed with partial results due to errors: %v", lastError)
return allPRs, nil
}
if lastError != nil {
return nil, lastError
}
return allPRs, nil
}