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-dco-rehome-adopted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Adopted contributor PRs whose unsigned commits are the hive's own no longer hold at stage 074 with `dco-missing`: the stage engine re-signs them itself (in place on base-repo heads, re-homed onto `hive/adopt-<n>` on forks) and the replay now carries merge commits from a base sync instead of refusing to re-sign (#14666)
28 changes: 17 additions & 11 deletions src/docs/labels-and-control-signals.md
Original file line number Diff line number Diff line change
Expand Up @@ -1082,17 +1082,23 @@ When a clanker-authored contributor PR goes red the hive does not just relabel i
contributor-controlled and never count. The stage pass caches that split
with the commit check (`dco_unsigned_hive`), so the decision does not
depend on which labels the PR currently carries. When every unsigned
commit is App-linked and the branch is still pushable (base-repo head, or
`maintainer_can_modify` re-checked with one `GET pulls/N`), an in-place
adoption stays in place and the fixer rewrites just that unsigned range
with `-s` and force-pushes with lease. If any unsigned commit is
contributor-authored or unknown, or the branch cannot be pushed, the
adoption converts to `mode=supersede` (`reason=dco-rehome`): the hive
pushes `hive/adopt-<n>` at the head, opens the superseding PR and the fix
agent re-homes the diff onto that hive-owned branch from the base:
cherry-pick each original commit with `git cherry-pick -s`, or squash with
`git commit --author=<original author> -s`, preserving authorship/credits
while adding the hive submitter sign-off. The replacement PR keeps the
commit is App-linked and the head lives in the **base repo**, an in-place
adoption stays in place and the stage engine itself re-signs the branch
once per head (`reason=dco-rehomed`): it replays the PR's commits through
the git-data API, keeping each author, message, tree and parents (merge
commits from a base sync included) and appending the App's
`Signed-off-by`, then force-updates the ref only if it still points at the
observed head. The engine's installation token cannot rewrite a fork
branch, so a fork head — even one whose only unsigned commits are the
hive's own — and any head with a contributor-authored or unknown unsigned
commit converts to `mode=supersede` (`reason=dco-rehome`): the hive
pushes `hive/adopt-<n>` at the head with the same re-signed replay
(falling back to the unsigned head only when the replay cannot be
built), opens the superseding PR and the fix agent re-homes the diff onto
that hive-owned branch from the base: cherry-pick each original commit
with `git cherry-pick -s`, or squash with `git commit --author=<original
author> -s`, preserving authorship/credits while adding the hive
submitter sign-off ([#14666](https://github.com/hivecommons/hive/issues/14666)). The replacement PR keeps the
original title, starts with `Supersedes #N`, carries the original
`Fixes/Closes/Resolves` target (including `Hive-Run: owner/repo#NN` as
`Fixes #NN`), and the original PR is commented and closed so it does not
Expand Down
126 changes: 114 additions & 12 deletions src/pkg/github/automerge/automerge_contributor_adopt.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package automerge

import (
Expand Down Expand Up @@ -282,16 +282,35 @@
return adoptionDCORepairMode(obs, canPushInPlace) == stagelabels.AdoptModeSupersede
}

// adoptionDCORepairMode decides how a DCO-red head gets its sign-off:
// AdoptModeInPlace rewrites the hive's own unsigned commits on the PR
// branch, which the stage pass can only do through the git-data API of the
// base repository — a fork branch is pushable by an agent's git but not by
// the App's REST token, so a fork head re-homes (AdoptModeSupersede) even
// when every unsigned commit is the hive's (hivecommons/hive#14666).
func adoptionDCORepairMode(obs stageObservation, canPushInPlace bool) string {
if obs.Detail.DCO != stagelabels.DCONo || obs.Detail.DCOUnsigned <= 0 {
return ""
}
if canPushInPlace && obs.Detail.DCOUnsignedHive == obs.Detail.DCOUnsigned && obs.Detail.DCOUnsignedOther == 0 {
if canPushInPlace && headInBaseRepo(obs) && obs.Detail.DCOUnsignedHive == obs.Detail.DCOUnsigned && obs.Detail.DCOUnsignedOther == 0 {
return stagelabels.AdoptModeInPlace
}
return stagelabels.AdoptModeSupersede
}

// headInBaseRepo reports whether the PR head lives in the base repository
// (not a fork), where the App's git-data API can rewrite it.
func headInBaseRepo(obs stageObservation) bool {
return obs.HeadRepo != "" && strings.EqualFold(obs.HeadRepo, obs.Owner+"/"+obs.Repo)
}

// adoptedHeadNeedsDCORepair decides, for an in-place-adopted PR, whether the
// unsigned commits are the hive's own on a base-repo branch it rewrites in
// place with its sign-off.
func adoptedHeadNeedsDCORepair(obs stageObservation, rec stagelabels.Record, canPushInPlace bool) bool {
return adoptedHeadDCORed(obs, rec) && adoptionDCORepairMode(obs, canPushInPlace) == stagelabels.AdoptModeInPlace
}

// adoptedHeadDCORed reports whether an in-place-adopted PR's head carries
// unsigned commits at all (the cheap pre-check before resolving push access).
func adoptedHeadDCORed(obs stageObservation, rec stagelabels.Record) bool {
Expand Down Expand Up @@ -759,12 +778,15 @@
return 1, nil
}

// resignAdoptionCommits replays the contributor's commits as a linear chain
// with the App's Signed-off-by added where missing (the API equivalent of
// resignAdoptionCommits replays the PR's commits with the App's
// Signed-off-by added where missing (the API equivalent of
// `git rebase --signoff`), keeping each commit's tree, author and message,
// and returns the new tip. It returns "" when the commits cannot be replayed
// without changing content (a merge commit, or no commits), so the caller
// keeps the contributor head.
// and returns the new tip. Parents inside the PR map to their rewritten
// counterparts, so a base-sync merge commit (which the DCO checks exempt and
// hivecommons/hive#14500's recovery adds) replays with its tree and both
// parents rather than refusing the rewrite (hivecommons/hive#14666). It
// returns "" when the listing does not end at the observed head or a parent
// cannot be resolved, so the caller keeps the unsigned head.
func (c *Engine) resignAdoptionCommits(ctx context.Context, obs stageObservation) (string, error) {
opts := &gh.ListOptions{PerPage: 100}
var all []*gh.RepositoryCommit
Expand All @@ -782,30 +804,107 @@
if len(all) == 0 || all[len(all)-1].GetSHA() != obs.HeadSHA {
return "", nil
}
inPR := make(map[string]bool, len(all))
for _, rc := range all {
if len(rc.Parents) != 1 {
if rc == nil || len(rc.Parents) == 0 || rc.GetSHA() == "" {
return "", nil
}
inPR[rc.GetSHA()] = true
}
trailer := hgithub.BotSignedOffByTrailer(c.transport.AppBotLogin())
parent := all[0].Parents[0].GetSHA()
rewritten := make(map[string]string, len(all))
for _, rc := range all {
parents := make([]*gh.Commit, 0, len(rc.Parents))
for _, p := range rc.Parents {
sha := p.GetSHA()
if inPR[sha] {
// The listing is oldest-first; a parent inside the PR
// that is not rewritten yet means the order is not
// topological and the replay would detach it.
mapped, ok := rewritten[sha]
if !ok {
return "", nil
}
sha = mapped
}
parents = append(parents, &gh.Commit{SHA: gh.Ptr(sha)})
}
msg := rc.GetCommit().GetMessage()
if !signedOffByRE.MatchString(msg) {
if len(rc.Parents) == 1 && !signedOffByRE.MatchString(msg) {
msg = strings.TrimRight(msg, "\n") + "\n\n" + trailer + "\n"
}
created, _, err := c.gh.Git.CreateCommit(hgithub.WithRESTCaller(ctx, "hive:automerge_sweep"), obs.Owner, obs.Repo, &gh.Commit{
Message: gh.Ptr(msg),
Tree: &gh.Tree{SHA: gh.Ptr(rc.GetCommit().GetTree().GetSHA())},
Parents: []*gh.Commit{{SHA: gh.Ptr(parent)}},
Parents: parents,
Author: rc.GetCommit().GetAuthor(),
}, nil)
if err != nil {
return "", fmt.Errorf("re-signing %s: %w", shortSHA(rc.GetSHA()), err)
}
parent = created.GetSHA()
rewritten[rc.GetSHA()] = created.GetSHA()
}
return rewritten[obs.HeadSHA], nil
}

// adoptionDCORepaired is the reason logged and recorded when the hive
// rewrote its own unsigned commits in place with its sign-off.
const adoptionDCORepaired = "dco-rehomed"

// repairAdoptedDCOInPlace rewrites the unsigned hive commits of an
// in-place-adopted base-repo head with the App's Signed-off-by and
// force-updates the PR branch to the new tip — once per head, with the ref
// re-read first so a push that raced the rewrite is not overwritten
// (hivecommons/hive#14666). The next sweep re-reads the new head's commits
// and the DCO gate clears. Returns 0: a branch push is not a comment, so
// nothing is added to the caller's count.
func (c *Engine) repairAdoptedDCOInPlace(ctx context.Context, store *stagelabels.Store, key string, obs stageObservation, rec stagelabels.Record) (int, error) {
if rec.DCORepairAttempted(obs.HeadSHA) {
return 0, nil
}
if obs.HeadRef == "" {
c.warn("stage labels: in-place DCO repair skipped, head ref unknown", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA)
return 0, nil
}
store.MarkDCORepair(key, obs.HeadSHA)
c.info("stage labels: re-signing hive commits on adopted PR in place", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "branch", obs.HeadRef, "unsigned_hive", obs.Detail.DCOUnsignedHive, "reason", adoptionDCORepaired)
ref := "refs/heads/" + obs.HeadRef
var tip string
_, err := effects.Execute(ctx, c.mutation, effects.Claim{
Repo: obs.Owner + "/" + obs.Repo, Kind: effects.KindBranchPush, Target: obs.HeadRef, Actor: "stage_labels",
Inputs: map[string]string{"pr": strconv.Itoa(obs.Number), "head_sha": obs.HeadSHA, "dco_repair": "true"},
}, func(ctx context.Context) (effects.Result, error) {
current, _, apiErr := c.gh.Git.GetRef(ctx, obs.Owner, obs.Repo, ref)
if apiErr != nil {
return effects.Result{}, effects.NotApplied(apiErr)
}
if got := current.GetObject().GetSHA(); got != obs.HeadSHA {
return effects.Result{}, effects.NotApplied(fmt.Errorf("head moved to %s before the rewrite; re-evaluating", shortSHA(got)))
}
signed, signErr := c.resignAdoptionCommits(ctx, obs)
if signErr != nil {
return effects.Result{}, effects.NotApplied(signErr)
}
if signed == "" {
return effects.Result{}, effects.NotApplied(fmt.Errorf("commits of %s cannot be replayed with sign-off", shortSHA(obs.HeadSHA)))
}
if _, _, apiErr := c.gh.Git.UpdateRef(ctx, obs.Owner, obs.Repo, &gh.Reference{
Ref: gh.Ptr(ref),
Object: &gh.GitObject{SHA: gh.Ptr(signed)},
}, true); apiErr != nil {
return effects.Result{}, effects.NotApplied(apiErr)
}
tip = signed
return effects.Result{Provenance: signed}, nil
})
if err != nil {
c.warn("stage labels: in-place DCO repair failed; branch keeps the unsigned head", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "branch", obs.HeadRef, "error", err)
return 0, err
}
return parent, nil
store.MarkHivePushedHead(key, tip)
c.info("stage labels: adopted PR re-signed in place", "repo", obs.Display, "pr", obs.Number, "head", obs.HeadSHA, "new_head", tip, "branch", obs.HeadRef, "reason", adoptionDCORepaired)
// A branch push, not a comment: nothing to add to the caller's count.
return 0, nil
}

// settleAdoption polls the superseding App PR for a PR in stage 074 (at
Expand All @@ -829,6 +928,9 @@
}
if adoptedHeadDCORed(obs, rec) {
canPushInPlace := c.canPushToPRBranch(ctx, obs)
if adoptedHeadNeedsDCORepair(obs, rec, canPushInPlace) {
return c.repairAdoptedDCOInPlace(ctx, store, key, obs, rec)
}
if adoptedHeadNeedsDCORehome(obs, rec, canPushInPlace) {
fence, err := c.adoptionDiffRefusal(ctx, obs)
if fence != "" && (err != nil || adoptionFenceDefers(fence)) {
Expand Down
Loading
Loading