diff --git a/.github/scripts/native-queue-policy.cjs b/.github/scripts/native-queue-policy.cjs index b90fc0f2da..6d0725082e 100644 --- a/.github/scripts/native-queue-policy.cjs +++ b/.github/scripts/native-queue-policy.cjs @@ -31,6 +31,11 @@ const FORWARD_MERGE = { author: { login: 'github-actions[bot]', id: 41898282, type: 'Bot' }, }; const PROW = { login: 'kubestellar-prow[bot]', id: 208851060, context: 'dco' }; +// Line-to-line forward-merge heads (v6-topup.yml's `sync/v5-to-v6`, a hand-carried +// `sync/v5-to-v6-`, `sync/v4-to-v5`, `topup/...`). They must land by merge +// commit: a squash or rebase keeps the content but drops the source line's +// history, and every later top-up then re-conflicts on all of it (#18931). +const FORWARD_MERGE_HEAD = /^(?:sync\/v\d+-to-v\d+|topup\/)/; // REST pull-request commit listing stops at 250; a longer list is unverifiable. const MAX_PR_COMMITS = 250; const PROBOT_DCO_PATH = 'DCO path: probot DCO (App 1861)'; @@ -417,7 +422,14 @@ async function evaluateOnce({ github, context }) { ...repo, basehead: `${head}...${group.head_sha}`, }); if (!['ahead', 'identical'].includes(comparison.status) || comparison.merge_base_commit?.sha !== head) { - throw new Error(`#${number}: current PR head is not contained in the tested group`); + // A queue configured to squash or rebase never contains the PR's own head + // commit, so the containment test above is also the merge-method guard + // for top-ups; name it, because "not contained" reads like a CI fault. + throw new Error(FORWARD_MERGE_HEAD.test(pr.head?.ref ?? '') + ? `#${number}: forward-merge PR ${pr.head.ref} must land with a merge commit, never a squash or rebase ` + + `(the tested group does not contain its head ${head}, so v5's ancestry would be lost; #18931). ` + + 'Remove it from the queue and merge it with the merge-commit method' + : `#${number}: current PR head is not contained in the tested group`); } await assertReviews(github, repo, number, head); paths.push(`#${number} ${await assertEvidence(github, repo, number, head, { ...POLICY_CHECKS, ...BUILD_CHECKS }, pr)}`); @@ -444,7 +456,7 @@ module.exports = { ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS, PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate, isTransientAPIError, isCosmeticLabelFlip, replayLastVerdict, BLOCKED_LABEL, - ProviderUnavailable, ProviderVerdict, PROVIDER_VERDICT_NOTE, FORWARD_MERGE, PROW, + ProviderUnavailable, ProviderVerdict, PROVIDER_VERDICT_NOTE, FORWARD_MERGE, FORWARD_MERGE_HEAD, PROW, DCO_ENUMERATION_SUMMARY_PREFIX, DCO_ENUMERATION_SUMMARY_FALLBACK, DCO_PROVIDER_UNAVAILABLE_TEXT, DCO_PROVIDER_RUNBOOK, }; diff --git a/.github/scripts/native-queue-policy.test.cjs b/.github/scripts/native-queue-policy.test.cjs index ea58100a1c..af3fd7346c 100644 --- a/.github/scripts/native-queue-policy.test.cjs +++ b/.github/scripts/native-queue-policy.test.cjs @@ -5,7 +5,7 @@ const { ACTIONS_APP_ID, DCO_APP_ID, POLICY_CHECKS, BUILD_CHECKS, PendingEvidence, membersForGroup, assertEligible, evidenceForHead, closedNoop, evaluate, isTransientAPIError, isCosmeticLabelFlip, replayLastVerdict, - ProviderUnavailable, ProviderVerdict, PROVIDER_VERDICT_NOTE, FORWARD_MERGE, PROW, + ProviderUnavailable, ProviderVerdict, PROVIDER_VERDICT_NOTE, FORWARD_MERGE, FORWARD_MERGE_HEAD, PROW, DCO_ENUMERATION_SUMMARY_PREFIX, DCO_ENUMERATION_SUMMARY_FALLBACK, DCO_PROVIDER_UNAVAILABLE_TEXT, @@ -290,6 +290,32 @@ test('one commit unreachable from v5 keeps the forward-merge on probot DCO', asy assert.match(await evaluate(unreachable), /DCO path: probot DCO \(App 1861\); sync\/v5-to-v6 fallback/); }); +// #18931: a squash or rebase queue never contains the PR's own head commit, so +// a forward-merge PR in such a group is refused by name. +test('a forward-merge PR whose head is not in the tested group is refused as a merge-method violation', async () => { + for (const ref of ['sync/v5-to-v6', 'sync/v5-to-v6-2026-11-18', 'sync/v4-to-v5', 'topup/v5-to-v6-202611171235']) { + const f = fixture('merge_group'); + f.state.prs[0].head = { ...f.state.prs[0].head, ref }; + f.state.ancestry = 'diverged'; + await assert.rejects(evaluate(f), error => { + assert.match(error.message, /must land with a merge commit, never a squash or rebase/); + assert.ok(error.message.includes(ref), error.message); + return true; + }); + // Contained head (the merge-commit method): the guard stays out of the way. + const ok = fixture('merge_group'); + ok.state.prs[0].head = { ...ok.state.prs[0].head, ref }; + await evaluate(ok); + } + // Any other branch keeps the generic message: the guard is scoped to forward-merges. + const other = fixture('merge_group'); + other.state.prs[0].head = { ...other.state.prs[0].head, ref: 'feature/sync-things' }; + other.state.ancestry = 'diverged'; + await assert.rejects(evaluate(other), /current PR head is not contained in the tested group/); + for (const ref of ['sync/v5-to-v6', 'topup/x', 'sync/v4-to-v5-3']) assert.ok(FORWARD_MERGE_HEAD.test(ref), ref); + for (const ref of ['feature/topup/x', 'fix/sync/v5-to-v6', 'sync-v5-to-v6', 'topup']) assert.ok(!FORWARD_MERGE_HEAD.test(ref), ref); +}); + test('another head branch, base, fork or author keeps requiring probot DCO', async () => { for (const change of [ pr => { pr.head.ref = 'sync/v5-to-v6-extra'; }, diff --git a/.github/scripts/test-v6-topup-ancestry.sh b/.github/scripts/test-v6-topup-ancestry.sh new file mode 100755 index 0000000000..27d7470647 --- /dev/null +++ b/.github/scripts/test-v6-topup-ancestry.sh @@ -0,0 +1,145 @@ +#!/usr/bin/env bash +# test-v6-topup-ancestry.sh — exercises .github/scripts/v6-topup-ancestry.sh +# against throwaway repositories: a merge-commit top-up (ancestry intact), a +# squash-merged top-up (ancestry lost, #18931), the ours-merge heal that makes +# the next forward-merge conflict-free again, and the forged-marker cases the +# heal must refuse. +set -euo pipefail + +ANCESTRY="$(cd "$(dirname "$0")" && pwd)/v6-topup-ancestry.sh" +WORK="$(mktemp -d)" +trap 'rm -rf "$WORK"' EXIT +fail=0 +export V5_REF=v5 V6_REF=v6 + +expect() { + local name="$1" want="$2" + shift 2 + local rc=0 + ( "$@" ) >"$WORK/$name.log" 2>&1 || rc=$? + if { [ "$want" = ok ] && [ "$rc" -ne 0 ]; } || { [ "$want" = fail ] && [ "$rc" -eq 0 ]; }; then + echo "FAIL $name (want $want, rc=$rc)"; sed 's/^/ /' "$WORK/$name.log"; fail=1 + else + echo "ok $name" + fi +} + +# new_repo DIR: base commit on v5 plus a v6 branch with one adapted file. +new_repo() { + git init -q -b v5 "$1" + cd "$1" + git config user.name test + git config user.email test@example.com + echo base > shared.txt + git add -A && git commit -qm base + git branch v6 +} + +# topup_pr: build the bot's top-up head (merge of v5 into v6) on sync/v5-to-v6 +# and leave the repo on v6. Prints nothing; sets HEAD_SHA and CARRIED. +topup_pr() { + CARRIED="$(git rev-parse v5)" + git checkout -q -B sync/v5-to-v6 v6 + # -X theirs: v5's side wins every overlap, standing in for the human resolution. + git merge --no-ff -q -X theirs --signoff v5 -m "topup" >/dev/null + HEAD_SHA="$(git rev-parse HEAD)" + git checkout -q v6 +} + +# v5 and v6 both touch shared.txt in the carried range (v6 adapts it), then a +# top-up PR carries v5. Leaves v5 one commit past the carried head. +scenario_lost() { + new_repo "$1" + echo v5-one > shared.txt && git commit -qam "v5 one" + echo v5-only > newfile.txt && git add -A && git commit -qm "v5 adds newfile" + git checkout -q v6 + echo v6-adapted > shared.txt && git commit -qam "v6 adapts" + topup_pr + # Squash-merge the PR: v6 gets the content as ONE single-parent commit. + git merge --squash -q sync/v5-to-v6 >/dev/null 2>&1 || true + git commit -qm "squash of the top-up" || true + MERGED_SHA="$(git rev-parse v6)" + # v5 moves on over the same lines. + git checkout -q v5 + echo v5-two > shared.txt && git commit -qam "v5 two" + echo v5-two-new > newfile.txt && git commit -qam "v5 two newfile" + git checkout -q v6 +} + +case_marker_roundtrip() { + new_repo "$WORK/m" && cd "$WORK/m" + local sha=0123456789abcdef0123456789abcdef01234567 line + line="$(bash "$ANCESTRY" marker-line "$sha")" + [ "$line" = "" ] || { echo "bad marker line: $line"; return 1; } + printf 'text\n%s\nmore\n' "$line" > "$WORK/body.md" + [ "$(bash "$ANCESTRY" marker "$WORK/body.md")" = "$sha" ] || { echo "marker not read back"; return 1; } + printf 'no marker here\n\n' > "$WORK/body2.md" + [ -z "$(bash "$ANCESTRY" marker "$WORK/body2.md")" ] || { echo "malformed marker accepted"; return 1; } + bash "$ANCESTRY" marker-line abc && { echo "short sha accepted"; return 1; } + return 0 +} + +case_level_after_merge_commit() { + new_repo "$WORK/lvl" && cd "$WORK/lvl" + echo v5-one > newfile.txt && git add -A && git commit -qm "v5 one" + git checkout -q v6 + topup_pr + git merge --no-ff -q sync/v5-to-v6 -m "merge commit landing" >/dev/null + [ "$(bash "$ANCESTRY" classify "$CARRIED")" = level ] || { echo "merge-commit landing not level"; return 1; } +} + +case_lost_after_squash() { + scenario_lost "$WORK/lost" && cd "$WORK/lost" + [ "$(bash "$ANCESTRY" classify "$CARRIED")" = lost ] || { echo "squash landing not detected"; return 1; } + bash "$ANCESTRY" verify "$CARRIED" "$HEAD_SHA" "$MERGED_SHA" || { echo "genuine marker refused"; return 1; } +} + +case_unknown_without_marker() { + scenario_lost "$WORK/unk" && cd "$WORK/unk" + [ "$(bash "$ANCESTRY" classify "")" = unknown ] || { echo "empty carried not unknown"; return 1; } +} + +# Without the heal the next forward-merge conflicts; with the ours-merge it is +# clean and v5 ends up an ancestor of the result. +case_heal_restores_clean_merge() { + scenario_lost "$WORK/heal" && cd "$WORK/heal" + git checkout -q -B plain v6 + git merge --no-ff -q --signoff v5 -m plain >/dev/null 2>&1 && { echo "expected the un-healed merge to conflict"; return 1; } + git merge --abort + git checkout -q -B healed v6 + git merge -s ours --no-ff --signoff "$CARRIED" -m "restore ancestry" >/dev/null + [ "$(git rev-parse 'HEAD^{tree}')" = "$(git rev-parse 'v6^{tree}')" ] || { echo "ours-merge changed the tree"; return 1; } + git merge --no-ff -q --signoff v5 -m healed >/dev/null 2>&1 || { echo "healed merge still conflicts"; return 1; } + git merge-base --is-ancestor v5 HEAD || { echo "v5 is not an ancestor after the heal"; return 1; } + git log -1 --format=%B HEAD~1 | grep -q '^Signed-off-by: test $' || { echo "heal commit not signed off"; return 1; } +} + +case_forged_marker_refused() { + scenario_lost "$WORK/forged" && cd "$WORK/forged" + # A v5 commit the top-up did NOT carry: ours-merging it would drop its content. + local uncarried + uncarried="$(git rev-parse v5)" + bash "$ANCESTRY" verify "$uncarried" "$HEAD_SHA" "$MERGED_SHA" && { echo "uncarried head accepted"; return 1; } + # The PR's merge commit must be on v6. + bash "$ANCESTRY" verify "$CARRIED" "$HEAD_SHA" "$uncarried" && { echo "merge commit off v6 accepted"; return 1; } + # The PR head must be a two-parent merge. + bash "$ANCESTRY" verify "$CARRIED" "$CARRIED" "$MERGED_SHA" && { echo "non-merge head accepted"; return 1; } + return 0 +} + +case_unrecoverable_off_v5() { + scenario_lost "$WORK/off" && cd "$WORK/off" + git checkout -q -b stray v5 && echo s > s.txt && git add -A && git commit -qm stray + [ "$(bash "$ANCESTRY" classify "$(git rev-parse stray)")" = unrecoverable ] || { echo "off-v5 head not unrecoverable"; return 1; } + [ "$(bash "$ANCESTRY" classify 0000000000000000000000000000000000000000)" = unrecoverable ] || { echo "missing commit not unrecoverable"; return 1; } +} + +expect marker-roundtrip ok case_marker_roundtrip +expect level-after-merge-commit ok case_level_after_merge_commit +expect lost-after-squash ok case_lost_after_squash +expect unknown-without-marker ok case_unknown_without_marker +expect heal-restores-clean-merge ok case_heal_restores_clean_merge +expect forged-marker-refused ok case_forged_marker_refused +expect unrecoverable-off-v5 ok case_unrecoverable_off_v5 + +exit "$fail" diff --git a/.github/scripts/v6-topup-ancestry.sh b/.github/scripts/v6-topup-ancestry.sh new file mode 100755 index 0000000000..c422a9fdb3 --- /dev/null +++ b/.github/scripts/v6-topup-ancestry.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# v6-topup-ancestry.sh — the v5-ancestry bookkeeping behind +# .github/workflows/v6-topup.yml (#18931). +# +# WHY. A v5 -> v6 top-up PR merged with a squash or rebase puts v5's CONTENT on +# v6 but not v5's HISTORY. `git merge-base origin/v5 origin/v6` then stays at +# the previous merge commit, and every later forward-merge replays the whole +# range against v6's already-adapted copies of the same files: #18689 was +# squash-merged and the next top-up hit 109 conflicted files (17 add/add). +# +# The workflow records the v5 head each top-up carried in the PR body as +# +# and, when that head is not an ancestor of origin/v6 (the PR was not merged +# with a merge commit), restores the ancestry with an ours-merge of it before +# merging origin/v5. An ours-merge records history and changes no file. +# +# Subcommands (run from the repo root): +# marker-line print the PR-body marker line +# marker print the carried sha in the body, if any +# verify +# exit 0 when the marker is trustworthy: the PR head is a two-parent +# merge whose second parent IS and the PR's merge commit is on +# v6. (Whether is still v5 history is classify's call.) A marker is only a claim in a PR body; +# an ours-merge of a v5 commit whose content is NOT on v6 would silently +# drop that content, so the claim is bound to the PR's own head commit. +# classify print one state: +# level carried head is an ancestor of origin/v6: ancestry intact +# unknown no carried head is known: nothing can be checked +# lost carried head is v5 history but not on v6: heal with an +# ours-merge of it +# unrecoverable carried head is not on origin/v5 (v5 history rewritten) +# Environment: V5_REF (default origin/v5), V6_REF (default origin/v6). +set -euo pipefail + +V5_REF="${V5_REF:-origin/v5}" +V6_REF="${V6_REF:-origin/v6}" +SHA_RE='^[0-9a-f]{40}$' +MARKER_RE='' + +is_commit() { git cat-file -e "$1^{commit}" 2>/dev/null; } +is_ancestor() { git merge-base --is-ancestor "$1" "$2"; } + +cmd="${1:-}" +shift || true +case "$cmd" in + marker-line) + [[ "${1:-}" =~ $SHA_RE ]] || { echo "marker-line: need a full 40-hex sha" >&2; exit 2; } + printf '\n' "$1" + ;; + marker) + [ -f "${1:-}" ] || { echo "marker: need a body file" >&2; exit 2; } + # Last marker wins; a body with none prints nothing and still exits 0. + while IFS= read -r line; do + if [[ "$line" =~ $MARKER_RE ]]; then found="${BASH_REMATCH[1]}"; fi + done < "$1" + [ -z "${found:-}" ] || printf '%s\n' "$found" + ;; + verify) + carried="${1:-}" head="${2:-}" merged="${3:-}" + for s in "$carried" "$head" "$merged"; do + [[ "$s" =~ $SHA_RE ]] || { echo "verify: need three full 40-hex shas" >&2; exit 2; } + is_commit "$s" || { echo "verify: $s is not a fetched commit" >&2; exit 1; } + done + parents="$(git rev-list --parents -n 1 "$head")" + # shellcheck disable=SC2086 + set -- $parents + [ "$#" -eq 3 ] && [ "$3" = "$carried" ] || { echo "verify: PR head $head is not a merge of $carried" >&2; exit 1; } + is_ancestor "$merged" "$V6_REF" || { echo "verify: merge commit $merged is not on $V6_REF" >&2; exit 1; } + ;; + classify) + carried="${1:-}" + if [ -z "$carried" ]; then echo unknown; exit 0; fi + [[ "$carried" =~ $SHA_RE ]] || { echo "classify: not a full sha: $carried" >&2; exit 2; } + if ! is_commit "$carried" || ! is_ancestor "$carried" "$V5_REF"; then echo unrecoverable; exit 0; fi + if is_ancestor "$carried" "$V6_REF"; then echo level; else echo lost; fi + ;; + *) + echo "usage: v6-topup-ancestry.sh marker-line|marker|verify|classify ..." >&2 + exit 2 + ;; +esac diff --git a/.github/workflows/v6-topup-autoresolve-tests.yml b/.github/workflows/v6-topup-autoresolve-tests.yml index c8c44cdc8b..be0d558864 100644 --- a/.github/workflows/v6-topup-autoresolve-tests.yml +++ b/.github/workflows/v6-topup-autoresolve-tests.yml @@ -5,6 +5,8 @@ on: paths: - '.github/scripts/v6-topup-autoresolve.sh' - '.github/scripts/test-v6-topup-autoresolve.sh' + - '.github/scripts/v6-topup-ancestry.sh' + - '.github/scripts/test-v6-topup-ancestry.sh' - '.github/workflows/v6-topup.yml' - '.github/workflows/v6-topup-autoresolve-tests.yml' push: @@ -12,6 +14,8 @@ on: paths: - '.github/scripts/v6-topup-autoresolve.sh' - '.github/scripts/test-v6-topup-autoresolve.sh' + - '.github/scripts/v6-topup-ancestry.sh' + - '.github/scripts/test-v6-topup-ancestry.sh' - '.github/workflows/v6-topup.yml' - '.github/workflows/v6-topup-autoresolve-tests.yml' @@ -32,3 +36,8 @@ jobs: # pattern and checks the resolver finishes them, and refuses others. - name: Test v6-topup-autoresolve.sh run: bash .github/scripts/test-v6-topup-autoresolve.sh + # Throwaway repos for the squash-merged-top-up case (#18931): the carried + # v5 head is detected as lost, the ours-merge heal restores a conflict-free + # forward-merge, and a forged PR-body marker is refused. + - name: Test v6-topup-ancestry.sh + run: bash .github/scripts/test-v6-topup-ancestry.sh diff --git a/.github/workflows/v6-topup.yml b/.github/workflows/v6-topup.yml index c8142c43d7..1b2d257c57 100644 --- a/.github/workflows/v6-topup.yml +++ b/.github/workflows/v6-topup.yml @@ -72,9 +72,110 @@ jobs: echo "v6 is ${behind} commit(s) behind v5." fi + - name: Ensure gh CLI is available + if: steps.drift.outputs.behind != '0' + run: | + set -euo pipefail + command -v gh >/dev/null 2>&1 && exit 0 + VER=2.63.2 + # #7976: pin and verify unconditionally (the v2-ci.yml `just` + # convention). This job's checkout persists a push credential — a + # workflows-write PAT when TOPUP_PUSH_TOKEN is set — so an + # unverified binary here would run with the repo's most powerful + # CI token in its working tree. Values from gh_${VER}_checksums.txt. + SHA256_amd64=912fdb1ca29cb005fb746fc5d2b787a289078923a29d0f9ec19a0b00272ded00 + SHA256_arm64=0f31e2a8549c64b5c1679f0b99ce5e0dac7c91da9e86f6246adb8805b0f0b4bb + ARCH="$(dpkg --print-architecture 2>/dev/null || uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')" + case "$ARCH" in + amd64) SHA256="$SHA256_amd64" ;; + arm64) SHA256="$SHA256_arm64" ;; + *) echo "::error::no pinned gh checksum for arch ${ARCH}"; exit 1 ;; + esac + curl -fsSL "https://github.com/cli/cli/releases/download/v${VER}/gh_${VER}_linux_${ARCH}.tar.gz" -o /tmp/gh.tgz + echo "${SHA256} /tmp/gh.tgz" | sha256sum -c - + tar -xzf /tmp/gh.tgz -C /tmp + mkdir -p "$HOME/.local/bin" + install "/tmp/gh_${VER}_linux_${ARCH}/bin/gh" "$HOME/.local/bin/gh" + echo "$HOME/.local/bin" >> "$GITHUB_PATH" + + # #18931: v5 -> v6 only stays conflict-free while every top-up lands on v6 + # with a MERGE COMMIT. A squash or rebase keeps v5's content but drops its + # history, so the merge base stays at the previous top-up and the next + # forward-merge replays the whole range (#18689 was squashed: 109 + # conflicted files, 17 add/add, on every v5 push afterwards). Each top-up + # PR body records the v5 head it carried; this step finds the newest + # merged one and checks that head is still an ancestor of origin/v6. + - name: Check v5 ancestry on v6 (#18931) + id: ancestry + if: steps.drift.outputs.behind != '0' + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + set -euo pipefail + # Run from a copy taken off the triggering ref: the merge step + # checks out v6's tree, which may not carry this script yet. + cp .github/scripts/v6-topup-ancestry.sh "$RUNNER_TEMP/v6-topup-ancestry.sh" + anc="$RUNNER_TEMP/v6-topup-ancestry.sh" + owner="${GITHUB_REPOSITORY%%/*}" + carried="" + source_pr="" + prs="$(gh api "repos/${GITHUB_REPOSITORY}/pulls?state=closed&base=v6&head=${owner}:sync/v5-to-v6&per_page=30" \ + --jq '[.[] | select(.merged_at != null)] | sort_by(.merged_at) | reverse | .[0] // empty')" + if [ -n "$prs" ]; then + number="$(jq -r .number <<<"$prs")" + author_id="$(jq -r .user.id <<<"$prs")" + author_type="$(jq -r .user.type <<<"$prs")" + jq -r '.body // ""' <<<"$prs" > "$RUNNER_TEMP/last-topup-body.md" + marked="$("$anc" marker "$RUNNER_TEMP/last-topup-body.md")" + if [ -z "$marked" ]; then + echo "Newest merged top-up PR #${number} has no carried-head marker; ancestry cannot be checked." + # A marker in a PR body is only a claim: only the workflow's own + # bot may have written one, and it must match that PR's real head. + elif [ "$author_id" != "41898282" ] || [ "$author_type" != "Bot" ]; then + echo "::warning::Ignoring the carried-head marker on #${number}: not opened by the top-up workflow." + else + pr_head="$(jq -r .head.sha <<<"$prs")" + merged_sha="$(jq -r .merge_commit_sha <<<"$prs")" + git fetch --quiet origin "+refs/pull/${number}/head:refs/remotes/pull/${number}/head" || true + if "$anc" verify "$marked" "$pr_head" "$merged_sha"; then + carried="$marked" + source_pr="$number" + else + echo "::warning::Ignoring the carried-head marker on #${number}: it does not match that PR's head commit." + fi + fi + fi + state="$("$anc" classify "$carried")" + { + echo "state=${state}" + echo "carried=${carried}" + echo "source_pr=${source_pr}" + } >> "$GITHUB_OUTPUT" + case "$state" in + level) + echo "v5 ancestry intact: ${carried} (carried by #${source_pr}) is an ancestor of origin/v6." ;; + unknown) + echo "No verified carried v5 head on record; merging origin/v5 as is." ;; + lost) + echo "::warning::v5 ancestry was lost: ${carried}, carried by top-up PR #${source_pr}, is not an ancestor of origin/v6, so #${source_pr} was squash- or rebase-merged. Top-up PRs must land with a merge commit. Restoring the ancestry with an ours-merge of ${carried} (no file changes) before merging origin/v5." + { + echo "### v5 ancestry was lost" + echo "" + echo "Top-up PR #${source_pr} carried \`${carried}\`, but that commit is not an ancestor of \`origin/v6\`: the PR was squash- or rebase-merged. Top-up PRs must be merged with a merge commit. The workflow restores the ancestry with an ours-merge of \`${carried}\` before merging \`origin/v5\`." + } >> "$GITHUB_STEP_SUMMARY" ;; + unrecoverable) + echo "::error::v5 ancestry was lost and cannot be restored automatically: ${carried}, carried by top-up PR #${source_pr}, is not an ancestor of origin/v5 (v5 history was rewritten)." + echo "Nothing was pushed. Carry the top-up by hand and merge it with a merge commit." + exit 1 ;; + esac + - name: Forward-merge v5 into the top-up branch id: merge if: steps.drift.outputs.behind != '0' + env: + ANCESTRY_STATE: ${{ steps.ancestry.outputs.state }} + CARRIED: ${{ steps.ancestry.outputs.carried }} + SOURCE_PR: ${{ steps.ancestry.outputs.source_pr }} run: | set -euo pipefail # Always rebuild the branch from v6's tip. Reusing a stale top-up @@ -83,6 +184,16 @@ jobs: cp .github/scripts/v6-topup-autoresolve.sh "$RUNNER_TEMP/v6-topup-autoresolve.sh" git checkout -B sync/v5-to-v6 origin/v6 + # #18931: a previous top-up was squash/rebase-merged, so its v5 head + # is not on v6's history. Record it as merged (an ours-merge keeps + # v6's tree: that content is already on v6) so the merge below only + # has to carry what v5 gained since. Signed off by this bot, for its + # own merge commit only. + if [ "${ANCESTRY_STATE}" = "lost" ]; then + git merge -s ours --no-ff --signoff "${CARRIED}" \ + -m "🩹 Restore v5 ancestry: record ${CARRIED:0:9} (carried by #${SOURCE_PR}) as merged" + fi + # --signoff because the DCO audits treat a merge commit as a commit # like any other. if git merge --no-ff --signoff origin/v5 \ @@ -146,6 +257,8 @@ jobs: done < /tmp/conflict-rows.tsv echo "" echo "**$files** conflicting file(s), **$total** conflicted line(s) in total." + echo "" + echo "Merge base of v5 and v6: \`$(git merge-base origin/v5 origin/v6)\` ($(git log -1 --format=%cs "$(git merge-base origin/v5 origin/v6)")). If that is far behind v5, a previous top-up lost v5's ancestry (it was squash- or rebase-merged); check that first (see below)." } > /tmp/conflict-report.md cat /tmp/conflict-report.md >> "$GITHUB_STEP_SUMMARY" @@ -157,18 +270,27 @@ jobs: - name: Report a conflicting top-up and stop if: steps.merge.outputs.conflict == 'true' + env: + CARRIED: ${{ steps.ancestry.outputs.carried }} run: | echo "::error::v5 -> v6 forward-merge conflicts and needs a human (see #7199 for the precedent)." - echo "Nothing was pushed. Resolve locally:" + echo "Nothing was pushed. Check the ancestry FIRST: a squash- or rebase-merged top-up (#18931) makes" + echo "every later merge conflict on files it already carried, hundreds of them, and no resolver can fix that." + echo " git fetch origin v5 v6" + echo " git merge-base --is-ancestor origin/v6 && echo intact || echo LOST" + echo "Then resolve locally:" echo " git checkout -B sync/v5-to-v6 origin/v6" + echo " # only if the ancestry check said LOST (records history, changes no file):" + echo " git merge -s ours --no-ff --signoff " echo " git merge --no-ff --signoff origin/v5" - echo " # resolve, then push and open a PR against v6" + echo " # resolve, then push and open a PR against v6; merge it with a MERGE COMMIT, never squash or rebase" exit 1 - name: Refuse a workflow-file delta the push credential cannot carry (#7959) if: steps.merge.outputs.conflict == 'false' env: HAS_PUSH_TOKEN: ${{ secrets.TOPUP_PUSH_TOKEN != '' }} + ANCESTRY_STATE: ${{ steps.ancestry.outputs.state }} run: | set -euo pipefail [ "${HAS_PUSH_TOKEN}" = "true" ] && exit 0 @@ -176,64 +298,68 @@ jobs: [ -z "${touched}" ] && exit 0 echo "::error::v5 -> v6 top-up touches workflow files and TOPUP_PUSH_TOKEN is not set — GITHUB_TOKEN cannot push them (#7959)." echo "Files: $(echo "${touched}" | tr '\n' ' ')" - echo "Nothing was pushed. Either add repo secret TOPUP_PUSH_TOKEN (fine-grained PAT, Contents: write + Workflows: write) or carry the sync by hand:" + echo "Nothing was pushed. Either add repo secret TOPUP_PUSH_TOKEN (fine-grained PAT, Contents: write + Workflows: write) or carry the sync by hand" + echo "(check the v5 ancestry first, see the conflict report above; this top-up's state was: ${ANCESTRY_STATE:-unknown}):" echo " git checkout -B sync/v5-to-v6 origin/v6" echo " git merge --no-ff --signoff origin/v5" - echo " git push -u origin sync/v5-to-v6 # then open a PR against v6" + echo " git push -u origin sync/v5-to-v6 # then open a PR against v6, to be merged with a MERGE COMMIT" exit 1 - name: Push the top-up branch if: steps.merge.outputs.conflict == 'false' run: git push --force-with-lease origin sync/v5-to-v6 - - name: Ensure gh CLI is available - if: steps.merge.outputs.conflict == 'false' - run: | - set -euo pipefail - command -v gh >/dev/null 2>&1 && exit 0 - VER=2.63.2 - # #7976: pin and verify unconditionally (the v2-ci.yml `just` - # convention). This job's checkout persists a push credential — a - # workflows-write PAT when TOPUP_PUSH_TOKEN is set — so an - # unverified binary here would run with the repo's most powerful - # CI token in its working tree. Values from gh_${VER}_checksums.txt. - SHA256_amd64=912fdb1ca29cb005fb746fc5d2b787a289078923a29d0f9ec19a0b00272ded00 - SHA256_arm64=0f31e2a8549c64b5c1679f0b99ce5e0dac7c91da9e86f6246adb8805b0f0b4bb - ARCH="$(dpkg --print-architecture 2>/dev/null || uname -m | sed 's/x86_64/amd64/;s/aarch64/arm64/')" - case "$ARCH" in - amd64) SHA256="$SHA256_amd64" ;; - arm64) SHA256="$SHA256_arm64" ;; - *) echo "::error::no pinned gh checksum for arch ${ARCH}"; exit 1 ;; - esac - curl -fsSL "https://github.com/cli/cli/releases/download/v${VER}/gh_${VER}_linux_${ARCH}.tar.gz" -o /tmp/gh.tgz - echo "${SHA256} /tmp/gh.tgz" | sha256sum -c - - tar -xzf /tmp/gh.tgz -C /tmp - mkdir -p "$HOME/.local/bin" - install "/tmp/gh_${VER}_linux_${ARCH}/bin/gh" "$HOME/.local/bin/gh" - echo "$HOME/.local/bin" >> "$GITHUB_PATH" - - name: Open or refresh the top-up PR if: steps.merge.outputs.conflict == 'false' env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} BEHIND: ${{ steps.drift.outputs.behind }} + ANCESTRY_STATE: ${{ steps.ancestry.outputs.state }} + CARRIED: ${{ steps.ancestry.outputs.carried }} + SOURCE_PR: ${{ steps.ancestry.outputs.source_pr }} run: | set -euo pipefail - existing="$(gh pr list --repo "$GITHUB_REPOSITORY" --base v6 \ - --head sync/v5-to-v6 --state open --json number --jq '.[0].number // empty')" + # The v5 head this PR carries is the top-up merge's second parent: + # the next run checks it is on v6 (#18931). Recorded on every push, + # so the body always names what the branch currently carries. + carried_now="$(git rev-parse --verify HEAD^2)" + { + printf '%s\n' \ + "Automated v5 → v6 top-up (#7563, Track 7)." \ + "" \ + "Carries ${BEHIND} commit(s) from v5 onto v6." \ + "" \ + "**Merge this PR with a merge commit, never a squash or a rebase.** A squash keeps v5's content but drops its history, and every later top-up then re-conflicts on everything this one carried (#18931)." \ + "" \ + "This runs on every push to v5 (v5 only moves when a v4→v5 top-up or a v5 PR merges), plus a daily safety net, so v6 feature work never ages against a stale base." \ + "" \ + "Opened by .github/workflows/v6-topup.yml. If this merge conflicts the workflow fails instead of opening it — a conflicting forward-merge is a human decision (see #7199)." + if [ "${ANCESTRY_STATE}" = "lost" ]; then + printf '%s\n' \ + "" \ + "The first commit restores v5 ancestry that was lost when #${SOURCE_PR} was squash- or rebase-merged: an ours-merge of ${CARRIED}, which records history and changes no file." \ + "" \ + "DCO: the native queue's prow-DCO path for top-ups (#17030) only covers a PR whose commits are all v5 history plus one merge of v5 onto v6, so this PR falls back to requiring the probot DCO check. Inherited v5 commits whose author and sign-off names differ must be remediated by their own authors under .github/dco.yml; nobody signs off on another author's behalf." + fi + printf '\n' + "$RUNNER_TEMP/v6-topup-ancestry.sh" marker-line "$carried_now" + } > "$RUNNER_TEMP/v6-topup-body.md" + existing="$(gh api "repos/${GITHUB_REPOSITORY}/pulls?state=open&base=v6&head=${GITHUB_REPOSITORY%%/*}:sync/v5-to-v6" \ + --jq '.[0] | select(. != null) | "\(.number) \(.user.id) \(.user.type)"')" if [ -n "$existing" ]; then - echo "PR #${existing} already open for this top-up; the force-push refreshed it." + read -r number author_id author_type <<<"$existing" + if [ "$author_id" = "41898282" ] && [ "$author_type" = "Bot" ]; then + # REST, not `gh pr edit`: the latter reads the PR through a + # GraphQL query that can fail on the repo's classic-project state. + gh api -X PATCH "repos/${GITHUB_REPOSITORY}/pulls/${number}" \ + -f title="🌱 Forward-merge v5 into v6 (${BEHIND} commits)" \ + -F body=@"$RUNNER_TEMP/v6-topup-body.md" >/dev/null + echo "PR #${number} already open for this top-up; the force-push refreshed it and its carried-head marker." + else + echo "::warning::PR #${number} on sync/v5-to-v6 was not opened by this workflow; its body was left alone, so no carried-head marker is recorded." + fi exit 0 fi - printf '%s\n' \ - "Automated v5 → v6 top-up (#7563, Track 7)." \ - "" \ - "Carries ${BEHIND} commit(s) from v5 onto v6." \ - "" \ - "This runs on every push to v5 (v5 only moves when a v4→v5 top-up or a v5 PR merges), plus a daily safety net, so v6 feature work never ages against a stale base." \ - "" \ - "Opened by .github/workflows/v6-topup.yml. If this merge conflicts the workflow fails instead of opening it — a conflicting forward-merge is a human decision (see #7199)." \ - > "$RUNNER_TEMP/v6-topup-body.md" gh pr create --repo "$GITHUB_REPOSITORY" --base v6 --head sync/v5-to-v6 \ --title "🌱 Forward-merge v5 into v6 (${BEHIND} commits)" \ --body-file "$RUNNER_TEMP/v6-topup-body.md" diff --git a/src/docs/native-merge-queue.md b/src/docs/native-merge-queue.md index b4b544effa..ae8c11b5d6 100644 --- a/src/docs/native-merge-queue.md +++ b/src/docs/native-merge-queue.md @@ -302,6 +302,15 @@ metadata, not a fabricated last-member PR event. a `dco` status from any other creator is ignored. Any unmet condition falls back to requiring probot `DCO`. The check output names the DCO path taken and, on fallback, why. +- A forward-merge PR (head `sync/vN-to-vM*` or `topup/*`) must land by merge + commit. On a group, every member head must already be an ancestor of the + synthetic head; a squash or rebase queue never contains the PR's own head, so + for these heads the failure names the merge-method violation (#18931) rather + than the generic "not contained" error. The narrow DCO exception above does + not cover a top-up that carries a history-restoring ours-merge (that commit is + not on `v5`); such a PR needs probot `DCO`, so its flagged inherited commits + are remediated by their own authors (see + [v5-sync-policy.md](v5-sync-policy.md#dco-on-top-up-prs)). - Draft/held PRs, changes requested, dismissed approvals and approvals on an old head fail closed. A `pull_request_target` run on an already closed or merged PR (re-fired by a post-merge label or comment edit) passes as a diff --git a/src/docs/v5-sync-policy.md b/src/docs/v5-sync-policy.md index 0515e9dc4e..78f02c198c 100644 --- a/src/docs/v5-sync-policy.md +++ b/src/docs/v5-sync-policy.md @@ -88,6 +88,79 @@ encoded in the workflow rather than left to the operator: release line must provision this secret or accept that those top-ups need a hand-carried sync. +### Top-up PRs land with a merge commit, never a squash or rebase + +A squash or rebase keeps v5's *content* on v6 but not v5's *history*, so +`git merge-base origin/v5 origin/v6` stays at the previous top-up and every +later forward-merge replays the whole range against v6's already-adapted copies +of the same files. That is what happened after #18689 was squash-merged: every +`v6-topup.yml` run then hit 109 conflicted files (17 of them `add/add`) that no +resolver pattern covers +([#18931](https://github.com/hivecommons/hive/issues/18931)). Three layers +keep it from recurring: + +- **Merge method.** Heads matching `sync/vN-to-vM*` or `topup/*` must be merged + with the merge-commit method. The hive's own merge paths already upgrade a + squash or rebase of such a PR to a merge commit + (`ResolveForwardMergeMethod`). In the native merge queue, a group that does + not contain a forward-merge PR's own head commit (what a squash or rebase + queue produces) fails `native-queue-policy` with a message naming the + violation instead of the generic "not contained" one. A human merging in the + GitHub UI must choose **Create a merge commit**. +- **Ancestry check.** Every PR the workflow opens ends with a marker line, + ``, naming the v5 head it carries (refreshed + on every push to the PR branch). On each run the workflow finds the newest + merged PR on `sync/v5-to-v6`, checks that the marker is genuine (the PR was + opened by the workflow identity, and the sha is the second parent of that + PR's own head commit) and that the sha is an ancestor of `origin/v6` + (`.github/scripts/v6-topup-ancestry.sh`). If it is not, the previous top-up + was squashed or rebased and the run says so in a warning and the job + summary. +- **Self-healing.** When the ancestry was lost and the carried head is still + v5 history, the run starts the top-up branch with + `git merge -s ours --no-ff --signoff ` (history only, no file + changes, signed off by the bot as the author of that merge commit), then + merges `origin/v5` as usual with the autoresolver unchanged. If the carried + head is no longer on v5 (v5 history was rewritten) the job fails fast and + nothing is pushed. If no verified marker exists (PRs from before #18931 and + hand-carried top-ups carry none), nothing can be checked and the merge runs + as before; the conflict report then prints the merge base and the manual + recipe, whose first step is the ancestry check: + + ``` + git merge-base --is-ancestor origin/v6 || echo LOST + git checkout -B sync/v5-to-v6 origin/v6 + git merge -s ours --no-ff --signoff # only when LOST + git merge --no-ff --signoff origin/v5 + ``` + +#### DCO on top-up PRs + +The inherited v5 commits keep the author and sign-off they had on v5. A squash +that GitHub authors as `Andy Anderson ` while the +trailer says `Andrew Anderson ` (or an image-bump +squash signed by `hive-release[bot]` and the noreply identity) fails the +probot `DCO` check, and nothing pushed to the top-up branch can change a v5 +commit. Nobody signs off on another author's behalf, and the gate is not +loosened: + +- **Plain automated top-up** (`sync/v5-to-v6`, opened by the workflow, every + commit v5-reachable except the bot's own merge of v5 onto v6): the narrow + prow-DCO path of the native queue applies + ([#17030](https://github.com/hivecommons/hive/issues/17030) decision B, see + [native-merge-queue.md](native-merge-queue.md)). The bot signs off only its + own merge commit. +- **Healed top-up** (it carries the ours-merge above) and **hand-carried + top-ups**: decision B does not cover them. The ours-merge is a commit that is + not on v5, and B requires every commit but the head to be reachable from v5; + the policy then falls back to requiring probot `DCO`. The owner of each + flagged commit remediates it with a `DCO Remediation Commit` under + `.github/dco.yml` (`allowRemediationCommits.individual: true`, + `thirdParty: false`), as #18929 did with two remediation commits by the + author covering 15 inherited commits. Widening B to accept the bot's + ancestry-only merge commit (its tree equals its first parent's) is a policy + decision for the owner and is not made here. + The automation covers only the mechanical open. Ownership, the verification checklist, the PR body contract, and the review contract below apply to an automatically opened sync PR exactly as they do to a hand-opened one.