Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-missing-check-alert-revalidation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Retire stale missing-required-check merge banners only after successful exact-current-head evidence, preserving required App identity and other PRs' unresolved blockers.
23 changes: 21 additions & 2 deletions src/docs/hive-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,27 @@ not one per PR or retry tick. The alert text names the fix: install/grant the
App Contents and Pull requests write permissions, adjust review/ruleset bypass
or approve the PR, make a missing required check report, or enable/use an
allowed merge method. Conflicts re-engage the fix loop instead of alerting, and
rate limits are treated as transient. A later successful merge in that repo
clears its merge-failure alerts automatically.
rate limits are treated as transient. Each aggregate banner retains every
originating PR; a successful merge or confirmed PR closure resolves only that
PR's blocker, not another PR's head-specific alert with the same reason.
Successful App merges still retire repository write-permission alerts and
disallowed-method alerts when the successful merge used that same method.
Method alerts also retire when a repository read positively confirms the
originally refused method is now enabled. A different method's success cannot
prove squash/rebase/merge settings, and unknown methods or failed reads retain
the banner. Reviews, absent CI and approval-held runs remain PR/head-specific;
another PR's successful merge does not prove those blockers resolved.

The existing five-minute alert revalidation also retires a missing-required-check
banner when every named check succeeds on the PR's exact current head. Check-run
success, neutral and skipped conclusions follow the existing CI policy; commit
statuses must succeed. Readable protection metadata binds checks to the required
App when specified. The observer completes bounded pagination, rejects older-head
evidence and re-reads the PR head/base before retiring the originating blocker.
API errors, incomplete evidence, unknown protection metadata and pending/failed
checks retain the alert. Concurrent refusals replace the observation token and
cannot be cleared by an older observation. This changes only alert freshness:
all CI, review, protection and merge gates remain authoritative and unchanged.

