| 1 | package cmd |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "log/slog" |
| 7 | "sort" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | "time" |
| 12 | |
| 13 | gh "the-valley/flow/internal/github" |
| 14 | ) |
| 15 | |
| 16 | // ghPoller reads the GitHub status of tracked PRs at a cost that doesn't grow |
| 17 | // with their number: one GraphQL query per repository per poll, plus a |
| 18 | // compare for a PR whose head or base moved since its last one. A rate limit |
| 19 | // pauses it until GitHub says to try again. A PR it can't read in full is |
| 20 | // left out of a poll, keeping its last status, rather than reported with |
| 21 | // unknown values the pipeline would act on. |
| 22 | type ghPoller struct { |
| 23 | client gh.Client |
| 24 | trusted []string // trusted reviewers; their review activity is read only when there are some |
| 25 | |
| 26 | mu sync.Mutex |
| 27 | until time.Time // GitHub rate-limited polling until then |
| 28 | behind map[string]behindAt // PR URL -> its last compare |
| 29 | } |
| 30 | |
| 31 | // behindAt is how many commits a PR's base had that its head lacked, at the |
| 32 | // head and base compared. |
| 33 | type behindAt struct { |
| 34 | head, base string |
| 35 | n int |
| 36 | } |
| 37 | |
| 38 | // trackedPR is a PR a poll asks about. |
| 39 | type trackedPR struct { |
| 40 | url, number string |
| 41 | } |
| 42 | |
| 43 | func newGHPoller(client gh.Client, trusted []string) *ghPoller { |
| 44 | return &ghPoller{client: client, trusted: trusted, behind: make(map[string]behindAt)} |
| 45 | } |
| 46 | |
| 47 | // poll returns the statuses it read, keyed by PR URL, and the time polling |
| 48 | // is paused until, zero when it isn't. |
| 49 | func (p *ghPoller) poll(ctx context.Context, prs []trackedPR) (map[string]*prStatus, time.Time) { |
| 50 | statuses := make(map[string]*prStatus) |
| 51 | bySlug := make(map[string]map[int]string) // slug -> PR number -> URL |
| 52 | var slugs []string |
| 53 | for _, pr := range prs { |
| 54 | slug := gh.OwnerRepoFromPRURL(pr.url) |
| 55 | n, err := strconv.Atoi(pr.number) |
| 56 | if slug == "" || err != nil { |
| 57 | continue |
| 58 | } |
| 59 | if bySlug[slug] == nil { |
| 60 | bySlug[slug] = make(map[int]string) |
| 61 | slugs = append(slugs, slug) |
| 62 | } |
| 63 | bySlug[slug][n] = pr.url |
| 64 | } |
| 65 | sort.Strings(slugs) |
| 66 | for _, slug := range slugs { |
| 67 | if until := p.pausedUntil(); !until.IsZero() { |
| 68 | return statuses, until |
| 69 | } |
| 70 | numbers := make([]int, 0, len(bySlug[slug])) |
| 71 | for n := range bySlug[slug] { |
| 72 | numbers = append(numbers, n) |
| 73 | } |
| 74 | sort.Ints(numbers) |
| 75 | snaps, err := p.client.PRSnapshots(ctx, slug, numbers, len(p.trusted) > 0) |
| 76 | if err != nil { |
| 77 | p.failed(err) |
| 78 | continue |
| 79 | } |
| 80 | for _, n := range numbers { |
| 81 | url := bySlug[slug][n] |
| 82 | if s := snaps[n]; s != nil { |
| 83 | if ps, ok := p.status(ctx, slug, url, s); ok { |
| 84 | statuses[url] = ps |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | return statuses, p.pausedUntil() |
| 90 | } |
| 91 | |
| 92 | // status turns a snapshot into the PR's status; ok is false when part of it |
| 93 | // couldn't be read. |
| 94 | func (p *ghPoller) status(ctx context.Context, slug, url string, s *gh.PRSnapshot) (*prStatus, bool) { |
| 95 | ps := &prStatus{PRNumber: strconv.Itoa(s.Number), State: s.State} |
| 96 | if s.State != "OPEN" { |
| 97 | return ps, true |
| 98 | } |
| 99 | ps.CI, ps.Conflicts, ps.ReviewDecision = s.CI, s.Conflicts, s.ReviewDecision |
| 100 | ps.Labels, ps.Draft, ps.HeadSHA = s.Labels, s.Draft, s.HeadSHA |
| 101 | n, ok := p.commitsBehind(ctx, slug, url, s) |
| 102 | if !ok { |
| 103 | return nil, false |
| 104 | } |
| 105 | ps.BehindBy = n |
| 106 | // GitHub's review decision doesn't reflect comments from trusted |
| 107 | // reviewers, unless it is already decided. |
| 108 | if !strings.EqualFold(ps.ReviewDecision, "CHANGES_REQUESTED") && !strings.EqualFold(ps.ReviewDecision, "APPROVED") { |
| 109 | ps.TrustedFeedbackAt, ps.HasNewTrustedComments = trustedFeedback(s.Activity, p.trusted) |
| 110 | } |
| 111 | return ps, true |
| 112 | } |
| 113 | |
| 114 | // commitsBehind returns how far the PR's head is behind its base, comparing |
| 115 | // them only when either moved since the last compare. |
| 116 | func (p *ghPoller) commitsBehind(ctx context.Context, slug, url string, s *gh.PRSnapshot) (int, bool) { |
| 117 | if s.HeadSHA == "" || s.BaseSHA == "" { |
| 118 | return 0, true |
| 119 | } |
| 120 | p.mu.Lock() |
| 121 | b, ok := p.behind[url] |
| 122 | p.mu.Unlock() |
| 123 | if ok && b.head == s.HeadSHA && b.base == s.BaseSHA { |
| 124 | return b.n, true |
| 125 | } |
| 126 | if !p.pausedUntil().IsZero() { |
| 127 | return 0, false |
| 128 | } |
| 129 | n, err := p.client.CommitsBehindSHA(ctx, slug, s.BaseSHA, s.HeadSHA) |
| 130 | if err != nil { |
| 131 | p.failed(err) |
| 132 | return 0, false |
| 133 | } |
| 134 | p.mu.Lock() |
| 135 | p.behind[url] = behindAt{head: s.HeadSHA, base: s.BaseSHA, n: n} |
| 136 | p.mu.Unlock() |
| 137 | return n, true |
| 138 | } |
| 139 | |
| 140 | // failed notes a failed call: a rate limit pauses polling, and is logged |
| 141 | // once per pause. |
| 142 | func (p *ghPoller) failed(err error) { |
| 143 | var rl *gh.RateLimitError |
| 144 | if !errors.As(err, &rl) { |
| 145 | return |
| 146 | } |
| 147 | p.mu.Lock() |
| 148 | defer p.mu.Unlock() |
| 149 | if !time.Now().Before(p.until) { |
| 150 | slog.Warn("GitHub rate limit: PR polling paused", "until", rl.Until.Format(time.RFC3339)) |
| 151 | } |
| 152 | if rl.Until.After(p.until) { |
| 153 | p.until = rl.Until |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // pausedUntil is when a rate limit lets polling resume, zero when it isn't |
| 158 | // paused. |
| 159 | func (p *ghPoller) pausedUntil() time.Time { |
| 160 | p.mu.Lock() |
| 161 | defer p.mu.Unlock() |
| 162 | if time.Now().Before(p.until) { |
| 163 | return p.until |
| 164 | } |
| 165 | return time.Time{} |
| 166 | } |