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-adopt-in-place-workflows-grant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Adoption no longer closes and re-opens a contributor PR whose branch lives in the base repository just because the base changed a file under `.github/workflows/`. The rule came from #12865, written when the App had no Workflows grant (#6985). That premise has changed: an installation can grant `workflows: write` (#14664). Under the old rule the hive superseded such PRs, which dropped their approvals and number (projectbluefin/common#1689 became #1708). Now the hive keeps the PR on its own branch when the fixer that rebases adopted heads (`scanner`) can push workflow files itself: its tier is trusted or merger and the App installation grants `workflows: write`. The same check lets a DCO-red adopted head on such a branch be repaired in place instead of re-homed. Fork PRs are unchanged, because GitHub refuses any App workflow-file push to a fork whatever the base installation grants. A refused `update-branch` during adopted-PR recovery now logs `fork-workflow-update` or `workflow-permission` instead of the catch-all `update-branch-failed`.
6 changes: 3 additions & 3 deletions src/cmd/hive/merge_eligibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,15 +1139,15 @@ func prFixAgent(pr github.PullRequest, audited string) string {
func routeConflictFixAgent(agent string, cfg *config.Config) (string, string) {
owner := strings.TrimSpace(agent)
if owner == "" {
return "scanner", ""
return github.FallbackFixAgent, ""
}
if strings.EqualFold(owner, "scanner") || cfg == nil {
if strings.EqualFold(owner, github.FallbackFixAgent) || cfg == nil {
return owner, ""
}
base := cfg.BaseAgentName(owner)
ac, ok := cfg.Agents[base]
if !ok || ac.Paused || !cfg.HasAnyCadence(base) || allConfiguredCadencesPaused(cfg, base) {
return "scanner", owner
return github.FallbackFixAgent, owner
}
return owner, ""
}
Expand Down
6 changes: 5 additions & 1 deletion src/docs/hive-merge.md
Original file line number Diff line number Diff line change
Expand Up @@ -1728,7 +1728,11 @@ maintainers may edit) or re-runs the failed workflow jobs when it is current,
at most once per (head, base tip) and three times per adoption, logged as
`adopted PR recovery` with `reason=update-branch`, `rerun-failed-jobs` or
`skipped:<why>` (`skipped:dependency-bot-branch` for a behind `renovate/*` or
dependency-bot branch, which is never updated); an escalation already in place for that head is released
dependency-bot branch, which is never updated; `skipped:fork-workflow-update`
when the base changed `.github/workflows/` and the head is in a fork, which
GitHub refuses to update for any App whatever the base installation grants;
`skipped:workflow-permission` when a branch of the base repository was refused
because the token lacks `workflows: write`); an escalation already in place for that head is released
(hivecommons/hive#14500). An adoption with nothing the hive can
push past the dwell β€” after that recovery was tried and the head is still
red β€” goes to `stage/085-needs-decision` as
Expand Down
17 changes: 14 additions & 3 deletions src/docs/labels-and-control-signals/stage-labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,14 @@ When a contributor-agent PR goes red the hive does not just relabel it (a human-
branch (`governor.labels.stage.adoption.in_place`, default `true`):
- **in place** (`mode=in-place`) β€” the head is in the base repo, or the
fork allows edits by maintainers (`maintainer_can_modify`, resolved with
one `GET pulls/N` at adoption time): the hive posts ONE
one `GET pulls/N` at adoption time). If the base changed a file under
`.github/workflows/` since the PR's merge-base, a head in the base repo
stays in place only when the fixer that rebases and fixes adopted heads
(`scanner`) can push workflow files itself: its token tier (trusted or
merger) asks for the Workflows permission and the App installation
grants `workflows: write`. A fork head never does (see above). The same
check decides whether a DCO-red adopted head is repaired in place or
re-homed. The hive posts ONE
`<!-- hive:adopted by=<hive-id> reason=<r> mode=in-place head=<sha> -->`
comment, moves the PR to `stage/074-adopted-by-hive` and files it in
`ci-failing.json` as its own (`"adopted": true`, `reachable_action:
Expand Down Expand Up @@ -1349,8 +1356,12 @@ When a contributor-agent PR goes red the hive does not just relabel it (a human-
over. A pending `tide` or missing `lgtm` is never the reason β€”
the lane has not admitted the head; the comment says so.
- **supersede** (`mode=supersede`) β€” the fork forbids maintainer edits,
`in_place: false`, or the adopted head has unsigned commits (`no-dco`)
that the hive cannot repair by amending the contributor branch:
`in_place: false`, the base-sync of a head in the base repo carries a
`.github/workflows/` change the hive cannot push (no `workflows: write`
grant, the fixer's token cannot push workflows, or the head...base
compare failed), or the adopted head has
unsigned commits (`no-dco`) that the hive cannot repair by amending the
contributor branch:
- pushes `hive/adopt-<n>` in the base repo at the contributor's head SHA
(the fork's commits, authorship and `Signed-off-by` preserved byte for
byte β€” "sign-off survives the squash");
Expand Down
52 changes: 52 additions & 0 deletions src/pkg/github/agent_workflows_pushable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package github

import (
"context"
"errors"
"testing"
)

// AgentWorkflowsPushable asks about the one lane that will push, not about
// any capable lane: the adoption sweep keeps a same-repo PR on its own branch
// across a base workflow change only when the fallback fixer can push it
// (#12865: a contributor-tier fixer's push was refused while another lane
// held the grant).
func TestAgentWorkflowsPushable(t *testing.T) {
write := map[string]string{"workflows": "write"}
cases := []struct {
name string
appAuth bool
fixerTrusted bool
perms map[string]string
err error
want bool
}{
{name: "grant present, fixer at contributor tier", appAuth: true, perms: write},
{name: "grant present, fixer at trusted tier", appAuth: true, fixerTrusted: true, perms: write, want: true},
{name: "token-authenticated client", fixerTrusted: true, perms: write},
{name: "installation grants read only", appAuth: true, fixerTrusted: true, perms: map[string]string{"workflows": "read"}},
{name: "installation read fails", appAuth: true, fixerTrusted: true, err: errors.New("502")},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
stubInstallation(t, tc.perms, tc.err)
c := &Client{}
if tc.appAuth {
c.appAuth = &AppAuth{}
}
// Another lane (ci-maintainer) always holds a trusted-tier
// token: the answer for the fixer must not borrow it.
c.SetWorkflowsSelfPushFunc(func(agent string) bool {
return agent == "ci-maintainer" || (agent == FallbackFixAgent && tc.fixerTrusted)
})
c.SetWorkflowsLaneFunc(func(string, string) string { return "ci-maintainer" })
if got := c.AgentWorkflowsPushable(context.Background(), FallbackFixAgent); got != tc.want {
t.Fatalf("AgentWorkflowsPushable(%s) = %v, want %v", FallbackFixAgent, got, tc.want)
}
})
}
var nilClient *Client
if nilClient.AgentWorkflowsPushable(context.Background(), FallbackFixAgent) {
t.Fatal("nil client reported a pushable fixer")
}
}
209 changes: 209 additions & 0 deletions src/pkg/github/automerge/automerge_adopt_workflows_grant_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package automerge

import (
"bytes"
"context"
"errors"
"log/slog"
"net/http"
"strings"
"sync"
"testing"
"time"

gh "github.com/google/go-github/v72/github"
hgithub "github.com/hivecommons/hive/pkg/github"
)

// workflowsGrantTransport is the engine's client with a fixed answer to
// AgentWorkflowsPushable: the fallback fixer's tier asks for the Workflows
// permission and the installation grants it (or not). The real predicate is
// covered in pkg/github (TestAgentWorkflowsPushable).
type workflowsGrantTransport struct {
*hgithub.Client
pushable bool

mu sync.Mutex
asked []string
}

func (t *workflowsGrantTransport) AgentWorkflowsPushable(_ context.Context, agent string) bool {
t.mu.Lock()
defer t.mu.Unlock()
t.asked = append(t.asked, agent)
return t.pushable
}

func (t *workflowsGrantTransport) askedAgents() []string {
t.mu.Lock()
defer t.mu.Unlock()
return append([]string(nil), t.asked...)
}

func withWorkflowsGrant(t *testing.T, h *adoptHarness, pushable bool) *workflowsGrantTransport {
t.Helper()
client, ok := h.e.transport.(*hgithub.Client)
if !ok {
t.Fatalf("engine transport is %T, want *hgithub.Client", h.e.transport)
}
tr := &workflowsGrantTransport{Client: client, pushable: pushable}
h.e.transport = tr
return tr
}

// A base that changed .github/workflows/ supersedes a branch of the base
// repository only when the fixer that works an adopted head cannot push
// workflow files there. When its tier asks for the Workflows permission and
// the installation grants `workflows: write`, the base-sync lands on the
// PR's own branch, so the PR is adopted in place and keeps its number and
// approvals (projectbluefin/common#1689 was closed and re-opened as #1708).
// A compare failure still supersedes.
func TestAdoptionInPlaceWorkflowGuardHonoursGrant(t *testing.T) {
cases := []struct {
name string
pushable bool
files []string
failCompare bool
wantInPlace bool
wantAsked bool
}{
{name: "fixer can push workflows: same-repo head adopted in place", pushable: true, files: []string{".github/workflows/build.yml"}, wantInPlace: true, wantAsked: true},
{name: "fixer cannot push workflows: same-repo head superseded", files: []string{".github/workflows/build.yml"}, wantAsked: true},
{name: "fixer can push, compare error still supersedes", pushable: true, failCompare: true},
{name: "no workflow change needs no capability read", pushable: true, files: []string{"src/a.go"}, wantInPlace: true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := adoptPolicy()
p.AdoptInPlace = true
h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p)
defer done()
tr := withWorkflowsGrant(t, h, tc.pushable)
h.adopt.allowMaintainerEdits(7, true)
h.adopt.compareFiles = tc.files
h.adopt.failCompare = tc.failCompare
h.observe(adoptPR(7, "aaa"), "check-failing:unit")
labels := []string{"stage/072-returned-to-contributor", "role/contributor", "author/clanker"}
h.clock.advance(30 * time.Minute)
h.observe(adoptPR(7, "aaa", labels...), "check-failing:unit")
comments := adoptionComments(h.api)
if len(comments) != 1 {
t.Fatalf("adoption comments: %v", h.api.comments)
}
if got := strings.Contains(comments[0], "mode=in-place"); got != tc.wantInPlace {
t.Fatalf("in-place = %v, want %v: %s", got, tc.wantInPlace, comments[0])
}
if superseded := len(h.adopt.pulls) == 1; superseded == tc.wantInPlace {
t.Fatalf("superseding PRs = %d, in-place want %v", len(h.adopt.pulls), tc.wantInPlace)
}
asked := tr.askedAgents()
if (len(asked) > 0) != tc.wantAsked {
t.Fatalf("capability read for %v, want asked=%v", asked, tc.wantAsked)
}
for _, agent := range asked {
if agent != hgithub.FallbackFixAgent {
t.Fatalf("capability read for %q, want the fallback fixer %q", agent, hgithub.FallbackFixAgent)
}
}
})
}
}

// A fork branch takes no workflow-file push from the App whatever the base
// installation grants (projectbluefin/utah-packages#1173, actions#752), so
// the grant never makes a fork PR adoptable in place across a base workflow
// change: it stays with its author and is not copied into the base repo.
func TestAdoptionForkWorkflowGuardIgnoresGrant(t *testing.T) {
p := adoptPolicy()
p.AdoptInPlace = true
h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p)
defer done()
tr := withWorkflowsGrant(t, h, true)
h.adopt.allowMaintainerEdits(7, true)
h.adopt.compareFiles = []string{".github/workflows/build-stage.yml"}
h.observe(forkPR(7, "aaa"), "check-failing:unit")
labels := []string{"stage/072-returned-to-contributor", "role/contributor", "author/clanker"}
h.clock.advance(30 * time.Minute)
h.observe(forkPR(7, "aaa", labels...), "check-failing:unit")
if c := adoptionComments(h.api); len(c) != 0 {
t.Fatalf("fork PR adopted across a base workflow change: %v", c)
}
if len(h.adopt.pulls) != 0 || len(h.adopt.refs) != 0 {
t.Fatalf("fork code copied into the base repository: pulls=%v refs=%v", h.adopt.pulls, h.adopt.refs)
}
if rec, _ := h.e.stageStoreOrShared().Get(stageKey("acme", "widget", 7)); !rec.ForkRehomeRefused("aaa") {
t.Fatalf("fork head not recorded as staying with its author: %+v", rec)
}
if asked := tr.askedAgents(); len(asked) != 0 {
t.Fatalf("fork head read the fixer's workflows capability: %v", asked)
}
}

// A refused update-branch on an adopted head is logged with the merge
// sweep's own reason, not the catch-all.
func TestAdoptionRecoveryUpdateBranchSkip(t *testing.T) {
cases := []struct {
name string
status int
msg string
fork bool
want string
}{
{name: "fork head, base changed workflows", status: http.StatusForbidden, msg: workflowPermissionRefusal, fork: true, want: "fork-workflow-update"},
{name: "same-repo head, token lacks workflows", status: http.StatusForbidden, msg: workflowPermissionRefusal, want: "workflow-permission"},
{name: "fork not editable", status: http.StatusUnprocessableEntity, msg: "maintainer edits are not allowed", fork: true, want: "fork-not-editable"},
{name: "conflict", status: http.StatusUnprocessableEntity, msg: "merge conflict between base and head", want: "conflict"},
{name: "server error", status: http.StatusBadGateway, msg: "bad gateway", want: "update-branch-failed"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := &gh.ErrorResponse{Response: &http.Response{StatusCode: tc.status}, Message: tc.msg}
if got := adoptionRecoveryUpdateBranchSkip(classifyUpdateBranchError(err, tc.fork)); got != tc.want {
t.Fatalf("skip reason %q, want %q", got, tc.want)
}
})
}
if got := adoptionRecoveryUpdateBranchSkip(classifyUpdateBranchError(errors.New("network"), false)); got != "update-branch-failed" {
t.Fatalf("plain error: %q", got)
}
}

// projectbluefin/utah-packages#1173 and projectbluefin/actions#752: an
// in-place adoption of a fork head whose base changed .github/workflows/.
// The stale-red recovery's update-branch is refused (GitHub will not let an
// App push workflow files to a fork) and the log names that refusal instead
// of the catch-all update-branch-failed.
func TestAdoptedRecoveryForkWorkflowRefusalNamed(t *testing.T) {
p := adoptPolicy()
p.AdoptInPlace = true
h, done := newAdoptHarness(t, ContributorNudgeOutcome{Mode: "comment"}, p)
defer done()
h.adopt.allowMaintainerEdits(7, true)
h.observe(forkPR(7, "aaa"), recoveryReason)
key := stageKey("acme", "widget", 7)
h.e.stageStoreOrShared().MarkAdoptedInPlace(key, "aaa", AdoptReasonGrace, h.clock.at)
h.api.reset()
h.adopt.compareBase = "base1"
h.adopt.compareBehind = 18
h.adopt.baseChecks = map[string]string{"coverage": "success"}
h.adopt.updateBranchStatus = http.StatusForbidden
h.adopt.updateBranchMessage = workflowPermissionRefusal
h.clock.advance(adoptedDwell + time.Minute)
var logs bytes.Buffer
h.e.logger = slog.New(slog.NewTextHandler(&logs, &slog.HandlerOptions{Level: slog.LevelWarn}))

h.observe(forkPR(7, "aaa", adoptedLabels...), recoveryReason)
if len(h.adopt.branchUpdates) != 1 {
t.Fatalf("branch updates=%v, want one refused attempt", h.adopt.branchUpdates)
}
if len(h.adopt.pulls) != 0 || len(h.adopt.refs) != 0 {
t.Fatalf("fork code copied into the base repository: pulls=%v refs=%v", h.adopt.pulls, h.adopt.refs)
}
out := logs.String()
if !strings.Contains(out, "reason=skipped:fork-workflow-update") {
t.Fatalf("recovery log does not name the fork workflow refusal:\n%s", out)
}
if strings.Contains(out, "update-branch-failed") {
t.Fatalf("recovery log kept the catch-all reason:\n%s", out)
}
}
31 changes: 22 additions & 9 deletions src/pkg/github/automerge/automerge_adoption_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,11 @@ func (c *Engine) recoverByUpdateBranch(ctx context.Context, store *stagelabels.S
if wait := preparationWaitReason(err); wait != "" {
return adoptionRecovery{Deferred: true, Reason: adoptionRecoverySkipPrefix + wait}, true
}
var out adoptionRecovery
switch classifyUpdateBranchError(err, fork) {
case updateBranchUpToDate:
class := classifyUpdateBranchError(err, fork)
if class == updateBranchUpToDate {
return adoptionRecovery{}, false
case updateBranchForkNotEditable:
out = adoptionRecoverySkipped("fork-not-editable")
case updateBranchConflict:
out = adoptionRecoverySkipped("conflict")
default:
out = adoptionRecoverySkipped("update-branch-failed")
}
out := adoptionRecoverySkipped(adoptionRecoveryUpdateBranchSkip(class))
c.warn("adopted PR recovery", append(logArgs, "reason", out.Reason, "error", err)...)
return out, true
}
Expand All @@ -317,6 +311,25 @@ func (c *Engine) recoverByUpdateBranch(ctx context.Context, store *stagelabels.S
return adoptionRecovery{Attempted: true, Reason: adoptionRecoveryUpdateBranch}, true
}

// adoptionRecoveryUpdateBranchSkip names why a refused update-branch left
// the adopted head as it was. A workflow-file refusal gets the merge
// sweep's own reason (fork-workflow-update on a fork head the App cannot
// push workflow files to, workflow-permission on a branch of the base
// repository when the token lacks the grant), not the catch-all.
func adoptionRecoveryUpdateBranchSkip(class updateBranchFailure) string {
switch class {
case updateBranchForkNotEditable:
return "fork-not-editable"
case updateBranchConflict:
return "conflict"
case updateBranchForkWorkflowUpdate:
return autoMergeReasonForkWorkflowUpdate
case updateBranchWorkflowPermission:
return autoMergeReasonWorkflowPermission
}
return "update-branch-failed"
}

// recoverByRerun re-runs the failed jobs of the latest failed run of every
// workflow on the adopted head.
func (c *Engine) recoverByRerun(ctx context.Context, obs stageObservation, logArgs []any) adoptionRecovery {
Expand Down
Loading
Loading