```yaml
auto_merge:
Expand Down
8 changes: 4 additions & 4 deletions src/pkg/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ type Client struct {
autoMergeMinHeadAge time.Duration
mergeAlertMu sync.Mutex
mergeAlertSink MergeFailureAlertSink
mergeAlertIDsByRepo map[string]map[string]mergeAlertEntry
mergeAlertIDsByRepo map[string]map[string]*mergeAlertEntry
mergeAlertLastRevalidate time.Time
logger *slog.Logger
stageFacts stageFactsState // out-of-cycle stage writes; see StageFacts
Expand Down Expand Up @@ -4696,9 +4696,9 @@ func (c *Client) RecordPRMergedAudit(repo string, number int, method, sha, path
// Every merge path ends here, so this is where the merged head's evidence
// bundle gains its merge event and the PR gets its evidence pointer.
c.recordReviewEvidenceMerge(repo, number, method, sha, path, credit.Logins())
// A successful App merge on any path proves the repo is mergeable, so
// retire its "merge blocked" alerts (#11924).
c.clearMergeFailureAlertsForRepo(repo)
// A merge resolves this PR's blockers and proven repository-level causes,
// not another PR's required checks or review/head-specific conditions.
c.clearMergeFailureAlertsAfterMerge(repo, number, method)
}

// RepoWorkBreakdown explains the raw open issue and PR totals for one
Expand Down
139 changes: 100 additions & 39 deletions src/pkg/github/merge_failure_alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type mergeFailureAlert struct {
type mergeAlertEntry struct {
key string
number int
head string
method string
}

// mergeAlertRevalidateInterval bounds how often the merge-request watcher
Expand All @@ -43,64 +45,72 @@ func (c *Client) SetMergeFailureAlertSink(sink MergeFailureAlertSink) {
defer c.mergeAlertMu.Unlock()
c.mergeAlertSink = sink
if c.mergeAlertIDsByRepo == nil {
c.mergeAlertIDsByRepo = make(map[string]map[string]mergeAlertEntry)
c.mergeAlertIDsByRepo = make(map[string]map[string]*mergeAlertEntry)
}
}

func (c *Client) raiseMergeFailureAlert(repo string, number int, errMsg string) {
func (c *Client) raiseMergeFailureAlert(repo string, number int, errMsg string, provenance ...string) {
alert, ok := classifyMergeFailureForOperator(repo, errMsg)
if !ok || c == nil {
return
}
c.mergeAlertMu.Lock()
defer c.mergeAlertMu.Unlock()
sink := c.mergeAlertSink
if sink == nil {
c.mergeAlertMu.Unlock()
return
}
id := mergeFailureAlertID(repo, alert.key)
if c.mergeAlertIDsByRepo == nil {
c.mergeAlertIDsByRepo = make(map[string]map[string]mergeAlertEntry)
c.mergeAlertIDsByRepo = make(map[string]map[string]*mergeAlertEntry)
}
repoKey := strings.ToLower(strings.TrimSpace(repo))
if c.mergeAlertIDsByRepo[repoKey] == nil {
c.mergeAlertIDsByRepo[repoKey] = make(map[string]mergeAlertEntry)
c.mergeAlertIDsByRepo[repoKey] = make(map[string]*mergeAlertEntry)
}
c.mergeAlertIDsByRepo[repoKey][id] = mergeAlertEntry{key: alert.key, number: number}
c.mergeAlertMu.Unlock()
entry := &mergeAlertEntry{key: alert.key, number: number}
if len(provenance) > 0 {
entry.head = provenance[0]
}
if alert.key == "merge-method" {
var requested string
if len(provenance) > 1 {
requested = provenance[1]
}
entry.method = mergeFailureMethod(errMsg, requested)
}
c.mergeAlertIDsByRepo[repoKey][fmt.Sprintf("%s/%d", id, number)] = entry

// Serialize sink updates with bookkeeping so an older observation cannot
// clear a newly raised alert between deleting its entry and updating the sink.
sink.AddSystemAlert(id, "error", alert.message)
}

func (c *Client) clearMergeFailureAlertsForRepo(repo string) {
func (c *Client) clearMergeFailureAlertsAfterMerge(repo string, number int, method string) {
if c == nil {
return
}
repoKey := strings.ToLower(strings.TrimSpace(repo))
c.mergeAlertMu.Lock()
sink := c.mergeAlertSink
ids := c.mergeAlertIDsByRepo[repoKey]
if len(ids) == 0 || sink == nil {
c.mergeAlertMu.Unlock()
return
}
delete(c.mergeAlertIDsByRepo, repoKey)
clearIDs := make([]string, 0, len(ids))
for id := range ids {
clearIDs = append(clearIDs, id)
}
c.mergeAlertMu.Unlock()

for _, id := range clearIDs {
sink.ClearSystemAlert(id)
defer c.mergeAlertMu.Unlock()
for member, entry := range c.mergeAlertIDsByRepo[repoKey] {
// An App merge proves repository write access, and a merge using the
// same method resolves that method's setting. Reviews, absent CI and
// approval-held runs on another PR/head are not proven by this merge.
if entry.number == number || entry.key == "app-permission" ||
(entry.key == "merge-method" && entry.method != "" && entry.method == method) {
c.clearMergeFailureAlertEntryLocked(repoKey, member, entry)
}
}
}

// revalidateMergeFailureAlerts retires "merge blocked" alerts whose cause has
// gone away without an App merge in that repo: the blocked PR is no longer
// open, (fork-run-approval) its head no longer has workflow runs awaiting
// maintainer approval — the operator approved them or relaxed the repo/org
// setting — or (app-permission) a check-runs read on its head succeeds again. Errors leave the alert in place; only positive evidence clears it.
// setting — or (app-permission) a check-runs read on its head succeeds again.
// Missing-check alerts require successful exact-current-head evidence.
// Errors leave the alert in place; only positive evidence clears it.
func (c *Client) revalidateMergeFailureAlerts(ctx context.Context, nowFn func() time.Time) {
if c == nil || c.client == nil {
return
Expand All @@ -118,7 +128,7 @@ func (c *Client) revalidateMergeFailureAlerts(ctx context.Context, nowFn func()
c.mergeAlertLastRevalidate = nowFn()
type pending struct {
repo, id string
entry mergeAlertEntry
entry *mergeAlertEntry
}
var work []pending
for repo, ids := range c.mergeAlertIDsByRepo {
Expand All @@ -140,7 +150,27 @@ func (c *Client) revalidateMergeFailureAlerts(ctx context.Context, nowFn func()
if err != nil || pr == nil {
continue
}
resolved := pr.GetState() != "open"
resolved := pr.GetState() == "closed"
if pr.GetState() != "open" && !resolved {
continue
}
if !resolved && strings.HasPrefix(w.entry.key, "missing-checks:") {
resolved = c.missingMergeChecksResolved(ctx, owner, name, pr,
splitCheckNames(strings.TrimPrefix(w.entry.key, "missing-checks:")))
}
if !resolved && w.entry.key == "merge-method" && w.entry.method != "" {
repository, _, rerr := c.client.Repositories.Get(ctx, owner, name)
if rerr == nil && repository != nil {
switch w.entry.method {
case "squash":
resolved = repository.GetAllowSquashMerge()
case "merge":
resolved = repository.GetAllowMergeCommit()
case "rebase":
resolved = repository.GetAllowRebaseMerge()
}
}
}
if !resolved && w.entry.key == "app-permission" {
// Positive evidence the App can act on the repo again: its
// check-runs read on the blocked head succeeds. A real merge
Expand All @@ -161,24 +191,55 @@ func (c *Client) revalidateMergeFailureAlerts(ctx context.Context, nowFn func()
if !resolved {
continue
}
c.clearMergeFailureAlertID(w.repo, w.id)
c.logger.Info("merge alert cleared: blocking condition resolved",
slog.String("repo", w.repo), slog.Int("number", w.entry.number), slog.String("key", w.entry.key))
c.mergeAlertMu.Lock()
retired, cleared := c.clearMergeFailureAlertEntryLocked(w.repo, w.id, w.entry)
c.mergeAlertMu.Unlock()
if retired {
c.logger.Info("merge alert origin retired: blocking condition resolved",
slog.String("repo", w.repo), slog.Int("number", w.entry.number),
slog.String("key", w.entry.key), slog.String("origin_head", w.entry.head),
slog.String("current_head", pr.GetHead().GetSHA()), slog.Bool("banner_retained", !cleared))
}
}
}

func (c *Client) clearMergeFailureAlertID(repoKey, id string) {
c.mergeAlertMu.Lock()
sink := c.mergeAlertSink
if ids := c.mergeAlertIDsByRepo[repoKey]; ids != nil {
delete(ids, id)
if len(ids) == 0 {
delete(c.mergeAlertIDsByRepo, repoKey)
// The pointer is an observation token: a refusal raised during network reads
// replaces it and must survive the old observation.
func (c *Client) clearMergeFailureAlertEntryLocked(repoKey, member string, entry *mergeAlertEntry) (retired, bannerCleared bool) {
entries := c.mergeAlertIDsByRepo[repoKey]
if entries[member] != entry {
return false, false
}
delete(entries, member)
for _, other := range entries {
if other.key == entry.key {
return true, false
}
}
c.mergeAlertMu.Unlock()
if sink != nil {
sink.ClearSystemAlert(id)
if len(entries) == 0 {
delete(c.mergeAlertIDsByRepo, repoKey)
}
if c.mergeAlertSink != nil {
c.mergeAlertSink.ClearSystemAlert(mergeFailureAlertID(repoKey, entry.key))
}
return true, true
}

func mergeFailureMethod(message, requested string) string {
lower := strings.ToLower(message)
switch {
case strings.Contains(lower, "squash"):
return "squash"
case strings.Contains(lower, "rebase"):
return "rebase"
case strings.Contains(lower, "merge commits"):
return "merge"
}
switch requested {
case "squash", "merge", "rebase":
return requested
default:
return ""
}
}

Expand Down
107 changes: 107 additions & 0 deletions src/pkg/github/merge_failure_alert_checks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package github

import (
"context"
"strings"

gh "github.com/google/go-github/v72/github"
)

const mergeAlertEvidenceMaxPages = 3

// missingMergeChecksResolved only retires the missing-check banner. It neither
// grants a CI verdict nor schedules another merge, and never substitutes an
// old head's success for evidence on the current PR head.
func (c *Client) missingMergeChecksResolved(ctx context.Context, owner, repo string, pr *gh.PullRequest, names []string) bool {
head, base := pr.GetHead().GetSHA(), pr.GetBase().GetRef()
if head == "" || base == "" || len(names) == 0 {
return false
}
required, known := c.nativeObservationRequired(ctx, owner, repo, base)
if !known {
// In particular, a denied protection read cannot establish that an
// identically named check from another App satisfies this blocker.
return false
}
producers := make(map[string]int64, len(names))
for _, name := range names {
appID, exists := required[name]
if !exists {
return false
}
producers[name] = appID
}
var checks []*gh.CheckRun
checkOpts := &gh.ListCheckRunsOptions{ListOptions: gh.ListOptions{PerPage: 100}}
for page := 0; ; page++ {
result, response, err := c.client.Checks.ListCheckRunsForRef(ctx, owner, repo, head, checkOpts)
if err != nil || result == nil || result.Total == nil {
return false
}
checks = append(checks, result.CheckRuns...)
if response == nil || response.NextPage == 0 {
if result.GetTotal() != len(checks) {
return false
}
break
}
if page+1 >= mergeAlertEvidenceMaxPages || response.NextPage <= checkOpts.Page {
return false
}
checkOpts.Page = response.NextPage
}
passed, blocked := make(map[string]bool), make(map[string]bool)
for _, check := range latestCheckRunsByNameAndApp(checks) {
name := check.GetName()
appID, wanted := producers[name]
if !wanted || (appID > 0 && check.GetApp().GetID() != appID) {
continue
}
conclusion := check.GetConclusion()
if check.GetID() > 0 && check.GetHeadSHA() == head && check.GetStatus() == "completed" &&
(conclusion == "success" || conclusion == "neutral" || conclusion == "skipped") {
passed[name] = true
} else {
blocked[name] = true
}
}
statusOpts := &gh.ListOptions{PerPage: 100}
var statusCount int
for page := 0; ; page++ {
result, response, err := c.client.Repositories.GetCombinedStatus(ctx, owner, repo, head, statusOpts)
if err != nil || result == nil || result.TotalCount == nil || result.GetSHA() != head {
return false
}
statusCount += len(result.Statuses)
for _, status := range result.Statuses {
name := status.GetContext()
appID, wanted := producers[name]
if !wanted || appID > 0 {
continue
}
if status.GetID() > 0 && status.GetState() == "success" {
passed[name] = true
} else {
blocked[name] = true
}
}
if response == nil || response.NextPage == 0 {
if result.GetTotalCount() != statusCount {
return false
}
break
}
if page+1 >= mergeAlertEvidenceMaxPages || response.NextPage <= statusOpts.Page {
return false
}
statusOpts.Page = response.NextPage
}
for _, name := range names {
if !passed[name] || blocked[name] {
return false
}
}
current, _, err := c.client.PullRequests.Get(ctx, owner, repo, pr.GetNumber())
return err == nil && current != nil && current.GetState() == "open" &&
strings.EqualFold(current.GetHead().GetSHA(), head) && current.GetBase().GetRef() == base
}
Loading
Loading