diff --git a/.github/DESIGN.md b/.github/DESIGN.md index 65c066f4c9..40138370f7 100644 --- a/.github/DESIGN.md +++ b/.github/DESIGN.md @@ -2,7 +2,7 @@ CI workflows. -**Read this when:** a workflow that installs dependencies fails on one Node line but not another, when changing which npm writes `package-lock.json`, when changing how the Docker image is built, smoke-tested or published, or when `Unit tests: lmdb` hits its cap or a new unit suite seeds or clears rows. +**Read this when:** a workflow that installs dependencies fails on one Node line but not another, when changing which npm writes `package-lock.json`, when changing how the Docker image is built, smoke-tested or published, when `Unit tests: lmdb` hits its cap or a new unit suite seeds or clears rows, or when changing what the release cherry-pick decides to pick. Index of every design note: [DESIGN.md](../DESIGN.md). @@ -27,3 +27,9 @@ Pushes to `main` and release branches run the same staging path, copying into a `test:unit:lmdb` re-runs `unitTests/resources`, `unitTests/apiTests` and `unitTests/bin` on LMDB, where every awaited commit waits on a disk sync. A fixture that seeds or clears one row per transaction costs seconds on the default engine and minutes on LMDB, and a runner with slow fsync multiplies exactly that part. `subscriptionPreviousCountScanBound.test.js` (harper#2826), `subscriptionSuperseded.test.js` (harper#2887) and `calibrationStore.test.js` each pushed the step past its cap this way. Nothing enforces this; the step's `timeout-minutes` is the only detector. Write a fixture's rows in one transaction and await a write only before the step that reads it. Empty a table with `Table.clear()`, or with every delete in one transaction where the table has a full-text index (`clear()` refuses it) or the suite reads the deletes from the audit log (`clear()` writes none). Where a test checks write order (calibration discovery scans decisions by time), keep those writes serial and let the independent ones commit alongside them. Before raising the cap, compare each suite's time in the `lmdb` step with the same suite in `resources (core)`/`resources (indexes)` of the same job, and fix the suite whose LMDB time grew. + +## A release cherry-pick skips a change its branch already has (`workflows/cherry-pick-patch.yml`, `scripts/change-landed.sh`) + +Before picking, the cherry-pick job asks `scripts/change-landed.sh` whether picking the PR onto the release branch could land anything new. The script replays the picked commits onto the release in order with `git merge-tree`, resolving each conflict toward the commit, and the result must be the release's own tree. A conflict then means only that a commit met content a later commit of the PR already wrote. It also merges the PR's net change, `MERGE_BASE..HEAD_SHA` (`MERGE_SHA^1..MERGE_SHA` for a merge-commit merge), into the release, and that must leave the tree unchanged; this covers merge resolutions that no commit carries. Only that answer skips the PR, with no branch and no PR. A change whose file content is partly present, reverted, absorbed by main from another PR, or undone by a later commit of its own picks as before. So does a conflicting change, and so does a check that cannot run. "Unchanged" is decided by Git's merge rules, the same rules the picks themselves run under: a gitlink the release already moved past, or a configured merge driver that keeps the release's side, counts as present for both. Replaying a landed PR one commit at a time re-applies each intermediate commit against its own final content, which conflicts instead of coming out empty. harper#3067 was the second run of a `demilestoned`/`milestoned` pair re-picking harper#3038 onto the v5.3 the first run had just landed it on. + +A hold is never cleared. When the check finds the change landed but a merge was excluded from the picks, the job reports it held, with no branch and no PR: a resolution that took one side outright leaves no diff in the net change, so "unchanged" cannot vouch for it. The fallback that builds from a lone merge commit is not provably the whole change, so it is never checked and keeps its hold. A PR whose merge of main rewrote its own commits' lines fails the replay merge, so after landing it is still re-picked into a conflict. So does a PR where one commit edits a file a later commit deletes and the release already has both: `-X theirs` only resolves content conflicts, not modify/delete, so the replay exits 1 at the edit and the job picks into a conflict PR whose net diff is empty. The check runs after the release branch is checked out, so it reads the same `.gitattributes` as the first pick; it gives up, and the job picks, when any picked commit changes a `.gitattributes` file, since later picks would read that copy. `unitTests/github/cherryPickPatch.test.mjs` runs the shipped job's steps against a fixture origin. The check needs git 2.45 or newer (`merge-tree` on trees); the runner images ship newer, and the suite fails under CI on older git rather than skipping. diff --git a/.github/scripts/change-landed.sh b/.github/scripts/change-landed.sh new file mode 100755 index 0000000000..1ef82050b0 --- /dev/null +++ b/.github/scripts/change-landed.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash +# Usage: change-landed.sh ... +# Exits 0 only when picking the commits onto would land nothing new: +# replayed onto it with each conflict resolved toward the commit, they end at +# 's own tree, and so does the net change ... 1 when they +# would land something, 2 when the check cannot tell; callers pick on either. +set -u + +if [ "$#" -lt 4 ]; then + echo "::warning::change-landed.sh needs a target, base, head and at least one commit, so the commits will be picked" + exit 2 +fi + +TARGET="$1" +BASE="$2" +HEAD="$3" +shift 3 + +could_not_run() { + echo "::warning::could not check whether $TARGET already contains $BASE..$HEAD ($1), so it will be picked" + exit 2 +} + +TARGET_TREE=$(git rev-parse --verify --quiet "$TARGET^{tree}") || could_not_run "$TARGET does not resolve" + +REPLAYED="$TARGET" +for COMMIT in "$@"; do + # A real pick reads the .gitattributes earlier picks wrote; merge-tree reads only the checkout's. + git diff-tree -z --name-only -r "$COMMIT^1" "$COMMIT" | grep -qzE '(^|/)\.gitattributes$' && + could_not_run "$COMMIT changes .gitattributes" + REPLAYED=$(git merge-tree --write-tree -X theirs --merge-base="$COMMIT^1" "$REPLAYED" "$COMMIT") + case $? in + 0) REPLAYED="${REPLAYED%%$'\n'*}" ;; + 1) exit 1 ;; + *) could_not_run "git merge-tree failed replaying $COMMIT" ;; + esac +done +[ "$REPLAYED" = "$TARGET_TREE" ] || exit 1 + +MERGED=$(git merge-tree --write-tree --merge-base="$BASE" "$TARGET" "$HEAD") +case $? in + 0) [ "${MERGED%%$'\n'*}" = "$TARGET_TREE" ] && exit 0 ;; + 1) ;; + *) could_not_run "git merge-tree failed merging the net change" ;; +esac +exit 1 diff --git a/.github/workflows/cherry-pick-patch.yml b/.github/workflows/cherry-pick-patch.yml index c31f3295dc..eb5e14f548 100644 --- a/.github/workflows/cherry-pick-patch.yml +++ b/.github/workflows/cherry-pick-patch.yml @@ -243,6 +243,7 @@ jobs: run: | cp .github/scripts/upsert-sticky-comment.js "$RUNNER_TEMP/upsert-sticky-comment.js" cp .github/scripts/apply-picks.sh "$RUNNER_TEMP/apply-picks.sh" + cp .github/scripts/change-landed.sh "$RUNNER_TEMP/change-landed.sh" - name: Configure git run: | @@ -311,9 +312,13 @@ jobs: # The PR was merged with a merge commit: it IS the change. PICK_FLAGS="-m 1" PICK_SHAS="$MERGE_SHA" + CHANGE_BASE="${MERGE_SHA}^1" + CHANGE_HEAD="$MERGE_SHA" else PICK_FLAGS="" PICK_SHAS=$(git rev-list --reverse --no-merges "${MERGE_BASE}..${HEAD_SHA}" | tr '\n' ' ') + CHANGE_BASE="$MERGE_BASE" + CHANGE_HEAD="$HEAD_SHA" # Only merges a human DECIDED something in need holding, so a # routine "merge main in, no conflicts" PR still lands # automatically. The test is whether the recorded merge matches what @@ -332,9 +337,11 @@ jobs: if [ "$IS_MERGED" = "true" ]; then # Nothing replayable: the PR's own content exists only in # commits that cannot be replayed. The merge SHA gives a human - # something to review, but it is not provably the whole change. + # something to review, but it is not provably the whole change, + # so it cannot prove the whole change landed either. PICK_SHAS="$MERGE_SHA" HOLD="the PR has no replayable non-merge commits, so this branch was built from the merge commit alone" + CHANGE_HEAD="" elif [ -n "$EXCLUDED_MERGES" ]; then echo "pushed=false" >> "$GITHUB_OUTPUT" # Nothing replayable and a merge that resolved something: the @@ -350,26 +357,48 @@ jobs: fi fi fi - echo "pick_flags=$PICK_FLAGS" >> "$GITHUB_OUTPUT" - echo "pick_shas=$PICK_SHAS" >> "$GITHUB_OUTPUT" + if [ -n "$EXCLUDED_MERGES" ]; then HOLD=$(printf 'merge commit(s) `%s` resolved content of their own and cannot be replayed' "$EXCLUDED_MERGES") fi - echo "excluded_merges=$EXCLUDED_MERGES" >> "$GITHUB_OUTPUT" - echo "hold=$HOLD" >> "$GITHUB_OUTPUT" # ── Create / reset cherry-pick branch off release branch ──────── git checkout -B "$BRANCH" "origin/$RELEASE_BRANCH" + # ── Skip a change the release branch already has ──────────────── + # + # Replaying a landed change commit by commit conflicts: each + # intermediate commit meets its own final content. A hold still + # stands: a resolution that took one side outright leaves nothing in + # the net change for the check to find. + # shellcheck disable=SC2086 + if [ -n "$CHANGE_HEAD" ] && "$RUNNER_TEMP/change-landed.sh" "origin/$RELEASE_BRANCH" "$CHANGE_BASE" "$CHANGE_HEAD" $PICK_SHAS; then + echo "The change from PR #$PR_NUMBER is already on $RELEASE_BRANCH — nothing to pick" + if [ -n "$HOLD" ]; then + echo "hold=its change appears to be on it already, but $HOLD" >> "$GITHUB_OUTPUT" + fi + echo "no_op=true" >> "$GITHUB_OUTPUT" + exit 0 + fi + + echo "pick_flags=$PICK_FLAGS" >> "$GITHUB_OUTPUT" + echo "pick_shas=$PICK_SHAS" >> "$GITHUB_OUTPUT" + echo "excluded_merges=$EXCLUDED_MERGES" >> "$GITHUB_OUTPUT" + echo "hold=$HOLD" >> "$GITHUB_OUTPUT" + # ── Apply commits, tracking which conflicted ──────────────────── CONFLICTS=$("$RUNNER_TEMP/apply-picks.sh" "$PICK_FLAGS" "$PICK_SHAS") echo "conflicts=$CONFLICTS" >> "$GITHUB_OUTPUT" - if [ -z "$CONFLICTS" ] && [ -z "$HOLD" ] && [ -z "$(git rev-list "origin/${RELEASE_BRANCH}..HEAD")" ]; then - echo "Nothing to land on $RELEASE_BRANCH — every commit was already present" - echo "no_op=true" >> "$GITHUB_OUTPUT" - # An earlier run may have pushed a branch that is now redundant. + if [ -z "$CONFLICTS" ] && [ -z "$(git rev-list "origin/${RELEASE_BRANCH}..HEAD")" ]; then + # Nothing landed on this branch, so there is nothing to push a + # human could review either — a hold does not change that. An + # earlier run may have pushed a branch that is now redundant. git push origin --delete "$BRANCH" 2>/dev/null || true + if [ -z "$HOLD" ]; then + echo "Nothing to land on $RELEASE_BRANCH — every commit was already present" + echo "no_op=true" >> "$GITHUB_OUTPUT" + fi exit 0 fi @@ -392,10 +421,10 @@ jobs: --base "$RELEASE_BRANCH" --head "$BRANCH" --json number --jq '.[0].number' 2>/dev/null || true) if [ -n "$STALE_PR" ] && [ "$STALE_PR" != "null" ]; then gh pr close "$STALE_PR" --repo "$GITHUB_REPOSITORY" \ - --comment "Superseded: PR #${PR_NUMBER} no longer produces any commits to cherry-pick onto \`${RELEASE_BRANCH}\`." >/dev/null || true + --comment "Superseded: PR #${PR_NUMBER} has nothing left to cherry-pick onto \`${RELEASE_BRANCH}\`." >/dev/null || true fi git push origin --delete "$BRANCH" 2>/dev/null || true - BODY=$(printf '%s\n## Release cherry-pick `%s`: already present\n\nEvery commit from this PR is already on `%s` — nothing to cherry-pick.\n' \ + BODY=$(printf '%s\n## Release cherry-pick `%s`: already present\n\nThe change from this PR is already on `%s` — nothing to cherry-pick.\n' \ "$STICKY_MARKER" "$RELEASE_BRANCH" "$RELEASE_BRANCH") node "$RUNNER_TEMP/upsert-sticky-comment.js" "$PR_NUMBER" "$STICKY_MARKER" "$BODY" @@ -487,10 +516,10 @@ jobs: --base "$RELEASE_BRANCH" --head "$BRANCH" --json number --jq '.[0].number' 2>/dev/null || true) if [ -n "$STALE_PR" ] && [ "$STALE_PR" != "null" ]; then gh pr close "$STALE_PR" --repo "$GITHUB_REPOSITORY" \ - --comment "Superseded: PR #${PR_NUMBER} no longer produces any commits to cherry-pick onto \`${RELEASE_BRANCH}\`." >/dev/null || true + --comment "Superseded: PR #${PR_NUMBER} has nothing left to cherry-pick onto \`${RELEASE_BRANCH}\`." >/dev/null || true fi git push origin --delete "$BRANCH" 2>/dev/null || true - BODY=$(printf '%s\n## Release cherry-pick `%s`: held for review\n\nNothing could be cherry-picked onto `%s`: %s.\n\n`%s` was **not** updated. Apply this change by hand if it belongs on that line.\n' \ + BODY=$(printf '%s\n## Release cherry-pick `%s`: held for review\n\nNothing could be cherry-picked onto `%s`: %s.\n\n`%s` was **not** updated. Check it, and apply this change by hand if any of it is missing and belongs on that line.\n' \ "$STICKY_MARKER" "$RELEASE_BRANCH" "$RELEASE_BRANCH" "$HOLD" "$RELEASE_BRANCH") node "$RUNNER_TEMP/upsert-sticky-comment.js" "$PR_NUMBER" "$STICKY_MARKER" "$BODY" exit 0 diff --git a/DESIGN.md b/DESIGN.md index bee0ab75ce..388e7f39e5 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -15,6 +15,7 @@ Index of the design notes for the harper core: one line per note, grouped by the - [The lock file must pass `npm ci` under npm 10 and npm 11 (`workflows/lockfile-npm-compat.yml`)](.github/DESIGN.md#the-lock-file-must-pass-npm-ci-under-npm-10-and-npm-11-workflowslockfile-npm-compatyml) — npm 11 writes the lock but accepts some that npm 10's `npm ci` rejects; fix the conflict at its source, never by regenerating with npm 10. - [A release copies to Docker Hub only the image it booted (`workflows/docker-smoke.yml`, `workflows/publish-docker.yaml`)](.github/DESIGN.md#a-release-copies-to-docker-hub-only-the-image-it-booted-workflowsdocker-smokeyml-workflowspublish-dockeryaml) — The release builds once into a runner-local registry, smoke-tests that digest and copies the same index to Docker Hub; never a rebuild. - [Unit-test fixtures don't spend `Unit tests: lmdb` on one durable commit per row (`workflows/unit-test.yml`)](.github/DESIGN.md#unit-test-fixtures-dont-spend-unit-tests-lmdb-on-one-durable-commit-per-row-workflowsunit-testyml) — Every resources suite re-runs on LMDB, where each awaited commit syncs; fixtures batch their writes, and the step cap is the only detector. +- [A release cherry-pick skips a change its branch already has (`workflows/cherry-pick-patch.yml`, `scripts/change-landed.sh`)](.github/DESIGN.md#a-release-cherry-pick-skips-a-change-its-branch-already-has-workflowscherry-pick-patchyml-scriptschange-landedsh) — skip only when replaying the PR's commits onto the release, conflicts resolved toward each commit, ends at the release's own tree; replaying a landed PR commit by commit conflicts instead. ## resources/ — records, transactions, tables, blobs, audit diff --git a/unitTests/github/cherryPickPatch.test.mjs b/unitTests/github/cherryPickPatch.test.mjs new file mode 100644 index 0000000000..443bbded35 --- /dev/null +++ b/unitTests/github/cherryPickPatch.test.mjs @@ -0,0 +1,661 @@ +import assert from 'node:assert'; +import { execFileSync, spawnSync } from 'node:child_process'; +import { chmodSync, cpSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { tmpdir } from 'node:os'; +import { dirname, join, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { parse } from 'yaml'; + +const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); +const job = parse(readFileSync(join(root, '.github/workflows/cherry-pick-patch.yml'), 'utf8')).jobs['cherry-pick']; +const changeLanded = join(root, '.github/scripts/change-landed.sh'); +const RELEASE = 'v5.3'; +const PR_NUMBER = '7'; +const DEADLINE = 60_000; +function gitHasTreeMerge() { + const { stdout = '' } = spawnSync('git', ['--version'], { encoding: 'utf8' }); + const [, major, minor] = /(\d+)\.(\d+)/.exec(stdout) ?? []; + return Number(major) > 2 || (Number(major) === 2 && Number(minor) >= 45); +} +// Skips where a prerequisite is missing; under CI a skip would hide the guard going untested. +function requirePrerequisites({ jq = false } = {}) { + return function () { + const missing = []; + if (!gitHasTreeMerge()) missing.push('git 2.45 or newer'); + if (jq && spawnSync('jq', ['--version']).status !== 0) missing.push('jq'); + if (missing.length === 0) return; + if (process.env.CI) throw new Error(`cherry-pick tests need ${missing.join(' and ')}`); + this.skip(); + }; +} +// The second commit rewrites the line the first one changed, so replaying the first onto a release +// that already has both conflicts instead of applying empty. +const FIRST_FIX = { 5: 'first fix' }; +const SECOND_FIX = { 5: 'second fix', 6: 'second fix, continued' }; + +describe('cherry-pick-patch.yml', function () { + let fixture; + + before(requirePrerequisites({ jq: true })); + + beforeEach(function () { + fixture = createFixture(); + }); + + afterEach(function () { + if (fixture) rmSync(fixture.dir, { recursive: true, force: true }); + fixture = undefined; + }); + + function incidentPr() { + fixture.prCommit(FIRST_FIX, 'First fix'); + fixture.prCommit(SECOND_FIX, 'Second fix'); + return fixture.squashMerge(); + } + + it('lands a squash-merged change the release branch lacks', function () { + incidentPr(); + const run = fixture.runJob(); + assert.ok(run.ran.includes('Merge into release branch'), run.log); + assert.strictEqual(fixture.releaseFile(), fixture.lines(SECOND_FIX), run.log); + assert.deepStrictEqual(run.prCreates, []); + assert.deepStrictEqual(fixture.cherryPickBranches(), []); + }); + + it('makes a second run for a landed change a no-op, and closes the conflict PR an earlier run left', function () { + // One milestone change fires `demilestoned` and `milestoned`: the first run lands the PR's + // commits and the second replays them onto the release tip the first one advanced. + incidentPr(); + fixture.runJob(); + const landedTip = fixture.releaseTip(); + const run = fixture.runJob({ openPr: '3067' }); + assert.strictEqual(run.outputs.no_op, 'true', run.log); + assert.match(run.log, /already on v5\.3 — nothing to pick/); + assert.deepStrictEqual(run.prCreates, [], run.log); + assert.ok(run.gh.some(([command, sub, number]) => command === 'pr' && sub === 'close' && number === '3067')); + assert.deepStrictEqual(fixture.cherryPickBranches(), []); + assert.strictEqual(fixture.releaseTip(), landedTip); + assert.match(run.stickies.at(-1), /already present/); + }); + + it('skips a change that reached the release branch as one squashed commit', function () { + const squash = incidentPr(); + const backport = fixture.onRelease((git) => git('cherry-pick', squash)); + const run = fixture.runJob(); + assert.strictEqual(run.outputs.no_op, 'true', run.log); + assert.deepStrictEqual(run.prCreates, []); + assert.deepStrictEqual(fixture.cherryPickBranches(), []); + assert.strictEqual(fixture.releaseTip(), backport); + }); + + it('picks the rest of a change the release branch has only part of', function () { + incidentPr(); + fixture.onRelease((git) => git('cherry-pick', fixture.prCommits[0])); + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.no_op, 'true', run.log); + assert.strictEqual(run.outputs.conflicts, '', run.log); + assert.strictEqual(fixture.releaseFile(), fixture.lines(SECOND_FIX), run.log); + }); + + it('picks again a change that landed and was then reverted', function () { + incidentPr(); + fixture.runJob(); + fixture.onRelease((git) => git('revert', '--no-edit', 'HEAD', 'HEAD~1')); + assert.strictEqual(fixture.releaseFile(), fixture.lines()); + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.no_op, 'true', run.log); + assert.strictEqual(fixture.releaseFile(), fixture.lines(SECOND_FIX), run.log); + }); + + it('lands a change merged with a merge commit, whose PR head is already on main, then skips it', function () { + fixture.prCommit(FIRST_FIX, 'First fix'); + fixture.prCommit(SECOND_FIX, 'Second fix'); + fixture.mergeCommit(); + const run = fixture.runJob(); + assert.strictEqual(run.outputs.pick_flags, '-m 1', run.log); + assert.strictEqual(fixture.releaseFile(), fixture.lines(SECOND_FIX), run.log); + const landedTip = fixture.releaseTip(); + const rerun = fixture.runJob(); + // The lone `-m 1` pick would also come out empty; the skip must come from the containment check. + assert.match(rerun.log, /already on v5\.3 — nothing to pick/); + assert.strictEqual(rerun.outputs.pick_flags, undefined, rerun.log); + assert.strictEqual(fixture.releaseTip(), landedTip); + }); + + it('still opens the conflict PR when the release branch changed the same lines differently', function () { + incidentPr(); + fixture.onRelease((git) => { + fixture.writeLib(fixture.lines({ 5: 'release-only edit' })); + git('commit', '-qam', 'Release-only edit'); + }); + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.conflicts, '', run.log); + assert.ok(run.ran.includes('Report conflict'), run.log); + assert.strictEqual(run.prCreates.length, 1, run.log); + assert.deepStrictEqual(fixture.cherryPickBranches(), [`cherry-pick/${RELEASE}/pr-${PR_NUMBER}`]); + const branchFile = fixture.git(fixture.origin, 'show', `cherry-pick/${RELEASE}/pr-${PR_NUMBER}:lib.txt`); + assert.match(branchFile, /^<<<<<<< /m); + }); + + it('picks a landed change, as before, when the containment check cannot run', function () { + incidentPr(); + fixture.runJob(); + const run = fixture.runJob({ + afterStash(temp) { + const stub = join(temp, 'change-landed.sh'); + writeFileSync(stub, '#!/usr/bin/env bash\necho "::warning::check failed"\nexit 2\n'); + chmodSync(stub, 0o755); + }, + }); + assert.match(run.log, /::warning::check failed/); + assert.notStrictEqual(run.outputs.no_op, 'true', run.log); + assert.notStrictEqual(run.outputs.conflicts, '', run.log); + }); + + it('picks a commit whose content main absorbed from another PR before this one merged main', function () { + // After the merge of main, the net change since the merge base is only the second commit, and + // the release already has that much. + const PR_ONLY = { 20: 'pr-only change' }; + fixture.prCommit(FIRST_FIX, 'First fix'); + fixture.prCommit(PR_ONLY, 'PR-only change'); + fixture.mainCommit(FIRST_FIX, 'The same fix, landed on main by another PR'); + fixture.mergeMainIntoPr(); + fixture.squashMerge(); + fixture.onRelease((git) => { + fixture.writeLib(fixture.lines(PR_ONLY)); + git('commit', '-qam', 'Backport the PR-only change'); + }); + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.no_op, 'true', run.log); + assert.strictEqual(fixture.releaseFile(), fixture.lines({ ...FIRST_FIX, ...PR_ONLY }), run.log); + }); + + it('holds a lone fallback merge commit even when the release branch has that commit', function () { + // The PR head reached main directly, so the API's merge SHA is just its last commit. The + // release has that commit but not the one before it; only a human can tell it is incomplete. + fixture.prCommit({ 10: 'first file fix' }, 'First fix'); + fixture.prCommit({ 20: 'second file fix' }, 'Second fix'); + fixture.fastForward(); + fixture.onRelease((git) => { + fixture.writeLib(fixture.lines({ 20: 'second file fix' })); + git('commit', '-qam', 'Backport the second fix only'); + }); + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.no_op, 'true', run.log); + assert.ok(run.ran.includes('Report held for review'), run.log); + // The lone fallback commit cherry-picks as a no-op, so this branch gets no new commits. + assert.deepStrictEqual(run.prCreates, []); + assert.deepStrictEqual(fixture.cherryPickBranches(), []); + assert.match(run.stickies.at(-1), /held for review/); + }); + + it('picks a later commit that undoes an earlier one the release branch already has', function () { + // The net change never mentions line 10, but the release holds the first commit's edit to it. + const FINAL = { 20: 'final' }; + fixture.prCommit({ 10: 'temporary' }, 'Temporary change'); + fixture.prCommit({ 10: 'line 10', ...FINAL }, 'Undo it and finish'); + fixture.squashMerge(); + fixture.onRelease((git) => { + fixture.writeLib(fixture.lines({ 10: 'temporary', ...FINAL })); + git('commit', '-qam', 'Backport the first commit and the final line'); + }); + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.no_op, 'true', run.log); + assert.strictEqual(fixture.releaseFile(), fixture.lines(FINAL), run.log); + }); + + it('still opens the conflict PR when the release has its own version of a commit the PR later undoes', function () { + const FINAL = { 20: 'final' }; + fixture.prCommit({ 10: 'temporary' }, 'Temporary change'); + fixture.prCommit({ 10: 'line 10', ...FINAL }, 'Undo it and finish'); + fixture.squashMerge(); + fixture.onRelease((git) => { + fixture.writeLib(fixture.lines({ 10: 'temporary, adapted', ...FINAL })); + git('commit', '-qam', 'Backport an adapted first commit and the final line'); + }); + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.no_op, 'true', run.log); + assert.notStrictEqual(run.outputs.conflicts, '', run.log); + assert.strictEqual(run.prCreates.length, 1, run.log); + }); + + it('stays held when its merge of main discarded its own edit, even if the release has that edit', function () { + const PR_ONLY = { 20: 'pr-only change' }; + fixture.prCommit({ 5: 'pr side', ...PR_ONLY }, 'PR change'); + fixture.mainCommit({ 5: 'main side' }, 'Main change to the same line'); + fixture.mergeMainIntoPr(); + fixture.squashMerge(); + fixture.onRelease((git) => git('cherry-pick', fixture.prCommits[0])); + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.hold ?? '', '', run.log); + assert.ok(run.ran.includes('Report held for review') && !run.ran.includes('Report no-op'), run.log); + assert.deepStrictEqual(run.prCreates, []); + }); + + describe('a PR whose merge of main added content of its own', function () { + const IN_MERGE = { 12: 'added in the merge' }; + + beforeEach(function () { + fixture.prCommit(FIRST_FIX, 'First fix'); + fixture.mainCommit({ 30: 'main edit' }, 'Main edit'); + fixture.mergeMainIntoPr(IN_MERGE); + fixture.squashMerge(); + }); + + it('stays held, with no branch or PR, when its change is already on the release branch', function () { + // A merge that took one side outright would leave nothing in the net change to check, so + // "landed" cannot clear the hold. + fixture.onRelease((git) => { + fixture.writeLib(fixture.lines({ ...FIRST_FIX, ...IN_MERGE })); + git('commit', '-qam', 'Backport the whole change'); + }); + const run = fixture.runJob(); + assert.strictEqual(run.outputs.no_op, 'true', run.log); + assert.match(run.outputs.hold, /^its change appears to be on it already, but merge commit/); + assert.ok(run.ran.includes('Report held for review') && !run.ran.includes('Report no-op'), run.log); + assert.deepStrictEqual(run.prCreates, []); + assert.deepStrictEqual(fixture.cherryPickBranches(), []); + assert.match(run.stickies.at(-1), /held for review/); + }); + + it('is held for review when the release branch lacks it', function () { + const run = fixture.runJob(); + assert.notStrictEqual(run.outputs.no_op, 'true', run.log); + assert.ok(run.ran.includes('Report held for review'), run.log); + assert.strictEqual(run.prCreates.length, 1, run.log); + }); + }); +}); + +describe('change-landed.sh', function () { + let dir; + let env; + const git = (...args) => execFileSync('git', args, { cwd: dir, env, encoding: 'utf8', timeout: DEADLINE }).trim(); + const landed = (target, base, head, commits = [head]) => + spawnSync('bash', [changeLanded, target, base, head, ...commits], { + cwd: dir, + env, + encoding: 'utf8', + timeout: DEADLINE, + }); + + before(requirePrerequisites()); + + beforeEach(function () { + dir = mkdtempSync(join(tmpdir(), 'change-landed-')); + env = isolatedGitEnv(dir); + git('init', '-q'); + git('config', 'user.name', 'Author'); + git('config', 'user.email', 'author@example.com'); + writeFileSync(join(dir, 'text.txt'), 'text\n'); + writeFileSync(join(dir, 'blob.bin'), Buffer.from([0, 1, 2, 3, 0, 255])); + writeFileSync(join(dir, 'run.sh'), 'echo run\n'); + writeFileSync(join(dir, 'gone.txt'), 'to be deleted\n'); + writeFileSync(join(dir, 'moved.txt'), Array.from({ length: 20 }, (_, index) => `moved ${index}\n`).join('')); + git('add', '.'); + git('commit', '-qm', 'Base'); + }); + + afterEach(function () { + if (dir) rmSync(dir, { recursive: true, force: true }); + dir = undefined; + }); + + const changes = { + 'an executable bit': () => { + chmodSync(join(dir, 'run.sh'), 0o755); + git('add', 'run.sh'); + }, + 'binary content': () => { + writeFileSync(join(dir, 'blob.bin'), Buffer.from([0, 9, 9, 9, 0, 255])); + git('add', 'blob.bin'); + }, + 'a deletion': () => git('rm', '-q', 'gone.txt'), + 'a rename': () => git('mv', 'moved.txt', 'renamed.txt'), + 'a new file': () => { + writeFileSync(join(dir, 'new.txt'), 'new\n'); + git('add', 'new.txt'); + }, + }; + + for (const [kind, change] of Object.entries(changes)) { + it(`tells ${kind} the target has from one it lacks`, function () { + const base = git('rev-parse', 'HEAD'); + change(); + git('commit', '-qm', kind); + const head = git('rev-parse', 'HEAD'); + git('checkout', '-qb', 'target', base); + writeFileSync(join(dir, 'text.txt'), 'an unrelated release edit\n'); + git('commit', '-qam', 'Unrelated release edit'); + const without = landed('target', base, head); + assert.strictEqual(without.status, 1, without.stdout + without.stderr); + git('cherry-pick', head); + const withChange = landed('target', base, head); + assert.strictEqual(withChange.status, 0, withChange.stdout + withChange.stderr); + }); + } + + it('does not take an empty net change as proof that its commits landed', function () { + const base = git('rev-parse', 'HEAD'); + changes['a new file'](); + git('commit', '-qm', 'New file'); + const commit = git('rev-parse', 'HEAD'); + git('checkout', '-qb', 'target', base); + const without = landed('target', commit, commit, [commit]); + assert.strictEqual(without.status, 1, without.stdout + without.stderr); + git('cherry-pick', commit); + const withCommit = landed('target', commit, commit, [commit]); + assert.strictEqual(withCommit.status, 0, withCommit.stdout + withCommit.stderr); + }); + + for (const path of ['.gitattributes', 'café/.gitattributes']) { + it(`cannot tell when a picked commit changes ${path}`, function () { + const base = git('rev-parse', 'HEAD'); + mkdirSync(dirname(join(dir, path)), { recursive: true }); + writeFileSync(join(dir, path), '*.txt merge=union\n'); + git('add', path); + git('commit', '-qm', 'Merge attributes'); + const commit = git('rev-parse', 'HEAD'); + git('checkout', '-qb', 'target', base); + git('cherry-pick', commit); + const result = landed('target', base, commit); + assert.strictEqual(result.status, 2, result.stdout + result.stderr); + assert.match(result.stdout, /^::warning::.*changes \.gitattributes/m); + }); + } + + it('reports a check that cannot run with a warning and exit 2', function () { + const result = landed('refs/heads/no-such-branch', 'HEAD', 'HEAD'); + assert.strictEqual(result.status, 2, result.stderr); + assert.match(result.stdout, /^::warning::/m); + }); + + it('exits 1 on an edit-then-delete pair even once the target has both', function () { + const base = git('rev-parse', 'HEAD'); + writeFileSync(join(dir, 'gone.txt'), 'edited\n'); + git('commit', '-qam', 'Edit'); + const edit = git('rev-parse', 'HEAD'); + git('rm', '-q', 'gone.txt'); + git('commit', '-qm', 'Delete'); + const head = git('rev-parse', 'HEAD'); + git('checkout', '-qb', 'target', base); + git('cherry-pick', edit, head); + const result = landed('target', base, head, [edit, head]); + assert.strictEqual(result.status, 1, result.stdout + result.stderr); + }); +}); + +function isolatedGitEnv(dir) { + const gitconfig = join(dir, '.gitconfig-test'); + writeFileSync( + gitconfig, + '[init]\n\tdefaultBranch = main\n[commit]\n\tgpgsign = false\n[advice]\n\tdetachedHead = false\n' + ); + return { + ...Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('GIT_'))), + GIT_CONFIG_GLOBAL: gitconfig, + GIT_CONFIG_NOSYSTEM: '1', + }; +} + +function createFixture() { + const dir = mkdtempSync(join(tmpdir(), 'cherry-pick-patch-')); + const bin = join(dir, 'bin'); + mkdirSync(bin); + const env = isolatedGitEnv(dir); + env.PATH = `${bin}:${process.env.PATH}`; + writeStub( + join(bin, 'gh'), + `const { appendFileSync } = require('node:fs'); +const { execFileSync } = require('node:child_process'); +const args = process.argv.slice(2); +appendFileSync(process.env.GH_LOG, JSON.stringify(args) + '\\n'); +const [command, sub] = args; +if (command === 'api' && /^repos\\/[^/]+\\/[^/]+\\/pulls\\/\\d+$/.test(sub) && args[2] === '--jq') { + process.stdout.write(execFileSync('jq', ['-c', args[3], process.env.GH_PR_JSON])); +} else if (command === 'pr' && sub === 'list') { + process.stdout.write(process.env.GH_OPEN_PR ?? ''); +} else if (command === 'pr' && sub === 'create') { + process.stdout.write('https://github.com/test/repo/pull/100\\n'); +} else if (!(command === 'pr' && ['edit', 'close'].includes(sub))) { + process.stderr.write('unexpected gh call: ' + args.join(' ') + '\\n'); + process.exit(1); +} +` + ); + const stickyStub = join(dir, 'upsert-sticky-comment.js'); + writeFileSync( + stickyStub, + `require('node:fs').appendFileSync(process.env.STICKY_LOG, JSON.stringify(process.argv[4]) + '\\n');\n` + ); + + const git = (cwd, ...args) => execFileSync('git', args, { cwd, env, encoding: 'utf8', timeout: DEADLINE }).trim(); + const origin = join(dir, 'origin.git'); + const seed = join(dir, 'seed'); + git(dir, 'init', '-q', '--bare', origin); + git(dir, 'init', '-q', seed); + git(seed, 'config', 'user.name', 'Author'); + git(seed, 'config', 'user.email', 'author@example.com'); + git(seed, 'remote', 'add', 'origin', origin); + const seedGit = (...args) => git(seed, ...args); + + const lines = (overrides = {}) => + Array.from({ length: 40 }, (_, index) => overrides[index + 1] ?? `line ${index + 1}`).join('\n') + '\n'; + const writeLib = (content) => writeFileSync(join(seed, 'lib.txt'), content); + const commitLib = (overrides, message) => { + writeLib(lines(overrides)); + seedGit('commit', '-qam', message); + return seedGit('rev-parse', 'HEAD'); + }; + + writeLib(lines()); + seedGit('add', 'lib.txt'); + seedGit('commit', '-qm', 'Base'); + seedGit('branch', RELEASE); + let mainState = { 35: 'main-only change' }; + commitLib(mainState, 'Main-only change'); + seedGit('branch', 'feature'); + let featureState = { ...mainState }; + const prCommits = []; + + let prJson; + const publish = (mergeSha) => { + seedGit('push', '-q', '--force', 'origin', 'main', RELEASE, `feature:refs/pull/${PR_NUMBER}/head`); + prJson = join(dir, 'pr.json'); + writeFileSync( + prJson, + JSON.stringify({ + title: 'Fix the thing', + head: { sha: seedGit('rev-parse', 'feature') }, + base: { sha: seedGit('merge-base', 'main', 'feature') }, + merge_commit_sha: mergeSha, + merged: true, + state: 'closed', + }) + ); + return mergeSha; + }; + const onBranch = (branch, change) => { + seedGit('checkout', '-q', branch); + const result = change(); + seedGit('checkout', '-q', 'main'); + return result; + }; + + return { + dir, + origin, + prCommits, + lines, + writeLib, + git, + prCommit(overrides, message) { + featureState = { ...featureState, ...overrides }; + prCommits.push(onBranch('feature', () => commitLib(featureState, message))); + }, + mainCommit(overrides, message) { + mainState = { ...mainState, ...overrides }; + commitLib(mainState, message); + }, + mergeMainIntoPr(extra = {}) { + featureState = { ...featureState, ...mainState, ...extra }; + onBranch('feature', () => { + spawnSync('git', ['merge', '-q', '--no-commit', '--no-ff', 'main'], { cwd: seed, env, timeout: DEADLINE }); + writeLib(lines(featureState)); + seedGit('commit', '-qam', 'Merge main'); + }); + }, + squashMerge() { + seedGit('merge', '-q', '--squash', 'feature'); + seedGit('commit', '-qm', `Fix the thing (#${PR_NUMBER})`); + return publish(seedGit('rev-parse', 'HEAD')); + }, + mergeCommit() { + seedGit('merge', '-q', '--no-ff', '-m', `Merge pull request #${PR_NUMBER}`, 'feature'); + return publish(seedGit('rev-parse', 'HEAD')); + }, + fastForward() { + seedGit('merge', '-q', '--ff-only', 'feature'); + return publish(seedGit('rev-parse', 'HEAD')); + }, + onRelease(change) { + seedGit('fetch', '-q', 'origin', `+${RELEASE}:${RELEASE}`); + return onBranch(RELEASE, () => { + change(seedGit); + seedGit('push', '-q', 'origin', RELEASE); + return seedGit('rev-parse', 'HEAD'); + }); + }, + releaseFile: () => git(origin, 'show', `${RELEASE}:lib.txt`) + '\n', + releaseTip: () => git(origin, 'rev-parse', RELEASE), + cherryPickBranches: () => + git(origin, 'for-each-ref', '--format=%(refname:short)', 'refs/heads/cherry-pick/').split('\n').filter(Boolean), + runJob(options = {}) { + return runJob({ dir, env, origin, prJson, stickyStub, git, ...options }); + }, + }; +} + +function runJob({ dir, env, origin, prJson, stickyStub, git, openPr, afterStash }) { + const run = mkdtempSync(join(dir, 'run-')); + const work = join(run, 'work'); + const temp = join(run, 'temp'); + mkdirSync(temp); + git(run, 'clone', '-q', origin, work); + const ghLog = join(run, 'gh.log'); + const stickyLog = join(run, 'sticky.log'); + const outputs = {}; + const ran = []; + let log = ''; + let failed = false; + let first = true; + for (const [index, step] of job.steps.entries()) { + if (step.uses || !stepCondition(step.if, outputs, failed)) continue; + // The real checkout is of main, which tracks .github/scripts; the fixture's main does not. + if (first) cpSync(join(root, '.github/scripts'), join(work, '.github/scripts'), { recursive: true }); + const outputFile = join(run, `output-${index}`); + writeFileSync(outputFile, ''); + const stepEnv = { + ...env, + GITHUB_OUTPUT: outputFile, + GITHUB_REPOSITORY: 'test/repo', + GITHUB_SERVER_URL: 'https://github.com', + GITHUB_RUN_ID: '1', + RUNNER_TEMP: temp, + PR_NUMBER, + GH_LOG: ghLog, + GH_PR_JSON: prJson, + STICKY_LOG: stickyLog, + }; + if (openPr) stepEnv.GH_OPEN_PR = openPr; + for (const [key, value] of Object.entries({ ...job.env, ...step.env })) { + stepEnv[key] = expand(String(value), outputs); + } + const script = join(run, `step-${index}.sh`); + writeFileSync(script, expand(step.run, outputs)); + const result = spawnSync('bash', ['--noprofile', '--norc', '-eo', 'pipefail', script], { + cwd: work, + env: stepEnv, + encoding: 'utf8', + timeout: DEADLINE, + }); + ran.push(step.name); + log += `\n── ${step.name} (exit ${result.status})\n${result.stdout}${result.stderr}`; + if (result.status !== 0) failed = true; + if (step.id) Object.assign(outputs, readOutputs(outputFile)); + if (first) { + rmSync(join(work, '.github'), { recursive: true, force: true }); + cpSync(stickyStub, join(temp, 'upsert-sticky-comment.js')); + afterStash?.(temp); + first = false; + } + } + assert.ok(!failed, log); + const readLog = (file) => + existsSync(file) + ? readFileSync(file, 'utf8') + .split('\n') + .filter(Boolean) + .map((line) => JSON.parse(line)) + : []; + const gh = readLog(ghLog); + return { + ran, + outputs, + log, + gh, + prCreates: gh.filter(([command, sub]) => command === 'pr' && sub === 'create'), + stickies: readLog(stickyLog), + }; +} + +function expand(text, outputs) { + return text.replace(/\$\{\{\s*(.+?)\s*\}\}/g, (_, expression) => { + const output = /^steps\.pick\.outputs\.(\w+)$/.exec(expression); + if (output) return outputs[output[1]] ?? ''; + if (expression === 'matrix.release') return RELEASE; + if (expression === 'secrets.GITHUB_TOKEN') return 'test-token'; + throw new Error(`unsupported expression: ${expression}`); + }); +} + +function stepCondition(condition, outputs, failed) { + if (condition === undefined) return !failed; + let checksStatus = false; + let result = true; + for (const term of condition.split('&&').map((part) => part.trim())) { + if (term === 'failure()') { + checksStatus = true; + result &&= failed; + continue; + } + const comparison = /^steps\.pick\.outputs\.(\w+) (==|!=) '([^']*)'$/.exec(term); + if (!comparison) throw new Error(`unsupported if: ${condition}`); + const value = outputs[comparison[1]] ?? ''; + result &&= comparison[2] === '==' ? value === comparison[3] : value !== comparison[3]; + } + return checksStatus ? result : result && !failed; +} + +function readOutputs(file) { + const outputs = {}; + const lines = readFileSync(file, 'utf8').split('\n'); + for (let index = 0; index < lines.length; index++) { + const heredoc = /^(\w+)<<(.+)$/.exec(lines[index]); + if (heredoc) { + const end = lines.indexOf(heredoc[2], index + 1); + if (end < 0) throw new Error(`output ${heredoc[1]} has no closing ${heredoc[2]}`); + outputs[heredoc[1]] = lines.slice(index + 1, end).join('\n'); + index = end; + continue; + } + const separator = lines[index].indexOf('='); + if (separator > 0) outputs[lines[index].slice(0, separator)] = lines[index].slice(separator + 1); + } + return outputs; +} + +function writeStub(path, source) { + writeFileSync(path, `#!/usr/bin/env node\n${source}`); + chmodSync(path, 0o755); +}