-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
1290 lines (1135 loc) · 36.1 KB
/
main.go
File metadata and controls
1290 lines (1135 loc) · 36.1 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
997
998
999
1000
// Package main implements a GitHub PR notifier CLI tool.
package main
import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"net/url"
"os"
"os/exec"
"os/signal"
"slices"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/charmbracelet/lipgloss"
"github.com/codeGROOVE-dev/fido/pkg/store/localfs"
"github.com/codeGROOVE-dev/retry"
"github.com/codeGROOVE-dev/sprinkler/pkg/client"
"github.com/codeGROOVE-dev/turnclient/pkg/turn"
"golang.org/x/term"
)
// PR represents a GitHub pull request with all relevant information.
type PR struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
TurnResponse *turn.CheckResponse `json:"turn_response,omitempty"`
Repository struct {
FullName string `json:"full_name"`
} `json:"repository"`
Title string `json:"title"`
User User `json:"user"`
State string `json:"state"`
HTMLURL string `json:"html_url"`
RequestedReviewers []User `json:"requested_reviewers"`
ReviewComments int `json:"review_comments"`
Comments int `json:"comments"`
Number int `json:"number"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
ChangedFiles int `json:"changed_files"`
Draft bool `json:"draft"`
}
// User represents a GitHub user.
type User struct {
Login string `json:"login"`
}
// Review represents a pull request review.
type Review struct {
User User `json:"user"`
State string `json:"state"`
}
// SearchResult represents the GitHub search API response.
type SearchResult struct {
Items []PR `json:"items"`
}
// prRefreshTracker tracks the last refresh time for PRs to avoid duplicate API calls.
type prRefreshTracker struct {
lastRefresh map[string]time.Time
mu sync.RWMutex
}
func (r *prRefreshTracker) shouldRefresh(prURL string) bool {
r.mu.RLock()
lastTime, exists := r.lastRefresh[prURL]
r.mu.RUnlock()
if !exists {
return true
}
return time.Since(lastTime) > time.Duration(prRefreshCooldownSecs)*time.Second
}
func (r *prRefreshTracker) markRefreshed(prURL string) {
r.mu.Lock()
r.lastRefresh[prURL] = time.Now()
r.mu.Unlock()
}
const (
defaultTimeout = 30 * time.Second
defaultWatchInterval = 60 * time.Second
maxPerPage = 100
retryAttempts = 3
retryDelay = time.Second
retryMaxDelay = 10 * time.Second
enrichRetries = 2
enrichDelay = 500 * time.Millisecond
enrichMaxDelay = 2 * time.Second
apiUserEndpoint = "https://api.github.com/user"
apiSearchEndpoint = "https://api.github.com/search/issues"
apiPullsEndpoint = "https://api.github.com/repos/%s/%s/pulls/%d"
maxConcurrent = 20 // Increased for better throughput
cacheTTL = 10 * 24 * time.Hour // 10 days
prRefreshCooldownSecs = 1 // Avoid refreshing same PR within 1 second
maxOrgNameLength = 39 // GitHub org name limit
minTokenLength = 10 // Minimum GitHub token length
maxIdleConnsPerHost = 10 // HTTP client setting
idleConnTimeout = 90 * time.Second
minPRURLParts = 6 // Minimum parts in PR URL
minOrgURLParts = 4 // Minimum parts in org URL
repoPartIndex = 4 // Index of repo in URL parts
prTypePartIndex = 5 // Index of PR type in URL parts
numberPartIndex = 6 // Index of PR number in URL parts
prURLParts = 7 // Number of parts in a full PR URL
truncatedURLLength = 80 // Max URL display length
defaultTerminalWidth = 80 // Default terminal width if detection fails
titlePadding = 5 // Space between title and URL
minTitleLength = 20 // Minimum title display length
stalePRDays = 90 // Days before a PR is considered stale
)
func main() {
var (
watch = flag.Bool("watch", false, "Continuously watch for PR updates")
blocked = flag.Bool("blocked", false, "Show only PRs blocking on you")
verbose = flag.Bool("verbose", false, "Show verbose logging from libraries")
excludeOrgs = flag.String("exclude-orgs", "", "Comma-separated list of orgs to exclude")
includeStale = flag.Bool("include-stale", false, "Include PRs that haven't been modified in 90 days")
user = flag.String("user", "", "View PRs for specified user instead of authenticated user")
noCache = flag.Bool("no-cache", false, "Disable caching of Turn API responses")
)
flag.Parse()
// Set up logger
var logger *log.Logger
if *verbose {
logger = log.New(os.Stderr, "[prs] ", log.Ltime)
} else {
logger = log.New(io.Discard, "", 0)
}
// Set up context with cancellation
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Set up HTTP client with optimized settings
httpClient := &http.Client{
Timeout: defaultTimeout,
Transport: &http.Transport{
MaxIdleConns: 100, // Increased for better connection reuse
MaxIdleConnsPerHost: maxIdleConnsPerHost,
IdleConnTimeout: idleConnTimeout,
DisableKeepAlives: false,
DisableCompression: false,
ForceAttemptHTTP2: true, // Use HTTP/2 when available
},
}
token, err := gitHubToken(ctx)
if err != nil {
logger.Printf("ERROR: Failed to get GitHub token: %v", err)
fmt.Fprint(os.Stderr, "error: failed to authenticate with github\n")
cancel()
return
}
logger.Print("INFO: Successfully retrieved GitHub token")
// Determine the username to use - priority: --user flag, GITHUB_USER env, authenticated user
var username string
if *user != "" {
username = *user
logger.Printf("INFO: Using specified user from --user flag: %s", username)
} else if envUser := os.Getenv("GITHUB_USER"); envUser != "" {
username = envUser
logger.Printf("INFO: Using user from GITHUB_USER environment variable: %s", username)
} else {
username, err = currentUser(ctx, token, logger, httpClient)
if err != nil {
logger.Printf("ERROR: Failed to get current user: %v", err)
fmt.Fprint(os.Stderr, "error: failed to identify github user\n")
cancel()
return
}
logger.Printf("INFO: Authenticated as user: %s", username)
}
// Set up turn client
var turnClient *turn.Client
turnClient, err = turn.NewDefaultClient()
if err != nil {
logger.Printf("ERROR: Failed to create turn client: %v", err)
turnClient = nil
} else {
logger.Print("INFO: Connected to turn server")
if *verbose {
turnClient.SetLogger(logger)
}
if token != "" {
turnClient.SetAuthToken(token)
}
}
// Set up cache store
cache, err := localfs.New[string, *turn.CheckResponse]("prs-turn-cache", "")
if err != nil {
logger.Printf("WARNING: Failed to create cache store: %v", err)
cache = nil
}
// Handle interrupts gracefully
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh
logger.Println("Received interrupt signal, shutting down...")
cancel()
}()
// Parse excluded orgs
var excludedOrgs []string
if *excludeOrgs != "" {
excludedOrgs = strings.Split(*excludeOrgs, ",")
for i := range excludedOrgs {
excludedOrgs[i] = strings.TrimSpace(excludedOrgs[i])
}
}
if *watch {
// Watch mode: hybrid WebSocket + polling with smart display updates
cfg := &watchConfig{
token: token,
username: username,
blockingOnly: *blocked,
notifyMode: false,
bell: false,
interval: defaultWatchInterval,
logger: logger,
httpClient: httpClient,
turnClient: turnClient,
cache: cache,
debug: *verbose,
org: "",
includeStale: *includeStale,
excludedOrgs: excludedOrgs,
noCache: *noCache,
}
runWatchMode(ctx, cfg)
} else {
// Default: one-time display
query := &prQuery{
token: token,
username: username,
org: "",
debug: *verbose,
noCache: *noCache,
}
cls := &clients{
http: httpClient,
turn: turnClient,
cache: cache,
}
prs, err := fetchPRsWithRetry(ctx, query, logger, cls)
if err != nil {
if errors.Is(err, context.Canceled) {
fmt.Fprint(os.Stderr, "\nOperation cancelled\n")
} else {
fmt.Fprintf(os.Stderr, "Error fetching PRs: %v\n", err)
}
cancel()
return
}
output := generatePRDisplay(prs, username, *blocked, *includeStale, excludedOrgs)
if output != "" {
fmt.Print(output)
}
}
}
func gitHubToken(ctx context.Context) (string, error) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "gh", "auth", "token")
output, err := cmd.Output()
if err != nil {
if ctx.Err() == context.DeadlineExceeded {
return "", errors.New("timeout getting auth token")
}
return "", fmt.Errorf("failed to get auth token (is 'gh' installed and authenticated?): %w", err)
}
token := strings.TrimSpace(string(output))
if token == "" {
return "", errors.New("empty auth token received")
}
// Basic validation - GitHub tokens should be non-empty alphanumeric strings
if len(token) < minTokenLength {
return "", errors.New("invalid token format")
}
return token, nil
}
func currentUser(ctx context.Context, token string, logger *log.Logger, httpClient *http.Client) (string, error) {
return retry.DoWithData(
func() (string, error) {
logger.Printf("Making API call to GET %s", apiUserEndpoint)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiUserEndpoint, http.NoBody)
if err != nil {
return "", err
}
req.Header.Set("Authorization", "token "+token)
req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("User-Agent", "prs-cli")
resp, err := httpClient.Do(req)
if err != nil {
logger.Printf("HTTP request failed: %v", err)
return "", err
}
defer resp.Body.Close() //nolint:errcheck // Best effort close
if resp.StatusCode == http.StatusUnauthorized {
return "", errors.New("invalid GitHub token")
}
if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
return "", fmt.Errorf("GitHub API error (status %d): %s", resp.StatusCode, body)
}
var user struct {
Login string `json:"login"`
}
if err := json.NewDecoder(resp.Body).Decode(&user); err != nil {
return "", err
}
return user.Login, nil
},
retry.Attempts(retryAttempts),
retry.Delay(retryDelay),
retry.MaxDelay(retryMaxDelay),
retry.DelayType(retry.BackOffDelay),
retry.LastErrorOnly(true),
retry.OnRetry(func(n uint, err error) {
logger.Printf("Retry attempt %d after error: %v", n+1, err)
}),
)
}
func fetchPRsWithRetry(ctx context.Context, query *prQuery, logger *log.Logger, cls *clients) ([]PR, error) {
return retry.DoWithData(
func() ([]PR, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
return fetchPRs(ctx, query, logger, cls)
},
retry.Context(ctx),
retry.Attempts(retryAttempts),
retry.Delay(retryDelay),
retry.MaxDelay(retryMaxDelay),
retry.DelayType(retry.BackOffDelay),
retry.LastErrorOnly(true),
retry.OnRetry(func(n uint, err error) {
logger.Printf("Retry attempt %d for fetchPRs: %v", n+1, err)
}),
)
}
func fetchPRs(ctx context.Context, query *prQuery, logger *log.Logger, cls *clients) ([]PR, error) {
// Query 1: PRs that involve the user (mentioned, assigned, review requested, etc.)
query1 := fmt.Sprintf("is:open is:pr involves:%s archived:false", query.username)
if query.org != "" {
// org is already validated, safe to use
query1 += fmt.Sprintf(" org:%s", query.org)
}
// Query 2: PRs authored by the user
query2 := fmt.Sprintf("is:open is:pr user:%s archived:false", query.username)
if query.org != "" {
query2 += fmt.Sprintf(" org:%s", query.org)
}
// Execute both queries
resp1, err := makeGitHubSearchRequest(ctx, query1, query.token, cls.http, logger)
if err != nil {
return nil, err
}
defer resp1.Body.Close() //nolint:errcheck // Best effort close
prs1, err := parseSearchResponse(resp1)
if err != nil {
return nil, err
}
resp2, err := makeGitHubSearchRequest(ctx, query2, query.token, cls.http, logger)
if err != nil {
return nil, err
}
defer resp2.Body.Close() //nolint:errcheck // Best effort close
prs2, err := parseSearchResponse(resp2)
if err != nil {
return nil, err
}
// Combine results
prs := make([]PR, 0, len(prs1)+len(prs2))
prs = append(prs, prs1...)
prs = append(prs, prs2...)
logger.Printf("Found %d PRs (before deduplication)", len(prs))
prs = deduplicatePRs(prs)
logger.Printf("Found %d PRs (after deduplication)", len(prs))
enrichCfg := &enrichConfig{
httpClient: cls.http,
turnClient: cls.turn,
cache: cls.cache,
logger: logger,
token: query.token,
username: query.username,
debug: query.debug,
noCache: query.noCache,
}
enrichPRsParallel(ctx, prs, enrichCfg)
logger.Printf("INFO: Successfully enriched all %d PRs", len(prs))
// Filter out closed/merged PRs (can happen due to GitHub search lag or stale cache)
openPRs := make([]PR, 0, len(prs))
for i := range prs {
if prs[i].State == "open" {
openPRs = append(openPRs, prs[i])
}
}
if len(openPRs) != len(prs) {
logger.Printf("Filtered out %d closed/merged PRs", len(prs)-len(openPRs))
}
return openPRs, nil
}
func makeGitHubSearchRequest(ctx context.Context, query, token string, httpClient *http.Client, logger *log.Logger) (*http.Response, error) {
params := url.Values{}
params.Add("q", query)
params.Add("per_page", strconv.Itoa(maxPerPage))
params.Add("sort", "updated")
apiURL := fmt.Sprintf("%s?%s", apiSearchEndpoint, params.Encode())
logger.Printf("Making API call to GET %s", apiURL)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, http.NoBody)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "token "+token)
req.Header.Set("Accept", "application/vnd.github.v3+json")
req.Header.Set("User-Agent", "prs-cli")
req.Header.Set("X-Github-Api-Version", "2022-11-28")
start := time.Now()
resp, err := httpClient.Do(req)
elapsed := time.Since(start)
if err != nil {
logger.Printf("ERROR: HTTP request failed after %v: %v", elapsed, err)
if errors.Is(err, context.DeadlineExceeded) {
return nil, fmt.Errorf("request timed out after %v", elapsed)
}
return nil, fmt.Errorf("github api request failed: %w", err)
}
logger.Printf("INFO: GitHub API request completed in %v with status %d", elapsed, resp.StatusCode)
return resp, nil
}
func parseSearchResponse(resp *http.Response) ([]PR, error) {
if resp.StatusCode == http.StatusUnauthorized {
return nil, errors.New("invalid github token")
}
if resp.StatusCode == http.StatusForbidden {
// Check for rate limit headers
if remaining := resp.Header.Get("X-Ratelimit-Remaining"); remaining == "0" {
resetTime := resp.Header.Get("X-Ratelimit-Reset")
return nil, fmt.Errorf("github api rate limit exceeded, resets at %s", resetTime)
}
return nil, fmt.Errorf("github api access forbidden: status %d", resp.StatusCode)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("github api error: status %d", resp.StatusCode)
}
var result SearchResult
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return result.Items, nil
}
func deduplicatePRs(prs []PR) []PR {
seen := make(map[string]PR, len(prs))
for i := range prs {
if existing, exists := seen[prs[i].HTMLURL]; !exists || prs[i].UpdatedAt.After(existing.UpdatedAt) {
seen[prs[i].HTMLURL] = prs[i]
}
}
result := make([]PR, 0, len(seen))
for u := range seen {
result = append(result, seen[u])
}
return result
}
func enrichPRsParallel(ctx context.Context, prs []PR, cfg *enrichConfig) {
// Simple semaphore pattern - Rob Pike style
sem := make(chan struct{}, maxConcurrent)
var wg sync.WaitGroup
for i := range prs {
sem <- struct{}{} // acquire semaphore
pr := &prs[i]
wg.Go(func() {
defer func() {
<-sem // release semaphore
}()
// Ignore non-critical errors - let the app continue
if err := enrichPRData(ctx, pr, cfg); err != nil {
if errors.Is(err, context.Canceled) {
return
}
cfg.logger.Printf("WARNING: Failed to enrich PR #%d: %v", pr.Number, err)
}
})
}
wg.Wait()
}
func fetchPRDetails(ctx context.Context, pr *PR, cfg *enrichConfig) error {
parts := strings.Split(pr.HTMLURL, "/")
if len(parts) < minPRURLParts {
return fmt.Errorf("invalid PR URL format: %s", pr.HTMLURL)
}
owner := parts[3]
repo := parts[repoPartIndex]
apiURL := fmt.Sprintf("https://api.github.com/repos/%s/%s/pulls/%d", owner, repo, pr.Number)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, apiURL, http.NoBody)
if err != nil {
return err
}
req.Header.Set("Authorization", "token "+cfg.token)
req.Header.Set("Accept", "application/vnd.github.v3+json")
resp, err := cfg.httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close() //nolint:errcheck // Best effort close
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("GitHub API returned status %d", resp.StatusCode)
}
var prDetails PR
if err := json.NewDecoder(resp.Body).Decode(&prDetails); err != nil {
return err
}
pr.Additions = prDetails.Additions
pr.Deletions = prDetails.Deletions
pr.ChangedFiles = prDetails.ChangedFiles
if cfg.debug {
cfg.logger.Printf("Fetched PR #%d size: +%d/-%d files:%d", pr.Number, pr.Additions, pr.Deletions, pr.ChangedFiles)
}
return nil
}
func enrichPRData(ctx context.Context, pr *PR, cfg *enrichConfig) error {
start := time.Now()
defer func() {
if cfg.debug {
cfg.logger.Printf("Enriched PR #%d in %v", pr.Number, time.Since(start))
}
}()
// Fetch individual PR data to get size information
if err := fetchPRDetails(ctx, pr, cfg); err != nil {
cfg.logger.Printf("WARNING: Failed to fetch PR details for #%d: %v", pr.Number, err)
// Continue without size info
}
// Enrich with turn server data if available
if cfg.turnClient == nil {
if cfg.debug {
cfg.logger.Printf("Turn client is nil, skipping turn enrichment for PR #%d", pr.Number)
}
return nil
}
return enrichWithTurnData(ctx, pr, cfg)
}
func enrichWithTurnData(ctx context.Context, pr *PR, cfg *enrichConfig) error {
if cfg.debug {
cfg.logger.Printf("enrichWithTurnData called for PR #%d, URL: %s", pr.Number, pr.HTMLURL)
}
// Validate PR URL before sending to turn server
if pr.HTMLURL == "" || !strings.HasPrefix(pr.HTMLURL, "https://github.com/") {
cfg.logger.Printf("WARNING: Invalid PR URL for turn enrichment: %s", pr.HTMLURL)
return nil
}
// Check cache first (unless noCache is enabled or cache is unavailable)
cacheKey := pr.HTMLURL + ":" + pr.UpdatedAt.Format(time.RFC3339)
if !cfg.noCache && cfg.cache != nil {
cached, _, found, err := cfg.cache.Get(ctx, cacheKey)
if err == nil && found {
if cfg.debug {
cfg.logger.Printf("INFO: Cache hit for PR #%d", pr.Number)
}
pr.TurnResponse = cached
return nil
}
if cfg.debug {
cfg.logger.Printf("INFO: Cache miss for PR #%d", pr.Number)
}
} else if cfg.debug {
msg := "Cache unavailable"
if cfg.noCache {
msg = "Cache disabled (--no-cache)"
}
cfg.logger.Printf("INFO: %s for PR #%d", msg, pr.Number)
}
return fetchAndCacheTurnData(ctx, pr, cacheKey, cfg)
}
func fetchAndCacheTurnData(ctx context.Context, pr *PR, cacheKey string, cfg *enrichConfig) error {
turnStart := time.Now()
if cfg.debug {
cfg.logger.Printf("Sending turnclient request for PR #%d: URL=%s, UpdatedAt=%s",
pr.Number, pr.HTMLURL, pr.UpdatedAt.Format(time.RFC3339))
}
turnResponse, err := cfg.turnClient.Check(ctx, pr.HTMLURL, cfg.username, pr.UpdatedAt)
if err != nil {
cfg.logger.Printf("WARNING: Failed to get turn data for PR #%d: %v", pr.Number, err)
return nil
}
if turnResponse == nil {
return nil
}
pr.TurnResponse = turnResponse
if !cfg.noCache && cfg.cache != nil {
expiry := time.Now().Add(cacheTTL)
if err := cfg.cache.Set(ctx, cacheKey, turnResponse, expiry); err != nil && cfg.debug {
cfg.logger.Printf("WARNING: Failed to cache turn data for PR #%d: %v", pr.Number, err)
}
}
if cfg.debug {
cfg.logger.Printf("Turn server call for PR #%d took %v", pr.Number, time.Since(turnStart))
responseJSON, err := json.MarshalIndent(turnResponse, "", " ")
if err != nil {
cfg.logger.Printf("WARNING: Failed to marshal turn response for PR #%d: %v", pr.Number, err)
cfg.logger.Printf("Turn response for PR #%d: Analysis.Tags=%v, NextActions=%d",
pr.Number, turnResponse.Analysis.Tags, len(turnResponse.Analysis.NextAction))
} else {
cfg.logger.Printf("Received turnclient response for PR #%d:\n%s", pr.Number, string(responseJSON))
}
}
return nil
}
func isBlockingOnUser(pr *PR, username string) bool {
// If we have turn client data, use that for blocking determination
if pr.TurnResponse != nil && pr.TurnResponse.Analysis.NextAction != nil {
_, hasAction := pr.TurnResponse.Analysis.NextAction[username]
return hasAction
}
// Fallback to GitHub API requested reviewers if no turn data
for _, reviewer := range pr.RequestedReviewers {
if reviewer.Login == username {
return true
}
}
return false
}
func isCriticalBlocker(pr *PR, username string) bool {
// Check if user has a critical action
if pr.TurnResponse != nil && pr.TurnResponse.Analysis.NextAction != nil {
if action, exists := pr.TurnResponse.Analysis.NextAction[username]; exists {
return action.Critical
}
}
return false
}
func categorizePRs(prs []PR, username string) (incoming, outgoing []PR) {
for i := range prs {
if prs[i].User.Login == username {
outgoing = append(outgoing, prs[i])
} else {
incoming = append(incoming, prs[i])
}
}
return incoming, outgoing
}
func wasBlockingBefore(pr *PR, previous []PR, username string) bool {
for i := range previous {
if previous[i].Number == pr.Number && previous[i].Repository.FullName == pr.Repository.FullName {
return isBlockingOnUser(&previous[i], username)
}
}
return false
}
func truncate(s string, n int) string {
if len(s) <= n {
return s
}
return s[:n-3] + "..."
}
func orgFromURL(urlStr string) string {
parts := strings.Split(urlStr, "/")
if len(parts) >= minOrgURLParts && strings.Contains(urlStr, "github.com") {
return parts[3]
}
return ""
}
// clients holds HTTP and Turn clients.
type clients struct {
http *http.Client
turn *turn.Client
cache *localfs.Store[string, *turn.CheckResponse]
}
// enrichConfig holds configuration for PR enrichment.
type enrichConfig struct {
httpClient *http.Client
turnClient *turn.Client
cache *localfs.Store[string, *turn.CheckResponse]
logger *log.Logger
token string
username string
debug bool
noCache bool
}
// prQuery holds parameters for PR queries.
type prQuery struct {
token string
username string
org string
debug bool
noCache bool
}
// displayConfig holds configuration for updateDisplay.
type displayConfig struct {
logger *log.Logger
httpClient *http.Client
turnClient *turn.Client
cache *localfs.Store[string, *turn.CheckResponse]
lastDisplayHash *string
token string
username string
excludedOrgs []string
blockingOnly bool
verbose bool
includeStale bool
force bool
noCache bool
}
// watchConfig holds configuration for watch mode.
type watchConfig struct {
httpClient *http.Client
turnClient *turn.Client
cache *localfs.Store[string, *turn.CheckResponse]
logger *log.Logger
org string
token string
username string
excludedOrgs []string
interval time.Duration
blockingOnly bool
notifyMode bool
bell bool
debug bool
includeStale bool
noCache bool
}
func runWatchMode(ctx context.Context, cfg *watchConfig) {
cfg.logger.Printf("Starting watch mode with WebSocket + polling")
// Track last displayed output to detect changes
var lastDisplayHash string
// Create refresh tracker to prevent duplicate API calls
refreshTracker := &prRefreshTracker{
lastRefresh: make(map[string]time.Time),
}
// Channel to trigger display updates
updateChan := make(chan bool, 10)
// Initial display
displayCfg := &displayConfig{
logger: cfg.logger,
httpClient: cfg.httpClient,
turnClient: cfg.turnClient,
cache: cfg.cache,
lastDisplayHash: &lastDisplayHash,
excludedOrgs: cfg.excludedOrgs,
token: cfg.token,
username: cfg.username,
blockingOnly: cfg.blockingOnly,
verbose: cfg.debug,
includeStale: cfg.includeStale,
force: true,
noCache: cfg.noCache,
}
err := updateDisplay(ctx, displayCfg)
if err != nil {
cfg.logger.Printf("ERROR: Initial display failed: %v", err)
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
return
}
// Start WebSocket monitoring
go func() {
// Create a custom logger for sprinkler client
var sprinklerLogger *slog.Logger
if cfg.debug {
// Use stderr with text handler for verbose mode
sprinklerLogger = slog.New(slog.NewTextHandler(os.Stderr, nil))
} else {
// Discard all logs in non-verbose mode
sprinklerLogger = slog.New(slog.DiscardHandler)
}
config := client.Config{
ServerURL: "wss://" + client.DefaultServerAddress + "/ws",
Token: cfg.token,
Organization: "*",
EventTypes: []string{"*"},
UserEventsOnly: false,
Verbose: cfg.debug,
NoReconnect: false,
Logger: sprinklerLogger,
OnConnect: func() {
cfg.logger.Println("✓ WebSocket connected")
},
OnDisconnect: func(err error) {
cfg.logger.Printf("WebSocket disconnected: %v", err)
},
OnEvent: func(event client.Event) {
if event.Type == "pull_request" && event.URL != "" {
if refreshTracker.shouldRefresh(event.URL) {
refreshTracker.markRefreshed(event.URL)
cfg.logger.Printf("WebSocket event: %s", event.URL)
select {
case updateChan <- true:
default: // Don't block if channel is full
}
}
}
},
}
wsClient, err := client.New(config)
if err != nil {
cfg.logger.Printf("WARNING: Failed to create WebSocket client: %v", err)
return
}
if err := wsClient.Start(ctx); err != nil && !errors.Is(err, context.Canceled) {
cfg.logger.Printf("WARNING: WebSocket client error: %v", err)
}
}()
// Start polling
go func() {
ticker := time.NewTicker(defaultWatchInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
select {
case updateChan <- true:
cfg.logger.Println("Polling trigger")
default:
}
}
}
}()
// Process updates
for {
select {
case <-ctx.Done():
fmt.Println("\nShutting down...")
return
case <-updateChan:
displayCfg.force = false
err := updateDisplay(ctx, displayCfg)
if err != nil {
if !errors.Is(err, context.Canceled) {
cfg.logger.Printf("ERROR: Update failed: %v", err)
}
}
}
}
}
func updateDisplay(ctx context.Context, cfg *displayConfig) error {
// Fetch current PRs
query := &prQuery{
token: cfg.token,
username: cfg.username,
org: "",
debug: cfg.verbose,
noCache: cfg.noCache,
}
cls := &clients{
http: cfg.httpClient,
turn: cfg.turnClient,
cache: cfg.cache,
}
prs, err := fetchPRsWithRetry(ctx, query, cfg.logger, cls)
if err != nil {
return err
}
// Generate display output
output := generatePRDisplay(prs, cfg.username, cfg.blockingOnly, cfg.includeStale, cfg.excludedOrgs)
// Check if display has changed
currentHash := fmt.Sprintf("%x", sha256.Sum256([]byte(output)))
if !cfg.force && currentHash == *cfg.lastDisplayHash {
cfg.logger.Println("No changes to display")
return nil
}
// Clear screen and display
fmt.Print("\033[H\033[2J")
fmt.Print(output)
*cfg.lastDisplayHash = currentHash
return nil
}
// filterExcludedOrgs removes PRs from excluded organizations.
func filterExcludedOrgs(prs []PR, excludedOrgs []string) []PR {
if len(excludedOrgs) == 0 {
return prs
}
filtered := make([]PR, 0, len(prs))
for i := range prs {