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-decide-plan-adopted-pr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- "Let hive decide" no longer calls a PR's CI red when only a non-required context (tide) is pending: the plan reads the head's own checks, derives its WHY from the recorded stage-entry reasons (reviewer requires_human / high-risk), and on a PR the hive adopted in place hands the finding to the hive's fixer instead of "sending it back to its author".
121 changes: 100 additions & 21 deletions src/pkg/dashboard/api_repo_item_decide_delegated.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package dashboard

import (
Expand All @@ -18,6 +18,8 @@
"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/delegatedecision"
ghpkg "github.com/hivecommons/hive/pkg/github"
"github.com/hivecommons/hive/pkg/review"
"github.com/hivecommons/hive/pkg/stageengine"
stageengineprstage "github.com/hivecommons/hive/pkg/stageengine/prstage"
"github.com/hivecommons/hive/pkg/stagelabels"
)
Expand Down Expand Up @@ -643,11 +645,12 @@
in.DiffEmpty = ghpkg.PRChangedFilesClaimsEmpty(pr)
state := strings.ToLower(pr.GetMergeableState())
in.Conflicting = state == "dirty" || (pr.Mergeable != nil && !pr.GetMergeable())
green, err := delegatedHeadCIGreen(ctx, api, owner, repoName, pr, in.Conflicting, state)
ci, err := delegatedHeadCI(ctx, api, owner, repoName, pr, state)
if err != nil {
return in, fmt.Errorf("reading checks for %s#%d: %w", repoFull, number, err)
}
in.CIGreen = green
in.CIGreen, in.CIPending = ci.green, ci.pending
s.gatherDelegatedRecordedPark(repoFull, number, pr.GetHead().GetSHA(), &in)
if files, _, err := api.PullRequests.ListFiles(ctx, owner, repoName, number, &gh.ListOptions{PerPage: delegatedDecisionGatherLimit}); err == nil && len(files) > 0 {
in.LowRiskPaths = true
for _, f := range files {
Expand Down Expand Up @@ -713,31 +716,107 @@
return rec.AdoptionBlocked(rec.NudgeHead) != ""
}

// delegatedHeadCIGreen decides Input.CIGreen for the PR under decision.
// mergeable_state alone cannot carry it: GitHub reports "blocked" both for a
// missing required review and for red required checks, and "unknown" /
// "behind" / "" say nothing about the head at all. Only "clean" and
// "has_hooks" assert a green head; "unstable" and a conflict deny it; every
// other state is settled from the head's combined status and check runs the
// way gatherDelegatedPRContext settles a competing PR β€” no red, nothing
// pending, and at least one green result, so a head whose checks never ran
// is not read as green either.
func delegatedHeadCIGreen(ctx context.Context, api *gh.Client, owner, repoName string, pr *gh.PullRequest, conflicting bool, state string) (bool, error) {
switch {
case conflicting, state == "unstable":
return false, nil
case state == "clean", state == "has_hooks":
return true, nil
// delegatedHeadCIState is what the head's checks say: green (every
// gating check passed), pending (some still running, none failed), or
// neither (a check failed, or none ever reported).
type delegatedHeadCIState struct {
green, pending bool
}

// delegatedHeadCI decides Input.CIGreen / CIPending for the PR under
// decision. mergeable_state alone cannot carry it: GitHub reports "blocked"
// both for a missing required review and for red required checks,
// "unstable" whenever ANY context is pending or failing β€” a non-required
// merge-metadata context like tide included β€” and "unknown" / "behind" /
// "" say nothing about the head at all. Only "clean" and "has_hooks"
// assert a green head; every other state is settled from the head's
// combined status and check runs (meta contexts excluded, the way
// gatherDelegatedPRContext settles a competing PR): green is no red,
// nothing pending and at least one green result; pending is no red with
// a check still running. hivecommons/hive#15722 read "unstable" as red
// while every check on the head was green and only tide was pending, and
// the preview told the operator "CI is red".
func delegatedHeadCI(ctx context.Context, api *gh.Client, owner, repoName string, pr *gh.PullRequest, state string) (delegatedHeadCIState, error) {
if state == "clean" || state == "has_hooks" {
return delegatedHeadCIState{green: true}, nil
}
sha := strings.TrimSpace(pr.GetHead().GetSHA())
if sha == "" {
return false, nil
return delegatedHeadCIState{}, nil
}
summary, err := ghpkg.SummarizeCommitChecks(ctx, api, owner, repoName, sha)
if err != nil {
return false, err
return delegatedHeadCIState{}, err
}
if len(summary.Red) > 0 {
return delegatedHeadCIState{}, nil
}
return delegatedHeadCIState{green: summary.Pending == 0 && summary.Green > 0, pending: summary.Pending > 0}, nil
}

// gatherDelegatedRecordedPark reads what the stage engine recorded about
// the PR β€” the reason it parked it, whether the hive adopted it in place,
// and the reviewer verdict on its head β€” so the delegate decides on the
// same facts the 085 entry comment named, not only on the words the
// tracker row sent along (hivecommons/hive#15722: the row said reviewer
// requires_human / high-risk, the plan said "CI is red").
func (s *Server) gatherDelegatedRecordedPark(repoFull string, number int, head string, in *delegatedecision.Input) {
head = strings.TrimSpace(head)
recorded := ""
if rec, ok := s.stageStoreOrShared().Get(stagelabels.Key(repoFull, number)); ok {
recorded = strings.TrimSpace(rec.Reason)
if recorded != "" && !slices.Contains(in.Reasons, recorded) {
in.Reasons = append(in.Reasons, recorded)
}
in.AdoptedByHive = rec.AdoptionLive(head)
}
for _, r := range in.Reasons {
lower := strings.ToLower(r)
if strings.Contains(lower, stageengineprstage.ContributorLaneReasonAdoptedByHive) {
in.AdoptedByHive = true
}
if stagelabels.ReasonClass(lower) == stageengine.StageReasonVerdictRequiresHuman || strings.Contains(lower, "requires_human") {
in.ReviewerRequiresHuman = true
}
}
art, err := review.LoadArtifact("")
if err != nil {
return
}
return len(summary.Red) == 0 && summary.Pending == 0 && summary.Green > 0, nil
lookup := stageTrackerDecisionVerdictLookup(&art)
agg, ok := lookup(repoFull, number, head, in.ReviewerRequiresHuman)
if !ok {
_, short, _ := strings.Cut(repoFull, "/")
agg, ok = lookup(short, number, head, in.ReviewerRequiresHuman)
}
if !ok {
return
}
if agg.RequiresHuman || agg.DecisionRequested {
in.ReviewerRequiresHuman = true
}
if in.Question == "" {
in.Question = strings.TrimSpace(agg.DecisionQuestion)
}
in.ReviewerFinding = strings.TrimSpace(agg.DecisionFinding)
in.SecurityFinding = delegatedSecurityFinding(agg)
}

// delegatedSecurityFinding: the reviewer's finding is the security
// review's own β€” that perspective withheld approval, or the finding names
// a security / credentials question. The reviewer's generic high-risk
// marker alone is not one: it is a risk-class flag the fixer addresses.
func delegatedSecurityFinding(agg review.Aggregate) bool {
if agg.Perspectives[review.PerspectiveSecurity] == review.VerdictRequiresHuman {
return true
}
lower := strings.ToLower(agg.DecisionFinding)
for _, term := range []string{"security", "credential", "secret", "permission", "privilege", "vulnerab", "insecure", "malicious", "supply chain", "supply-chain"} {
if strings.Contains(lower, term) {
return true
}
}
return false
}

// delegatedWorkflowPath reports a file under .github/workflows: the adoption
Expand Down Expand Up @@ -791,7 +870,7 @@
notes = append(notes, "parking labels lifted, `"+ghpkg.HumanAckLabel+"` applied")
s.noteDelegatedUnpark(repoFull, number)
}
} else if d.Verdict == delegatedecision.VerdictDirection && in.HiveAuthored {
} else if d.Verdict == delegatedecision.VerdictDirection && (in.HiveAuthored || in.AdoptedByHive) {
if _, err := client.RecordOperatorPullRequestDecision(ctx, repoFull, number, user, d.Text, repoItemDecisionChangeRequests()); err != nil && !errors.Is(err, ghpkg.ErrPullRequestNotHiveAuthored) {
return notes, fmt.Errorf("recording PR direction: %w", err)
} else if err == nil {
Expand Down
53 changes: 47 additions & 6 deletions src/pkg/dashboard/api_repo_item_decide_delegated_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ func delegatedPlanWording(in delegatedecision.Input, d delegatedecision.Decision
return "Close " + ref + " as superseded by #" + strconv.Itoa(target) + ".", []string{ref + " is closed with a comment pointing at #" + strconv.Itoa(target) + "."}
case d.Verdict == delegatedecision.VerdictCloseDuplicate, d.Verdict == delegatedecision.VerdictCloseSuperseded:
return "Close " + ref + ".", []string{ref + " is closed with a comment saying why."}
case d.Verdict == delegatedecision.VerdictDirection && in.AdoptedByHive:
// The hive adopted the PR in place: it is the author now, so the
// instruction is its own fixer's, not the contributor's
// (hivecommons/hive#15722).
return "Have hive's fixer " + delegatedPlanInstruction(in, delegatedecision.Decision{}) + " on " + ref + "; no action needed from you.",
[]string{"Hive records that instruction on " + ref + " for its fixer β€” the hive owns this branch now, not the contributor.", "The fixer pushes; the row updates when the swarm re-reviews."}
case d.Verdict == delegatedecision.VerdictDirection:
return "Send " + ref + " back to its author with one instruction: " + delegatedPlanInstruction(in, d) + ".",
[]string{"Hive posts that instruction on " + ref + ".", "The author pushes a fix; the row updates when it lands."}
Expand All @@ -237,17 +243,27 @@ func delegatedPlanWording(in delegatedecision.Input, d delegatedecision.Decision
}

// delegatedPlanInstruction is the direction in the author's terms: the
// fix list from the head's facts ("make CI green, nothing else"), or the
// decision's own text when the direction is a reviewer's question.
// fix list from the head's facts ("make CI green, nothing else"), the
// reviewer's finding when that is what parked it, or the decision's own
// text when the direction is a reviewer's question. "make CI green" is
// said only of a head with a failing check: pending checks are not red
// (hivecommons/hive#15722).
func delegatedPlanInstruction(in delegatedecision.Input, d delegatedecision.Decision) string {
var fix []string
if in.ReviewerRequiresHuman {
item := "address the reviewer's finding"
if f := strings.TrimSpace(in.ReviewerFinding); f != "" {
item += " (" + f + ")"
}
fix = append(fix, item)
}
if in.Conflicting {
fix = append(fix, "rebase onto the base branch")
}
if in.DCOMissing {
fix = append(fix, "add the sign-off")
}
if !in.CIGreen {
if in.CIRed() {
fix = append(fix, "make CI green")
}
if len(fix) == 0 || strings.TrimSpace(in.Question) != "" {
Expand All @@ -258,6 +274,9 @@ func delegatedPlanInstruction(in delegatedecision.Input, d delegatedecision.Deci
if len(fix) == 0 {
return "address the review"
}
if in.ReviewerRequiresHuman {
return strings.Join(fix, ", ")
}
return strings.Join(fix, ", ") + ", nothing else"
}

Expand All @@ -282,7 +301,7 @@ func delegatedPlanWhy(in delegatedecision.Input, d delegatedecision.Decision) st
}
why := strings.Join(parts, " and ")
if why == "" && d.Verdict == delegatedecision.VerdictDirection {
why = delegatedPlanBlockers(in)
why = delegatedPlanParkReason(in)
}
if why == "" {
why = delegatedPlanPlain(d.Rationale)
Expand Down Expand Up @@ -313,12 +332,34 @@ func delegatedPlanLandedWhy(in delegatedecision.Input, d delegatedecision.Decisi
return strings.ToUpper(landed[:1]) + landed[1:] + "."
}

// delegatedPlanParkReason is WHY a direction is owed, from the same facts
// the stage engine recorded when it parked the PR: the reviewer's verdict
// first β€” "the reviewer returned requires_human: the reviewer marked this
// change high-risk" β€” then the head's blockers. hivecommons/hive#15722's
// preview said "CI is red" of a PR parked on requires_human with every
// check green.
func delegatedPlanParkReason(in delegatedecision.Input) string {
var parts []string
if in.ReviewerRequiresHuman {
why := "the reviewer returned requires_human"
if f := strings.TrimSpace(in.ReviewerFinding); f != "" {
why += ": " + f
}
parts = append(parts, why)
}
if b := delegatedPlanBlockers(in); b != "" {
parts = append(parts, b)
}
return strings.Join(parts, "; ")
}

// delegatedPlanBlockers names what keeps a head from merging, in plain
// words: "CI is red", "it conflicts with the base branch", "the sign-off
// is missing".
// is missing". A head whose checks are still running is not red and is
// not a blocker a direction can name.
func delegatedPlanBlockers(in delegatedecision.Input) string {
var b []string
if !in.CIGreen {
if in.CIRed() {
b = append(b, "CI is red")
}
if in.Conflicting {
Expand Down
107 changes: 107 additions & 0 deletions src/pkg/dashboard/api_repo_item_decide_delegated_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/hivecommons/hive/pkg/delegatedecision"
"github.com/hivecommons/hive/pkg/stageengine"
"github.com/hivecommons/hive/pkg/stagelabels"
)

func postDelegatedPlan(t *testing.T, srv *Server, number int, user, body string, hdr map[string]string) *httptest.ResponseRecorder {
Expand Down Expand Up @@ -333,3 +336,107 @@ func TestDelegatedDecisionPlanIssueAndAdvisory(t *testing.T) {
})
}
}

// hivecommons/hive#15722, the live case: a clanker's PR the hive adopted in
// place (contributor-lane:adopted-by-hive), parked at 085 by the reviewer's
// requires_human / high-risk marker, every check green and only the tide
// context pending (mergeable_state "unstable"). The preview said "Send
// #15722 back to its author with one instruction: make CI green" / "CI is
// red". It must read the recorded park reason instead, never call a
// pending-only head red, and β€” the hive being the author now β€” hand the
// finding to the hive's own fixer.
func TestDelegatedDecisionPlanAdoptedRequiresHumanIsFixersNotCI(t *testing.T) {
mock := &repoItemDecisionMock{prForeign: true, prMergeState: "unstable", prTidePending: true, prLabels: []string{"stage/085-needs-decision"}}
srv := newDelegatedServer(t, mock)
srv.stageStore = stagelabels.NewStore("", time.Now)
key := stagelabels.Key("testorg/testrepo", 8)
srv.stageStore.Observe(key, stagelabels.NeedsDecision, stageengine.StageReasonVerdictRequiresHuman)
srv.stageStore.MarkAdoptedInPlace(key, "feedfacefeedfacefeedfacefeedfacefeedface", "contributor-offline", time.Now())

// The tracker row's words, as the SPA sends them.
body := `{"type":"pr","reason":"reviewer-verdict:requires_human","caption":"the reviewer marked this change high-risk"}`
w := postDelegatedPlan(t, srv, 8, "writer", body, map[string]string{"X-Hive-Role": "merger"})
if w.Code != http.StatusOK {
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
}
var plan delegatedDecisionPlan
if err := json.Unmarshal(w.Body.Bytes(), &plan); err != nil {
t.Fatal(err)
}
t.Logf("summary: %s\nwhy: %s\nconsequences: %v", plan.Summary, plan.Why, plan.Consequences)
if plan.Would != string(delegatedecision.VerdictDirection) || plan.Rule != delegatedecision.RuleAdoptedFixer {
t.Fatalf("plan = %+v", plan)
}
if !strings.HasPrefix(plan.Summary, "Have hive's fixer address the reviewer's finding") || !strings.Contains(plan.Summary, "no action needed from you") {
t.Fatalf("summary %q", plan.Summary)
}
if !strings.Contains(plan.Why, "requires_human") {
t.Fatalf("why %q does not name the recorded park reason", plan.Why)
}
for _, text := range append([]string{plan.Summary, plan.Why}, plan.Consequences...) {
lower := strings.ToLower(text)
if strings.Contains(text, "CI") || strings.Contains(lower, "back to its author") {
t.Errorf("a green head parked on requires_human is described as red or returned: %q", text)
}
}
if len(mock.comments) != 0 {
t.Fatalf("a plan may not write: %v", mock.comments)
}

// The same head, not adopted: the clanker's PR is not "red" either β€”
// the plan never says CI is red of a head with no failing check.
mock2 := &repoItemDecisionMock{prForeign: true, prMergeState: "unstable", prTidePending: true, prLabels: []string{"stage/085-needs-decision"}}
srv2 := newDelegatedServer(t, mock2)
srv2.stageStore = stagelabels.NewStore("", time.Now)
srv2.stageStore.Observe(key, stagelabels.NeedsDecision, stageengine.StageReasonVerdictRequiresHuman)
w = postDelegatedPlan(t, srv2, 8, "writer", body, map[string]string{"X-Hive-Role": "merger"})
if w.Code != http.StatusOK {
t.Fatalf("code=%d body=%s", w.Code, w.Body.String())
}
var plan2 delegatedDecisionPlan
if err := json.Unmarshal(w.Body.Bytes(), &plan2); err != nil {
t.Fatal(err)
}
t.Logf("not adopted: summary: %s\nwhy: %s", plan2.Summary, plan2.Why)
for _, text := range append([]string{plan2.Summary, plan2.Why}, plan2.Consequences...) {
if strings.Contains(text, "CI is red") || strings.Contains(text, "make CI green") {
t.Errorf("not adopted: head with green checks called red: %q", text)
}
}
}

// delegatedPlanInstruction / delegatedPlanBlockers: pending checks are not
// red, and the recorded reviewer finding is the WHY.
func TestDelegatedPlanPendingCIIsNotRed(t *testing.T) {
in := delegatedecision.Input{Kind: delegatedecision.KindPR, Number: 15722, Author: "clubanderson", OwnerAuthor: true, TrustedAuthor: true, CIGreen: false, CIPending: true, Conflicting: true}
d := delegatedecision.Decide(in)
if d.Verdict != delegatedecision.VerdictDirection {
t.Fatalf("verdict %s rule %s", d.Verdict, d.Rule)
}
summary, _ := delegatedPlanWording(in, d)
if summary != "Send #15722 back to its author with one instruction: rebase onto the base branch, nothing else." {
t.Fatalf("summary %q", summary)
}
if why := delegatedPlanWhy(in, d); why != "It conflicts with the base branch." {
t.Fatalf("why %q", why)
}
in.Conflicting, in.ReviewerRequiresHuman, in.ReviewerFinding = false, true, "the reviewer marked this change high-risk"
in.AdoptedByHive, in.OwnerAuthor, in.TrustedAuthor, in.Author = true, false, false, "mendezr"
d = delegatedecision.Decide(in)
if d.Rule != delegatedecision.RuleAdoptedFixer {
t.Fatalf("verdict %s rule %s", d.Verdict, d.Rule)
}
summary, next := delegatedPlanWording(in, d)
if summary != "Have hive's fixer address the reviewer's finding (the reviewer marked this change high-risk) on #15722; no action needed from you." {
t.Fatalf("summary %q", summary)
}
if why := delegatedPlanWhy(in, d); why != "The reviewer returned requires_human: the reviewer marked this change high-risk." {
t.Fatalf("why %q", why)
}
if len(next) != 2 {
t.Fatalf("then items %v", next)
}
if delegatedPlanMergeableAsIs(in, d) {
t.Fatal("a head with checks still running is not mergeable as it is")
}
}
6 changes: 5 additions & 1 deletion src/pkg/dashboard/api_repo_item_decide_delegated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ func TestDelegatedDecisionPRBlockedStateReadsHeadChecks(t *testing.T) {
{"blocked by red required checks", "blocked", true, false, delegatedecision.VerdictDirection, "fix-then-merge"},
{"blocked with checks never run", "blocked", false, true, delegatedecision.VerdictDirection, "fix-then-merge"},
{"unknown state with checks never run", "unknown", false, true, delegatedecision.VerdictDirection, "fix-then-merge"},
{"unstable head", "unstable", false, false, delegatedecision.VerdictDirection, "fix-then-merge"},
// "unstable" means some context is pending or failing β€” the
// non-required tide included β€” so the head's own checks decide
// (hivecommons/hive#15722: all green, only tide pending, "CI is red").
{"unstable head with green checks", "unstable", false, false, delegatedecision.VerdictApprove, "default-release"},
{"unstable head with red checks", "unstable", true, false, delegatedecision.VerdictDirection, "fix-then-merge"},
{"blocked by missing review only", "blocked", false, false, delegatedecision.VerdictApprove, "default-release"},
{"clean head", "clean", false, false, delegatedecision.VerdictApprove, "default-release"},
}
Expand Down
Loading
Loading