From 24b8c71222c4400e9d2e3605e15b20756de674af Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Tue, 3 Nov 2026 22:05:53 -0500 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix:=20drive=20Activity=20Mo?= =?UTF-8?q?nitor=20CI=20counts=20from=20check=20webhooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit check_run / workflow_job / status deliveries are applied straight into a per-(repo, head) check set the tracker's CI chevron counts from; a single coalesced check-run listing runs only when the head has no listing yet. Payload-served check deliveries no longer queue a PR re-read. Sidelined PRs carry HeadSHA so their rows count too. /api/status.webhooks reports last_ci_event_at, ci_events_1h, ci_events_applied_1h, ci_refetches_1h. The hub relays workflow_run and workflow_job. Fixes #16521 Signed-off-by: Andrew Anderson --- changelog.d/fixed-webhook-ci-counts.md | 1 + src/cmd/hive/main.go | 4 + .../operator-reference/runtime-and-quota.md | 29 +- src/pkg/dashboard/api_github_webhook.go | 3 + src/pkg/dashboard/stage_tracker_build.go | 6 +- .../stage_tracker_status_collection_test.go | 3 + .../stage_tracker_webhook_ci_test.go | 145 ++++ src/pkg/dashboard/webhook_tracker_refresh.go | 21 + src/pkg/github/ci_failure_signal_test.go | 9 +- src/pkg/github/client.go | 14 + src/pkg/github/protection_facts.go | 32 + src/pkg/github/webhook_cache.go | 110 ++- src/pkg/github/webhook_ci_evidence.go | 732 ++++++++++++++++++ src/pkg/github/webhook_ci_evidence_test.go | 298 +++++++ src/pkg/hub/webhook.go | 2 +- 15 files changed, 1381 insertions(+), 28 deletions(-) create mode 100644 changelog.d/fixed-webhook-ci-counts.md create mode 100644 src/pkg/dashboard/stage_tracker_webhook_ci_test.go create mode 100644 src/pkg/github/webhook_ci_evidence.go create mode 100644 src/pkg/github/webhook_ci_evidence_test.go diff --git a/changelog.d/fixed-webhook-ci-counts.md b/changelog.d/fixed-webhook-ci-counts.md new file mode 100644 index 0000000000..5070ebf3dc --- /dev/null +++ b/changelog.d/fixed-webhook-ci-counts.md @@ -0,0 +1 @@ +- Activity Monitor CI chevron counts (`CI 7/11`) are now driven by `check_run`/`workflow_job`/`status` webhook deliveries applied straight into a per-head check set — no PR re-read per check fan-out — with one coalesced check-run listing only when the head has none yet, so rows no longer sit at `CI ?/?` after a push or for sidelined PRs until the next eval cycle; `/api/status.webhooks` gains `last_ci_event_at`, `ci_events_1h`, `ci_events_applied_1h`, `ci_refetches_1h` and the hub relays `workflow_run`/`workflow_job` (hivecommons/hive#16521) diff --git a/src/cmd/hive/main.go b/src/cmd/hive/main.go index 16ecd5157a..7d5be57141 100644 --- a/src/cmd/hive/main.go +++ b/src/cmd/hive/main.go @@ -3440,6 +3440,10 @@ func (b *boot) bootDashboardAPIWith(deps bootDashboardAPIDeps) { // re-reads the issue into the cached snapshot and the Activity Monitor // the way an inbound label webhook does (hivecommons/hive#14952). github.SetItemRefreshHook(b.dashSrv.RefreshSelfEditedItem) + // A check/status delivery applied to a head's check set, or the + // coalesced re-read it scheduled, re-derives the Activity Monitor row + // at once (hivecommons/hive#16521). + github.SetCIEvidenceHook(b.dashSrv.RefreshCIEvidence) wireSpektacularRunner(b.cfg, b.dashSrv, b.logger) // #8380: chain GitHub comments/labels behind the relay yank on takeover. ghClientFn := func() *github.Client { return b.ghClient } diff --git a/src/docs/operator-reference/runtime-and-quota.md b/src/docs/operator-reference/runtime-and-quota.md index 687a6f0753..b2f0a62153 100644 --- a/src/docs/operator-reference/runtime-and-quota.md +++ b/src/docs/operator-reference/runtime-and-quota.md @@ -148,7 +148,9 @@ manage are ignored. webhook with the same secret works too). Subscribe the App to these events: `pull_request`, `pull_request_review`, `pull_request_review_comment`, `issue_comment`, `issues`, `check_suite`, `check_run`, `status`, `push`, - and, with `governor.work_source.type: github_projects`, `projects_v2_item` + optionally `workflow_job` (Actions: read; lets the CI counts on the + activity monitor move from the job payload for Apps that do not surface + check runs), and, with `governor.work_source.type: github_projects`, `projects_v2_item` (organization-scoped; see [Work sources](../work-sources.md#type-github_projects--github-projects-projects-v2)). 3. **Hub relay.** The hub verifies the HMAC and relays the body byte-for-byte, with the original signature and `X-GitHub-Delivery`, to the spoke whose @@ -171,8 +173,8 @@ v5 and v6 spokes) tracking one repo all stay on webhook mode. #### What a delivery does - `pull_request`, `pull_request_review`, `pull_request_review_comment`, - `check_suite`, `check_run`, `status`, `issue_comment` (on PRs only) and - `push` invalidate the cached PR detail and the GraphQL batch + `check_suite`, `check_run`, `workflow_job`, `status`, `issue_comment` (on + PRs only) and `push` invalidate the cached PR detail and the GraphQL batch check-run/review entries for the affected PR and mark it dirty. Check and status events are matched by PR number and head SHA (fork PRs arrive without a PR list); `push` invalidates every open PR whose base or head @@ -288,8 +290,7 @@ v5 and v6 spokes) tracking one repo all stay on webhook mode. `issues` — any action) also drops the `/api/stage-tracker` cache at once and queues a **targeted re-read of just that item**: one `GET` of the PR or issue, debounced by `dashboard.stage_tracker.webhook_refresh_debounce_ms` - (default `2000`) and coalesced per item, so a check fan-out of twenty - `check_run` deliveries costs one request. The re-read patches that row of + (default `2000`) and coalesced per item. The re-read patches that row of the status snapshot in place — labels, title, draft flag, last activity; a closed or merged item leaves — then drops the tracker cache again and sends the `stage-tracker` SSE nudge. The same re-read is handed to the @@ -318,6 +319,24 @@ v5 and v6 spokes) tracking one repo all stay on webhook mode. also lists each repo's items closed since its last look every `dashboard.stage_tracker.closed_sweep_interval_s` (default `300`; one list call per repo; negative disables) and evicts any still shown. +- **CI counts on the activity monitor move from the check payload.** A + `check_run`, `workflow_job` or `status` delivery is applied directly into + the per-head check set the tracker's CI chevron counts (`CI 7/11`): the + named check is upserted by run id (or check name and producing App), a + newer run replaces an older one, and the row is pushed over the + `stage-tracker` SSE nudge without any GitHub request — a fan-out of + twenty `check_run` deliveries costs zero requests. The payload-only + `check_run`/`status` delivery no longer queues the PR re-read above. + When no check-run listing exists yet for that head (first delivery after + a push, or a sidelined PR the eval cycle never enriched) one coalesced + listing of the head's check runs is scheduled instead (debounced by the + same `webhook_refresh_debounce_ms`), so the required-check set and the + `pass/total` denominator are right from the first row. The polling + path's `EnrichCIStatus` still refreshes every actionable PR each cycle + and is the backstop for spokes without webhooks. + `/api/status.webhooks.last_ci_event_at`, `ci_events_1h`, + `ci_events_applied_1h` and `ci_refetches_1h` show whether check + deliveries actually arrive and how many were served from the payload. #### Polling cadence vs webhook patch vs reconcile diff --git a/src/pkg/dashboard/api_github_webhook.go b/src/pkg/dashboard/api_github_webhook.go index f6ab79c8b1..b8ad183a36 100644 --- a/src/pkg/dashboard/api_github_webhook.go +++ b/src/pkg/dashboard/api_github_webhook.go @@ -136,6 +136,9 @@ func (s *Server) handleGitHubWebhook(w http.ResponseWriter, r *http.Request) { if reactions := s.reactToCIFailure(res); len(reactions) > 0 { resp["ci_failure"] = reactions } + if res.CIEvidence != nil { + resp["ci_evidence"] = res.CIEvidence + } if n, ok := sentinelPREventTarget(event, body); ok && s.deps.SentinelPREvent != nil { s.deps.SentinelPREvent(res.ConfiguredRepo, n) resp["sentinel"] = n diff --git a/src/pkg/dashboard/stage_tracker_build.go b/src/pkg/dashboard/stage_tracker_build.go index 9b9ce770bc..bd4bf231c4 100644 --- a/src/pkg/dashboard/stage_tracker_build.go +++ b/src/pkg/dashboard/stage_tracker_build.go @@ -456,6 +456,10 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto stageTrackerApplyFixerStatus(&it, review.FixDispatchStatusFor(dispatchState, qualifyRepo(repoName), pr.Number, pr.HeadSHA), workers) activityPR := pr activityPR.Repo = qualifyRepo(repoName) + // Checks delivered by webhook since the last listing, laid over it; + // a head the cycle never listed counts from the deliveries alone + // (hivecommons/hive#16521). + activityPR.CIActivity = github.HeadCIActivity(activityPR.Repo, pr.HeadSHA, pr.CIActivity) it.CIActivity = stageTrackerCIActivity(activityPR, now) var bundle *evidence.Bundle if evidenceVerifier != nil && pr.HeadSHA != "" { @@ -501,7 +505,7 @@ func (s *Server) buildStageTracker(status *StatusPayload, store *stagelabels.Sto default: w := it.WorkPR addPR(repoName, github.PullRequest{Repo: w.Repo, Number: w.Number, Title: w.Title, Author: w.Author, Labels: w.Labels, Draft: w.Draft, - AppAuthored: w.AppAuthored, HiveAttributed: w.HiveAttributed, CreatedAt: w.CreatedAt, UpdatedAt: w.UpdatedAt, URL: w.URL, BaseRef: w.BaseRef, LinkedIssues: w.LinkedIssues, ReferencedIssues: w.ReferencedIssues}, nil, w.Bucket, w.Reason) + AppAuthored: w.AppAuthored, HiveAttributed: w.HiveAttributed, CreatedAt: w.CreatedAt, UpdatedAt: w.UpdatedAt, URL: w.URL, BaseRef: w.BaseRef, HeadSHA: w.HeadSHA, LinkedIssues: w.LinkedIssues, ReferencedIssues: w.ReferencedIssues}, nil, w.Bucket, w.Reason) } } } diff --git a/src/pkg/dashboard/stage_tracker_status_collection_test.go b/src/pkg/dashboard/stage_tracker_status_collection_test.go index fa8d775336..1c8daa6229 100644 --- a/src/pkg/dashboard/stage_tracker_status_collection_test.go +++ b/src/pkg/dashboard/stage_tracker_status_collection_test.go @@ -18,6 +18,9 @@ import ( func TestStageTrackerRESTCheckPaginationMustCompleteBeforeProjection(t *testing.T) { for _, mode := range []string{"complete", "partial", "malformed"} { t.Run(mode, func(t *testing.T) { + // Each mode observes the same head; the shared per-head CI + // evidence store must not carry one mode's listing into the next. + t.Cleanup(github.ResetCIEvidenceForTest(nil)) reads := 0 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch { diff --git a/src/pkg/dashboard/stage_tracker_webhook_ci_test.go b/src/pkg/dashboard/stage_tracker_webhook_ci_test.go new file mode 100644 index 0000000000..e7f8e6a5b4 --- /dev/null +++ b/src/pkg/dashboard/stage_tracker_webhook_ci_test.go @@ -0,0 +1,145 @@ +package dashboard + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/hivecommons/hive/pkg/github" +) + +// TestStageTrackerCICountsFromWebhookEvidence: check deliveries applied to +// the per-head evidence store drive the CI chevron counts for a PR whose +// status snapshot carries no (or stale) CIActivity — including sidelined +// PRs the eval cycle never enriches (hivecommons/hive#16521). +func TestStageTrackerCICountsFromWebhookEvidence(t *testing.T) { + now := time.Date(2026, 10, 17, 9, 0, 0, 0, time.UTC) + t.Cleanup(github.ResetCIEvidenceForTest(func() time.Time { return now })) + s, store := stageTrackerFixture(t, now) + + appID := int64(15368) + done := now.Add(-time.Minute) + checks := []github.PRActivityCheck{ + {Name: "build", State: "completed", Conclusion: "success", NodeID: "CR_build", AppID: &appID, Since: &done}, + {Name: "test", State: "in_progress", NodeID: "CR_test", AppID: &appID, Since: &done}, + } + if n := github.RecordCIEvidenceForTest("acme/console", "sha-103", checks); n != 2 { + t.Fatalf("recorded %d checks, want 2", n) + } + if n := github.RecordCIEvidenceForTest("acme/console", "sha-106", checks[:1]); n != 1 { + t.Fatalf("recorded %d checks, want 1", n) + } + + s.statusMu.Lock() + repo := &s.status.Repos[0] + for i, raw := range repo.OpenPrs { + pr, ok := raw.(FrontendPR) + if !ok || pr.Number != 103 { + continue + } + pr.HeadSHA = "sha-103" + // Evidence from a previous head must not win over the new head. + stale := now.Add(-time.Hour) + pr.CIActivity = &github.PRCIActivity{Head: "old-103", Source: "github-check-runs", ObservedAt: &stale, + Checks: []github.PRActivityCheck{{Name: "build", State: "completed", Conclusion: "failure"}}} + repo.OpenPrs[i] = pr + } + repo.NonActionablePrs = []any{ + github.RepoWorkPR{Repo: "acme/console", Number: 106, Title: "WIP draft", Author: "zoe", Draft: true, Bucket: "draft", Reason: "Draft", HeadSHA: "sha-106", CreatedAt: now.Add(-24 * time.Hour)}, + } + s.statusMu.Unlock() + + resp := s.buildStageTracker(s.status, store, now) + pr103 := trackerItem(t, resp, "pr", 103) + if pr103.CIActivity == nil || pr103.CIActivity.Head != "sha-103" || pr103.CIActivity.ChecksTotal != 2 || pr103.CIActivity.ChecksPassed != 1 || pr103.CIActivity.ChecksFailed != 0 { + t.Fatalf("pr103 CI = %+v, want 1/2 on sha-103 from webhook evidence", pr103.CIActivity) + } + pr106 := trackerItem(t, resp, "pr", 106) + if pr106.CIActivity == nil || pr106.CIActivity.ChecksTotal != 1 || pr106.CIActivity.ChecksPassed != 1 { + t.Fatalf("sidelined pr106 CI = %+v, want 1/1 from webhook evidence", pr106.CIActivity) + } +} + +// TestGitHubWebhookCheckRunAppliesCIEvidenceWithoutGitHubCall: a +// `check_run` delivery that names its PR updates the CI evidence from the +// payload alone — no PR re-read is queued, the GitHub test server would +// fail the test on any call — and /api/status.webhooks reports CI event +// health. +func TestGitHubWebhookCheckRunAppliesCIEvidenceWithoutGitHubCall(t *testing.T) { + const secret = "s3cr3t" + t.Setenv(githubWebhookSecretEnvVar, secret) + now := time.Date(2026, 10, 17, 9, 0, 0, 0, time.UTC) + t.Cleanup(github.ResetWebhookTrackerForTestWithClock(func() time.Time { return now })) + t.Cleanup(github.ResetCIEvidenceForTest(func() time.Time { return now })) + s := newGitHubWebhookTestServer(t) + + // A prior listing exists for the head, so the delivery needs no re-read. + appID := int64(15368) + started := now.Add(-2 * time.Minute) + listedAt := now.Add(-time.Minute) + github.RecordCIListingForTest("webhookorg/webhookrepo", "abc123", &github.PRCIActivity{ + Head: "abc123", Source: "github-check-runs", ObservedAt: &listedAt, + Checks: []github.PRActivityCheck{{Name: "unit", State: "in_progress", NodeID: "CR_unit", AppID: &appID, Since: &started}}, + }) + + body := []byte(`{"action":"completed","repository":{"full_name":"webhookorg/webhookrepo"}, + "check_run":{"id":1,"node_id":"CR_unit","name":"unit","status":"completed","conclusion":"success","head_sha":"abc123", + "started_at":"2026-10-17T08:58:00Z","completed_at":"2026-10-17T08:59:30Z","app":{"id":15368,"name":"GitHub Actions"}, + "pull_requests":[{"number":5}]}}`) + rec := postGitHubWebhook(s, "check_run", signGitHubWebhookForTest(secret, body), body) + if rec.Code != http.StatusOK { + t.Fatalf("status = %d (%s)", rec.Code, rec.Body.String()) + } + var got struct { + Status string `json:"status"` + TrackerRefresh *int `json:"tracker_refresh"` + CIEvidence *struct { + HeadSHA string `json:"head_sha"` + Applied int `json:"applied"` + Refetch bool `json:"refetch"` + PRs []int `json:"prs"` + } `json:"ci_evidence"` + } + if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil { + t.Fatal(err) + } + if got.Status != "ok" || got.CIEvidence == nil || got.CIEvidence.Applied != 1 || got.CIEvidence.Refetch || got.CIEvidence.HeadSHA != "abc123" || len(got.CIEvidence.PRs) != 1 || got.CIEvidence.PRs[0] != 5 { + t.Fatalf("response = %s, want ci_evidence applied 1 on abc123 for PR 5 with no refetch", rec.Body.String()) + } + if got.TrackerRefresh != nil { + t.Fatalf("payload-served check delivery must not queue a PR re-read: %s", rec.Body.String()) + } + completed := now.Add(-30 * time.Second) + merged := github.HeadCIActivity("webhookorg/webhookrepo", "abc123", nil) + if merged == nil || len(merged.Checks) != 1 || merged.Checks[0].Conclusion != "success" || merged.Checks[0].Since == nil || !merged.Checks[0].Since.Equal(completed) { + t.Fatalf("head evidence = %+v, want the completed unit run", merged) + } + pr := github.PullRequest{Number: 5, HeadSHA: "abc123"} + if ci := stageTrackerCIActivity(github.PullRequest{Number: 5, HeadSHA: "abc123", CIActivity: merged}, now); ci.ChecksTotal != 1 || ci.ChecksPassed != 1 { + t.Fatalf("tracker counts for %d = %+v, want 1/1", pr.Number, ci) + } + + s.statusMu.Lock() + s.status = &StatusPayload{HiveID: "webhook-test"} + s.statusMu.Unlock() + statusRec := httptest.NewRecorder() + s.handleStatus(statusRec, httptest.NewRequest(http.MethodGet, "/api/status?fields=webhooks", bytes.NewReader(nil))) + var status struct { + Webhooks struct { + LastCIEventAt *time.Time `json:"last_ci_event_at"` + CIEvents1h int `json:"ci_events_1h"` + CIEventsApplied1h int `json:"ci_events_applied_1h"` + CIRefetches1h int `json:"ci_refetches_1h"` + } `json:"webhooks"` + } + if err := json.Unmarshal(statusRec.Body.Bytes(), &status); err != nil { + t.Fatal(err) + } + w := status.Webhooks + if w.LastCIEventAt == nil || !w.LastCIEventAt.Equal(now) || w.CIEvents1h != 1 || w.CIEventsApplied1h != 1 || w.CIRefetches1h != 0 { + t.Fatalf("/api/status webhooks CI health = %+v, want one event applied at %s", w, now) + } +} diff --git a/src/pkg/dashboard/webhook_tracker_refresh.go b/src/pkg/dashboard/webhook_tracker_refresh.go index f8dcced9df..c7a6e4db62 100644 --- a/src/pkg/dashboard/webhook_tracker_refresh.go +++ b/src/pkg/dashboard/webhook_tracker_refresh.go @@ -252,6 +252,13 @@ func (s *Server) scheduleWebhookTrackerRefresh(res github.WebhookEventResult, de if !s.webhookTrackerRefreshEnabled() { return 0 } + if res.CIEvidence != nil && res.CIEvidence.Applied > 0 { + // The payload itself moved the row's CI evidence + // (hivecommons/hive#16521); the PR object did not change, so the + // re-read would only spend a request to confirm labels and title. + s.invalidateStageTrackerForWebhook() + return 0 + } keys := webhookTrackerKeys(res) if len(keys) == 0 { return 0 @@ -261,6 +268,17 @@ func (s *Server) scheduleWebhookTrackerRefresh(res github.WebhookEventResult, de return len(keys) } +// RefreshCIEvidence is the github.CIEvidenceHook: a check delivery or a +// coalesced check-run re-read changed the CI evidence for repo's head, so +// the tracker payload is dropped and the page nudged over SSE; the next GET +// re-derives the row's counts from the merged evidence. +func (s *Server) RefreshCIEvidence(repo, head string) { + if s == nil { + return + } + s.invalidateStageTrackerForWebhook() +} + // selfEditDeliveryID tags the re-reads the hive's own GitHub edits ask for, // in place of a webhook delivery id, in ItemPatches and the logs. const selfEditDeliveryID = "self:edit" @@ -804,6 +822,9 @@ func patchWorkPRAny(raw any, snap github.WebhookItemSnapshot) (any, bool) { w.Title = snap.Title w.Labels = append([]string(nil), snap.Labels...) w.Draft = snap.Draft + if snap.HeadSHA != "" { + w.HeadSHA = snap.HeadSHA + } if !snap.UpdatedAt.IsZero() { w.UpdatedAt = snap.UpdatedAt } diff --git a/src/pkg/github/ci_failure_signal_test.go b/src/pkg/github/ci_failure_signal_test.go index 9e5de4a4a8..5488a0ea63 100644 --- a/src/pkg/github/ci_failure_signal_test.go +++ b/src/pkg/github/ci_failure_signal_test.go @@ -74,14 +74,7 @@ func TestWebhookCIFailure_EventsAndConclusions(t *testing.T) { } } -type checkRunPayload = struct { - HeadSHA string `json:"head_sha"` - Name string `json:"name"` - Conclusion string `json:"conclusion"` - DetailsURL string `json:"details_url"` - HTMLURL string `json:"html_url"` - PullRequests []webhookPRRef `json:"pull_requests"` -} +type checkRunPayload = webhookCheckRunPayload type checkSuitePayload = struct { HeadSHA string `json:"head_sha"` diff --git a/src/pkg/github/client.go b/src/pkg/github/client.go index 92823f7cbf..4c67ca69c8 100644 --- a/src/pkg/github/client.go +++ b/src/pkg/github/client.go @@ -64,6 +64,10 @@ type Client struct { graphQLPRBatchEnabled func() bool graphQLPRBatchPageSize func() int prBatchMu sync.Mutex + // ciRefetch coalesces the check-run re-reads CI webhook deliveries ask + // for on heads no listing has covered (webhook_ci_evidence.go). + ciRefetchOnce sync.Once + ciRefetch *ciRefetcher prBatchCheckRuns map[prBatchCheckRunKey][]*gh.CheckRun prBatchCheckObservedAt map[prBatchCheckRunKey]time.Time prBatchReviews map[string]map[int]prReviewState @@ -2560,6 +2564,11 @@ func (c *Client) EnrichCIStatus(ctx context.Context, prs []PullRequest) { prs[i].SharedCIIncident = c.openSharedCIIncident(ctx, &prs[i], incidents) } facts.attach(ctx, &prs[i], reported) + // The listing is the head's complete observation; checks delivered + // by webhook since then are laid over it (hivecommons/hive#16521). + owner, name := c.splitRepo(prs[i].Repo) + sharedCIEvidence().recordListed(owner+"/"+name, prs[i].HeadSHA, prs[i].CIActivity) + prs[i].CIActivity = sharedCIEvidence().merged(owner+"/"+name, prs[i].HeadSHA, prs[i].CIActivity) } c.enrichBaseBranchRed(ctx, prs) } @@ -4843,6 +4852,10 @@ type RepoWorkPR struct { Reason string `json:"reason,omitempty"` // BaseRef is the target branch, retained for the covering-PR chip. BaseRef string `json:"base_ref,omitempty"` + // HeadSHA is the PR's head commit from the list payload, so the activity + // monitor can match webhook-delivered CI evidence to a sidelined row + // (hivecommons/hive#16521). + HeadSHA string `json:"head_sha,omitempty"` // LinkedIssues mirrors PullRequest.LinkedIssues so a sidelined PR can // still stand under the issue its `Fixes #N` names in the activity // monitor. Stamped by EnrichReviewSignalsAndWorkPRs, not by the @@ -4874,6 +4887,7 @@ func (c *Client) repoWorkPR(repo string, pr *gh.PullRequest, labels []string, hi UpdatedAt: pr.GetUpdatedAt().Time, URL: pr.GetHTMLURL(), BaseRef: prBaseRef(pr), + HeadSHA: prHeadSHA(pr), Bucket: bucket, Reason: reason, // The list payload carries the body, so the soft issue links cost diff --git a/src/pkg/github/protection_facts.go b/src/pkg/github/protection_facts.go index 6ad86958d0..d0689a36fd 100644 --- a/src/pkg/github/protection_facts.go +++ b/src/pkg/github/protection_facts.go @@ -166,6 +166,38 @@ func (pc *protectionCollector) attach(ctx context.Context, pr *PullRequest, repo pr.Protection = &f } +// attachRequiredChecks stamps the base branch's required-check policy onto +// pr.CIActivity exactly as attach does, plus the commit statuses an any-App +// or unreported required context needs, without the protection facts: it +// is the webhook CI re-read's half (webhook_ci_evidence.go), where no +// mergeability or review decision is being collected. +func (pc *protectionCollector) attachRequiredChecks(ctx context.Context, pr *PullRequest) { + if pc == nil || pc.c == nil || pr == nil || pr.CIActivity == nil || pr.BaseRef == "" { + return + } + req := pc.requiredChecks(ctx, pr.Repo, pr.BaseRef) + a := pr.CIActivity + a.setRequired(req.set, req.known) + a.RequiredChecks = req.bindings + a.RequiredSource = req.source + a.RequiredKnown = req.known && req.source != "config" && observedRequiredBindings(req.set, req.bindings) + if !req.known { + return + } + needsStatuses := a.NeedsCommitStatusEvidence() + for name := range req.set { + if len(a.ChecksForRequired(name)) == 0 { + needsStatuses = true + } + } + if !needsStatuses { + return + } + if collected, known := pc.c.commitStatuses(ctx, pr.Repo, pr.HeadSHA); known { + a.addCommitStatuses(collected, time.Now().UTC()) + } +} + // commitStatuses retains state and identity, not just context presence. // A failed, incomplete or explicitly wrong-head listing is unknown. func (c *Client) commitStatuses(ctx context.Context, repo, sha string) ([]*gh.RepoStatus, bool) { diff --git a/src/pkg/github/webhook_cache.go b/src/pkg/github/webhook_cache.go index dd9f29d45e..0dc3ae0eff 100644 --- a/src/pkg/github/webhook_cache.go +++ b/src/pkg/github/webhook_cache.go @@ -31,6 +31,17 @@ type WebhookHealth struct { // deliveries and refreshes caused. TargetedRefreshes1h int `json:"targeted_refreshes_1h"` TrackerInvalidations1h int `json:"tracker_invalidations_1h"` + // CI deliveries (check_run, check_suite, status, workflow_run, + // workflow_job) for managed repositories (hivecommons/hive#16521): + // when the last one arrived, how many in the window, how many check + // observations they applied to a head's set without a GitHub call, and + // how many coalesced check-run re-reads they caused. A nil + // LastCIEventAt with webhooks otherwise healthy means the App or the + // relay does not deliver check events. + LastCIEventAt *time.Time `json:"last_ci_event_at"` + CIEvents1h int `json:"ci_events_1h"` + CIEventsApplied1h int `json:"ci_events_applied_1h"` + CIRefetches1h int `json:"ci_refetches_1h"` // WebhookModeStatus is the reconcile cadence the health drives: // `mode` webhook|polling and `since` (hivecommons/hive#13381). WebhookModeStatus @@ -79,6 +90,10 @@ type WebhookEventResult struct { // reported a failed check (failure / timed_out / error) on a head SHA, // with the open PRs that head resolved to. CIFailure *WebhookCIFailure `json:"ci_failure,omitempty"` + // CIEvidence reports what a check_run / check_suite / status / + // workflow_run / workflow_job delivery did to the head's check set + // (hivecommons/hive#16521). + CIEvidence *WebhookCIEvidence `json:"ci_evidence,omitempty"` } // WebhookItemHint is the state a webhook payload reports for the item it @@ -112,6 +127,10 @@ type webhookTracker struct { invalidations []time.Time refreshes []time.Time trackerDrops []time.Time + ciEvents []time.Time + ciApplied []time.Time + ciRefetches []time.Time + lastCIEvent time.Time wasHealthy bool // lastActivity is the newest GitHub `updated_at` a poll listed and // lastFailure the newest delivery that failed verification; both feed @@ -213,6 +232,18 @@ func RecordWebhookTrackerInvalidations(n int) { t.recordStamped(&t.trackerDrops, n) } +func (t *webhookTracker) recordCIEvent() { + t.mu.Lock() + defer t.mu.Unlock() + now := t.now() + t.lastCIEvent = now + t.ciEvents = append(pruneWebhookTimes(t.ciEvents, now), now) +} + +func (t *webhookTracker) recordCIApplied(n int) { t.recordStamped(&t.ciApplied, n) } + +func (t *webhookTracker) recordCIRefetches(n int) { t.recordStamped(&t.ciRefetches, n) } + func (t *webhookTracker) recordStamped(dst *[]time.Time, n int) { if n <= 0 { return @@ -364,14 +395,22 @@ func (t *webhookTracker) snapshot() WebhookHealth { t.invalidations = pruneWebhookTimes(t.invalidations, now) t.refreshes = pruneWebhookTimes(t.refreshes, now) t.trackerDrops = pruneWebhookTimes(t.trackerDrops, now) + t.ciEvents = pruneWebhookTimes(t.ciEvents, now) + t.ciApplied = pruneWebhookTimes(t.ciApplied, now) + t.ciRefetches = pruneWebhookTimes(t.ciRefetches, now) healthy, latest := t.healthyLocked(now) out := WebhookHealth{Healthy: healthy, Events1h: len(t.events), Invalidations1h: len(t.invalidations), TargetedRefreshes1h: len(t.refreshes), TrackerInvalidations1h: len(t.trackerDrops), + CIEvents1h: len(t.ciEvents), CIEventsApplied1h: len(t.ciApplied), CIRefetches1h: len(t.ciRefetches), WebhookModeStatus: t.mode.status(t.window, now)} if !latest.IsZero() { at := latest.UTC() out.LastEventAt = &at } + if !t.lastCIEvent.IsZero() { + at := t.lastCIEvent.UTC() + out.LastCIEventAt = &at + } return out } @@ -425,6 +464,39 @@ type webhookPRRef struct { Number int `json:"number"` } +// webhookCheckRunPayload is the `check_run` object of a check_run delivery. +type webhookCheckRunPayload struct { + ID int64 `json:"id"` + NodeID string `json:"node_id"` + HeadSHA string `json:"head_sha"` + Name string `json:"name"` + Status string `json:"status"` + Conclusion string `json:"conclusion"` + DetailsURL string `json:"details_url"` + HTMLURL string `json:"html_url"` + StartedAt *time.Time `json:"started_at"` + CompletedAt *time.Time `json:"completed_at"` + PullRequests []webhookPRRef `json:"pull_requests"` + App *struct { + ID int64 `json:"id"` + Name string `json:"name"` + } `json:"app"` +} + +// webhookWorkflowJobPayload is the `workflow_job` object: one GitHub Actions +// job, which is its check run as the Actions App reports it. +type webhookWorkflowJobPayload struct { + ID int64 `json:"id"` + NodeID string `json:"node_id"` + HeadSHA string `json:"head_sha"` + Name string `json:"name"` + Status string `json:"status"` + Conclusion string `json:"conclusion"` + HTMLURL string `json:"html_url"` + StartedAt *time.Time `json:"started_at"` + CompletedAt *time.Time `json:"completed_at"` +} + type webhookEventPayload struct { Repository struct { FullName string `json:"full_name"` @@ -472,23 +544,23 @@ type webhookEventPayload struct { Name string `json:"name"` } `json:"app"` } `json:"check_suite"` - CheckRun *struct { - HeadSHA string `json:"head_sha"` - Name string `json:"name"` - Conclusion string `json:"conclusion"` - DetailsURL string `json:"details_url"` - HTMLURL string `json:"html_url"` - PullRequests []webhookPRRef `json:"pull_requests"` - } `json:"check_run"` + CheckRun *webhookCheckRunPayload `json:"check_run"` WorkflowRun *struct { HeadSHA string `json:"head_sha"` PullRequests []webhookPRRef `json:"pull_requests"` } `json:"workflow_run"` - SHA string `json:"sha"` - State string `json:"state"` - Context string `json:"context"` - TargetURL string `json:"target_url"` - Branches []struct { + // WorkflowJob is one GitHub Actions job: its check run, as the Actions + // App reports it, without the check_run envelope. + WorkflowJob *webhookWorkflowJobPayload `json:"workflow_job"` + ID int64 `json:"id"` + NodeID string `json:"node_id"` + SHA string `json:"sha"` + State string `json:"state"` + Context string `json:"context"` + TargetURL string `json:"target_url"` + CreatedAt *time.Time `json:"created_at"` + UpdatedAt *time.Time `json:"updated_at"` + Branches []struct { Name string `json:"name"` } `json:"branches"` Ref string `json:"ref"` @@ -579,6 +651,9 @@ func (c *Client) HandleWebhookEvent(event string, payload []byte) (WebhookEventR sharedCIReportingCache.invalidate(p.Repository.FullName, res.HeadSHA) } numbers := webhookInvalidationTargets(event, p, aliases) + if IsWebhookCIEvent(event) && res.HeadSHA != "" { + numbers = sharedCIEvidence().resolvePRs(p.Repository.FullName, res.HeadSHA, numbers) + } // Capture the failed-check identity and the PRs it resolved to BEFORE // invalidation drops their cache entries: the receiver reacts to a red // check on the event rather than on the next sweep. @@ -588,6 +663,7 @@ func (c *Client) HandleWebhookEvent(event string, payload []byte) (WebhookEventR } sharedWebhookTracker().recordInvalidations(len(numbers)) res.Invalidated = numbers + res.CIEvidence = c.applyWebhookCIEvidence(event, p, configured, numbers) return res, nil } @@ -732,6 +808,10 @@ func webhookHeadSHA(event string, p webhookEventPayload) string { if p.WorkflowRun != nil { return strings.TrimSpace(p.WorkflowRun.HeadSHA) } + case "workflow_job": + if p.WorkflowJob != nil { + return strings.TrimSpace(p.WorkflowJob.HeadSHA) + } } return "" } @@ -789,6 +869,10 @@ func webhookInvalidationTargets(event string, p webhookEventPayload, aliases []s addRefs(p.WorkflowRun.PullRequests) bySHA(p.WorkflowRun.HeadSHA) } + case "workflow_job": + if p.WorkflowJob != nil { + bySHA(p.WorkflowJob.HeadSHA) + } case "push": if ref, ok := strings.CutPrefix(p.Ref, "refs/heads/"); ok && ref != "" { addMatching(func(_, headRef, baseRef string) bool { return headRef == ref || baseRef == ref }) diff --git a/src/pkg/github/webhook_ci_evidence.go b/src/pkg/github/webhook_ci_evidence.go new file mode 100644 index 0000000000..57913dbb64 --- /dev/null +++ b/src/pkg/github/webhook_ci_evidence.go @@ -0,0 +1,732 @@ +package github + +import ( + "context" + "sort" + "strings" + "sync" + "sync/atomic" + "time" + + gh "github.com/google/go-github/v72/github" +) + +// Webhook-driven CI evidence (hivecommons/hive#16521). +// +// The Activity Monitor's CI chevron counts check runs on a PR's current head +// (PullRequest.CIActivity). Until now that evidence had one producer, the +// per-cycle check-run listing in EnrichCIStatus, so a row read "CI ?/?" +// from the moment a head was pushed until the next full cycle, and a +// sidelined PR never had any. A `check_run`, `workflow_job` or `status` +// delivery already names the head, the check and its result, so the payload +// is applied straight into a per-(repo, head) check set here. The tracker +// merges that set over whatever the last listing produced, and a head no +// listing has covered yet is re-read once, coalesced, so the required-check +// set behind the counts becomes known. The listing stays as the backstop +// for spokes without webhooks. + +// githubActionsAppID is the App id GitHub Actions reports on its check runs; +// a `workflow_job` payload carries no app object, but its check run is +// always this producer's. +const githubActionsAppID int64 = 15368 + +// ciEvidenceMaxHeads bounds the per-head store; the oldest-touched heads are +// evicted first. ciEvidenceTTL drops heads no delivery or listing has +// touched for that long (a merged PR's head, for one). +const ( + ciEvidenceMaxHeads = 4096 + ciEvidenceTTL = 48 * time.Hour +) + +// webhookCIEventTypes are the deliveries that carry CI evidence for a head. +var webhookCIEventTypes = map[string]bool{ + "check_run": true, + "check_suite": true, + "status": true, + "workflow_run": true, + "workflow_job": true, +} + +// IsWebhookCIEvent reports whether event is one of the check/status/workflow +// deliveries that drive CI evidence. +func IsWebhookCIEvent(event string) bool { return webhookCIEventTypes[event] } + +// WebhookCIEvidence reports what a CI delivery did to the per-head check set. +type WebhookCIEvidence struct { + HeadSHA string `json:"head_sha"` + // Applied is the number of check observations taken from the payload + // itself (check_run, workflow_job, status). A check_suite or + // workflow_run payload carries none. + Applied int `json:"applied"` + // Refetch is set when the head had no check-run listing yet (no + // required-check set to count against) and one coalesced re-read of + // the head's check runs was scheduled. + Refetch bool `json:"refetch,omitempty"` + // PRs are the open PR numbers the head resolved to. + PRs []int `json:"prs,omitempty"` +} + +type ciHeadKey struct { + repo string // canonical owner/name, lower-case + head string // lower-case SHA +} + +type ciHeadEvidence struct { + // prs are the open PR numbers deliveries resolved this head to. A + // check delivery invalidates the PR detail cache, so a later `status` + // or `workflow_job` payload (which names no PR) could not resolve the + // head by SHA without this memory. + prs map[int]bool + // listed is the last complete observation of the head (check-run + // listing plus required set and statuses), nil until one ran. + listed *PRCIActivity + // events holds the checks taken from deliveries, newest per identity. + events []PRActivityCheck + eventsAt time.Time + touched time.Time +} + +type ciEvidenceStore struct { + mu sync.Mutex + now func() time.Time + heads map[ciHeadKey]*ciHeadEvidence +} + +func newCIEvidenceStore() *ciEvidenceStore { + return &ciEvidenceStore{now: time.Now, heads: map[ciHeadKey]*ciHeadEvidence{}} +} + +var ciEvidencePtr = func() *atomic.Pointer[ciEvidenceStore] { + p := &atomic.Pointer[ciEvidenceStore]{} + p.Store(newCIEvidenceStore()) + return p +}() + +func sharedCIEvidence() *ciEvidenceStore { return ciEvidencePtr.Load() } + +// ResetCIEvidenceForTest swaps in an empty per-head CI evidence store with +// the given clock and returns a restore func. +func ResetCIEvidenceForTest(now func() time.Time) func() { + old := sharedCIEvidence() + s := newCIEvidenceStore() + if now != nil { + s.now = now + } + ciEvidencePtr.Store(s) + return func() { ciEvidencePtr.Store(old) } +} + +func ciEvidenceKey(repo, head string) ciHeadKey { + return ciHeadKey{repo: canonicalPRDetailRepo(repo), head: strings.ToLower(strings.TrimSpace(head))} +} + +// entryLocked returns the head's record, creating it when create is set. A +// bare repo name (no owner) matches the one stored full name ending in it. +func (s *ciEvidenceStore) entryLocked(key ciHeadKey, create bool) *ciHeadEvidence { + if e, ok := s.heads[key]; ok { + return e + } + if !strings.Contains(key.repo, "/") { + for k, e := range s.heads { + if k.head == key.head && strings.HasSuffix(k.repo, "/"+key.repo) { + return e + } + } + } + if !create { + return nil + } + e := &ciHeadEvidence{} + s.heads[key] = e + return e +} + +func (s *ciEvidenceStore) pruneLocked(now time.Time) { + for k, e := range s.heads { + if now.Sub(e.touched) >= ciEvidenceTTL { + delete(s.heads, k) + } + } + for len(s.heads) > ciEvidenceMaxHeads { + var oldestKey ciHeadKey + var oldest time.Time + first := true + for k, e := range s.heads { + if first || e.touched.Before(oldest) { + oldestKey, oldest, first = k, e.touched, false + } + } + delete(s.heads, oldestKey) + } +} + +// recordListed stores a complete observation of the head. Event checks +// older than the listing are dropped: the listing already reflects them. +func (s *ciEvidenceStore) recordListed(repo, head string, a *PRCIActivity) { + if a == nil || strings.TrimSpace(head) == "" || !strings.EqualFold(a.Head, head) { + return + } + s.mu.Lock() + defer s.mu.Unlock() + now := s.now() + e := s.entryLocked(ciEvidenceKey(repo, head), true) + e.listed = cloneCIActivity(a) + e.touched = now + if a.ObservedAt != nil && !e.eventsAt.After(*a.ObservedAt) { + e.events, e.eventsAt = nil, time.Time{} + } + s.pruneLocked(now) +} + +// hasListed reports whether a check-run listing has covered the head. +func (s *ciEvidenceStore) hasListed(repo, head string) bool { + s.mu.Lock() + defer s.mu.Unlock() + e := s.entryLocked(ciEvidenceKey(repo, head), false) + return e != nil && e.listed != nil +} + +// applyEvent upserts the checks a delivery reported for the head and +// returns how many it kept. +func (s *ciEvidenceStore) applyEvent(repo, head string, checks []PRActivityCheck) int { + if strings.TrimSpace(head) == "" || len(checks) == 0 { + return 0 + } + s.mu.Lock() + defer s.mu.Unlock() + now := s.now() + e := s.entryLocked(ciEvidenceKey(repo, head), true) + applied := 0 + for _, check := range checks { + if check.Name == "" { + continue + } + check.Head = head + e.events = upsertActivityCheck(e.events, check) + applied++ + } + if applied > 0 { + e.eventsAt = now + } + e.touched = now + s.pruneLocked(now) + return applied +} + +// resolvePRs remembers the PRs a CI delivery resolved the head to and +// returns them joined with the ones earlier deliveries remembered, sorted. +func (s *ciEvidenceStore) resolvePRs(repo, head string, numbers []int) []int { + if strings.TrimSpace(head) == "" { + return numbers + } + s.mu.Lock() + defer s.mu.Unlock() + now := s.now() + e := s.entryLocked(ciEvidenceKey(repo, head), true) + if e.prs == nil { + e.prs = map[int]bool{} + } + for _, n := range numbers { + if n > 0 { + e.prs[n] = true + } + } + e.touched = now + out := make([]int, 0, len(e.prs)) + for n := range e.prs { + out = append(out, n) + } + sort.Ints(out) + s.pruneLocked(now) + return out +} + +// touch keeps the head alive without evidence (check_suite, workflow_run). +func (s *ciEvidenceStore) touch(repo, head string) { + if strings.TrimSpace(head) == "" { + return + } + s.mu.Lock() + defer s.mu.Unlock() + now := s.now() + e := s.entryLocked(ciEvidenceKey(repo, head), true) + e.touched = now + s.pruneLocked(now) +} + +// merged returns the head's evidence: the newest listing (the store's or the +// caller's base, whichever observed later) with the delivered checks laid +// over it. Nil when neither exists. +func (s *ciEvidenceStore) merged(repo, head string, base *PRCIActivity) *PRCIActivity { + if strings.TrimSpace(head) == "" { + return base + } + if base != nil && !strings.EqualFold(base.Head, head) { + base = nil + } + s.mu.Lock() + e := s.entryLocked(ciEvidenceKey(repo, head), false) + var listed *PRCIActivity + var events []PRActivityCheck + var eventsAt time.Time + if e != nil { + listed = e.listed + events = append([]PRActivityCheck(nil), e.events...) + eventsAt = e.eventsAt + } + s.mu.Unlock() + if base != nil && (listed == nil || observedAfter(base.ObservedAt, listed.ObservedAt)) { + listed = base + } + if len(events) == 0 { + return listed + } + var out *PRCIActivity + if listed != nil { + out = cloneCIActivity(listed) + if out.ObservedAt != nil && !eventsAt.After(*out.ObservedAt) { + return out + } + } else { + out = &PRCIActivity{Head: head} + } + applied := 0 + for _, check := range events { + out.Checks = upsertActivityCheck(out.Checks, check) + applied++ + } + if applied > 0 { + at := eventsAt.UTC() + out.ObservedAt = &at + switch { + case out.Source == "": + out.Source = "github-webhook" + case !strings.Contains(out.Source, "github-webhook"): + out.Source += "+github-webhook" + } + sort.SliceStable(out.Checks, func(i, j int) bool { return out.Checks[i].Name < out.Checks[j].Name }) + } + return out +} + +func observedAfter(a, b *time.Time) bool { + if a == nil { + return false + } + if b == nil { + return true + } + return a.After(*b) +} + +// upsertActivityCheck places check into list the way GitHub's own "latest" +// listing would: the same run (node id) is updated in place, a newer run of +// the same check (kind, name, producing App) replaces the older one, an +// older observation than the one held is dropped, and anything else is +// appended. +func upsertActivityCheck(list []PRActivityCheck, check PRActivityCheck) []PRActivityCheck { + for i := range list { + if check.NodeID != "" && list[i].NodeID == check.NodeID { + if checkBefore(check, list[i]) { + return list + } + list[i] = check + return list + } + } + for i := range list { + if !sameCheckIdentity(list[i], check) { + continue + } + if checkBefore(check, list[i]) { + return list + } + list[i] = check + return list + } + return append(list, check) +} + +// checkBefore reports whether a is a strictly older observation than b; +// an observation without a time is never older. +func checkBefore(a, b PRActivityCheck) bool { + return a.Since != nil && b.Since != nil && a.Since.Before(*b.Since) +} + +// sameCheckIdentity matches two runs of one check by kind, name and +// producing App (an unknown App matches any). +func sameCheckIdentity(a, b PRActivityCheck) bool { + if a.Kind != b.Kind || a.Name != b.Name { + return false + } + if a.AppID == nil || b.AppID == nil { + return true + } + return *a.AppID == *b.AppID +} + +func cloneCIActivity(a *PRCIActivity) *PRCIActivity { + if a == nil { + return nil + } + out := *a + out.Required = append([]string(nil), a.Required...) + out.RequiredChecks = append([]PRRequiredCheck(nil), a.RequiredChecks...) + out.Checks = append([]PRActivityCheck(nil), a.Checks...) + return &out +} + +// HeadCIActivity is the tracker's read: the CI evidence for repo's head, +// combining the last listing (base, from the status snapshot, or the +// store's own) with every check delivered since. It returns base unchanged +// when no delivery has named the head. +func HeadCIActivity(repo, head string, base *PRCIActivity) *PRCIActivity { + return sharedCIEvidence().merged(repo, head, base) +} + +// webhookCIChecks extracts the check observations a CI delivery carries. +func webhookCIChecks(event string, p webhookEventPayload) []PRActivityCheck { + switch event { + case "check_run": + cr := p.CheckRun + if cr == nil || strings.TrimSpace(cr.Name) == "" { + return nil + } + check := PRActivityCheck{ + Name: strings.TrimSpace(cr.Name), State: cr.Status, Conclusion: cr.Conclusion, + URL: firstNonEmpty(cr.DetailsURL, cr.HTMLURL), ID: cr.ID, NodeID: cr.NodeID, Head: strings.TrimSpace(cr.HeadSHA), + } + if cr.App != nil && cr.App.ID > 0 { + id := cr.App.ID + check.AppID = &id + } + check.Since = checkSince(cr.Status, cr.StartedAt, cr.CompletedAt) + return []PRActivityCheck{check} + case "workflow_job": + job := p.WorkflowJob + if job == nil || strings.TrimSpace(job.Name) == "" { + return nil + } + id := githubActionsAppID + check := PRActivityCheck{ + Name: strings.TrimSpace(job.Name), State: job.Status, Conclusion: job.Conclusion, + URL: job.HTMLURL, ID: job.ID, NodeID: job.NodeID, AppID: &id, Head: strings.TrimSpace(job.HeadSHA), + } + check.Since = checkSince(job.Status, job.StartedAt, job.CompletedAt) + return []PRActivityCheck{check} + case "status": + if strings.TrimSpace(p.Context) == "" { + return nil + } + check := PRActivityCheck{ + Name: strings.TrimSpace(p.Context), State: "completed", URL: p.TargetURL, + ID: p.ID, NodeID: p.NodeID, Head: strings.TrimSpace(p.SHA), Kind: "commit_status", + } + switch p.State { + case "success": + check.Conclusion = "success" + case "failure", "error": + check.Conclusion = "failure" + case "pending": + check.State = "pending" + default: + check.State = "unknown" + } + if at := firstTime(p.UpdatedAt, p.CreatedAt); at != nil { + check.Since = at + } + return []PRActivityCheck{check} + } + return nil +} + +func checkSince(status string, started, completed *time.Time) *time.Time { + if status == "completed" && completed != nil && !completed.IsZero() { + t := completed.UTC() + return &t + } + if started != nil && !started.IsZero() { + t := started.UTC() + return &t + } + return nil +} + +func firstTime(ts ...*time.Time) *time.Time { + for _, t := range ts { + if t != nil && !t.IsZero() { + u := t.UTC() + return &u + } + } + return nil +} + +// applyWebhookCIEvidence is HandleWebhookEvent's CI step: it applies the +// payload's checks to the head's set and, when no listing has covered the +// head, schedules one coalesced re-read of its check runs. numbers are the +// open PRs the delivery resolved to; the first one's base branch names the +// required-check policy the re-read resolves. +func (c *Client) applyWebhookCIEvidence(event string, p webhookEventPayload, configured string, numbers []int) *WebhookCIEvidence { + if !IsWebhookCIEvent(event) { + return nil + } + head := webhookHeadSHA(event, p) + if head == "" && p.WorkflowJob != nil { + head = strings.TrimSpace(p.WorkflowJob.HeadSHA) + } + if head == "" { + return nil + } + repo := p.Repository.FullName + sharedWebhookTracker().recordCIEvent() + out := &WebhookCIEvidence{HeadSHA: head, PRs: append([]int(nil), numbers...)} + store := sharedCIEvidence() + checks := webhookCIChecks(event, p) + if len(checks) > 0 { + out.Applied = store.applyEvent(repo, head, checks) + sharedWebhookTracker().recordCIApplied(out.Applied) + } else { + store.touch(repo, head) + } + if len(numbers) > 0 && !store.hasListed(repo, head) { + target := ciRefetchTarget{repo: configured, fullName: repo, head: head, number: numbers[0]} + for _, alias := range webhookRepoAliases(configured, repo) { + if pr, _, ok := sharedPRDetailCache.peek(alias, numbers[0]); ok { + target.baseRef = pr.GetBase().GetRef() + break + } + } + out.Refetch = c.ciRefetcher().enqueue(target) + } + if hook := ciEvidenceHook(); hook != nil && (out.Applied > 0) { + hook(repo, head) + } + return out +} + +// CIEvidenceHook is told when a head's CI evidence changed (a delivery was +// applied or a re-read landed) so the dashboard can re-derive the row. +type CIEvidenceHook func(repo, head string) + +var ( + ciEvidenceHookMu sync.RWMutex + ciEvidenceHookFn CIEvidenceHook +) + +// SetCIEvidenceHook installs (or, with nil, removes) the process-wide hook. +func SetCIEvidenceHook(fn CIEvidenceHook) { + ciEvidenceHookMu.Lock() + ciEvidenceHookFn = fn + ciEvidenceHookMu.Unlock() +} + +func ciEvidenceHook() CIEvidenceHook { + ciEvidenceHookMu.RLock() + defer ciEvidenceHookMu.RUnlock() + return ciEvidenceHookFn +} + +// ciRefetchTarget is one head whose check runs are to be listed once. +type ciRefetchTarget struct { + repo string // as configured (the detail cache's key) + fullName string // owner/name from the payload + head string + number int + baseRef string +} + +// ciRefetchTimer is the one-shot debounce timer; tests hand-fire it. +type ciRefetchTimer interface{ Stop() bool } + +// ciRefetcher coalesces head re-reads: a check fan-out of twenty deliveries +// on a fresh head costs one listing, not twenty. +type ciRefetcher struct { + mu sync.Mutex + pending map[ciHeadKey]ciRefetchTarget + timer ciRefetchTimer + debounce time.Duration + newTimer func(d time.Duration, f func()) ciRefetchTimer + fetch func(ctx context.Context, t ciRefetchTarget) error + ctx func() context.Context + // flushes counts debounce windows that fired (tests). + flushes int +} + +const defaultCIRefetchDebounce = 2 * time.Second + +// webhookCIRefetchRESTCaller tags the check-run listings made on behalf of +// CI deliveries so /api/gh-rate-limits lists them on their own. +const webhookCIRefetchRESTCaller = "hive:webhook_ci_refetch" + +func (c *Client) ciRefetcher() *ciRefetcher { + c.ciRefetchOnce.Do(func() { + if c.ciRefetch == nil { + c.ciRefetch = &ciRefetcher{} + } + r := c.ciRefetch + if r.pending == nil { + r.pending = map[ciHeadKey]ciRefetchTarget{} + } + if r.debounce <= 0 { + r.debounce = defaultCIRefetchDebounce + } + if r.newTimer == nil { + r.newTimer = func(d time.Duration, f func()) ciRefetchTimer { return time.AfterFunc(d, f) } + } + if r.fetch == nil { + r.fetch = c.refetchHeadCI + } + if r.ctx == nil { + r.ctx = context.Background + } + }) + return c.ciRefetch +} + +// enqueue queues one head; a head already waiting keeps its slot (the PR +// number and base ref of the first delivery). It reports whether the head +// is now pending. +func (r *ciRefetcher) enqueue(t ciRefetchTarget) bool { + if r == nil || t.head == "" || t.number <= 0 { + return false + } + r.mu.Lock() + defer r.mu.Unlock() + key := ciEvidenceKey(t.fullName, t.head) + if cur, ok := r.pending[key]; ok { + if cur.baseRef == "" && t.baseRef != "" { + cur.baseRef = t.baseRef + r.pending[key] = cur + } + return true + } + r.pending[key] = t + if r.timer == nil { + r.timer = r.newTimer(r.debounce, r.flush) + } + return true +} + +func (r *ciRefetcher) pendingCount() int { + r.mu.Lock() + defer r.mu.Unlock() + return len(r.pending) +} + +// flush lists every pending head once. Heads enqueued while the listings +// run start a new window. +func (r *ciRefetcher) flush() { + r.mu.Lock() + targets := make([]ciRefetchTarget, 0, len(r.pending)) + for _, t := range r.pending { + targets = append(targets, t) + } + r.pending = map[ciHeadKey]ciRefetchTarget{} + r.timer = nil + r.flushes++ + fetch, ctxFn := r.fetch, r.ctx + r.mu.Unlock() + if fetch == nil { + return + } + ctx := context.Background() + if ctxFn != nil { + if c := ctxFn(); c != nil { + ctx = c + } + } + sort.Slice(targets, func(i, j int) bool { + if targets[i].fullName != targets[j].fullName { + return targets[i].fullName < targets[j].fullName + } + return targets[i].number < targets[j].number + }) + for _, t := range targets { + if ctx.Err() != nil { + return + } + _ = fetch(ctx, t) + } +} + +// refetchHeadCI lists the head's check runs once, resolves the base branch's +// required-check policy (one cached GraphQL query per repo@branch) and the +// commit statuses it needs, records the result as the head's listing and +// tells the hook. It refuses to spend when the REST budget is critical or +// GitHub is pacing the hive; the next eval cycle is then the backstop. +func (c *Client) refetchHeadCI(ctx context.Context, t ciRefetchTarget) error { + if c == nil { + return ErrNoGitHubClient + } + if mode, _ := c.APIBudgetMode(); mode == APIBudgetCritical { + return ErrAPIBudgetCritical + } + if RateLimitCautionEngaged() { + return ErrRateLimitCaution + } + owner, name := c.splitRepo(firstNonEmpty(t.fullName, t.repo)) + ctx = WithRESTCaller(ctx, webhookCIRefetchRESTCaller) + var all []*gh.CheckRun + options := &gh.ListCheckRunsOptions{ListOptions: gh.ListOptions{PerPage: 100}} + for { + runs, resp, err := c.client.Checks.ListCheckRunsForRef(ctx, owner, name, t.head, options) + if err != nil || runs == nil || runs.CheckRuns == nil { + c.logger.Warn("webhook CI refetch: failed to list check runs", "repo", owner+"/"+name, "pr", t.number, "head", t.head, "error", err) + if err == nil { + err = ErrNoGitHubClient + } + return err + } + all = append(all, runs.CheckRuns...) + if resp == nil || resp.NextPage == 0 { + break + } + options.Page = resp.NextPage + } + at := sharedWebhookTracker().now().UTC() + activity := prCIActivity(t.head, "github-check-runs", activityCheckRunsByNameAndApp(checkRunsOnHead(all, t.head)), &at) + if t.baseRef != "" { + pr := &PullRequest{Repo: owner + "/" + name, Number: t.number, HeadSHA: t.head, BaseRef: t.baseRef, CIActivity: activity} + newProtectionCollector(c).attachRequiredChecks(ctx, pr) + } + sharedCIEvidence().recordListed(owner+"/"+name, t.head, activity) + sharedWebhookTracker().recordCIRefetches(1) + if hook := ciEvidenceHook(); hook != nil { + hook(owner+"/"+name, t.head) + } + return nil +} + +// FlushWebhookCIRefetch runs any pending head re-reads now, synchronously, +// instead of waiting out the debounce. The governor calls it before a +// webhook-wake cycle; tests use it in place of a timer. +func (c *Client) FlushWebhookCIRefetch() { + if c == nil || c.ciRefetch == nil { + return + } + r := c.ciRefetcher() + r.mu.Lock() + if len(r.pending) == 0 { + r.mu.Unlock() + return + } + if r.timer != nil { + r.timer.Stop() + } + r.mu.Unlock() + r.flush() +} + +// RecordCIEvidenceForTest applies checks to repo's head as a delivery would, +// so tests outside this package can drive the tracker's merged read. +func RecordCIEvidenceForTest(repo, head string, checks []PRActivityCheck) int { + return sharedCIEvidence().applyEvent(repo, head, checks) +} + +// RecordCIListingForTest records a complete check-run listing for repo's +// head, as the enrich path or a webhook re-read would, so tests can exercise +// the payload-only delivery path that needs no re-read. +func RecordCIListingForTest(repo, head string, activity *PRCIActivity) { + sharedCIEvidence().recordListed(repo, head, activity) +} diff --git a/src/pkg/github/webhook_ci_evidence_test.go b/src/pkg/github/webhook_ci_evidence_test.go new file mode 100644 index 0000000000..446b9eb1f4 --- /dev/null +++ b/src/pkg/github/webhook_ci_evidence_test.go @@ -0,0 +1,298 @@ +package github + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "reflect" + "strings" + "testing" + "time" + + gh "github.com/google/go-github/v72/github" +) + +func ciTestCheckRunBody(number int, head, name, status, conclusion string, at time.Time) []byte { + completed := "null" + if status == "completed" { + completed = fmt.Sprintf("%q", at.Format(time.RFC3339)) + } + return []byte(fmt.Sprintf(`{"action":%q,"repository":{"full_name":"org/repo"},"check_run":{"id":%d,"node_id":"CR_%s_%d","head_sha":%q,"name":%q,"status":%q,"conclusion":%q,"details_url":"https://ci/%s","started_at":%q,"completed_at":%s,"pull_requests":[{"number":%d}],"app":{"id":15368,"name":"GitHub Actions"}}}`, + status, len(name)*100+number, name, len(name), head, name, status, conclusion, name, at.Add(-time.Minute).Format(time.RFC3339), completed, number)) +} + +type ciTestTimer struct{ fired bool } + +func (t *ciTestTimer) Stop() bool { return true } + +// A check_run delivery is applied to the head's check set from the payload +// alone — no GitHub call — and the merged view the tracker reads counts it +// at once; the head's one coalesced re-read is queued because no listing +// has covered it yet. +func TestHandleWebhookEventCheckRunAppliesCIEvidenceFromPayload(t *testing.T) { + c, clock := setupWebhookCacheTest(t) + t.Cleanup(ResetCIEvidenceForTest(clock.Now)) + var fired func() + c.ciRefetch = &ciRefetcher{newTimer: func(_ time.Duration, f func()) ciRefetchTimer { fired = f; return &ciTestTimer{} }} + c.storePRDetail("repo", 7, testPRDetail(7, "sha-7", clock.now, "clean")) + + res, err := c.HandleWebhookEvent("check_run", ciTestCheckRunBody(7, "sha-7", "unit", "in_progress", "", clock.now)) + if err != nil { + t.Fatal(err) + } + if res.CIEvidence == nil || res.CIEvidence.Applied != 1 || !res.CIEvidence.Refetch || !reflect.DeepEqual(res.CIEvidence.PRs, []int{7}) || res.CIEvidence.HeadSHA != "sha-7" { + t.Fatalf("ci_evidence = %+v, want applied 1, refetch, PRs [7]", res.CIEvidence) + } + if fired == nil || c.ciRefetch.pendingCount() != 1 { + t.Fatalf("expected one pending head re-read, pending=%d timer armed=%v", c.ciRefetch.pendingCount(), fired != nil) + } + + clock.now = clock.now.Add(30 * time.Second) + if _, err := c.HandleWebhookEvent("check_run", ciTestCheckRunBody(7, "sha-7", "unit", "completed", "success", clock.now)); err != nil { + t.Fatal(err) + } + if _, err := c.HandleWebhookEvent("check_run", ciTestCheckRunBody(7, "sha-7", "lint", "completed", "failure", clock.now)); err != nil { + t.Fatal(err) + } + if c.ciRefetch.pendingCount() != 1 { + t.Fatalf("fan-out must coalesce to one pending head, got %d", c.ciRefetch.pendingCount()) + } + + got := HeadCIActivity("org/repo", "sha-7", nil) + if got == nil || got.Source != "github-webhook" || got.ObservedAt == nil || !got.ObservedAt.Equal(clock.now) { + t.Fatalf("merged activity = %+v, want webhook-sourced, observed at the last delivery", got) + } + want := map[string]string{"unit": "completed/success", "lint": "completed/failure"} + if len(got.Checks) != len(want) { + t.Fatalf("checks = %+v, want %v", got.Checks, want) + } + for _, check := range got.Checks { + if want[check.Name] != check.State+"/"+check.Conclusion { + t.Fatalf("check %s = %s/%s, want %s", check.Name, check.State, check.Conclusion, want[check.Name]) + } + if check.Head != "sha-7" || check.AppID == nil || *check.AppID != githubActionsAppID { + t.Fatalf("check %+v must carry the head and producing App", check) + } + } + // A bare configured name resolves to the same head. + if bare := HeadCIActivity("repo", "sha-7", nil); bare == nil || len(bare.Checks) != 2 { + t.Fatalf("bare repo lookup = %+v", bare) + } + // An out-of-order re-delivery of the older in_progress run must not + // roll the completed result back. + if _, err := c.HandleWebhookEvent("check_run", ciTestCheckRunBody(7, "sha-7", "unit", "in_progress", "", clock.now.Add(-time.Hour))); err != nil { + t.Fatal(err) + } + for _, check := range HeadCIActivity("org/repo", "sha-7", nil).Checks { + if check.Name == "unit" && check.Conclusion != "success" { + t.Fatalf("stale re-delivery rolled unit back: %+v", check) + } + } + health := WebhookHealthSnapshot() + if health.CIEvents1h != 4 || health.CIEventsApplied1h != 4 || health.LastCIEventAt == nil || health.CIRefetches1h != 0 { + t.Fatalf("webhook health CI block = %+v", health) + } +} + +// Delivered checks lay over the cycle's listing: a newer delivery replaces +// the listed run of the same check, the required set the listing resolved +// is kept, and a listing taken after the deliveries supersedes them. +func TestHeadCIActivityMergesDeliveriesOverListing(t *testing.T) { + clock := &webhookTestClock{now: time.Date(2026, 11, 3, 12, 0, 0, 0, time.UTC)} + t.Cleanup(ResetCIEvidenceForTest(clock.Now)) + t.Cleanup(resetWebhookTrackerForTest(clock.Now)) + listedAt := clock.now + appID := githubActionsAppID + listed := &PRCIActivity{Head: "sha-1", Source: "github-check-runs", ObservedAt: &listedAt, + RequiredKnown: true, Required: []string{"lint", "unit"}, RequiredSource: "protection", + RequiredChecks: []PRRequiredCheck{{Name: "lint", AppKnown: true, AppID: &appID}, {Name: "unit", AppKnown: true, AppID: &appID}}, + Checks: []PRActivityCheck{ + {Name: "lint", State: "completed", Conclusion: "success", NodeID: "CR_lint", AppID: &appID, Head: "sha-1"}, + {Name: "unit", State: "in_progress", NodeID: "CR_unit", AppID: &appID, Head: "sha-1"}, + }} + sharedCIEvidence().recordListed("org/repo", "sha-1", listed) + + // The tracker's base (the status snapshot's copy) is the same listing. + if got := HeadCIActivity("org/repo", "sha-1", listed); !reflect.DeepEqual(got, listed) { + t.Fatalf("no deliveries: merged = %+v, want the listing unchanged", got) + } + + clock.now = clock.now.Add(time.Minute) + done := clock.now + n := RecordCIEvidenceForTest("org/repo", "sha-1", []PRActivityCheck{{Name: "unit", State: "completed", Conclusion: "success", NodeID: "CR_unit", AppID: &appID, Since: &done}}) + if n != 1 { + t.Fatalf("applied = %d", n) + } + got := HeadCIActivity("org/repo", "sha-1", listed) + if got.Source != "github-check-runs+github-webhook" || !got.RequiredKnown || !reflect.DeepEqual(got.Required, []string{"lint", "unit"}) { + t.Fatalf("merged = %+v, want listing's required set with webhook source", got) + } + if len(got.Checks) != 2 { + t.Fatalf("checks = %+v, want unit replaced not appended", got.Checks) + } + for _, check := range got.Checks { + if check.Name == "unit" && (check.State != "completed" || check.Conclusion != "success") { + t.Fatalf("unit = %+v, want the delivered completion", check) + } + } + if len(listed.Checks) != 2 || listed.Checks[1].State != "in_progress" { + t.Fatalf("merge mutated the caller's base: %+v", listed.Checks) + } + if got.ObservedAt == nil || !got.ObservedAt.Equal(done) { + t.Fatalf("observed_at = %v, want the delivery time %v", got.ObservedAt, done) + } + + // A fresher listing (the next cycle) carries the delivered result + // itself; the stored deliveries are dropped and the listing wins. + clock.now = clock.now.Add(time.Minute) + relistedAt := clock.now + relisted := cloneCIActivity(listed) + relisted.ObservedAt = &relistedAt + relisted.Checks[1] = PRActivityCheck{Name: "unit", State: "completed", Conclusion: "failure", NodeID: "CR_unit", AppID: &appID, Head: "sha-1"} + sharedCIEvidence().recordListed("org/repo", "sha-1", relisted) + if got := HeadCIActivity("org/repo", "sha-1", nil); !strings.HasSuffix(got.Source, "github-check-runs") || got.Checks[1].Conclusion != "failure" { + t.Fatalf("after re-listing merged = %+v, want the listing alone", got) + } + // A stale base from the snapshot (older than the store's listing) does + // not win over it either. + if got := HeadCIActivity("org/repo", "sha-1", listed); got.Checks[1].Conclusion != "failure" { + t.Fatalf("older base won over the newer listing: %+v", got.Checks) + } + // Another head is untouched. + if got := HeadCIActivity("org/repo", "sha-2", nil); got != nil { + t.Fatalf("other head = %+v, want nil", got) + } +} + +// A `status` delivery becomes a commit_status observation (the legacy +// context side the required-check merge reads), and a `workflow_job` +// delivery is the Actions check run it describes. +func TestWebhookCIChecksStatusAndWorkflowJob(t *testing.T) { + c, clock := setupWebhookCacheTest(t) + t.Cleanup(ResetCIEvidenceForTest(clock.Now)) + c.ciRefetch = &ciRefetcher{newTimer: func(_ time.Duration, f func()) ciRefetchTimer { return &ciTestTimer{} }} + c.storePRDetail("repo", 3, testPRDetail(3, "sha-3", clock.now, "clean")) + + status := []byte(`{"id":91,"node_id":"SC_91","sha":"sha-3","state":"success","context":"dco","target_url":"https://dco/3","created_at":"2026-10-08T12:00:00Z","repository":{"full_name":"org/repo"}}`) + res, err := c.HandleWebhookEvent("status", status) + if err != nil || res.CIEvidence == nil || res.CIEvidence.Applied != 1 { + t.Fatalf("status delivery: res=%+v err=%v", res.CIEvidence, err) + } + job := []byte(`{"action":"completed","repository":{"full_name":"org/repo"},"workflow_job":{"id":501,"node_id":"WJ_501","head_sha":"sha-3","name":"gate","status":"completed","conclusion":"success","html_url":"https://gh/job/501","started_at":"2026-10-08T11:58:00Z","completed_at":"2026-10-08T12:00:00Z"}}`) + res, err = c.HandleWebhookEvent("workflow_job", job) + if err != nil || res.CIEvidence == nil || res.CIEvidence.Applied != 1 || !reflect.DeepEqual(res.Invalidated, []int{3}) { + t.Fatalf("workflow_job delivery: res=%+v invalidated=%v err=%v", res.CIEvidence, res.Invalidated, err) + } + got := HeadCIActivity("org/repo", "sha-3", nil) + if got == nil || len(got.Checks) != 2 { + t.Fatalf("merged = %+v", got) + } + byName := map[string]PRActivityCheck{} + for _, check := range got.Checks { + byName[check.Name] = check + } + if dco := byName["dco"]; dco.Kind != "commit_status" || dco.State != "completed" || dco.Conclusion != "success" || dco.NodeID != "SC_91" { + t.Fatalf("dco = %+v", dco) + } + if gate := byName["gate"]; gate.AppID == nil || *gate.AppID != githubActionsAppID || gate.Conclusion != "success" || gate.URL != "https://gh/job/501" { + t.Fatalf("gate = %+v", gate) + } + // check_suite carries no per-check result: nothing applied, head kept + // pending for its re-read, and a non-CI event reports no evidence. + suite := []byte(`{"action":"completed","repository":{"full_name":"org/repo"},"check_suite":{"head_sha":"sha-3","conclusion":"success","pull_requests":[{"number":3}],"app":{"name":"GitHub Actions"}}}`) + res, err = c.HandleWebhookEvent("check_suite", suite) + if err != nil || res.CIEvidence == nil || res.CIEvidence.Applied != 0 || !res.CIEvidence.Refetch { + t.Fatalf("check_suite delivery: res=%+v err=%v", res.CIEvidence, err) + } + pr := []byte(`{"action":"synchronize","number":3,"pull_request":{"number":3,"state":"open"},"repository":{"full_name":"org/repo"}}`) + if res, err := c.HandleWebhookEvent("pull_request", pr); err != nil || res.CIEvidence != nil { + t.Fatalf("pull_request delivery must carry no CI evidence: %+v err=%v", res.CIEvidence, err) + } +} + +// The coalesced re-read lists the head's check runs once for any number +// of deliveries, records the result as the head's listing (so later +// deliveries lay over it) and tells the hook; its REST caller tag is its +// own. Required checks are resolved only when the PR's base is known. +func TestWebhookCIRefetchListsHeadOnceAndRecordsListing(t *testing.T) { + clock := &webhookTestClock{now: time.Date(2026, 10, 8, 12, 0, 0, 0, time.UTC)} + t.Cleanup(resetPRDetailCacheForTest(clock.Now, 0)) + t.Cleanup(resetWebhookTrackerForTest(clock.Now)) + t.Cleanup(ResetCIEvidenceForTest(clock.Now)) + listings := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/org/repo/commits/sha-9/check-runs" { + t.Errorf("unexpected GitHub call %s %s", r.Method, r.URL.Path) + w.WriteHeader(http.StatusInternalServerError) + return + } + listings++ + _ = json.NewEncoder(w).Encode(gh.ListCheckRunsResults{Total: gh.Ptr(2), CheckRuns: []*gh.CheckRun{ + {ID: gh.Ptr(int64(1)), NodeID: gh.Ptr("CR_1"), Name: gh.Ptr("unit"), Status: gh.Ptr("completed"), Conclusion: gh.Ptr("success"), HeadSHA: gh.Ptr("sha-9"), App: &gh.App{ID: gh.Ptr(githubActionsAppID)}}, + {ID: gh.Ptr(int64(2)), NodeID: gh.Ptr("CR_2"), Name: gh.Ptr("lint"), Status: gh.Ptr("in_progress"), HeadSHA: gh.Ptr("sha-9"), App: &gh.App{ID: gh.Ptr(githubActionsAppID)}}, + }}) + })) + t.Cleanup(srv.Close) + c := newTestClient(t, srv, "org", []string{"repo"}) + var fired func() + c.ciRefetch = &ciRefetcher{newTimer: func(_ time.Duration, f func()) ciRefetchTimer { fired = f; return &ciTestTimer{} }} + c.storePRDetail("repo", 9, testPRDetail(9, "sha-9", clock.now, "clean")) + var hooked []string + SetCIEvidenceHook(func(repo, head string) { hooked = append(hooked, repo+"@"+head) }) + t.Cleanup(func() { SetCIEvidenceHook(nil) }) + + suite := []byte(`{"action":"requested","repository":{"full_name":"org/repo"},"check_suite":{"head_sha":"sha-9","pull_requests":[{"number":9}],"app":{"name":"GitHub Actions"}}}`) + for i := 0; i < 3; i++ { + if _, err := c.HandleWebhookEvent("check_suite", suite); err != nil { + t.Fatal(err) + } + } + if c.ciRefetch.pendingCount() != 1 || fired == nil { + t.Fatalf("pending = %d, want 1 coalesced head", c.ciRefetch.pendingCount()) + } + fired() + if listings != 1 { + t.Fatalf("check-run listings = %d, want 1", listings) + } + if !sharedCIEvidence().hasListed("org/repo", "sha-9") || c.ciRefetch.pendingCount() != 0 { + t.Fatal("re-read did not record the head's listing") + } + if !reflect.DeepEqual(hooked, []string{"org/repo@sha-9"}) { + t.Fatalf("hook calls = %v", hooked) + } + got := HeadCIActivity("org/repo", "sha-9", nil) + if got == nil || got.Source != "github-check-runs" || len(got.Checks) != 2 || got.RequiredKnown { + t.Fatalf("listing = %+v, want two listed checks, required unknown without a base ref", got) + } + if WebhookHealthSnapshot().CIRefetches1h != 1 { + t.Fatalf("health = %+v, want one CI refetch", WebhookHealthSnapshot()) + } + // Now that a listing exists, a delivery lays over it without queuing + // another re-read. + clock.now = clock.now.Add(time.Minute) + res, err := c.HandleWebhookEvent("check_run", ciTestCheckRunBody(9, "sha-9", "lint", "completed", "success", clock.now)) + if err != nil || res.CIEvidence.Refetch || c.ciRefetch.pendingCount() != 0 { + t.Fatalf("listed head must not re-read: %+v pending=%d err=%v", res.CIEvidence, c.ciRefetch.pendingCount(), err) + } + for _, check := range HeadCIActivity("org/repo", "sha-9", nil).Checks { + if check.Name == "lint" && check.Conclusion != "success" { + t.Fatalf("lint = %+v, want delivered completion over the listing", check) + } + } + if !strings.Contains(hooked[len(hooked)-1], "sha-9") || len(hooked) != 2 { + t.Fatalf("hook calls = %v, want the applied delivery to notify too", hooked) + } + // A context already cancelled makes the flush a no-op rather than a call. + c.ciRefetch.ctx = func() context.Context { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + return ctx + } + c.ciRefetch.enqueue(ciRefetchTarget{repo: "repo", fullName: "org/repo", head: "sha-9", number: 9}) + c.FlushWebhookCIRefetch() + if listings != 1 { + t.Fatalf("cancelled context still listed: %d", listings) + } +} diff --git a/src/pkg/hub/webhook.go b/src/pkg/hub/webhook.go index 4612a42e42..b6dbe65dbd 100644 --- a/src/pkg/hub/webhook.go +++ b/src/pkg/hub/webhook.go @@ -95,7 +95,7 @@ func (s *HubServer) handleGitHubWebhook(w http.ResponseWriter, r *http.Request) switch event { case "installation", "installation_repositories": s.handleInstallationEvent(body) - case "pull_request", "pull_request_review", "pull_request_review_comment", "check_suite", "check_run", "status", "issue_comment", "issues", "push": + case "pull_request", "pull_request_review", "pull_request_review_comment", "check_suite", "check_run", "workflow_run", "workflow_job", "status", "issue_comment", "issues", "push": s.relayWebhookToSpoke(event, r.Header, body) default: s.logger.Debug("ignoring webhook event", "event", event) From 0c076839ec0be6dab760b5d1d59df8486bc861a8 Mon Sep 17 00:00:00 2001 From: Andrew Anderson Date: Tue, 3 Nov 2026 22:20:31 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=90=9B=20fix:=20drop=20unused=20test?= =?UTF-8?q?=20timer=20field,=20refresh=20scale-envelope=20citation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Andrew Anderson --- src/docs/scale-envelope.md | 2 +- src/pkg/github/webhook_ci_evidence_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/docs/scale-envelope.md b/src/docs/scale-envelope.md index 8beda804f2..7c240be68b 100644 --- a/src/docs/scale-envelope.md +++ b/src/docs/scale-envelope.md @@ -34,7 +34,7 @@ ones to: 2. **Truncation must preserve the prioritization signal, not cut an arbitrary slice.** This holds today and is easy to assume backwards: - Issues are sorted by actionable rank tier, then **oldest first** within each tier before any cap is applied - (`RankActionableIssues`, `src/pkg/github/client.go:5110-5120`, descending `AgeMinutes` within each tier). + (`RankActionableIssues`, `src/pkg/github/client.go:5122-5134`, descending `AgeMinutes` within each tier). - PRs are sorted by **review class then oldest-first within class** (`SortPullRequestsForReview`, `src/pkg/github/review_priority.go:334`; fixes → refactors/docs → tests). diff --git a/src/pkg/github/webhook_ci_evidence_test.go b/src/pkg/github/webhook_ci_evidence_test.go index 446b9eb1f4..acd2b946ee 100644 --- a/src/pkg/github/webhook_ci_evidence_test.go +++ b/src/pkg/github/webhook_ci_evidence_test.go @@ -23,7 +23,7 @@ func ciTestCheckRunBody(number int, head, name, status, conclusion string, at ti status, len(name)*100+number, name, len(name), head, name, status, conclusion, name, at.Add(-time.Minute).Format(time.RFC3339), completed, number)) } -type ciTestTimer struct{ fired bool } +type ciTestTimer struct{} func (t *ciTestTimer) Stop() bool { return true }