fix(watchpr): pause on GitHub rate limits and skip settled CI reads
This commit is contained in:
@@ -2,12 +2,14 @@ package agent
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// DefaultGitHubAgentLogins are the GitHub identities whose activity watchpr
|
||||
@@ -37,6 +39,49 @@ type GitHubClient struct {
|
||||
Token func() (string, error)
|
||||
// Refresh mints a replacement when GitHub rejects the current token.
|
||||
Refresh func() (string, error)
|
||||
|
||||
lastCI map[string]githubCI
|
||||
}
|
||||
|
||||
// githubCI is the CI state last fetched for a PR's head.
|
||||
type githubCI struct {
|
||||
sha, state string
|
||||
}
|
||||
|
||||
// maxRateLimitWait caps how long a watch pauses for one rate limit.
|
||||
const maxRateLimitWait = 15 * time.Minute
|
||||
|
||||
// RateLimitError is a GitHub 403/429 rate limit, which clears at Reset.
|
||||
type RateLimitError struct {
|
||||
Path string
|
||||
Status int
|
||||
Reset time.Time
|
||||
}
|
||||
|
||||
func (e *RateLimitError) Error() string {
|
||||
return fmt.Sprintf("github GET %s: rate limited (HTTP %d) until %s", e.Path, e.Status, e.Reset.Format(time.RFC3339))
|
||||
}
|
||||
|
||||
// rateLimitReset reads Retry-After (seconds), then X-RateLimit-Reset (epoch
|
||||
// seconds), falling back to a minute from now.
|
||||
func rateLimitReset(h http.Header, now time.Time) time.Time {
|
||||
if s, err := strconv.Atoi(h.Get("Retry-After")); err == nil {
|
||||
return now.Add(time.Duration(s) * time.Second)
|
||||
}
|
||||
if s, err := strconv.ParseInt(h.Get("X-RateLimit-Reset"), 10, 64); err == nil {
|
||||
return time.Unix(s, 0)
|
||||
}
|
||||
return now.Add(time.Minute)
|
||||
}
|
||||
|
||||
// RateLimitWait reports whether err is a rate limit and how long from now to
|
||||
// wait it out, between a second and maxRateLimitWait.
|
||||
func RateLimitWait(err error, now time.Time) (time.Duration, bool) {
|
||||
var rl *RateLimitError
|
||||
if !errors.As(err, &rl) {
|
||||
return 0, false
|
||||
}
|
||||
return min(max(rl.Reset.Sub(now), time.Second), maxRateLimitWait), true
|
||||
}
|
||||
|
||||
// NewGitHubClient builds an anonymous client for GITHUB_API_URL.
|
||||
@@ -105,7 +150,7 @@ func (c *GitHubClient) attempt(path, token string, out any) error {
|
||||
// A rate limit is a 403/429 that clears on its own, so it must not end a watch as an auth failure.
|
||||
if resp.StatusCode == http.StatusTooManyRequests || resp.Header.Get("X-RateLimit-Remaining") == "0" ||
|
||||
(resp.StatusCode == http.StatusForbidden && strings.Contains(strings.ToLower(body), "rate limit")) {
|
||||
return fmt.Errorf("github GET %s: rate limited (HTTP %d)", path, resp.StatusCode)
|
||||
return &RateLimitError{Path: path, Status: resp.StatusCode, Reset: rateLimitReset(resp.Header, time.Now())}
|
||||
}
|
||||
return &APIError{Service: "github", Method: http.MethodGet, Path: path, StatusCode: resp.StatusCode, Body: body}
|
||||
}
|
||||
@@ -274,9 +319,19 @@ func (c *GitHubClient) FetchState(ref PRRef, _ string) (PRState, error) {
|
||||
}
|
||||
return PRState{}, err
|
||||
}
|
||||
ci, err := c.ciState(repo, pr.Head.Sha)
|
||||
if err != nil {
|
||||
return PRState{}, err
|
||||
// CI is re-read only while it can still change on its own: a new head, or a
|
||||
// state that is pending or not yet reported.
|
||||
ci, ok := c.lastCI[ref.String()]
|
||||
if !ok || ci.sha != pr.Head.Sha || ci.state == "pending" || ci.state == "" {
|
||||
state, err := c.ciState(repo, pr.Head.Sha)
|
||||
if err != nil {
|
||||
return PRState{}, err
|
||||
}
|
||||
ci = githubCI{sha: pr.Head.Sha, state: state}
|
||||
if c.lastCI == nil {
|
||||
c.lastCI = map[string]githubCI{}
|
||||
}
|
||||
c.lastCI[ref.String()] = ci
|
||||
}
|
||||
comments := 0
|
||||
for _, path := range []string{"/issues/" + n + "/comments", "/pulls/" + n + "/comments", "/pulls/" + n + "/reviews"} {
|
||||
@@ -294,7 +349,7 @@ func (c *GitHubClient) FetchState(ref PRRef, _ string) (PRState, error) {
|
||||
BaseSHA: pr.Base.Sha,
|
||||
Mergeable: githubMergeability(pr.Mergeable),
|
||||
MergeableState: pr.MergeableState,
|
||||
CIStatus: ci,
|
||||
CIStatus: ci.state,
|
||||
NonAgentComments: comments,
|
||||
Title: pr.Title,
|
||||
URL: pr.HTMLURL,
|
||||
|
||||
Reference in New Issue
Block a user