fix(watchpr): re-read GitHub CI every poll and page through check runs
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful

This commit is contained in:
2026-11-22 02:07:25 +11:00
parent 6261b8e8d3
commit 08340cce22
3 changed files with 78 additions and 70 deletions
+26 -32
View File
@@ -39,13 +39,6 @@ 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.
@@ -162,13 +155,27 @@ func (c *GitHubClient) attempt(path, token string, out any) error {
const githubPageSize = 100
// listAll fetches every page of a list endpoint.
func listAll[T any](c *GitHubClient, path string) ([]T, error) {
// listAll fetches every page of a list endpoint; a non-empty key names the
// field holding the list when each page is wrapped in an object.
func listAll[T any](c *GitHubClient, path, key string) ([]T, error) {
var all []T
for page := 1; ; page++ {
var items []T
if err := c.do(path+"?per_page="+strconv.Itoa(githubPageSize)+"&page="+strconv.Itoa(page), &items); err != nil {
return nil, err
pagePath := path + "?per_page=" + strconv.Itoa(githubPageSize) + "&page=" + strconv.Itoa(page)
if key == "" {
if err := c.do(pagePath, &items); err != nil {
return nil, err
}
} else {
var wrapped map[string]json.RawMessage
if err := c.do(pagePath, &wrapped); err != nil {
return nil, err
}
if raw, ok := wrapped[key]; ok {
if err := json.Unmarshal(raw, &items); err != nil {
return nil, fmt.Errorf("github GET %s: decoding %s: %w", pagePath, key, err)
}
}
}
all = append(all, items...)
if len(items) < githubPageSize {
@@ -294,17 +301,14 @@ func (c *GitHubClient) ciState(repo, sha string) (string, error) {
}
return "", err
}
// Only the first page of check runs is read; a head with more than 100 is not expected.
var checks struct {
CheckRuns []githubCheckRun `json:"check_runs"`
}
if err := c.do("/repos/"+repo+"/commits/"+sha+"/check-runs?per_page="+strconv.Itoa(githubPageSize), &checks); err != nil {
checks, err := listAll[githubCheckRun](c, "/repos/"+repo+"/commits/"+sha+"/check-runs", "check_runs")
if err != nil {
if IsNotFound(err) {
return "", nil
}
return "", err
}
return mergeGitHubCI(status.State, status.TotalCount, checks.CheckRuns), nil
return mergeGitHubCI(status.State, status.TotalCount, checks), nil
}
// FetchState makes *GitHubClient a StateFetcher. The agent logins come from the
@@ -319,23 +323,13 @@ func (c *GitHubClient) FetchState(ref PRRef, _ string) (PRState, error) {
}
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
ci, err := c.ciState(repo, pr.Head.Sha)
if err != nil {
return PRState{}, err
}
comments := 0
for _, path := range []string{"/issues/" + n + "/comments", "/pulls/" + n + "/comments", "/pulls/" + n + "/reviews"} {
items, err := listAll[githubActivity](c, "/repos/"+repo+path)
items, err := listAll[githubActivity](c, "/repos/"+repo+path, "")
if err != nil {
return PRState{}, err
}
@@ -349,7 +343,7 @@ func (c *GitHubClient) FetchState(ref PRRef, _ string) (PRState, error) {
BaseSHA: pr.Base.Sha,
Mergeable: githubMergeability(pr.Mergeable),
MergeableState: pr.MergeableState,
CIStatus: ci.state,
CIStatus: ci,
NonAgentComments: comments,
Title: pr.Title,
URL: pr.HTMLURL,