From 595afcc3ed8d6a1ee79e3b951f700b193d9ba615 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 2 Dec 2026 17:45:03 -0300 Subject: [PATCH 01/13] release-merge: write the base and develop merges together or not at all The action merged the release branch into the base, then the base into develop, each with POST /merges, which writes the branch it is given. A conflict on develop stopped the run with the release already on the base, with no tag and no build. A hotfix branch cut from the base before the retry would ship that code. Each merge is now made on a scratch branch at the tip the action read. When both merge commits exist, the base and develop move to them in one updateRefs mutation, which applies every update or none. A branch that received a commit in the meantime is refused, and neither branch moves. A conflict on either branch leaves both as they were. The message now says to merge the release branch into the branch it names. SOFT-5829 --- .github/actions/release-merge/README.md | 112 ++++--- .github/actions/release-merge/action.yml | 17 +- .../actions/release-merge/release-merge.js | 300 +++++++++++++----- README.md | 2 +- templates/workflows/release.yml | 3 +- .../release-merge/release-merge.test.js | 234 ++++++++++++-- 6 files changed, 517 insertions(+), 151 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index f31d8c15..5da101d9 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -1,8 +1,9 @@ # release-merge Merges a release: the release branch into the base branch, then the base branch back into develop, -through the GitHub API. **The base branch is `main`, or `master` where that is the repository's name -for it**, set with `base-branch`: +through the GitHub API. The two merges are written together or not at all: a conflict in either one +leaves both branches as they were. **The base branch is `main`, or `master` where that is the +repository's name for it**, set with `base-branch`: ```yaml - uses: stellarwp/plugin-toolbox/.github/actions/release-merge@v1 @@ -47,22 +48,43 @@ In this order, stopping at the first failure: the branch's `.puprc` lists holds the version and, when the version is already tagged, that the tag contains the branch: a retry of this release goes on, a tag made from another commit is refused. -3. Merges the release branch into the base branch as a merge commit, `Merge release/… into main` - (or `into master`). When the base already holds the branch, a retry, nothing is merged: the - commit to build is then the one the tag points at when the version is already published, and - otherwise the base's tip, which has to be the commit that merged the branch. A base that has - moved on since, by any commit, is refused. See [Versions it refuses](#versions-it-refuses). -4. Merges the **merged commit** into develop as a merge commit, `Merge main back into develop - (release/…)`. The base branch rather than the release branch, so develop descends from the - base and consecutive releases bring one version bump each instead of divergent copies of the - same generated lines. Skipped when `develop-branch` is empty, for a repository that has no - develop branch. +3. Makes the merge of the release branch into the base branch, a merge commit `Merge release/… into + main` (or `into master`), on a scratch branch: the base is not written yet. When the base + already holds the branch, a retry, nothing is merged: the commit to build is then the one the + tag points at when the version is already published, and otherwise the base's tip, which has to + be the commit that merged the branch. A base that has moved on since, by any commit, is refused. + See [Versions it refuses](#versions-it-refuses). +4. Makes the merge of that **merged commit** into develop, a merge commit `Merge main back into + develop (release/…)`, on a second scratch branch. The base branch rather than the release + branch, so develop descends from the base and consecutive releases bring one version bump each + instead of divergent copies of the same generated lines. Skipped when `develop-branch` is empty, + for a repository that has no develop branch. +5. Moves the base and develop to their merge commits in one update, then deletes the scratch + branches. See [Both merges or neither](#both-merges-or-neither). The merge commit on the base branch is the `base-sha` output: what the caller checks out and builds from, and what release-publish tags. GitHub makes both merge commits under the identity of `token`, `github-actions[bot]` for the default token, and signs them. The API never fast-forwards, so each release is one merge commit in the history. +## Both merges or neither + +The merge endpoint has no dry run: it writes the merge commit to the branch it is given. Asked for +the base and then for develop, a conflict on develop would leave the release on the base with no +tag and no build, and a hotfix branch cut from the base in that window would ship it. + +So each merge is made on a scratch branch, created at the tip the action read: +`release-merge/-/main` and `release-merge/-/develop`. Only when +both merge commits exist do the branches move, with the GraphQL mutation +[`updateRefs`](https://docs.github.com/en/graphql/reference/mutations#updaterefs), which applies +every update or none. Each branch moves from the tip its merge was made on to the merge commit, a +fast-forward. A branch that received a commit in the meantime is no longer at that tip: GitHub +refuses the update, neither branch moves, and the step fails with a message to run it again. + +The scratch branches are deleted when the step ends, whatever it did. One that could not be deleted +is a warning in the log; a run cancelled in the few seconds they exist can leave them too. Nothing +reads them, and they can be deleted by hand. + The action reads and writes no checkout: it needs none before it, and leaves whatever checkout the job has as it was. Check out `base-sha` after it for the steps that build the release. @@ -78,24 +100,34 @@ does: - A version already published counts as a retry only when its tag contains the release branch, that is, when it was published from this branch. See [Versions it refuses](#versions-it-refuses). release-publish then skips the release. -- A conflict resolved by hand on the branch, then pushed, is found as an already-merged branch on - the next run, which goes on to the step that had not run. +- A conflict resolved by hand, then pushed, is not found again: the branch then holds the release + branch, and the next run goes on from there. +- A release that is on the base and not on develop, which a deploy stopped by a conflict left + before the two merges were written together, gets the merge it lacks: only develop moves. -So a run that failed after merging the base, or in a later step of the caller, is rerun as it is; -nothing has to be undone first. +So a run that failed in a later step of the caller is rerun as it is; nothing has to be undone +first. ## When a merge conflicts -GitHub refuses the merge, nothing is written for that branch, and the step fails with the branch -named. The branches already merged stay merged: a conflict merging the base into develop leaves the -release on the base. Resolve the conflict on the branch by hand, push, and run the step again. The -API does not say which files conflict; the merge done by hand shows them. +GitHub refuses the merge and the step fails with the branch named. Neither the base nor develop is +written, whichever merge conflicted: a conflict merging the base into develop does not leave the +release on the base. + +To resolve it, merge the release branch into the branch named, by hand, resolve the conflict, push, +and run the step again. That is the release branch for a conflict on develop too: the base does not +hold the release yet, so there is nothing else to merge from. The next run finds the release +branch in the branch resolved by hand, with nothing left to conflict on. The API does not say which +files conflict; the merge done by hand shows them. If the run after that conflicts on develop +again, the conflict is with a commit the base has and develop lacks: merge the base into develop as +well. The log carries this line, on one line: ``` -MERGE_CONFLICT: merging main into develop conflicted. Resolve it on develop by hand and push, -then run this step again. Nothing was merged into develop. +MERGE_CONFLICT: merging main into develop conflicted. Merge release/M26.test into develop by hand, +resolve the conflict and push, then run this step again. Nothing was merged: main and develop are +as they were. ``` and the `conflict` output names the branch. The [release @@ -104,8 +136,9 @@ marker `MERGE_CONFLICT` at the start of an output line of the job log, every lin runner prefixes with a timestamp. The marker is printed from the script, not written in the workflow, so it is not in the step's echoed inputs and only appears when a merge did conflict. -A merge GitHub refuses for another reason (the base branch moved while the merge was being made) -fails the step without the marker; running the step again is enough. +A branch that moved while the merges were being made fails the step without the marker, with +neither branch written; running the step again is enough. So does a merge GitHub refuses for any +other reason. ## Versions it refuses @@ -168,7 +201,7 @@ this; without `head`, the tip at that moment is what merges. | `version` | yes | | The version being released: three or four numeric parts. | | `base-branch` | no | `main` | The branch the release is merged into: `main`, or `master` where that is the repository's name for it. | | `develop-branch` | no | `develop` | The branch the base is merged back into. Empty for a repository without one: only the first merge is made. Write `develop-branch: ''` in the caller itself; a `workflow_dispatch` input left empty takes its default, so the empty value cannot be passed through one. | -| `token` | no | `github.token` | The token for the merges and the reads. Needs `contents: write`. See [The token](#the-token). | +| `token` | no | `github.token` | The token for the merges, the scratch branches and the reads. Needs `contents: write`. See [The token](#the-token). | ## Outputs @@ -177,7 +210,7 @@ this; without `head`, the tip at that moment is what merges. | `version` | The version released. | | `branch` | The release branch merged. | | `base-sha` | The commit on the base branch that holds the release: the merge commit, or the base's tip on a retry that found the merge done. The commit to check out and build from; pass it to release-publish as `target`. | -| `conflict` | The branch a merge conflicted on, or empty. A conflict fails the step, so read it with `continue-on-error` or in a later `if: failure()` step. | +| `conflict` | The branch a merge conflicted on, or empty. A conflict on either branch leaves both as they were. It fails the step, so read it with `continue-on-error` or in a later `if: failure()` step. | | `already-published` | `yes` when the version's GitHub release already existed when the step started (a retry of a published release), `no` otherwise. release-publish skips a published version by itself; read this to skip the steps of your own that write somewhere a retry of an older version must not touch, such as a zip copied to a site or a version written there: `if: steps.merge.outputs.already-published == 'no'`, or `if: steps.merge.outcome == 'success' && steps.merge.outputs.already-published == 'no'` when the merge step runs under `continue-on-error`. Compare with `no`, not with `yes`: `already-published` is among the outputs written last, so a step that failed before them leaves it empty; the `outcome` check covers a failure after them, in the runner's own wrap-up of the step. `conflict` is the one output a failure sets. It says nothing about whether those steps ran: keep them before release-publish, so the release is published only once they are done, and make each one safe to repeat for the same version (the same zip copied again, the same version written again), since a retry after a failed publish runs them again with `no`. | ## One deploy at a time @@ -202,18 +235,23 @@ actionlint 1.7 does not know the key and the plugin repositories lint their work ## The token -The job needs `contents: write`, and the token is used for the two merges and for the branch, tag, -release and file reads. It only ever reaches github-script's client: it is never handed to git, -never on a command line and never in the log. The caller's checkout, when there is one, can use +The job needs `contents: write`, and the token is used for the two merges, the scratch branches +they are made on, the update that moves the base and develop, and the branch, tag, release and file +reads. It only ever reaches github-script's client: it is never handed to git, never on a command +line and never in the log. The caller's checkout, when there is one, can use `persist-credentials: false`. The default `github.token` is enough when the repository's rules allow what the action does: a -merge written straight to the base branch and to develop. The merges are made through the API, not -opened as pull requests, so a base branch that only accepts pull requests refuses the merge, with -any token that is not on the rule's bypass list; a rule that requires signed commits is satisfied, -since GitHub signs the merge commits it makes. GiveWP's rules are the reference shape: the branches -are protected from deletion and non-fast-forward pushes, and `github.token` writes the merges. The -human gate is not the token: the caller puts the job in an environment with required reviewers, and +fast-forward of the base branch and of develop to a merge commit, and branches created and deleted +under `release-merge/`. The merges are made through the API, not opened as pull requests, so a base +branch that only accepts pull requests refuses the update, with any token that is not on the rule's +bypass list, and so does a rule that restricts who creates branches, for the scratch ones; a rule +that requires signed commits is satisfied, since GitHub signs the merge commits it makes. GiveWP's +rules are the reference shape: the branches are protected from deletion and non-fast-forward pushes, +and `github.token` writes the merges. What `github.token` writes starts no workflow run. Another +token does, so a workflow that runs on every pushed branch would run on the scratch branches for +the seconds they exist; leave `release-merge/**` out of its branches then. The human gate is not the +token: the caller puts the job in an environment with required reviewers, and the release branch reached this step after its review and QA approval. A repository whose rules still need another actor should pass a GitHub App installation token minted in the run rather than a personal access token. What the token needs for the tag is in @@ -222,8 +260,8 @@ a personal access token. What the token needs for the tag is in ## How it runs One [actions/github-script](https://github.com/actions/github-script) step runs -[`release-merge.js`](./release-merge.js): the checks and the merges, all through github-script's -Octokit. The inputs reach the script through environment variables, never interpolated into it, so +[`release-merge.js`](./release-merge.js): the checks, the merges and the update that moves the two +branches, all through github-script's Octokit. The inputs reach the script through environment variables, never interpolated into it, so a crafted value is data rather than part of the program. The tests, `node --test` from the repository root, run the action against a recorded client and diff --git a/.github/actions/release-merge/action.yml b/.github/actions/release-merge/action.yml index d595187d..990529fb 100644 --- a/.github/actions/release-merge/action.yml +++ b/.github/actions/release-merge/action.yml @@ -4,12 +4,16 @@ # publishing the GitHub release is the release-publish action's, at the commit this one leaves on the # base (`base-sha`). # +# The two merges are written together or not at all: each is made on a scratch branch, and the +# base and develop then move to their merge commits in one update. A conflict in either merge +# leaves both branches as they were. +# # The checks and the merges live in release-merge.js, run by actions/github-script, so there is no # dependency to install and nothing to build. Everything goes through the GitHub API: the branch, # tag and release reads, the version-file reads and the merges themselves. No checkout is needed; # the caller checks out `base-sha` afterwards to build from it. name: Release merge -description: Merges the release branch into the base and the base back into develop +description: Merges the release branch into the base and the base back into develop, both or neither inputs: branch: @@ -50,7 +54,8 @@ inputs: token: description: >- Token the merges are made with, and the branch, tag, release and file reads. It needs - `contents: write` on the repository, so the job needs that permission too. GitHub makes the + `contents: write` on the repository, so the job needs that permission too, and it has to be + allowed to create and delete the scratch branches under `release-merge/`. GitHub makes the merge commits under its identity and signs them. It only ever reaches github-script's client, never git or the log. required: false @@ -72,8 +77,8 @@ outputs: conflict: description: >- Which branch a merge conflicted on, `main` or `develop` for the defaults, or empty when the - merges went through. A conflict fails the step, so a caller reads this with `continue-on-error` - or in a later `if: failure()` step. + merges went through. A conflict on either leaves both branches as they were. It fails the step, + so a caller reads this with `continue-on-error` or in a later `if: failure()` step. value: ${{ steps.merge.outputs.conflict }} already-published: description: >- @@ -89,7 +94,7 @@ outputs: runs: using: 'composite' steps: - - name: Merge the release into the base and the base back into develop + - name: Merge the release into the base and the base back into develop, both or neither id: merge uses: actions/github-script@v9 env: @@ -105,7 +110,7 @@ runs: INPUT_BASE_BRANCH: ${{ inputs.base-branch }} INPUT_DEVELOP_BRANCH: ${{ inputs.develop-branch }} with: - # Every read and the merges go through github-script's own client, built from this token. + # Every read and every write go through github-script's own client, built from this token. github-token: ${{ inputs.token }} script: | const { runMerge } = require(`${process.env.ACTION_PATH}/release-merge.js`) diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index a10b8e58..76505493 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -6,7 +6,9 @@ * after this action and its build, at the commit this one leaves on the base (`base-sha`). * * runMerge checks the inputs and the version against the repository's tags, then asks GitHub for - * the two merges. Everything goes through the Octokit github-script supplies: the action reads and + * the two merges. Each is made on a scratch branch first, and the base and develop then move to + * their merge commits in one update, both or neither: a conflict in either merge leaves the two as + * they were. Everything goes through the Octokit github-script supplies: the action reads and * writes no checkout, so a test runs it against a recorded client and asserts the requests. * * Every step is safe to run again after a late failure: a branch that already holds the release @@ -117,7 +119,7 @@ function mergeMessage(from, into) { * Whether a failed merge request is GitHub reporting a conflict, as opposed to any other refusal. * * The merge endpoint answers 409 for two things: a conflict between the branches, and a base that - * moved while the merge was being made. Only the first is a conflict for the caller to resolve. + * moved while the merge was being made. Only the first is a conflict for a person to resolve. * * @param {Error} error The Octokit error. * @@ -341,65 +343,167 @@ async function commitMergesBranch({ github, owner, repo, branch, ref, sha }) { } /** - * Merges one branch, or commit, into another as a merge commit, through the API. + * Creates the scratch branch a merge is tried on, at the tip of the branch the merge is for. * - * GitHub makes the merge commit under the token's identity and signs it. A base that already holds + * The merge endpoint has no dry run: it writes the merge commit to the branch it is given. Given + * the base or develop itself, the first merge would be on the remote before the second was tried. + * + * @param {object} options + * @param {object} options.github The Octokit github-script supplies. + * @param {string} options.owner The repository owner. + * @param {string} options.repo The repository name. + * @param {string} options.name The scratch branch to create. + * @param {string} options.sha The commit to create it at. + * @param {string} options.unchanged What to close the message with: the branches that were not written. + * + * @throws {Error} When GitHub refuses to create the branch. + * + * @returns {Promise} Resolves once the branch exists. + */ +async function createScratch({ github, owner, repo, name, sha, unchanged }) { + try { + await github.rest.git.createRef({ owner, repo, ref: `refs/heads/${name}`, sha }) + } catch (error) { + throw new Error( + `Creating the scratch branch ${name} failed (HTTP ${error?.status}): ${error?.message}. Each merge is ` + + `tried on a scratch branch first, so the token has to be allowed to create and delete it. ${unchanged}` + ) + } +} + +/** + * Deletes the scratch branches of a run, whatever the run did with them. + * + * A branch that cannot be deleted is a warning, not a failure: by then the release is merged or + * the run has already failed with its own message, and nothing reads a scratch branch. + * + * @param {object} options + * @param {object} options.github The Octokit github-script supplies. + * @param {object} options.core The @actions/core toolkit. + * @param {string} options.owner The repository owner. + * @param {string} options.repo The repository name. + * @param {string[]} options.names The scratch branches that were created. + * + * @returns {Promise} Resolves once every deletion was tried. + */ +async function deleteScratch({ github, core, owner, repo, names }) { + for (const name of names) { + try { + await github.rest.git.deleteRef({ owner, repo, ref: `heads/${name}` }) + } catch (error) { + core.warning( + `The scratch branch ${name} could not be deleted (HTTP ${error?.status}): ${error?.message}. ` + + 'Delete it by hand; nothing reads it.' + ) + } + } +} + +/** + * Makes one merge commit, on a scratch branch at the tip of the branch the commit is for. That + * branch is not written here: moveBranches moves it to the commit once every merge is made. + * + * GitHub makes the merge commit under the token's identity and signs it. A tip that already holds * the head answers 204 and nothing is written, which is what a retry after a late failure finds. A * conflict prints the MERGE_CONFLICT marker on a line of its own, where the release dashboard looks - * for it, and fails; nothing is written for that branch. + * for it, and fails. * * @param {object} options - * @param {object} options.github The Octokit github-script supplies. - * @param {object} options.core The @actions/core toolkit. - * @param {string} options.owner The repository owner. - * @param {string} options.repo The repository name. - * @param {string} options.into The branch merged into, e.g. `main`. - * @param {string} options.head What is merged: a branch name or a commit hash. - * @param {string} options.message The merge commit message. - * @param {string} options.label How to name `head` in the log, e.g. `release/M26.test`. + * @param {object} options.github The Octokit github-script supplies. + * @param {object} options.core The @actions/core toolkit. + * @param {string} options.owner The repository owner. + * @param {string} options.repo The repository name. + * @param {string} options.into The branch the merge is for, e.g. `main`. + * @param {string} options.scratch The scratch branch the merge is made on, at `tip`. + * @param {string} options.tip The commit `into` was read at. + * @param {string} options.head What is merged: a commit hash. + * @param {string} options.message The merge commit message. + * @param {string} options.label How to name `head` in the log, e.g. `release/M26.test`. + * @param {string} options.branch The release branch: what a person merges into `into` to resolve + * a conflict, whichever merge it was. + * @param {string} options.unchanged What to close a failure message with: the branches that were + * not written. * * @throws {Error} When the merge conflicts or GitHub refuses it. * * @returns {Promise<{merged: boolean, sha: string}>} Whether a merge commit was made, and the commit - * `into` points at afterwards: the merge commit, or its tip when there was nothing to merge. + * `into` is to hold: the merge commit, or `tip` when there was nothing to merge. */ -async function mergeInto({ github, core, owner, repo, into, head, message, label }) { +async function mergeInto({ github, core, owner, repo, into, scratch, tip, head, message, label, branch, unchanged }) { let response try { - response = await github.rest.repos.merge({ owner, repo, base: into, head, commit_message: message }) + response = await github.rest.repos.merge({ owner, repo, base: scratch, head, commit_message: message }) } catch (error) { if (isConflict(error)) { + const resolve = `Merge ${branch} into ${into} by hand, resolve the conflict and push, then run this step again.` + // core.info prints a plain line: the marker has to open an output line to count, see the README. core.setOutput('conflict', into) - core.info( - `MERGE_CONFLICT: merging ${label} into ${into} conflicted. Resolve it on ${into} by hand and ` + - `push, then run this step again. Nothing was merged into ${into}.` - ) - throw new Error( - `Merging ${label} into ${into} stopped on a conflict. Resolve it on ${into} by hand and push, ` + - `then run this step again: the merges already done are kept. Nothing was merged into ${into}.` - ) - } - - if (error?.status === 409) { - throw new Error( - `Merging ${label} into ${into} was refused: ${into} moved while the merge was being made ` + - `(${error.message}). Run this step again; the merges already done are kept.` - ) + core.info(`MERGE_CONFLICT: merging ${label} into ${into} conflicted. ${resolve} ${unchanged}`) + throw new Error(`Merging ${label} into ${into} stopped on a conflict. ${resolve} ${unchanged}`) } - throw new Error(`Merging ${label} into ${into} failed (HTTP ${error?.status}): ${error?.message}`) + throw new Error(`Merging ${label} into ${into} failed (HTTP ${error?.status}): ${error?.message}. ${unchanged}`) } if (response.status === 204) { core.info(`${into} already has ${label}: nothing to merge.`) - return { merged: false, sha: await readBranchTip({ github, owner, repo, branch: into }) } + return { merged: false, sha: tip } } return { merged: true, sha: response.data.sha } } +/** The mutation that moves several branches at once: GitHub applies every update or none. */ +const UPDATE_REFS = `mutation ($input: UpdateRefsInput!) { + updateRefs(input: $input) { + clientMutationId + } +}` + +/** + * Moves the base and develop to their merge commits in one update, so the remote never holds one + * merge without the other. + * + * Each branch is moved from the tip its merge was made on. A branch that is no longer at that tip + * is refused, and GitHub then moves none of them; the merge commit descends from the tip, so the + * move is a fast-forward and never needs to be forced. + * + * @param {object} options + * @param {object} options.github The Octokit github-script supplies. + * @param {string} options.owner The repository owner. + * @param {string} options.repo The repository name. + * @param {object[]} options.moves `{branch, from, to}` per branch: the tip it was read at and + * the merge commit made on that tip. + * @param {string} options.unchanged What to close the message with: the branches that were not written. + * + * @throws {Error} When the repository cannot be read, or GitHub refuses the update. + * + * @returns {Promise} Resolves once the branches are moved. + */ +async function moveBranches({ github, owner, repo, moves, unchanged }) { + let repositoryId + try { + repositoryId = (await github.rest.repos.get({ owner, repo })).data.node_id + } catch (error) { + throw new Error(`Reading ${owner}/${repo} failed (HTTP ${error?.status}): ${error?.message}. ${unchanged}`) + } + + const refUpdates = moves.map(({ branch, from, to }) => ({ name: `refs/heads/${branch}`, beforeOid: from, afterOid: to })) + try { + await github.graphql(UPDATE_REFS, { input: { repositoryId, refUpdates } }) + } catch (error) { + const names = moves.map(({ branch }) => branch) + + throw new Error( + `GitHub refused to move ${names.join(' and ')}${names.length > 1 ? ' together' : ''}: ${error?.message}. ` + + `${unchanged} A branch that received a commit while the merges were being made is refused ` + + 'this way. Run this step again.' + ) + } +} + /** * Checks the inputs and the version, then merges the release branch into the base branch and the * base branch back into develop. The version has to be the one the release branch holds in every @@ -411,6 +515,11 @@ async function mergeInto({ github, core, owner, repo, into, head, message, label * commit in the history. A repository without a develop branch leaves `develop-branch` empty, and * only the first merge is made. * + * The two merges are written together or not at all. Each is made on a scratch branch, and the + * base and develop move to their merge commits in one update once both exist. Written one after + * the other, a conflict on develop would leave the release on the base with no tag and no build, + * where a hotfix branch cut from the base would ship it. + * * Nothing is read from or written to a checkout: the caller checks out `base-sha` afterwards for * the steps that build the release from it. * @@ -420,9 +529,9 @@ async function mergeInto({ github, core, owner, repo, into, head, message, label * @param {object} options.env The environment the action manifest put its inputs in. * * @throws {Error} When an input is wrong, a branch is missing, the tag exists without a release, a - * merge conflicts, or a read or a merge fails. + * merge conflicts, a branch moved while the merges were made, or a read or a write fails. * - * @returns {Promise} Resolves once both merges are made and the outputs are set, + * @returns {Promise} Resolves once both branches hold their merge and the outputs are set, * `already-published` among them: `yes` when the version's release already existed when the step started. */ async function runMerge({ github, core, env }) { @@ -454,9 +563,11 @@ async function runMerge({ github, core, env }) { 'the deploy again on the branch as it is now. Nothing was merged.' ) } - for (const name of develop === null ? [base] : [base, develop]) { - await readBranchTip({ github, owner, repo, branch: name }) - } + + // The tips of the base and develop are read once too: each merge is made on its tip, and the + // branch moves from that tip to the merge commit or not at all. + const baseTip = await readBranchTip({ github, owner, repo, branch: base }) + const developTip = develop === null ? null : await readBranchTip({ github, owner, repo, branch: develop }) await assertBranchHoldsVersion({ github, owner, repo, branch, ref: branchSha, version }) @@ -475,58 +586,83 @@ async function runMerge({ github, core, env }) { core.info(`${version} is already published (${releaseUrl}) from ${branch}: nothing to publish again.`) } - const { merged: mergedBase, sha: mergedSha } = await mergeInto({ - github, - core, - owner, - repo, - into: base, - head: branchSha, - message: mergeMessage(branch, base), - label: branch, - }) - - // base-sha is the commit the release is published at and built from. A merge that was made is - // that merge commit, which holds the version the branch was just checked for. A retry that found - // the merge done gets the base's tip, and that tip is only this release while nothing has been - // merged or pushed since: a publish that failed after the deploy, then another release, then a - // retry would build the later release's code and publish it under this version, since the - // caller's deploy and publish each skip what they find done. So when the version is already - // published, the retry builds the commit the tag was made from, what was published, and the base - // moving on changes nothing; when it is not, the tip has to be the commit that merged the branch. - let baseSha = mergedSha - if (!mergedBase) { - if (tagged) { - baseSha = tagSha - core.info(`${version} builds at ${tagSha}, the commit it was published from.`) - } else if (!(await commitMergesBranch({ github, owner, repo, branch, ref: branchSha, sha: mergedSha }))) { - throw new Error( - `${base} already had ${branch} and has moved on since: its tip ${mergedSha} is not the commit that merged ` + - `${branch}, so a build from it would publish other changes under ${version}. There is nothing left ` + - `to merge for ${version}; finish it by hand at the commit that merged ${branch} into ${base}, or ` + - 'release another version. Nothing was merged.' - ) - } + // Until the move at the end, the only branches written are the scratch ones, named after the + // run so that no two runs share one. Every failure from here on closes with this sentence. + const unchanged = `Nothing was merged: ${develop === null ? `${base} is as it was` : `${base} and ${develop} are as they were`}.` + const scratchPrefix = `release-merge/${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` + const scratch = [] + const mergeOnScratch = async ({ into, tip, ...merge }) => { + const name = `${scratchPrefix}/${into}` + await createScratch({ github, owner, repo, name, sha: tip, unchanged }) + scratch.push(name) + + return mergeInto({ github, core, owner, repo, into, scratch: name, tip, branch, unchanged, ...merge }) } - // The second merge takes that commit, not the base's name, so develop receives exactly what is - // being released even if the base moves on. + let mergedBase let mergedDevelop = null - if (develop === null) { - core.info(`develop-branch is empty: ${base} is not merged back into a develop branch.`) - } else { - mergedDevelop = ( - await mergeInto({ - github, - core, - owner, - repo, + let baseSha + try { + const onBase = await mergeOnScratch({ + into: base, + tip: baseTip, + head: branchSha, + message: mergeMessage(branch, base), + label: branch, + }) + mergedBase = onBase.merged + + // base-sha is the commit the release is published at and built from. A merge that was made is + // that merge commit, which holds the version the branch was just checked for. A retry that found + // the merge done gets the base's tip, and that tip is only this release while nothing has been + // merged or pushed since: a publish that failed after the deploy, then another release, then a + // retry would build the later release's code and publish it under this version, since the + // caller's deploy and publish each skip what they find done. So when the version is already + // published, the retry builds the commit the tag was made from, what was published, and the base + // moving on changes nothing; when it is not, the tip has to be the commit that merged the branch. + baseSha = onBase.sha + if (!mergedBase) { + if (tagged) { + baseSha = tagSha + core.info(`${version} builds at ${tagSha}, the commit it was published from.`) + } else if (!(await commitMergesBranch({ github, owner, repo, branch, ref: branchSha, sha: baseTip }))) { + throw new Error( + `${base} already had ${branch} and has moved on since: its tip ${baseTip} is not the commit that merged ` + + `${branch}, so a build from it would publish other changes under ${version}. There is nothing left ` + + `to merge for ${version}; finish it by hand at the commit that merged ${branch} into ${base}, or ` + + 'release another version. Nothing was merged.' + ) + } + } + + const moves = mergedBase ? [{ branch: base, from: baseTip, to: baseSha }] : [] + + // The second merge takes that commit, not the base's name, so develop receives exactly what is + // being released even if the base moves on. + if (develop === null) { + core.info(`develop-branch is empty: ${base} is not merged back into a develop branch.`) + } else { + const onDevelop = await mergeOnScratch({ into: develop, + tip: developTip, head: baseSha, message: `Merge ${base} back into ${develop} (${branch})`, label: base, }) - ).merged + mergedDevelop = onDevelop.merged + if (mergedDevelop) { + moves.push({ branch: develop, from: developTip, to: onDevelop.sha }) + } + } + + // Both merges exist: the branches move to them now, together. A retry that found a merge done + // has nothing to move for that branch. + if (moves.length > 0) { + await moveBranches({ github, owner, repo, moves, unchanged }) + } + } finally { + // After the move, not before: until then the scratch branches are what keeps the merge commits. + await deleteScratch({ github, core, owner, repo, names: scratch }) } // One item of the list the deploy's actions write to the job summary, with each branch as a link. diff --git a/README.md b/README.md index c578e292..85e45e17 100644 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ Each phase ends with something people can actually use. - [x] Port `analyze-hooks` from TEC: the actions and filters a release adds, updates, deprecates or removes and the views it adds, changes or removes, as pending changelog entries. The search paths, the views paths and the changelog types are inputs, and a `v` on the release tag is accepted. - [ ] Add `setup-node` for the npm crowd. - [x] Add `release-prep`: version bump, TBD replacement and changelog through pup and the changelogger action, committed to the release branch through the GitHub API. A caller to copy is in `templates/workflows/release-prep.yml`. -- [x] Add `release-merge`: the release branch into the base branch, the base back into develop, each step safe to rerun and a conflict reported with a `MERGE_CONFLICT` line. +- [x] Add `release-merge`: the release branch into the base branch, the base back into develop, both merges written together or not at all, each step safe to rerun and a conflict reported with a `MERGE_CONFLICT` line. - [x] Add `release-publish`: the GitHub release for a version at a commit, with a title, notes from a file and assets uploaded before the release is public, skipped when it already exists. The complete deploy workflow to copy, both actions with the build between them, is `templates/workflows/release.yml`. - [x] Add `zip-name` and `release-zip`: the zip's name before the build, with a pre-release label (`dev`, `alpha`, `beta`, `rc`) and an optional build number, then `pup build`, `check`, `i18n` and `package` for that version. What `stellarwp/github-actions`' `zip.yml` does, as two actions so the bucket can be asked for the name first. The caller to copy, with the S3 check, the upload, the Slack notice and the artifact around them, is `templates/workflows/zip.yml`. - [x] Add `herald-publish`: the build uploaded to Herald and published as a version of a product, with a `ci` API key. A ZIP of another version is refused, and a rerun updates the version instead of creating it twice. `templates/workflows/release.yml` shows the step, commented out, between the build and `release-publish`. diff --git a/templates/workflows/release.yml b/templates/workflows/release.yml index eb7925b9..6a052828 100644 --- a/templates/workflows/release.yml +++ b/templates/workflows/release.yml @@ -71,7 +71,8 @@ jobs: exit 1 fi - # The merges go through the GitHub API: no checkout is needed before this step. + # The merges go through the GitHub API: no checkout is needed before this step. The base and + # develop receive their merge together, so a conflict on either leaves both as they were. - uses: stellarwp/plugin-toolbox/.github/actions/release-merge@v1 id: merge with: diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index f4804339..bf868f38 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -28,6 +28,8 @@ function mergeEnv(overrides = {}) { INPUT_BASE_BRANCH: 'main', INPUT_DEVELOP_BRANCH: 'develop', GITHUB_REPOSITORY: 'stellarwp/plugin', + GITHUB_RUN_ID: '77', + GITHUB_RUN_ATTEMPT: '1', ...overrides, } } @@ -49,6 +51,15 @@ const TIP = { develop: 'cccccccccccccccccccccccccccccccccccccccc', } +/** The scratch branches of run 77, first attempt: where each merge is tried before a branch moves. */ +const SCRATCH = { + main: 'release-merge/77-1/main', + develop: 'release-merge/77-1/develop', +} + +/** The repository's node ID, which the mutation that moves the branches is addressed with. */ +const REPOSITORY_ID = 'R_kgDOplugin' + /** The merge commits GitHub answers with: the release on main, then main on develop. */ const MERGED = { main: 'dddddddddddddddddddddddddddddddddddddddd', @@ -72,6 +83,10 @@ const API = { compare: (sha, ref = TIP['release/M26.test']) => `${REPO}/compare/${encodeURIComponent(`${ref}...${sha}`)}`, commit: (sha) => `${REPO}/commits/${sha}`, merges: `${REPO}/merges`, + repository: REPO, + refs: `${REPO}/git/refs`, + ref: (name) => `${REPO}/git/refs/${encodeURIComponent(`heads/${name}`)}`, + graphql: '/graphql', } /** @@ -153,14 +168,24 @@ function merges(responses) { return { method: 'POST', path: API.merges, responses } } +/** What the mutation that moves the branches answers: done, or refused with nothing moved. */ +const MOVE = { + done: { body: { data: { updateRefs: { clientMutationId: null } } } }, + refused: { body: { data: { updateRefs: null }, errors: [{ type: 'UNPROCESSABLE', message: 'Ref refs/heads/main is at 9999, not at bbbb' }] } }, +} + /** - * The rules of the common case: no tag, three branches, a prepared release branch, both merges made. + * The rules of the common case: no tag, three branches, a prepared release branch, both merges made + * on their scratch branches, and the two branches moved together. * * @param {object} options * @param {object[]} options.tag Rules for the tag and release reads; none for an untagged version. * @param {string[]} options.branches The branches the repository has. * @param {object} options.files Files of the release branch to change from the fixture. * @param {object[]} options.merges What the merge requests are answered with. + * @param {object} options.scratch What creating a scratch branch is answered with. + * @param {object} options.cleanup What deleting a scratch branch is answered with. + * @param {object} options.move What the mutation that moves the branches is answered with. * @param {object[]} options.extra Any other rule. * * @returns {object[]} The rules. @@ -170,9 +195,23 @@ function repositoryWith({ branches: names = Object.keys(TIP), files: contents = {}, merges: answers = [MERGE.main, MERGE.develop], + scratch = { status: 201, body: {} }, + cleanup = { status: 204 }, + move = MOVE.done, extra = [], } = {}) { - return [...tag, ...branches(names), ...files(contents), merges(answers), ...extra] + return [ + ...tag, + ...branches(names), + ...files(contents), + merges(answers), + { method: 'POST', path: API.refs, ...scratch }, + { method: 'DELETE', path: API.ref(SCRATCH.main), ...cleanup }, + { method: 'DELETE', path: API.ref(SCRATCH.develop), ...cleanup }, + { method: 'GET', path: API.repository, body: { node_id: REPOSITORY_ID } }, + { method: 'POST', path: API.graphql, ...move }, + ...extra, + ] } /** @@ -197,6 +236,45 @@ function mergesAsked(requests) { return requests.filter((request) => request.method === 'POST' && request.path === API.merges).map((request) => request.body) } +/** + * The branches each request to move them named, in order: one entry per mutation sent. + * + * @param {object[]} requests The requests recordingFetch recorded. + * + * @returns {object[][]} `{name, beforeOid, afterOid}` per branch, per mutation. + */ +function movesAsked(requests) { + return requests + .filter((request) => request.method === 'POST' && request.path === API.graphql) + .map((request) => { + assert.match(request.body.query, /updateRefs\(input: \$input\)/) + assert.strictEqual(request.body.variables.input.repositoryId, REPOSITORY_ID) + + return request.body.variables.input.refUpdates + }) +} + +/** + * The writes to the repository, in order, as `METHOD path`: everything but the reads. + * + * @param {object[]} requests The requests recordingFetch recorded. + * + * @returns {string[]} One line per write. + */ +function written(requests) { + return requested(requests.filter((request) => request.method !== 'GET')) +} + +/** The writes of a run that tried both merges and moved nothing: the scratch branches come and go. */ +const SCRATCH_ONLY = [ + `POST ${API.refs}`, + `POST ${API.merges}`, + `POST ${API.refs}`, + `POST ${API.merges}`, + `DELETE ${API.ref(SCRATCH.main)}`, + `DELETE ${API.ref(SCRATCH.develop)}`, +] + describe('release-merge', () => { describe('validateVersion', () => { it('accepts three or four numeric parts, the shape the dashboard publishes', () => { @@ -325,7 +403,7 @@ describe('release-merge', () => { }) describe('runMerge', () => { - it('merges the release into the base, then the merged commit back into develop, as two merge commits', async (t) => { + it('tries both merges on scratch branches, then moves the base and develop together in one update', async (t) => { const { github, requests } = await octokitFor(repositoryWith()) const { core, outputs, summary } = await coreWithOutputs(t) @@ -339,14 +417,39 @@ describe('release-merge', () => { `GET ${API.file('.puprc')}`, `GET ${API.file('plugin.php')}`, `GET ${API.file('readme.txt')}`, + // Each merge is made on a scratch branch at the tip that was read: nothing reaches main or + // develop until both merges exist. + `POST ${API.refs}`, `POST ${API.merges}`, + `POST ${API.refs}`, `POST ${API.merges}`, + `GET ${API.repository}`, + `POST ${API.graphql}`, + // The scratch branches go last, so the merge commits are reachable when the branches move. + `DELETE ${API.ref(SCRATCH.main)}`, + `DELETE ${API.ref(SCRATCH.develop)}`, ]) + assert.deepStrictEqual( + requests.filter((request) => request.method === 'POST' && request.path === API.refs).map((request) => request.body), + [ + { ref: `refs/heads/${SCRATCH.main}`, sha: TIP.main }, + { ref: `refs/heads/${SCRATCH.develop}`, sha: TIP.develop }, + ] + ) assert.deepStrictEqual(mergesAsked(requests), [ // The tip the version check read, not the branch by name: what was checked is what is merged. - { base: 'main', head: TIP['release/M26.test'], commit_message: 'Merge release/M26.test into main' }, - // The commit just made on main, not main by name: develop gets exactly what is released. - { base: 'develop', head: MERGED.main, commit_message: 'Merge main back into develop (release/M26.test)' }, + // The message names main, the branch the commit is for, not the scratch branch. + { base: SCRATCH.main, head: TIP['release/M26.test'], commit_message: 'Merge release/M26.test into main' }, + // The commit just made for main, not main by name: develop gets exactly what is released. + { base: SCRATCH.develop, head: MERGED.main, commit_message: 'Merge main back into develop (release/M26.test)' }, + ]) + // One mutation for both branches, each from the tip the merge was made on to its merge commit: + // GitHub moves both or neither, and a branch that is no longer at that tip is refused. + assert.deepStrictEqual(movesAsked(requests), [ + [ + { name: 'refs/heads/main', beforeOid: TIP.main, afterOid: MERGED.main }, + { name: 'refs/heads/develop', beforeOid: TIP.develop, afterOid: MERGED.develop }, + ], ]) assert.deepStrictEqual(outputs(), { branch: 'release/M26.test', @@ -451,6 +554,7 @@ describe('release-merge', () => { assert.ok(!requested(requests).some((line) => line.includes('develop')), requested(requests).join('\n')) assert.strictEqual(mergesAsked(requests).length, 1) + assert.deepStrictEqual(movesAsked(requests), [[{ name: 'refs/heads/main', beforeOid: TIP.main, afterOid: MERGED.main }]]) assert.strictEqual(outputs()['base-sha'], MERGED.main) assert.strictEqual(outputs()['develop-branch'], '') assert.strictEqual(outputs().conflict, '') @@ -485,6 +589,26 @@ describe('release-merge', () => { assert.ok(!requested(requests).some((line) => line.startsWith(`GET ${API.commit('')}`)), requested(requests).join('\n')) // The second merge still asks for that commit, which develop already has. assert.strictEqual(mergesAsked(requests)[1].head, MERGED.main) + // Neither branch has anything to receive, so nothing is moved: the scratch branches come and go. + assert.deepStrictEqual(written(requests), SCRATCH_ONLY) + }) + + it('moves only develop on a retry that finds the release on the base and not yet on develop', async (t) => { + // What a deploy stopped by a develop conflict left before the two merges were written + // together: the release on main, published or not, and develop without it. + const { github, requests } = await octokitFor( + repositoryWith({ + merges: [MERGE.done, MERGE.develop], + extra: [commit(MERGED.main, [TIP.main, TIP['release/M26.test']])], + }).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: MERGED.main } } } : rule)) + ) + const { core, outputs, summary } = await coreWithOutputs(t) + + await runMerge({ github, core, env: mergeEnv() }) + + assert.deepStrictEqual(movesAsked(requests), [[{ name: 'refs/heads/develop', beforeOid: TIP.develop, afterOid: MERGED.develop }]]) + assert.strictEqual(outputs()['base-sha'], MERGED.main) + assert.match(summary(), /\[main\]\(.*\) already had \[release\/M26\.test\]\(.*\), merged \[main\]\(.*\) into \[develop\]\(.*\)\.$/m) }) it('goes on with an unpublished retry whose base tip is the commit that merged the branch', async (t) => { @@ -500,14 +624,16 @@ describe('release-merge', () => { await runMerge({ github, core, env: mergeEnv() }) - // The tip is read after the merge answered already merged, by the hash it then read, and - // before the develop merge; no version file is read at the base. - assert.deepStrictEqual(requested(requests).slice(-5), [ + // The tip the scratch branch was made at is the one checked, after the merge answered already + // merged and before the develop merge; main is not read again, and no version file at the base. + assert.deepStrictEqual(requested(requests).slice(-7), [ + `POST ${API.refs}`, `POST ${API.merges}`, - `GET ${API.branch('main')}`, `GET ${API.commit(MERGED.main)}`, + `POST ${API.refs}`, `POST ${API.merges}`, - `GET ${API.branch('develop')}`, + `DELETE ${API.ref(SCRATCH.main)}`, + `DELETE ${API.ref(SCRATCH.develop)}`, ]) assert.strictEqual(outputs()['base-sha'], MERGED.main) assert.strictEqual(mergesAsked(requests)[1].head, MERGED.main) @@ -537,7 +663,7 @@ describe('release-merge', () => { ) // Refused before the develop merge, and the outputs are not set. - assert.strictEqual(mergesAsked(requests).length, 1) + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(SCRATCH.main)}`]) assert.deepStrictEqual(outputs(), {}) } }) @@ -612,47 +738,107 @@ describe('release-merge', () => { assert.deepStrictEqual(mergesAsked(requests), []) }) - it('prints the MERGE_CONFLICT marker and merges nothing when the release conflicts with the base', async (t) => { + it('prints the MERGE_CONFLICT marker and writes nothing when the release conflicts with the base', async (t) => { const { github, requests } = await octokitFor(repositoryWith({ merges: [MERGE.conflict] })) const { core, outputs, logged } = await coreWithOutputs(t) await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /release\/M26\.test into main stopped on a conflict.*Nothing was merged into main/s + /release\/M26\.test into main stopped on a conflict\. Merge release\/M26\.test into main by hand, resolve the conflict and push, then run this step again\. Nothing was merged: main and develop are as they were\./ ) // The marker opens a line of its own: that is what the dashboard's detector anchors on. - assert.match(logged(), /^MERGE_CONFLICT: .*release\/M26\.test.*main/m) + assert.match(logged(), /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand.*main and develop are as they were\.$/m) assert.strictEqual(outputs().conflict, 'main') - assert.strictEqual(mergesAsked(requests).length, 1, 'develop was never asked for') + // Develop was never tried, and the one scratch branch is gone. + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(SCRATCH.main)}`]) }) - it('prints the marker and names develop when the merged base conflicts with develop', async (t) => { + it('leaves the base as it was when the merge back into develop conflicts, and names develop', async (t) => { const { github, requests } = await octokitFor(repositoryWith({ merges: [MERGE.main, MERGE.conflict] })) const { core, outputs, logged } = await coreWithOutputs(t) await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /main into develop stopped on a conflict.*merges already done are kept.*Nothing was merged into develop/s + /main into develop stopped on a conflict\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. Nothing was merged: main and develop are as they were\./ ) - assert.match(logged(), /^MERGE_CONFLICT: .*main.*develop/m) + assert.match(logged(), /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into develop by hand.*main and develop are as they were\.$/m) assert.strictEqual(outputs().conflict, 'develop') - // The release did reach the base: a retry finds that merge done and only redoes develop. - assert.deepStrictEqual(mergesAsked(requests).map((merge) => merge.base), ['main', 'develop']) + // The merge for main was made, on its scratch branch only: main is never asked to move, so + // the release is not on it while develop waits for a person. + assert.deepStrictEqual(written(requests), SCRATCH_ONLY) + assert.deepStrictEqual(movesAsked(requests), []) + assert.ok(!requested(requests).includes(`GET ${API.repository}`), requested(requests).join('\n')) }) - it('fails without the marker when GitHub refuses the merge because the base moved', async (t) => { - const { github } = await octokitFor(repositoryWith({ merges: [MERGE.moved] })) + it('names only the base as unchanged when there is no develop branch', async (t) => { + const { github } = await octokitFor(repositoryWith({ branches: ['release/M26.test', 'main'], merges: [MERGE.conflict] })) + const { core, logged } = await coreWithOutputs(t) + + await assert.rejects(runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }), /Nothing was merged: main is as it was\./) + assert.match(logged(), /^MERGE_CONFLICT: .*Nothing was merged: main is as it was\.$/m) + }) + + it('fails without the marker when GitHub refuses a merge for any other reason', async (t) => { + const { github, requests } = await octokitFor(repositoryWith({ merges: [MERGE.moved] })) const { core, outputs, logged } = await coreWithOutputs(t) await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /main moved while the merge was being made.*Base branch was modified.*Run this step again/s + /Merging release\/M26\.test into main failed \(HTTP 409\): Base branch was modified.*Nothing was merged: main and develop are as they were\./s ) assert.doesNotMatch(logged(), /MERGE_CONFLICT/) assert.strictEqual(outputs().conflict, undefined) + assert.deepStrictEqual(written(requests).at(-1), `DELETE ${API.ref(SCRATCH.main)}`) + }) + + it('fails with neither branch moved, and says to run again, when a branch moved while the merges were made', async (t) => { + // A commit reached main after its tip was read. The merge commit was made on the old tip, so + // GitHub refuses the update, and with it the one for develop. + const { github, requests } = await octokitFor(repositoryWith({ move: MOVE.refused })) + const { core, outputs, logged } = await coreWithOutputs(t) + + await assert.rejects( + runMerge({ github, core, env: mergeEnv() }), + /GitHub refused to move main and develop together.*Ref refs\/heads\/main is at 9999, not at bbbb.*Nothing was merged: main and develop are as they were\..*Run this step again/s + ) + + // Not a conflict for a person to resolve: no marker, so the dashboard shows a failure to retry. + assert.doesNotMatch(logged(), /MERGE_CONFLICT/) + assert.deepStrictEqual(outputs(), {}) + assert.strictEqual(movesAsked(requests).length, 1, 'the update is asked for once, not again') + assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(SCRATCH.main)}`, `DELETE ${API.ref(SCRATCH.develop)}`]) + }) + + it('fails before any merge when the scratch branch cannot be created', async (t) => { + const { github, requests } = await octokitFor( + repositoryWith({ scratch: { status: 403, body: { message: 'Resource not accessible by integration' } } }) + ) + const { core, outputs } = await coreWithOutputs(t) + + await assert.rejects( + runMerge({ github, core, env: mergeEnv() }), + /Creating the scratch branch release-merge\/77-1\/main failed \(HTTP 403\): Resource not accessible by integration.*Nothing was merged: main and develop are as they were\./s + ) + + // Nothing was created, so there is nothing to merge on and nothing to delete. + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`]) + assert.deepStrictEqual(outputs(), {}) + }) + + it('warns and still succeeds when a scratch branch cannot be deleted after the branches moved', async (t) => { + const { github, requests } = await octokitFor(repositoryWith({ cleanup: { status: 500, body: { message: 'boom' } } })) + const { core, outputs, logged } = await coreWithOutputs(t) + + await runMerge({ github, core, env: mergeEnv() }) + + // Both deletions are tried, and the release is merged whatever they answer. + assert.match(logged(), /::warning::.*scratch branch release-merge\/77-1\/main.*boom.*Delete it by hand/) + assert.match(logged(), /::warning::.*scratch branch release-merge\/77-1\/develop/) + assert.strictEqual(movesAsked(requests).length, 1) + assert.strictEqual(outputs()['base-sha'], MERGED.main) }) it('refuses a version the branch does not hold, naming the file, before merging anything', async (t) => { From 1fc947ee92b47c7793d09ca9d40f70bc122faf38 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 2 Dec 2026 19:42:59 -0300 Subject: [PATCH 02/13] release-merge: say what the branches hold when the update fails GitHub answers an update refused for a branch that moved with "Something went wrong while executing your query", the same text for any failure, and a request that got no answer may have been applied. The message said "Nothing was merged" and "Run this step again" for all of them, which is wrong for an update that was applied and useless for one a rule refuses. The step now reads the two branches again after a failed update and says what it found: the branch that received a commit, a refusal with both branches where they were, or an update that was applied, which it takes as done. The MERGE_CONFLICT line of the merge back also names the second merge by hand. A conflict with a commit only the base has is not found by merging the release branch into develop, and the next run stopped on the same line. The update fixtures are the answers GitHub gave to real updates on a test plugin. Two failure paths of the scratch branches get a test, and the README links the mutation at its current address. SOFT-5829 --- .github/actions/release-merge/README.md | 56 ++++-- .../actions/release-merge/release-merge.js | 137 +++++++++++-- .../release-merge/release-merge.test.js | 186 +++++++++++++++++- 3 files changed, 340 insertions(+), 39 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 5da101d9..2abf07af 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -76,11 +76,22 @@ tag and no build, and a hotfix branch cut from the base in that window would shi So each merge is made on a scratch branch, created at the tip the action read: `release-merge/-/main` and `release-merge/-/develop`. Only when both merge commits exist do the branches move, with the GraphQL mutation -[`updateRefs`](https://docs.github.com/en/graphql/reference/mutations#updaterefs), which applies +[`updateRefs`](https://docs.github.com/en/graphql/reference/git#mutation-updaterefs), which applies every update or none. Each branch moves from the tip its merge was made on to the merge commit, a fast-forward. A branch that received a commit in the meantime is no longer at that tip: GitHub refuses the update, neither branch moves, and the step fails with a message to run it again. +GitHub's answer to an update it does not make gives no reason for a branch that moved, so the step +reads the two branches again before it fails, and the message says what it found: + +- A branch received a commit. The message names it. Run the step again. +- Both branches are where they were. A rule or the token refused the update, and GitHub's answer + names it, such as a base branch that only takes pull requests. Change that first. +- Both branches hold their merge commits. The request failed after GitHub applied it: the step + warns and goes on. +- One of them could not be read, or holds its merge commit while the other does not. The message + says what each one holds, to check by hand. + The scratch branches are deleted when the step ends, whatever it did. One that could not be deleted is a warning in the log; a run cancelled in the few seconds they exist can leave them too. Nothing reads them, and they can be deleted by hand. @@ -101,7 +112,8 @@ does: that is, when it was published from this branch. See [Versions it refuses](#versions-it-refuses). release-publish then skips the release. - A conflict resolved by hand, then pushed, is not found again: the branch then holds the release - branch, and the next run goes on from there. + branch, and the next run goes on from there. The one case that takes a second merge by hand is in + [When a merge conflicts](#when-a-merge-conflicts). - A release that is on the base and not on develop, which a deploy stopped by a conflict left before the two merges were written together, gets the merge it lacks: only develop moves. @@ -118,16 +130,20 @@ To resolve it, merge the release branch into the branch named, by hand, resolve and run the step again. That is the release branch for a conflict on develop too: the base does not hold the release yet, so there is nothing else to merge from. The next run finds the release branch in the branch resolved by hand, with nothing left to conflict on. The API does not say which -files conflict; the merge done by hand shows them. If the run after that conflicts on develop -again, the conflict is with a commit the base has and develop lacks: merge the base into develop as -well. +files conflict; the merge done by hand shows them. + +A conflict on develop can also be with a commit the base has and develop lacks, a hotfix that never +went back to develop. Merging the release branch into develop then shows no conflict, and the next +run stops on develop again: merge the base into develop as well, resolve, push, and run the step +again. The line for develop says both steps. The log carries this line, on one line: ``` MERGE_CONFLICT: merging main into develop conflicted. Merge release/M26.test into develop by hand, -resolve the conflict and push, then run this step again. Nothing was merged: main and develop are -as they were. +resolve the conflict and push, then run this step again. If develop already holds release/M26.test, +the conflict is with a commit of main that develop lacks: merge main into develop as well. Nothing +was merged: main and develop are as they were. ``` and the `conflict` output names the branch. The [release @@ -136,9 +152,10 @@ marker `MERGE_CONFLICT` at the start of an output line of the job log, every lin runner prefixes with a timestamp. The marker is printed from the script, not written in the workflow, so it is not in the step's echoed inputs and only appears when a merge did conflict. -A branch that moved while the merges were being made fails the step without the marker, with -neither branch written; running the step again is enough. So does a merge GitHub refuses for any -other reason. +A failure that is not a conflict has no marker and writes neither branch: a branch that received a +commit while the merges were being made, an update a rule refuses, or a merge GitHub refuses for any +other reason. The message says which, and whether running the step again is enough; see +[Both merges or neither](#both-merges-or-neither). ## Versions it refuses @@ -248,21 +265,22 @@ branch that only accepts pull requests refuses the update, with any token that i bypass list, and so does a rule that restricts who creates branches, for the scratch ones; a rule that requires signed commits is satisfied, since GitHub signs the merge commits it makes. GiveWP's rules are the reference shape: the branches are protected from deletion and non-fast-forward pushes, -and `github.token` writes the merges. What `github.token` writes starts no workflow run. Another -token does, so a workflow that runs on every pushed branch would run on the scratch branches for -the seconds they exist; leave `release-merge/**` out of its branches then. The human gate is not the -token: the caller puts the job in an environment with required reviewers, and -the release branch reached this step after its review and QA approval. A repository whose rules -still need another actor should pass a GitHub App installation token minted in the run rather than -a personal access token. What the token needs for the tag is in +and `github.token` writes the merges. It also writes a release that changes files under +`.github/workflows/`: the scratch branches and the update take them. What `github.token` writes +starts no workflow run. Another token does, so a workflow that runs on every pushed branch would +run on the scratch branches for the seconds they exist; leave `release-merge/**` out of its +branches then. The human gate is not the token: the caller puts the job in an environment with +required reviewers, and the release branch reached this step after its review and QA approval. A +repository whose rules still need another actor should pass a GitHub App installation token minted +in the run rather than a personal access token. What the token needs for the tag is in [release-publish → The token](../release-publish/README.md#the-token). ## How it runs One [actions/github-script](https://github.com/actions/github-script) step runs [`release-merge.js`](./release-merge.js): the checks, the merges and the update that moves the two -branches, all through github-script's Octokit. The inputs reach the script through environment variables, never interpolated into it, so -a crafted value is data rather than part of the program. +branches, all through github-script's Octokit. The inputs reach the script through environment +variables, never interpolated into it, so a crafted value is data rather than part of the program. The tests, `node --test` from the repository root, run the action against a recorded client and assert the requests it sends and the ones it does not; no git and no network. diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index 76505493..a48ab960 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -435,7 +435,15 @@ async function mergeInto({ github, core, owner, repo, into, scratch, tip, head, response = await github.rest.repos.merge({ owner, repo, base: scratch, head, commit_message: message }) } catch (error) { if (isConflict(error)) { - const resolve = `Merge ${branch} into ${into} by hand, resolve the conflict and push, then run this step again.` + // The merge back takes the base, not the release branch, so it can also conflict with a + // commit that only the base has. Merging the release branch by hand shows no conflict then, + // and the next run stops on this line again: the second sentence is the way out. + const resolve = + `Merge ${branch} into ${into} by hand, resolve the conflict and push, then run this step again.` + + (label === branch + ? '' + : ` If ${into} already holds ${branch}, the conflict is with a commit of ${label} that ${into} lacks: ` + + `merge ${label} into ${into} as well.`) // core.info prints a plain line: the marker has to open an output line to count, see the README. core.setOutput('conflict', into) @@ -455,13 +463,65 @@ async function mergeInto({ github, core, owner, repo, into, scratch, tip, head, return { merged: true, sha: response.data.sha } } -/** The mutation that moves several branches at once: GitHub applies every update or none. */ +/** + * The mutation that moves several branches at once: GitHub applies every update or none. The REST + * API moves one ref per request, so two requests could move the base and then be refused for + * develop, which is the state the scratch branches exist to avoid. + */ const UPDATE_REFS = `mutation ($input: UpdateRefsInput!) { updateRefs(input: $input) { clientMutationId } }` +/** + * What GitHub answered to a failed update, on one line. + * + * A refusal comes as GraphQL errors, which Octokit joins with a line break before each. A request + * that got no usable answer has a status and a message instead. + * + * @param {Error} error The Octokit error. + * + * @returns {string} The answer. + */ +function failureOf(error) { + if (Array.isArray(error?.errors)) { + return error.errors.map(({ message }) => String(message).replace(/\.+$/, '')).join('; ') + } + + return `HTTP ${error?.status}: ${error?.message}` +} + +/** + * Reads where the branches of a failed update are now. + * + * GitHub's answer does not say: a branch that moved gets the same "Something went wrong" as a + * failure on GitHub's side, and a request that got no answer may have been applied. + * + * @param {object} options + * @param {object} options.github The Octokit github-script supplies. + * @param {string} options.owner The repository owner. + * @param {string} options.repo The repository name. + * @param {object[]} options.moves `{branch, from, to}` per branch. + * + * @returns {Promise} Each move with `now`: the branch's tip, or null when it could not + * be read. + */ +async function tipsAfter({ github, owner, repo, moves }) { + const tips = [] + for (const move of moves) { + let now = null + try { + now = (await github.rest.repos.getBranch({ owner, repo, branch: move.branch })).data.commit.sha + } catch { + // Left as null: the message says the branch could not be read. + } + tips.push({ ...move, now }) + } + + return tips +} + /** * Moves the base and develop to their merge commits in one update, so the remote never holds one * merge without the other. @@ -470,19 +530,25 @@ const UPDATE_REFS = `mutation ($input: UpdateRefsInput!) { * is refused, and GitHub then moves none of them; the merge commit descends from the tip, so the * move is a fast-forward and never needs to be forced. * + * When the update fails, the branches are read again and the message says what they hold: a + * failure only states what was checked, and each case asks something different of the reader. + * * @param {object} options * @param {object} options.github The Octokit github-script supplies. + * @param {object} options.core The @actions/core toolkit. * @param {string} options.owner The repository owner. * @param {string} options.repo The repository name. * @param {object[]} options.moves `{branch, from, to}` per branch: the tip it was read at and * the merge commit made on that tip. * @param {string} options.unchanged What to close the message with: the branches that were not written. * - * @throws {Error} When the repository cannot be read, or GitHub refuses the update. + * @throws {Error} When the repository cannot be read, a branch received a commit meanwhile, GitHub + * does not move the branches, or the update failed and what they hold cannot be confirmed. * - * @returns {Promise} Resolves once the branches are moved. + * @returns {Promise} Resolves once the branches are moved, also when the request failed and + * the branches hold their merge commits all the same. */ -async function moveBranches({ github, owner, repo, moves, unchanged }) { +async function moveBranches({ github, core, owner, repo, moves, unchanged }) { let repositoryId try { repositoryId = (await github.rest.repos.get({ owner, repo })).data.node_id @@ -491,17 +557,63 @@ async function moveBranches({ github, owner, repo, moves, unchanged }) { } const refUpdates = moves.map(({ branch, from, to }) => ({ name: `refs/heads/${branch}`, beforeOid: from, afterOid: to })) + let failure try { await github.graphql(UPDATE_REFS, { input: { repositoryId, refUpdates } }) + + return } catch (error) { - const names = moves.map(({ branch }) => branch) + failure = failureOf(error) + } + + const names = moves.map(({ branch }) => branch) + const both = `${names.join(' and ')}${names.length > 1 ? ' together' : ''}` + const short = (sha) => sha.slice(0, 7) + const tips = await tipsAfter({ github, owner, repo, moves }) + + // The request failed and the update went through: a connection lost after GitHub applied it. + if (tips.every(({ now, to }) => now === to)) { + core.warning( + `Moving ${both} failed (${failure}), but the update was applied: ` + + `${names.length > 1 ? `${names.join(' and ')} hold their merge commits` : `${names[0]} holds its merge commit`}.` + ) + + return + } + + // A branch that could not be read, or one at its merge commit while another is not. + if (tips.some(({ now, to }) => now === null || now === to)) { + const found = tips.map(({ branch, now, to }) => { + if (now === null) { + return `${branch} could not be read` + } + + return now === to ? `${branch} holds its merge commit` : `${branch} is at ${short(now)}` + }) throw new Error( - `GitHub refused to move ${names.join(' and ')}${names.length > 1 ? ' together' : ''}: ${error?.message}. ` + - `${unchanged} A branch that received a commit while the merges were being made is refused ` + - 'this way. Run this step again.' + `Moving ${both} failed (${failure}), and what the branches hold now could not be confirmed: ` + + `${found.join(', ')}. Check them before running this step again.` ) } + + const moved = tips.filter(({ now, from }) => now !== from) + if (moved.length > 0) { + const which = moved + .map(({ branch, from, now }) => `${branch} (at ${short(now)}, the merge was made on ${short(from)})`) + .join(' and ') + + throw new Error( + `${which} received a commit while the merges were being made, so GitHub moved ` + + `${names.length > 1 ? 'neither branch' : 'nothing'} (${failure}). Nothing was merged. Run this step again.` + ) + } + + throw new Error( + `GitHub did not move ${both}: ${failure}. ${unchanged} No commit reached ` + + `${names.length > 1 ? 'them' : 'it'} while the merges were being made: when the answer names a rule or a ` + + 'permission, change that before running this step again.' + ) } /** @@ -587,7 +699,8 @@ async function runMerge({ github, core, env }) { } // Until the move at the end, the only branches written are the scratch ones, named after the - // run so that no two runs share one. Every failure from here on closes with this sentence. + // run so that no two runs share one. A failure from here on that found the base and develop where + // they were closes with this sentence. const unchanged = `Nothing was merged: ${develop === null ? `${base} is as it was` : `${base} and ${develop} are as they were`}.` const scratchPrefix = `release-merge/${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` const scratch = [] @@ -630,7 +743,7 @@ async function runMerge({ github, core, env }) { `${base} already had ${branch} and has moved on since: its tip ${baseTip} is not the commit that merged ` + `${branch}, so a build from it would publish other changes under ${version}. There is nothing left ` + `to merge for ${version}; finish it by hand at the commit that merged ${branch} into ${base}, or ` + - 'release another version. Nothing was merged.' + `release another version. ${unchanged}` ) } } @@ -658,7 +771,7 @@ async function runMerge({ github, core, env }) { // Both merges exist: the branches move to them now, together. A retry that found a merge done // has nothing to move for that branch. if (moves.length > 0) { - await moveBranches({ github, owner, repo, moves, unchanged }) + await moveBranches({ github, core, owner, repo, moves, unchanged }) } } finally { // After the move, not before: until then the scratch branches are what keeps the merge commits. diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index bf868f38..7a847db2 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -168,10 +168,59 @@ function merges(responses) { return { method: 'POST', path: API.merges, responses } } -/** What the mutation that moves the branches answers: done, or refused with nothing moved. */ +/** + * What the mutation that moves the branches answers, as GitHub sent each one to a real update: done, + * a branch that is no longer at the tip it was read at, a base that only takes pull requests, and a + * request that got no usable answer. The second names neither the branch nor the reason. + */ const MOVE = { done: { body: { data: { updateRefs: { clientMutationId: null } } } }, - refused: { body: { data: { updateRefs: null }, errors: [{ type: 'UNPROCESSABLE', message: 'Ref refs/heads/main is at 9999, not at bbbb' }] } }, + moved: { + body: { + errors: [ + { + message: + 'Something went wrong while executing your query on 2026-12-02T22:36:20Z. Please include `0F82:23A134:76FFF:C508E:6B109D62` when reporting this issue.', + }, + ], + }, + }, + protected: { + body: { + data: { updateRefs: null }, + errors: [ + { + type: 'UNPROCESSABLE', + path: ['updateRefs'], + locations: [{ line: 2, column: 3 }], + message: "Branch 'refs/heads/main' is protected: Changes must be made through a pull request..", + }, + ], + }, + }, + unanswered: { status: 502, body: { message: 'Server Error' } }, +} + +/** + * Makes a branch answer differently from its second read on: what the action finds when it reads + * the branches again after an update that failed. + * + * @param {object[]} rules The rules, with one per branch as `branches` builds them. + * @param {object} later Branch name to the tip it is at by then, or to a whole response. + * + * @returns {object[]} The rules, with those branches answering their first read as before. + */ +function readAgain(rules, later) { + return rules.map((rule) => { + const name = Object.keys(later).find((branch) => rule.method === 'GET' && rule.path === API.branch(branch)) + if (name === undefined) { + return rule + } + + const second = typeof later[name] === 'string' ? { body: { name, commit: { sha: later[name] } } } : later[name] + + return { method: rule.method, path: rule.path, responses: [{ body: rule.body }, second] } + }) } /** @@ -749,6 +798,8 @@ describe('release-merge', () => { // The marker opens a line of its own: that is what the dashboard's detector anchors on. assert.match(logged(), /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand.*main and develop are as they were\.$/m) + // The release branch itself is what was merged, so there is no other branch to merge by hand. + assert.doesNotMatch(logged(), /already holds/) assert.strictEqual(outputs().conflict, 'main') // Develop was never tried, and the one scratch branch is gone. assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(SCRATCH.main)}`]) @@ -760,10 +811,15 @@ describe('release-merge', () => { await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /main into develop stopped on a conflict\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. Nothing was merged: main and develop are as they were\./ + /main into develop stopped on a conflict\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. If develop already holds release\/M26\.test, the conflict is with a commit of main that develop lacks: merge main into develop as well\. Nothing was merged: main and develop are as they were\./ ) - assert.match(logged(), /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into develop by hand.*main and develop are as they were\.$/m) + // The merge back can conflict with a commit only main has. Merging the release branch by hand + // shows no conflict then, so the line also says what to do when that was already done. + assert.match( + logged(), + /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. If develop already holds release\/M26\.test, the conflict is with a commit of main that develop lacks: merge main into develop as well\. Nothing was merged: main and develop are as they were\.$/m + ) assert.strictEqual(outputs().conflict, 'develop') // The merge for main was made, on its scratch branch only: main is never asked to move, so // the release is not on it while develop waits for a person. @@ -794,24 +850,138 @@ describe('release-merge', () => { assert.deepStrictEqual(written(requests).at(-1), `DELETE ${API.ref(SCRATCH.main)}`) }) - it('fails with neither branch moved, and says to run again, when a branch moved while the merges were made', async (t) => { + it('fails with neither branch moved, naming the branch that received a commit, and says to run again', async (t) => { // A commit reached main after its tip was read. The merge commit was made on the old tip, so - // GitHub refuses the update, and with it the one for develop. - const { github, requests } = await octokitFor(repositoryWith({ move: MOVE.refused })) + // GitHub refuses the update, and with it the one for develop. Its answer names no branch and + // no reason, so the action reads the two again to say which one moved. + const later = '9999999999999999999999999999999999999999' + const { github, requests } = await octokitFor(readAgain(repositoryWith({ move: MOVE.moved }), { main: later })) const { core, outputs, logged } = await coreWithOutputs(t) await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /GitHub refused to move main and develop together.*Ref refs\/heads\/main is at 9999, not at bbbb.*Nothing was merged: main and develop are as they were\..*Run this step again/s + /^Error: main \(at 9999999, the merge was made on bbbbbbb\) received a commit while the merges were being made, so GitHub moved neither branch \(Something went wrong while executing your query.*when reporting this issue\)\. Nothing was merged\. Run this step again\.$/ ) // Not a conflict for a person to resolve: no marker, so the dashboard shows a failure to retry. assert.doesNotMatch(logged(), /MERGE_CONFLICT/) assert.deepStrictEqual(outputs(), {}) assert.strictEqual(movesAsked(requests).length, 1, 'the update is asked for once, not again') + assert.deepStrictEqual(requested(requests).slice(-5), [ + `POST ${API.graphql}`, + `GET ${API.branch('main')}`, + `GET ${API.branch('develop')}`, + `DELETE ${API.ref(SCRATCH.main)}`, + `DELETE ${API.ref(SCRATCH.develop)}`, + ]) + }) + + it('says to change the rule, not to run again, when GitHub does not move the branches and neither received a commit', async (t) => { + // A base that only takes pull requests: the merges are made on the scratch branches, and the + // update is where the rule answers. Running the step again would get the same answer. + const { github, requests } = await octokitFor(repositoryWith({ move: MOVE.protected })) + const { core, outputs, logged } = await coreWithOutputs(t) + + await assert.rejects( + runMerge({ github, core, env: mergeEnv() }), + /^Error: GitHub did not move main and develop together: Branch 'refs\/heads\/main' is protected: Changes must be made through a pull request\. Nothing was merged: main and develop are as they were\. No commit reached them while the merges were being made: when the answer names a rule or a permission, change that before running this step again\.$/ + ) + + assert.doesNotMatch(logged(), /MERGE_CONFLICT/) + assert.deepStrictEqual(outputs(), {}) + assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(SCRATCH.main)}`, `DELETE ${API.ref(SCRATCH.develop)}`]) + }) + + it('goes on with a warning when the request for the update failed and the branches hold their merge commits', async (t) => { + // The connection dropped after GitHub applied the update. Read again, both branches are at + // their merge commits: the release is merged, and the caller builds it. + const { github, requests } = await octokitFor( + readAgain(repositoryWith({ move: MOVE.unanswered }), { main: MERGED.main, develop: MERGED.develop }) + ) + const { core, outputs, logged } = await coreWithOutputs(t) + + await runMerge({ github, core, env: mergeEnv() }) + + assert.match( + logged(), + /::warning::Moving main and develop together failed \(HTTP 502: Server Error\), but the update was applied: main and develop hold their merge commits\./ + ) + assert.strictEqual(movesAsked(requests).length, 1) + assert.strictEqual(outputs()['base-sha'], MERGED.main) + assert.strictEqual(outputs().conflict, '') assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(SCRATCH.main)}`, `DELETE ${API.ref(SCRATCH.develop)}`]) }) + it('says nothing was merged when the request for the update failed and the branch is where it was', async (t) => { + const { github } = await octokitFor( + repositoryWith({ branches: ['release/M26.test', 'main'], merges: [MERGE.main], move: MOVE.unanswered }) + ) + const { core, outputs } = await coreWithOutputs(t) + + await assert.rejects( + runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }), + /^Error: GitHub did not move main: HTTP 502: Server Error\. Nothing was merged: main is as it was\. No commit reached it while the merges were being made/ + ) + assert.deepStrictEqual(outputs(), {}) + }) + + it('says what each branch holds, and not that nothing was merged, when a failed update cannot be confirmed', async (t) => { + // No answer to the update, and develop cannot be read afterwards: main holds the release and + // nobody knows about develop. The message must not claim either way. + const { github } = await octokitFor( + readAgain(repositoryWith({ move: MOVE.unanswered }), { main: MERGED.main, develop: { status: 500, body: { message: 'boom' } } }) + ) + const { core, outputs } = await coreWithOutputs(t) + + await assert.rejects(runMerge({ github, core, env: mergeEnv() }), (error) => { + assert.match( + error.message, + /^Moving main and develop together failed \(HTTP 502: Server Error\), and what the branches hold now could not be confirmed: main holds its merge commit, develop could not be read\. Check them before running this step again\.$/ + ) + assert.doesNotMatch(error.message, /Nothing was merged/) + + return true + }) + assert.deepStrictEqual(outputs(), {}) + }) + + it('fails before the update when the repository cannot be read, with both scratch branches deleted', async (t) => { + const { github, requests } = await octokitFor( + repositoryWith().map((rule) => (rule.path === API.repository ? { ...rule, status: 500, body: { message: 'boom' } } : rule)) + ) + const { core, outputs } = await coreWithOutputs(t) + + await assert.rejects( + runMerge({ github, core, env: mergeEnv() }), + /Reading stellarwp\/plugin failed \(HTTP 500\): boom\. Nothing was merged: main and develop are as they were\./ + ) + + // Both merges were made, on the scratch branches only: no update is asked for. + assert.deepStrictEqual(written(requests), SCRATCH_ONLY) + assert.deepStrictEqual(movesAsked(requests), []) + assert.deepStrictEqual(outputs(), {}) + }) + + it('deletes the first scratch branch when the second one cannot be created', async (t) => { + // By then the first one holds the merge commit for main: left behind, it would be a branch + // with the release merged and nothing pointing at why. + const { github, requests } = await octokitFor( + repositoryWith({ + scratch: { responses: [{ status: 201, body: {} }, { status: 422, body: { message: 'Reference already exists' } }] }, + }) + ) + const { core, outputs } = await coreWithOutputs(t) + + await assert.rejects( + runMerge({ github, core, env: mergeEnv() }), + /Creating the scratch branch release-merge\/77-1\/develop failed \(HTTP 422\): Reference already exists.*Nothing was merged: main and develop are as they were\./s + ) + + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.refs}`, `DELETE ${API.ref(SCRATCH.main)}`]) + assert.deepStrictEqual(movesAsked(requests), []) + assert.deepStrictEqual(outputs(), {}) + }) + it('fails before any merge when the scratch branch cannot be created', async (t) => { const { github, requests } = await octokitFor( repositoryWith({ scratch: { status: 403, body: { message: 'Resource not accessible by integration' } } }) From 5aaad048be85b99f7295c370e9b635a48b65e561 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 10:01:16 -0300 Subject: [PATCH 03/13] release-merge: say that the name is taken when a scratch branch exists GitHub answers "Reference already exists" when the name of a scratch branch is taken, by a branch another run of the step left under the same run and attempt. The message then said the token has to be allowed to create the branch, which sends the reader to the wrong place. It now says to delete the branch and run the step again. Any other answer keeps the general sentence, with both causes GitHub reports as "Reference update failed": a rule that restricts who creates branches, and a branch named release-merge, which leaves no room for a branch under that name. The plural of the messages in moveBranches is decided once. SOFT-5829 --- .github/actions/release-merge/README.md | 3 +- .../actions/release-merge/release-merge.js | 25 ++++++++--- .../release-merge/release-merge.test.js | 41 +++++++++++-------- 3 files changed, 45 insertions(+), 24 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 2abf07af..6129b456 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -94,7 +94,8 @@ reads the two branches again before it fails, and the message says what it found The scratch branches are deleted when the step ends, whatever it did. One that could not be deleted is a warning in the log; a run cancelled in the few seconds they exist can leave them too. Nothing -reads them, and they can be deleted by hand. +reads them, and they can be deleted by hand. A repository with a branch named `release-merge` cannot +hold them, since git keeps no branch under the name of another: the step fails before any merge. The action reads and writes no checkout: it needs none before it, and leaves whatever checkout the job has as it was. Check out `base-sha` after it for the steps that build the release. diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index a48ab960..3d89c7c9 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -364,9 +364,21 @@ async function createScratch({ github, owner, repo, name, sha, unchanged }) { try { await github.rest.git.createRef({ owner, repo, ref: `refs/heads/${name}`, sha }) } catch (error) { + const failed = `Creating the scratch branch ${name} failed (HTTP ${error?.status}): ${error?.message}.` + + // "Reference already exists" is GitHub's answer for a name that is taken, which a token or a + // rule has nothing to do with. Any other answer, "Reference update failed" for a rule that + // restricts who creates branches and for a branch named release-merge alike, gets both causes. + if (error?.status === 422 && /already exists/i.test(String(error?.message))) { + throw new Error( + `${failed} A branch of that name is left from another run of this step: delete it, then run this ` + + `step again. ${unchanged}` + ) + } + throw new Error( - `Creating the scratch branch ${name} failed (HTTP ${error?.status}): ${error?.message}. Each merge is ` + - `tried on a scratch branch first, so the token has to be allowed to create and delete it. ${unchanged}` + `${failed} Each merge is tried on a scratch branch first, so the token and the repository's rules have ` + + `to allow creating and deleting it, and the repository cannot have a branch named release-merge. ${unchanged}` ) } } @@ -567,7 +579,8 @@ async function moveBranches({ github, core, owner, repo, moves, unchanged }) { } const names = moves.map(({ branch }) => branch) - const both = `${names.join(' and ')}${names.length > 1 ? ' together' : ''}` + const several = names.length > 1 + const both = `${names.join(' and ')}${several ? ' together' : ''}` const short = (sha) => sha.slice(0, 7) const tips = await tipsAfter({ github, owner, repo, moves }) @@ -575,7 +588,7 @@ async function moveBranches({ github, core, owner, repo, moves, unchanged }) { if (tips.every(({ now, to }) => now === to)) { core.warning( `Moving ${both} failed (${failure}), but the update was applied: ` + - `${names.length > 1 ? `${names.join(' and ')} hold their merge commits` : `${names[0]} holds its merge commit`}.` + `${several ? `${names.join(' and ')} hold their merge commits` : `${names[0]} holds its merge commit`}.` ) return @@ -605,13 +618,13 @@ async function moveBranches({ github, core, owner, repo, moves, unchanged }) { throw new Error( `${which} received a commit while the merges were being made, so GitHub moved ` + - `${names.length > 1 ? 'neither branch' : 'nothing'} (${failure}). Nothing was merged. Run this step again.` + `${several ? 'neither branch' : 'nothing'} (${failure}). Nothing was merged. Run this step again.` ) } throw new Error( `GitHub did not move ${both}: ${failure}. ${unchanged} No commit reached ` + - `${names.length > 1 ? 'them' : 'it'} while the merges were being made: when the answer names a rule or a ` + + `${several ? 'them' : 'it'} while the merges were being made: when the answer names a rule or a ` + 'permission, change that before running this step again.' ) } diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index 7a847db2..2c524d72 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -962,39 +962,46 @@ describe('release-merge', () => { assert.deepStrictEqual(outputs(), {}) }) - it('deletes the first scratch branch when the second one cannot be created', async (t) => { - // By then the first one holds the merge commit for main: left behind, it would be a branch - // with the release merged and nothing pointing at why. + it('fails before any merge when the scratch branch cannot be created, naming the token, the rules and a branch in the way', async (t) => { + // What GitHub answers when a ruleset restricts who creates branches, and also when a branch + // named release-merge is in the way: the same words for both, so the message gives both causes. const { github, requests } = await octokitFor( - repositoryWith({ - scratch: { responses: [{ status: 201, body: {} }, { status: 422, body: { message: 'Reference already exists' } }] }, - }) + repositoryWith({ scratch: { status: 422, body: { message: 'Reference update failed' } } }) ) const { core, outputs } = await coreWithOutputs(t) await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /Creating the scratch branch release-merge\/77-1\/develop failed \(HTTP 422\): Reference already exists.*Nothing was merged: main and develop are as they were\./s + /Creating the scratch branch release-merge\/77-1\/main failed \(HTTP 422\): Reference update failed\. Each merge is tried on a scratch branch first, so the token and the repository's rules have to allow creating and deleting it, and the repository cannot have a branch named release-merge\. Nothing was merged: main and develop are as they were\./ ) - assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.refs}`, `DELETE ${API.ref(SCRATCH.main)}`]) - assert.deepStrictEqual(movesAsked(requests), []) + // Nothing was created, so there is nothing to merge on and nothing to delete. + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`]) assert.deepStrictEqual(outputs(), {}) }) - it('fails before any merge when the scratch branch cannot be created', async (t) => { + it('says to delete the branch, not to change the token, when a scratch branch of that name already exists', async (t) => { + // A branch another run of this step left behind under the same run and attempt. The second + // one is refused here, so the first, which holds the merge commit for main by then, is deleted. const { github, requests } = await octokitFor( - repositoryWith({ scratch: { status: 403, body: { message: 'Resource not accessible by integration' } } }) + repositoryWith({ + scratch: { responses: [{ status: 201, body: {} }, { status: 422, body: { message: 'Reference already exists' } }] }, + }) ) const { core, outputs } = await coreWithOutputs(t) - await assert.rejects( - runMerge({ github, core, env: mergeEnv() }), - /Creating the scratch branch release-merge\/77-1\/main failed \(HTTP 403\): Resource not accessible by integration.*Nothing was merged: main and develop are as they were\./s - ) + await assert.rejects(runMerge({ github, core, env: mergeEnv() }), (error) => { + assert.match( + error.message, + /^Creating the scratch branch release-merge\/77-1\/develop failed \(HTTP 422\): Reference already exists\. A branch of that name is left from another run of this step: delete it, then run this step again\. Nothing was merged: main and develop are as they were\.$/ + ) + assert.doesNotMatch(error.message, /token/) - // Nothing was created, so there is nothing to merge on and nothing to delete. - assert.deepStrictEqual(written(requests), [`POST ${API.refs}`]) + return true + }) + + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.refs}`, `DELETE ${API.ref(SCRATCH.main)}`]) + assert.deepStrictEqual(movesAsked(requests), []) assert.deepStrictEqual(outputs(), {}) }) From 58f01dc98e8197565fcc041e1f04669aeaa90eec Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 10:03:26 -0300 Subject: [PATCH 04/13] release-merge: make no scratch branch when there is nothing to merge A retry after a late failure finds the release on the base and on develop. It still created a scratch branch for each, asked for a merge that answered 204, and deleted the branches: four writes to learn there was nothing to do, on the run the README describes as writing nothing. The action now asks GitHub whether the tip already holds what is merged before it makes the scratch branch, with the comparison it already uses for the tag. A run that finds both branches that way only reads. The merge back is not asked about a commit this run has just made, since no branch holds that one yet. SOFT-5829 --- .github/actions/release-merge/README.md | 9 +- .../actions/release-merge/release-merge.js | 27 +++-- .../release-merge/release-merge.test.js | 101 ++++++++++++------ 3 files changed, 94 insertions(+), 43 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 6129b456..093a555b 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -105,10 +105,11 @@ job has as it was. Check out `base-sha` after it for the steps that build the re Every step is safe to run again after a late failure, which is what a retry from a release tool does: -- A branch that already holds the other merges as nothing to merge: GitHub answers that the base - already contains the head, and nothing is written. What the caller then builds and publishes is - the commit the tag points at, or the base's tip when there is no tag yet, which has to be the - commit that merged the branch; see [Versions it refuses](#versions-it-refuses). +- A branch that already holds the other has nothing to merge. The action asks GitHub that before it + makes the scratch branch, so a run that finds both branches that way writes nothing: no scratch + branch, no merge, no update. What the caller then builds and publishes is the commit the tag + points at, or the base's tip when there is no tag yet, which has to be the commit that merged + the branch; see [Versions it refuses](#versions-it-refuses). - A version already published counts as a retry only when its tag contains the release branch, that is, when it was published from this branch. See [Versions it refuses](#versions-it-refuses). release-publish then skips the release. diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index 3d89c7c9..845d4422 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -12,7 +12,7 @@ * writes no checkout, so a test runs it against a recorded client and asserts the requests. * * Every step is safe to run again after a late failure: a branch that already holds the release - * merges as a no-op. + * has nothing to merge, and a run that finds both that way writes nothing, scratch branches included. */ const { validateVersion: validateReleaseVersion } = require('../../lib/versions.js') @@ -289,7 +289,8 @@ async function assertBranchHoldsVersion({ github, owner, repo, branch, ref, vers } /** - * Whether a commit contains a branch: the tag of a published version, against the release branch. + * Whether a commit contains a branch: the tag of a published version against the release branch, + * and the tip of the base or of develop against what is about to be merged into it. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. @@ -415,10 +416,10 @@ async function deleteScratch({ github, core, owner, repo, names }) { * Makes one merge commit, on a scratch branch at the tip of the branch the commit is for. That * branch is not written here: moveBranches moves it to the commit once every merge is made. * - * GitHub makes the merge commit under the token's identity and signs it. A tip that already holds - * the head answers 204 and nothing is written, which is what a retry after a late failure finds. A - * conflict prints the MERGE_CONFLICT marker on a line of its own, where the release dashboard looks - * for it, and fails. + * GitHub makes the merge commit under the token's identity and signs it. A conflict prints the + * MERGE_CONFLICT marker on a line of its own, where the release dashboard looks for it, and fails. + * A tip that already holds the head is found before this, by runMerge, so no scratch branch is made + * for it; the 204 the endpoint answers in that case is read the same way all the same. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. @@ -717,12 +718,21 @@ async function runMerge({ github, core, env }) { const unchanged = `Nothing was merged: ${develop === null ? `${base} is as it was` : `${base} and ${develop} are as they were`}.` const scratchPrefix = `release-merge/${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` const scratch = [] - const mergeOnScratch = async ({ into, tip, ...merge }) => { + const mergeOnScratch = async ({ into, tip, head, label, made = false, ...merge }) => { + // A tip that already holds the head has nothing to merge, which is what a retry after a late + // failure finds for both branches. Asked before the scratch branch is created, so that run + // writes nothing at all. A head this run has just made is on no branch yet: nothing to ask. + if (!made && (await commitContainsBranch({ github, owner, repo, branch: label, ref: head, sha: tip }))) { + core.info(`${into} already has ${label}: nothing to merge.`) + + return { merged: false, sha: tip } + } + const name = `${scratchPrefix}/${into}` await createScratch({ github, owner, repo, name, sha: tip, unchanged }) scratch.push(name) - return mergeInto({ github, core, owner, repo, into, scratch: name, tip, branch, unchanged, ...merge }) + return mergeInto({ github, core, owner, repo, into, scratch: name, tip, head, label, branch, unchanged, ...merge }) } let mergedBase @@ -774,6 +784,7 @@ async function runMerge({ github, core, env }) { head: baseSha, message: `Merge ${base} back into ${develop} (${branch})`, label: base, + made: mergedBase, }) mergedDevelop = onDevelop.merged if (mergedDevelop) { diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index 2c524d72..bb5491ba 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -224,8 +224,21 @@ function readAgain(rules, later) { } /** - * The rules of the common case: no tag, three branches, a prepared release branch, both merges made - * on their scratch branches, and the two branches moved together. + * The rule under which a commit already holds another: what the action asks before it makes a + * scratch branch, and what a retry after a late failure finds for the base and for develop. + * + * @param {string} tip The commit asked about: the tip of the base or of develop. + * @param {string} head What it holds; the release branch's tip when left out. + * + * @returns {object} The rule. + */ +function holds(tip, head = TIP['release/M26.test']) { + return { method: 'GET', path: API.compare(tip, head), body: { status: 'ahead' } } +} + +/** + * The rules of the common case: no tag, three branches, a prepared release branch that main does + * not hold yet, both merges made on their scratch branches, and the two branches moved together. * * @param {object} options * @param {object[]} options.tag Rules for the tag and release reads; none for an untagged version. @@ -253,6 +266,7 @@ function repositoryWith({ ...tag, ...branches(names), ...files(contents), + { method: 'GET', path: API.compare(TIP.main), body: { status: 'diverged' } }, merges(answers), { method: 'POST', path: API.refs, ...scratch }, { method: 'DELETE', path: API.ref(SCRATCH.main), ...cleanup }, @@ -466,6 +480,9 @@ describe('release-merge', () => { `GET ${API.file('.puprc')}`, `GET ${API.file('plugin.php')}`, `GET ${API.file('readme.txt')}`, + // Main is asked whether it already holds the release branch. Develop is not asked about the + // commit made for main a moment later: no branch holds that one yet. + `GET ${API.compare(TIP.main)}`, // Each merge is made on a scratch branch at the tip that was read: nothing reaches main or // develop until both merges exist. `POST ${API.refs}`, @@ -554,6 +571,7 @@ describe('release-merge', () => { `GET ${API.file('plugin.php', tip)}`, `GET ${API.file('readme.txt', tip)}`, `GET ${API.compare(TAG_COMMIT, tip)}`, + `GET ${API.compare(TIP.main, tip)}`, ], 'every read names the tip, never the branch' ) @@ -611,7 +629,7 @@ describe('release-merge', () => { assert.match(summary(), /^- \*\*Release 4\.17\.0:\*\* merged \[release\/M26\.test\]\(.*\) into \[main\]\(.*\), no develop branch to merge back into\.$/m) }) - it('does nothing on a retry after a late failure: both merges answer already merged', async (t) => { + it('writes nothing on a retry after a late failure: both branches already hold the release', async (t) => { // The publish step tagged the first run's merge commit, so the retry finds the tag with its // release, and main's tip is that commit. const { github, requests } = await octokitFor( @@ -620,15 +638,14 @@ describe('release-merge', () => { { method: 'GET', path: API.tag, body: { ref: 'refs/tags/4.17.0', object: { sha: MERGED.main, type: 'commit' } } }, PUBLISHED[1], ], - merges: [MERGE.done, MERGE.done], - extra: [{ method: 'GET', path: API.compare(MERGED.main), body: { status: 'ahead' } }], + // One answer for two questions: the tag contains the branch, and so does main, at the same commit. + extra: [holds(MERGED.main), holds(TIP.develop, MERGED.main)], }).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: MERGED.main } } } : rule)) ) const { core, outputs, logged } = await coreWithOutputs(t) await runMerge({ github, core, env: mergeEnv() }) - assert.strictEqual(mergesAsked(requests).length, 2) assert.match(logged(), /main already has release\/M26\.test: nothing to merge/) assert.match(logged(), /develop already has main: nothing to merge/) assert.strictEqual(outputs().conflict, '') @@ -636,10 +653,11 @@ describe('release-merge', () => { // published. Nothing else is read about main's tip. assert.strictEqual(outputs()['base-sha'], MERGED.main) assert.ok(!requested(requests).some((line) => line.startsWith(`GET ${API.commit('')}`)), requested(requests).join('\n')) - // The second merge still asks for that commit, which develop already has. - assert.strictEqual(mergesAsked(requests)[1].head, MERGED.main) - // Neither branch has anything to receive, so nothing is moved: the scratch branches come and go. - assert.deepStrictEqual(written(requests), SCRATCH_ONLY) + // Develop is asked about that commit, which it already has. + assert.ok(requested(requests).includes(`GET ${API.compare(TIP.develop, MERGED.main)}`), requested(requests).join('\n')) + // Neither branch has anything to receive: no scratch branch is made, no merge is asked for + // and nothing is moved. The run only reads. + assert.deepStrictEqual(written(requests), []) }) it('moves only develop on a retry that finds the release on the base and not yet on develop', async (t) => { @@ -647,8 +665,12 @@ describe('release-merge', () => { // together: the release on main, published or not, and develop without it. const { github, requests } = await octokitFor( repositoryWith({ - merges: [MERGE.done, MERGE.develop], - extra: [commit(MERGED.main, [TIP.main, TIP['release/M26.test']])], + merges: [MERGE.develop], + extra: [ + holds(MERGED.main), + commit(MERGED.main, [TIP.main, TIP['release/M26.test']]), + { method: 'GET', path: API.compare(TIP.develop, MERGED.main), body: { status: 'diverged' } }, + ], }).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: MERGED.main } } } : rule)) ) const { core, outputs, summary } = await coreWithOutputs(t) @@ -656,6 +678,8 @@ describe('release-merge', () => { await runMerge({ github, core, env: mergeEnv() }) assert.deepStrictEqual(movesAsked(requests), [[{ name: 'refs/heads/develop', beforeOid: TIP.develop, afterOid: MERGED.develop }]]) + // One scratch branch, for the one merge there is to make. + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.graphql}`, `DELETE ${API.ref(SCRATCH.develop)}`]) assert.strictEqual(outputs()['base-sha'], MERGED.main) assert.match(summary(), /\[main\]\(.*\) already had \[release\/M26\.test\]\(.*\), merged \[main\]\(.*\) into \[develop\]\(.*\)\.$/m) }) @@ -665,27 +689,22 @@ describe('release-merge', () => { // first run's merge commit, whose second parent is the branch's tip: the retry publishes it. const { github, requests } = await octokitFor( repositoryWith({ - merges: [MERGE.done, MERGE.done], - extra: [commit(MERGED.main, [TIP.main, TIP['release/M26.test']])], + extra: [holds(MERGED.main), commit(MERGED.main, [TIP.main, TIP['release/M26.test']]), holds(TIP.develop, MERGED.main)], }).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: MERGED.main } } } : rule)) ) const { core, outputs } = await coreWithOutputs(t) await runMerge({ github, core, env: mergeEnv() }) - // The tip the scratch branch was made at is the one checked, after the merge answered already - // merged and before the develop merge; main is not read again, and no version file at the base. - assert.deepStrictEqual(requested(requests).slice(-7), [ - `POST ${API.refs}`, - `POST ${API.merges}`, + // The tip that was read is the one checked, once main is found to hold the branch and before + // develop is asked about that same commit; main is not read again, and no version file at the base. + assert.deepStrictEqual(requested(requests).slice(-3), [ + `GET ${API.compare(MERGED.main)}`, `GET ${API.commit(MERGED.main)}`, - `POST ${API.refs}`, - `POST ${API.merges}`, - `DELETE ${API.ref(SCRATCH.main)}`, - `DELETE ${API.ref(SCRATCH.develop)}`, + `GET ${API.compare(TIP.develop, MERGED.main)}`, ]) + assert.deepStrictEqual(written(requests), []) assert.strictEqual(outputs()['base-sha'], MERGED.main) - assert.strictEqual(mergesAsked(requests)[1].head, MERGED.main) }) it('refuses an unpublished retry once the base has moved on, bump or not, so other changes are not published under this version', async (t) => { @@ -699,8 +718,7 @@ describe('release-merge', () => { for (const parents of [[MERGED.main, other], [MERGED.main]]) { const { github, requests } = await octokitFor( repositoryWith({ - merges: [MERGE.done, MERGE.done], - extra: [commit(later, parents)], + extra: [holds(later), commit(later, parents)], }).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: later } } } : rule)) ) const { core, outputs } = await coreWithOutputs(t) @@ -711,12 +729,33 @@ describe('release-merge', () => { `parents ${parents.length}` ) - // Refused before the develop merge, and the outputs are not set. - assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(SCRATCH.main)}`]) + // Refused before develop is asked about, with nothing written and no output set. + assert.deepStrictEqual(written(requests), []) assert.deepStrictEqual(outputs(), {}) } }) + it('reads a merge GitHub answers as already merged as nothing to merge, and deletes the scratch branch', async (t) => { + // The comparison said main lacks the branch and the merge endpoint then answers 204, its + // answer for a base that already contains the head. The two agree in practice; if they ever + // do not, the 204 is not read as a merge commit. + const other = '8888888888888888888888888888888888888888' + const { github, requests } = await octokitFor( + repositoryWith({ + branches: ['release/M26.test', 'main'], + merges: [MERGE.done], + extra: [commit(TIP.main, [other, TIP['release/M26.test']])], + }) + ) + const { core, outputs, logged } = await coreWithOutputs(t) + + await runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }) + + assert.match(logged(), /main already has release\/M26\.test: nothing to merge/) + assert.strictEqual(outputs()['base-sha'], TIP.main) + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(SCRATCH.main)}`]) + }) + it('sets no output when the step fails after the merges, so a caller reading published under continue-on-error skips its writes', async (t) => { // The job summary is the last write before the outputs; a summary that fails fails the step // with every output still empty, so `already-published == 'no'` is false for a caller that kept going. @@ -736,8 +775,7 @@ describe('release-merge', () => { const { github, requests } = await octokitFor( repositoryWith({ tag: PUBLISHED, - merges: [MERGE.done, MERGE.done], - extra: [{ method: 'GET', path: API.compare(TAG_COMMIT), body: { status: 'ahead' } }], + extra: [holds(TAG_COMMIT), holds(later), holds(TIP.develop, TAG_COMMIT)], }).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: later } } } : rule)) ) const { core, outputs, logged } = await coreWithOutputs(t) @@ -748,7 +786,8 @@ describe('release-merge', () => { // What a caller reads to skip the writes its deploy would repeat: the site's zip, a version // stored on the site, anything that is not idempotent. assert.strictEqual(outputs()['already-published'], 'yes') - assert.strictEqual(mergesAsked(requests)[1].head, TAG_COMMIT, 'develop is asked for the published commit') + assert.ok(requested(requests).includes(`GET ${API.compare(TIP.develop, TAG_COMMIT)}`), 'develop is asked about the published commit') + assert.deepStrictEqual(written(requests), []) assert.match(logged(), /main already has release\/M26\.test.*4\.17\.0 builds at ffffffffffffffffffffffffffffffffffffffff, the commit it was published from/s) assert.ok(!requested(requests).includes(`GET ${API.commit(later)}`), 'nothing is read about the tip') }) From 51f085d38d3e00743f0998f4c2f533d7bbe0bb4b Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 12:37:40 -0300 Subject: [PATCH 05/13] release-merge: check that a moved branch lacks its merge commit After a failed update the step read each branch again and compared its tip with two commits: the one the merge was made on and the merge commit. Any other tip was read as a branch that moved before the update, and the message said GitHub moved nothing and nothing was merged. A tip that descends from the merge commit is the other case: the update was applied, its answer was lost, and the branch received a commit before it was read again. The message was false there, and the retry it asked for found the base moved on and left the release to finish by hand. The step now compares such a tip with the merge commit. A branch that holds it counts as moved by the update, so the step goes on and the caller builds the merge commit. A comparison that fails is reported as what it is, without saying that nothing was merged. SOFT-5829 --- .github/actions/release-merge/README.md | 11 ++- .../actions/release-merge/release-merge.js | 39 +++++--- .../release-merge/release-merge.test.js | 93 ++++++++++++++++++- 3 files changed, 125 insertions(+), 18 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 093a555b..7cdfd0bb 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -84,13 +84,14 @@ refuses the update, neither branch moves, and the step fails with a message to r GitHub's answer to an update it does not make gives no reason for a branch that moved, so the step reads the two branches again before it fails, and the message says what it found: -- A branch received a commit. The message names it. Run the step again. +- A branch received a commit before the update, and neither holds its merge commit. The message + names the branch. Run the step again. - Both branches are where they were. A rule or the token refused the update, and GitHub's answer names it, such as a base branch that only takes pull requests. Change that first. -- Both branches hold their merge commits. The request failed after GitHub applied it: the step - warns and goes on. -- One of them could not be read, or holds its merge commit while the other does not. The message - says what each one holds, to check by hand. +- Both branches hold their merge commits, at their tip or under a commit that reached them since. + The request failed after GitHub applied it: the step warns and goes on. +- One of them could not be read or compared with its merge commit, or holds it while the other + does not. The message says what each one holds, to check by hand. The scratch branches are deleted when the step ends, whatever it did. One that could not be deleted is a warning in the log; a run cancelled in the few seconds they exist can leave them too. Nothing diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index 845d4422..c3c0f520 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -506,30 +506,40 @@ function failureOf(error) { } /** - * Reads where the branches of a failed update are now. + * Reads where the branches of a failed update are now, and whether each holds its merge commit. * * GitHub's answer does not say: a branch that moved gets the same "Something went wrong" as a * failure on GitHub's side, and a request that got no answer may have been applied. * + * A tip that is neither the one the merge was made on nor the merge commit is compared with the + * merge commit. A tip that descends from it means the update was applied and the branch received a + * commit afterwards; read as "the branch moved before the update", the message would say nothing + * was merged about a branch that holds the release. + * * @param {object} options * @param {object} options.github The Octokit github-script supplies. * @param {string} options.owner The repository owner. * @param {string} options.repo The repository name. * @param {object[]} options.moves `{branch, from, to}` per branch. * - * @returns {Promise} Each move with `now`: the branch's tip, or null when it could not - * be read. + * @returns {Promise} Each move with `now`, the branch's tip or null when it could not be + * read, and `holds`: whether that tip is the merge commit or descends from it, or null + * when that could not be found out. */ async function tipsAfter({ github, owner, repo, moves }) { const tips = [] for (const move of moves) { let now = null + let holds = null try { now = (await github.rest.repos.getBranch({ owner, repo, branch: move.branch })).data.commit.sha + holds = + now === move.to || + (now !== move.from && (await commitContainsBranch({ github, owner, repo, branch: move.branch, ref: move.to, sha: now }))) } catch { - // Left as null: the message says the branch could not be read. + // Left as null: the message says what could not be read or compared. } - tips.push({ ...move, now }) + tips.push({ ...move, now, holds }) } return tips @@ -585,8 +595,9 @@ async function moveBranches({ github, core, owner, repo, moves, unchanged }) { const short = (sha) => sha.slice(0, 7) const tips = await tipsAfter({ github, owner, repo, moves }) - // The request failed and the update went through: a connection lost after GitHub applied it. - if (tips.every(({ now, to }) => now === to)) { + // The request failed and the update went through: a connection lost after GitHub applied it. A + // branch that received a commit since then holds its merge commit too. + if (tips.every(({ holds }) => holds === true)) { core.warning( `Moving ${both} failed (${failure}), but the update was applied: ` + `${several ? `${names.join(' and ')} hold their merge commits` : `${names[0]} holds its merge commit`}.` @@ -595,14 +606,18 @@ async function moveBranches({ github, core, owner, repo, moves, unchanged }) { return } - // A branch that could not be read, or one at its merge commit while another is not. - if (tips.some(({ now, to }) => now === null || now === to)) { - const found = tips.map(({ branch, now, to }) => { + // A branch that could not be read or compared, or one that holds its merge commit while another + // does not. + if (tips.some(({ holds }) => holds !== false)) { + const found = tips.map(({ branch, now, holds }) => { if (now === null) { return `${branch} could not be read` } + if (holds === null) { + return `${branch} is at ${short(now)}, which could not be compared with its merge commit` + } - return now === to ? `${branch} holds its merge commit` : `${branch} is at ${short(now)}` + return holds ? `${branch} holds its merge commit` : `${branch} is at ${short(now)}` }) throw new Error( @@ -611,6 +626,8 @@ async function moveBranches({ github, core, owner, repo, moves, unchanged }) { ) } + // From here on no branch holds its merge commit: that was checked, not taken from the tip being + // another commit. const moved = tips.filter(({ now, from }) => now !== from) if (moved.length > 0) { const which = moved diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index bb5491ba..faedb8fd 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -894,7 +894,17 @@ describe('release-merge', () => { // GitHub refuses the update, and with it the one for develop. Its answer names no branch and // no reason, so the action reads the two again to say which one moved. const later = '9999999999999999999999999999999999999999' - const { github, requests } = await octokitFor(readAgain(repositoryWith({ move: MOVE.moved }), { main: later })) + const { github, requests } = await octokitFor( + readAgain( + repositoryWith({ + move: MOVE.moved, + // The commit that reached main does not hold the merge commit, which is on the scratch + // branch only: the update was not applied. + extra: [{ method: 'GET', path: API.compare(later, MERGED.main), body: { status: 'diverged' } }], + }), + { main: later } + ) + ) const { core, outputs, logged } = await coreWithOutputs(t) await assert.rejects( @@ -906,9 +916,10 @@ describe('release-merge', () => { assert.doesNotMatch(logged(), /MERGE_CONFLICT/) assert.deepStrictEqual(outputs(), {}) assert.strictEqual(movesAsked(requests).length, 1, 'the update is asked for once, not again') - assert.deepStrictEqual(requested(requests).slice(-5), [ + assert.deepStrictEqual(requested(requests).slice(-6), [ `POST ${API.graphql}`, `GET ${API.branch('main')}`, + `GET ${API.compare(later, MERGED.main)}`, `GET ${API.branch('develop')}`, `DELETE ${API.ref(SCRATCH.main)}`, `DELETE ${API.ref(SCRATCH.develop)}`, @@ -951,6 +962,84 @@ describe('release-merge', () => { assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(SCRATCH.main)}`, `DELETE ${API.ref(SCRATCH.develop)}`]) }) + it('takes the update as applied when the branch holds its merge commit under a commit it received since', async (t) => { + // The update moved main to its merge commit, the answer was lost, and a commit reached main + // before it was read again. Its tip is neither the old one nor the merge commit, and it + // descends from the merge commit: the release is on main. "Nothing was merged" would be false, + // and a retry would find main moved on and ask for the release to be finished by hand. + const later = '9999999999999999999999999999999999999999' + const { github, requests } = await octokitFor( + readAgain( + repositoryWith({ + branches: ['release/M26.test', 'main'], + merges: [MERGE.main], + move: MOVE.unanswered, + extra: [holds(later, MERGED.main)], + }), + { main: later } + ) + ) + const { core, outputs, logged } = await coreWithOutputs(t) + + await runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }) + + assert.match(logged(), /::warning::Moving main failed \(HTTP 502: Server Error\), but the update was applied: main holds its merge commit\./) + // The merge commit, not main's tip: what the caller builds is the release, without the later commit. + assert.strictEqual(outputs()['base-sha'], MERGED.main) + assert.deepStrictEqual(requested(requests).slice(-4), [ + `POST ${API.graphql}`, + `GET ${API.branch('main')}`, + `GET ${API.compare(later, MERGED.main)}`, + `DELETE ${API.ref(SCRATCH.main)}`, + ]) + }) + + it('takes the update as applied when both branches received a commit after it', async (t) => { + const later = '9999999999999999999999999999999999999999' + const other = '8888888888888888888888888888888888888888' + const { github } = await octokitFor( + readAgain( + repositoryWith({ move: MOVE.unanswered, extra: [holds(later, MERGED.main), holds(other, MERGED.develop)] }), + { main: later, develop: other } + ) + ) + const { core, outputs, logged } = await coreWithOutputs(t) + + await runMerge({ github, core, env: mergeEnv() }) + + assert.match(logged(), /::warning::Moving main and develop together failed \(HTTP 502: Server Error\), but the update was applied: main and develop hold their merge commits\./) + assert.strictEqual(outputs()['base-sha'], MERGED.main) + }) + + it('does not say nothing was merged when a moved branch cannot be compared with its merge commit', async (t) => { + // No answer to the update, main is at another commit, and the comparison that would tell + // whether that commit holds the merge commit fails. Either can be true. + const later = '9999999999999999999999999999999999999999' + const { github } = await octokitFor( + readAgain( + repositoryWith({ + branches: ['release/M26.test', 'main'], + merges: [MERGE.main], + move: MOVE.unanswered, + extra: [{ method: 'GET', path: API.compare(later, MERGED.main), status: 500, body: { message: 'boom' } }], + }), + { main: later } + ) + ) + const { core, outputs } = await coreWithOutputs(t) + + await assert.rejects(runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }), (error) => { + assert.match( + error.message, + /^Moving main failed \(HTTP 502: Server Error\), and what the branches hold now could not be confirmed: main is at 9999999, which could not be compared with its merge commit\. Check them before running this step again\.$/ + ) + assert.doesNotMatch(error.message, /Nothing was merged/) + + return true + }) + assert.deepStrictEqual(outputs(), {}) + }) + it('says nothing was merged when the request for the update failed and the branch is where it was', async (t) => { const { github } = await octokitFor( repositoryWith({ branches: ['release/M26.test', 'main'], merges: [MERGE.main], move: MOVE.unanswered }) From 85ac1481931a94fb6f4cd5cd0552d3ab9375bb45 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 12:45:32 -0300 Subject: [PATCH 06/13] release-merge: call the branches temporary, and name what the code holds "Scratch branch" was the word for the branches the merges are made on. The ticket and git's own vocabulary say temporary, which also tells the reader of a log what to do with one that is left behind. The messages, the README and the action manifest now say "temporary branch"; the code says temp. Names that needed the code around them to be understood are spelled out: the closing sentence is nothingMerged, the answer of a failed update is reason, a branch read again carries currentTip and hasMergeCommit, and the instruction of a conflict is howToResolve, not resolve. The rest parameter that held only the merge message is the message. No behavior changes. The three messages that named a scratch branch name a temporary branch. SOFT-5829 --- .github/actions/release-merge/README.md | 22 +- .github/actions/release-merge/action.yml | 4 +- .../actions/release-merge/release-merge.js | 229 +++++++++--------- .../release-merge/release-merge.test.js | 108 ++++----- 4 files changed, 183 insertions(+), 180 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 7cdfd0bb..1a0a0fb5 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -49,17 +49,17 @@ In this order, stopping at the first failure: tag contains the branch: a retry of this release goes on, a tag made from another commit is refused. 3. Makes the merge of the release branch into the base branch, a merge commit `Merge release/… into - main` (or `into master`), on a scratch branch: the base is not written yet. When the base + main` (or `into master`), on a temporary branch: the base is not written yet. When the base already holds the branch, a retry, nothing is merged: the commit to build is then the one the tag points at when the version is already published, and otherwise the base's tip, which has to be the commit that merged the branch. A base that has moved on since, by any commit, is refused. See [Versions it refuses](#versions-it-refuses). 4. Makes the merge of that **merged commit** into develop, a merge commit `Merge main back into - develop (release/…)`, on a second scratch branch. The base branch rather than the release + develop (release/…)`, on a second temporary branch. The base branch rather than the release branch, so develop descends from the base and consecutive releases bring one version bump each instead of divergent copies of the same generated lines. Skipped when `develop-branch` is empty, for a repository that has no develop branch. -5. Moves the base and develop to their merge commits in one update, then deletes the scratch +5. Moves the base and develop to their merge commits in one update, then deletes the temporary branches. See [Both merges or neither](#both-merges-or-neither). The merge commit on the base branch is the `base-sha` output: what the caller checks out and builds @@ -73,7 +73,7 @@ The merge endpoint has no dry run: it writes the merge commit to the branch it i the base and then for develop, a conflict on develop would leave the release on the base with no tag and no build, and a hotfix branch cut from the base in that window would ship it. -So each merge is made on a scratch branch, created at the tip the action read: +So each merge is made on a temporary branch, created at the tip the action read: `release-merge/-/main` and `release-merge/-/develop`. Only when both merge commits exist do the branches move, with the GraphQL mutation [`updateRefs`](https://docs.github.com/en/graphql/reference/git#mutation-updaterefs), which applies @@ -93,7 +93,7 @@ reads the two branches again before it fails, and the message says what it found - One of them could not be read or compared with its merge commit, or holds it while the other does not. The message says what each one holds, to check by hand. -The scratch branches are deleted when the step ends, whatever it did. One that could not be deleted +The temporary branches are deleted when the step ends, whatever it did. One that could not be deleted is a warning in the log; a run cancelled in the few seconds they exist can leave them too. Nothing reads them, and they can be deleted by hand. A repository with a branch named `release-merge` cannot hold them, since git keeps no branch under the name of another: the step fails before any merge. @@ -107,7 +107,7 @@ Every step is safe to run again after a late failure, which is what a retry from does: - A branch that already holds the other has nothing to merge. The action asks GitHub that before it - makes the scratch branch, so a run that finds both branches that way writes nothing: no scratch + makes the temporary branch, so a run that finds both branches that way writes nothing: no temporary branch, no merge, no update. What the caller then builds and publishes is the commit the tag points at, or the base's tip when there is no tag yet, which has to be the commit that merged the branch; see [Versions it refuses](#versions-it-refuses). @@ -221,7 +221,7 @@ this; without `head`, the tip at that moment is what merges. | `version` | yes | | The version being released: three or four numeric parts. | | `base-branch` | no | `main` | The branch the release is merged into: `main`, or `master` where that is the repository's name for it. | | `develop-branch` | no | `develop` | The branch the base is merged back into. Empty for a repository without one: only the first merge is made. Write `develop-branch: ''` in the caller itself; a `workflow_dispatch` input left empty takes its default, so the empty value cannot be passed through one. | -| `token` | no | `github.token` | The token for the merges, the scratch branches and the reads. Needs `contents: write`. See [The token](#the-token). | +| `token` | no | `github.token` | The token for the merges, the temporary branches and the reads. Needs `contents: write`. See [The token](#the-token). | ## Outputs @@ -255,7 +255,7 @@ actionlint 1.7 does not know the key and the plugin repositories lint their work ## The token -The job needs `contents: write`, and the token is used for the two merges, the scratch branches +The job needs `contents: write`, and the token is used for the two merges, the temporary branches they are made on, the update that moves the base and develop, and the branch, tag, release and file reads. It only ever reaches github-script's client: it is never handed to git, never on a command line and never in the log. The caller's checkout, when there is one, can use @@ -265,13 +265,13 @@ The default `github.token` is enough when the repository's rules allow what the fast-forward of the base branch and of develop to a merge commit, and branches created and deleted under `release-merge/`. The merges are made through the API, not opened as pull requests, so a base branch that only accepts pull requests refuses the update, with any token that is not on the rule's -bypass list, and so does a rule that restricts who creates branches, for the scratch ones; a rule +bypass list, and so does a rule that restricts who creates branches, for the temporary ones; a rule that requires signed commits is satisfied, since GitHub signs the merge commits it makes. GiveWP's rules are the reference shape: the branches are protected from deletion and non-fast-forward pushes, and `github.token` writes the merges. It also writes a release that changes files under -`.github/workflows/`: the scratch branches and the update take them. What `github.token` writes +`.github/workflows/`: the temporary branches and the update take them. What `github.token` writes starts no workflow run. Another token does, so a workflow that runs on every pushed branch would -run on the scratch branches for the seconds they exist; leave `release-merge/**` out of its +run on the temporary branches for the seconds they exist; leave `release-merge/**` out of its branches then. The human gate is not the token: the caller puts the job in an environment with required reviewers, and the release branch reached this step after its review and QA approval. A repository whose rules still need another actor should pass a GitHub App installation token minted diff --git a/.github/actions/release-merge/action.yml b/.github/actions/release-merge/action.yml index 990529fb..e5996bc2 100644 --- a/.github/actions/release-merge/action.yml +++ b/.github/actions/release-merge/action.yml @@ -4,7 +4,7 @@ # publishing the GitHub release is the release-publish action's, at the commit this one leaves on the # base (`base-sha`). # -# The two merges are written together or not at all: each is made on a scratch branch, and the +# The two merges are written together or not at all: each is made on a temporary branch, and the # base and develop then move to their merge commits in one update. A conflict in either merge # leaves both branches as they were. # @@ -55,7 +55,7 @@ inputs: description: >- Token the merges are made with, and the branch, tag, release and file reads. It needs `contents: write` on the repository, so the job needs that permission too, and it has to be - allowed to create and delete the scratch branches under `release-merge/`. GitHub makes the + allowed to create and delete the temporary branches under `release-merge/`. GitHub makes the merge commits under its identity and signs them. It only ever reaches github-script's client, never git or the log. required: false diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index c3c0f520..d3bd8465 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -6,13 +6,13 @@ * after this action and its build, at the commit this one leaves on the base (`base-sha`). * * runMerge checks the inputs and the version against the repository's tags, then asks GitHub for - * the two merges. Each is made on a scratch branch first, and the base and develop then move to + * the two merges. Each is made on a temporary branch first, and the base and develop then move to * their merge commits in one update, both or neither: a conflict in either merge leaves the two as * they were. Everything goes through the Octokit github-script supplies: the action reads and * writes no checkout, so a test runs it against a recorded client and asserts the requests. * * Every step is safe to run again after a late failure: a branch that already holds the release - * has nothing to merge, and a run that finds both that way writes nothing, scratch branches included. + * has nothing to merge, and a run that finds both that way writes nothing, temporary branches included. */ const { validateVersion: validateReleaseVersion } = require('../../lib/versions.js') @@ -344,68 +344,68 @@ async function commitMergesBranch({ github, owner, repo, branch, ref, sha }) { } /** - * Creates the scratch branch a merge is tried on, at the tip of the branch the merge is for. + * Creates the temporary branch a merge is tried on, at the tip of the branch the merge is for. * * The merge endpoint has no dry run: it writes the merge commit to the branch it is given. Given * the base or develop itself, the first merge would be on the remote before the second was tried. * * @param {object} options - * @param {object} options.github The Octokit github-script supplies. - * @param {string} options.owner The repository owner. - * @param {string} options.repo The repository name. - * @param {string} options.name The scratch branch to create. - * @param {string} options.sha The commit to create it at. - * @param {string} options.unchanged What to close the message with: the branches that were not written. + * @param {object} options.github The Octokit github-script supplies. + * @param {string} options.owner The repository owner. + * @param {string} options.repo The repository name. + * @param {string} options.name The temporary branch to create. + * @param {string} options.sha The commit to create it at. + * @param {string} options.nothingMerged The sentence that closes the message: no branch was written. * * @throws {Error} When GitHub refuses to create the branch. * * @returns {Promise} Resolves once the branch exists. */ -async function createScratch({ github, owner, repo, name, sha, unchanged }) { +async function createTempBranch({ github, owner, repo, name, sha, nothingMerged }) { try { await github.rest.git.createRef({ owner, repo, ref: `refs/heads/${name}`, sha }) } catch (error) { - const failed = `Creating the scratch branch ${name} failed (HTTP ${error?.status}): ${error?.message}.` + const whatFailed = `Creating the temporary branch ${name} failed (HTTP ${error?.status}): ${error?.message}.` // "Reference already exists" is GitHub's answer for a name that is taken, which a token or a // rule has nothing to do with. Any other answer, "Reference update failed" for a rule that // restricts who creates branches and for a branch named release-merge alike, gets both causes. if (error?.status === 422 && /already exists/i.test(String(error?.message))) { throw new Error( - `${failed} A branch of that name is left from another run of this step: delete it, then run this ` + - `step again. ${unchanged}` + `${whatFailed} A branch of that name is left from another run of this step: delete it, then run this ` + + `step again. ${nothingMerged}` ) } throw new Error( - `${failed} Each merge is tried on a scratch branch first, so the token and the repository's rules have ` + - `to allow creating and deleting it, and the repository cannot have a branch named release-merge. ${unchanged}` + `${whatFailed} Each merge is tried on a temporary branch first, so the token and the repository's rules have ` + + `to allow creating and deleting it, and the repository cannot have a branch named release-merge. ${nothingMerged}` ) } } /** - * Deletes the scratch branches of a run, whatever the run did with them. + * Deletes the temporary branches of a run, whatever the run did with them. * * A branch that cannot be deleted is a warning, not a failure: by then the release is merged or - * the run has already failed with its own message, and nothing reads a scratch branch. + * the run has already failed with its own message, and nothing reads a temporary branch. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. * @param {object} options.core The @actions/core toolkit. * @param {string} options.owner The repository owner. * @param {string} options.repo The repository name. - * @param {string[]} options.names The scratch branches that were created. + * @param {string[]} options.names The temporary branches that were created. * * @returns {Promise} Resolves once every deletion was tried. */ -async function deleteScratch({ github, core, owner, repo, names }) { +async function deleteTempBranches({ github, core, owner, repo, names }) { for (const name of names) { try { await github.rest.git.deleteRef({ owner, repo, ref: `heads/${name}` }) } catch (error) { core.warning( - `The scratch branch ${name} could not be deleted (HTTP ${error?.status}): ${error?.message}. ` + + `The temporary branch ${name} could not be deleted (HTTP ${error?.status}): ${error?.message}. ` + 'Delete it by hand; nothing reads it.' ) } @@ -413,45 +413,45 @@ async function deleteScratch({ github, core, owner, repo, names }) { } /** - * Makes one merge commit, on a scratch branch at the tip of the branch the commit is for. That + * Makes one merge commit, on a temporary branch at the tip of the branch the commit is for. That * branch is not written here: moveBranches moves it to the commit once every merge is made. * * GitHub makes the merge commit under the token's identity and signs it. A conflict prints the * MERGE_CONFLICT marker on a line of its own, where the release dashboard looks for it, and fails. - * A tip that already holds the head is found before this, by runMerge, so no scratch branch is made + * A tip that already holds the head is found before this, by runMerge, so no temporary branch is made * for it; the 204 the endpoint answers in that case is read the same way all the same. * * @param {object} options - * @param {object} options.github The Octokit github-script supplies. - * @param {object} options.core The @actions/core toolkit. - * @param {string} options.owner The repository owner. - * @param {string} options.repo The repository name. - * @param {string} options.into The branch the merge is for, e.g. `main`. - * @param {string} options.scratch The scratch branch the merge is made on, at `tip`. - * @param {string} options.tip The commit `into` was read at. - * @param {string} options.head What is merged: a commit hash. - * @param {string} options.message The merge commit message. - * @param {string} options.label How to name `head` in the log, e.g. `release/M26.test`. - * @param {string} options.branch The release branch: what a person merges into `into` to resolve - * a conflict, whichever merge it was. - * @param {string} options.unchanged What to close a failure message with: the branches that were - * not written. + * @param {object} options.github The Octokit github-script supplies. + * @param {object} options.core The @actions/core toolkit. + * @param {string} options.owner The repository owner. + * @param {string} options.repo The repository name. + * @param {string} options.into The branch the merge is for, e.g. `main`. + * @param {string} options.tempBranch The temporary branch the merge is made on, at `tip`. + * @param {string} options.tip The commit `into` was read at. + * @param {string} options.head What is merged: a commit hash. + * @param {string} options.message The merge commit message. + * @param {string} options.label How to name `head` in the log, e.g. `release/M26.test`. + * @param {string} options.branch The release branch: what a person merges into `into` to + * resolve a conflict, whichever merge it was. + * @param {string} options.nothingMerged The sentence that closes a failure message: no branch was + * written. * * @throws {Error} When the merge conflicts or GitHub refuses it. * * @returns {Promise<{merged: boolean, sha: string}>} Whether a merge commit was made, and the commit * `into` is to hold: the merge commit, or `tip` when there was nothing to merge. */ -async function mergeInto({ github, core, owner, repo, into, scratch, tip, head, message, label, branch, unchanged }) { +async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, branch, nothingMerged }) { let response try { - response = await github.rest.repos.merge({ owner, repo, base: scratch, head, commit_message: message }) + response = await github.rest.repos.merge({ owner, repo, base: tempBranch, head, commit_message: message }) } catch (error) { if (isConflict(error)) { // The merge back takes the base, not the release branch, so it can also conflict with a // commit that only the base has. Merging the release branch by hand shows no conflict then, // and the next run stops on this line again: the second sentence is the way out. - const resolve = + const howToResolve = `Merge ${branch} into ${into} by hand, resolve the conflict and push, then run this step again.` + (label === branch ? '' @@ -460,11 +460,11 @@ async function mergeInto({ github, core, owner, repo, into, scratch, tip, head, // core.info prints a plain line: the marker has to open an output line to count, see the README. core.setOutput('conflict', into) - core.info(`MERGE_CONFLICT: merging ${label} into ${into} conflicted. ${resolve} ${unchanged}`) - throw new Error(`Merging ${label} into ${into} stopped on a conflict. ${resolve} ${unchanged}`) + core.info(`MERGE_CONFLICT: merging ${label} into ${into} conflicted. ${howToResolve} ${nothingMerged}`) + throw new Error(`Merging ${label} into ${into} stopped on a conflict. ${howToResolve} ${nothingMerged}`) } - throw new Error(`Merging ${label} into ${into} failed (HTTP ${error?.status}): ${error?.message}. ${unchanged}`) + throw new Error(`Merging ${label} into ${into} failed (HTTP ${error?.status}): ${error?.message}. ${nothingMerged}`) } if (response.status === 204) { @@ -479,7 +479,7 @@ async function mergeInto({ github, core, owner, repo, into, scratch, tip, head, /** * The mutation that moves several branches at once: GitHub applies every update or none. The REST * API moves one ref per request, so two requests could move the base and then be refused for - * develop, which is the state the scratch branches exist to avoid. + * develop, which is the state the temporary branches exist to avoid. */ const UPDATE_REFS = `mutation ($input: UpdateRefsInput!) { updateRefs(input: $input) { @@ -497,7 +497,7 @@ const UPDATE_REFS = `mutation ($input: UpdateRefsInput!) { * * @returns {string} The answer. */ -function failureOf(error) { +function failureReason(error) { if (Array.isArray(error?.errors)) { return error.errors.map(({ message }) => String(message).replace(/\.+$/, '')).join('; ') } @@ -522,27 +522,28 @@ function failureOf(error) { * @param {string} options.repo The repository name. * @param {object[]} options.moves `{branch, from, to}` per branch. * - * @returns {Promise} Each move with `now`, the branch's tip or null when it could not be - * read, and `holds`: whether that tip is the merge commit or descends from it, or null - * when that could not be found out. + * @returns {Promise} Each move with `currentTip`, the branch's tip or null when it could + * not be read, and `hasMergeCommit`: whether that tip is the merge commit or descends from + * it, or null when that could not be found out. */ -async function tipsAfter({ github, owner, repo, moves }) { - const tips = [] +async function readBranchStates({ github, owner, repo, moves }) { + const states = [] for (const move of moves) { - let now = null - let holds = null + let currentTip = null + let hasMergeCommit = null try { - now = (await github.rest.repos.getBranch({ owner, repo, branch: move.branch })).data.commit.sha - holds = - now === move.to || - (now !== move.from && (await commitContainsBranch({ github, owner, repo, branch: move.branch, ref: move.to, sha: now }))) + currentTip = (await github.rest.repos.getBranch({ owner, repo, branch: move.branch })).data.commit.sha + hasMergeCommit = + currentTip === move.to || + (currentTip !== move.from && + (await commitContainsBranch({ github, owner, repo, branch: move.branch, ref: move.to, sha: currentTip }))) } catch { // Left as null: the message says what could not be read or compared. } - tips.push({ ...move, now, holds }) + states.push({ ...move, currentTip, hasMergeCommit }) } - return tips + return states } /** @@ -557,13 +558,14 @@ async function tipsAfter({ github, owner, repo, moves }) { * failure only states what was checked, and each case asks something different of the reader. * * @param {object} options - * @param {object} options.github The Octokit github-script supplies. - * @param {object} options.core The @actions/core toolkit. - * @param {string} options.owner The repository owner. - * @param {string} options.repo The repository name. - * @param {object[]} options.moves `{branch, from, to}` per branch: the tip it was read at and - * the merge commit made on that tip. - * @param {string} options.unchanged What to close the message with: the branches that were not written. + * @param {object} options.github The Octokit github-script supplies. + * @param {object} options.core The @actions/core toolkit. + * @param {string} options.owner The repository owner. + * @param {string} options.repo The repository name. + * @param {object[]} options.moves `{branch, from, to}` per branch: the tip it was read at + * and the merge commit made on that tip. + * @param {string} options.nothingMerged The sentence that closes a message when no branch was + * written. * * @throws {Error} When the repository cannot be read, a branch received a commit meanwhile, GitHub * does not move the branches, or the update failed and what they hold cannot be confirmed. @@ -571,36 +573,37 @@ async function tipsAfter({ github, owner, repo, moves }) { * @returns {Promise} Resolves once the branches are moved, also when the request failed and * the branches hold their merge commits all the same. */ -async function moveBranches({ github, core, owner, repo, moves, unchanged }) { +async function moveBranches({ github, core, owner, repo, moves, nothingMerged }) { let repositoryId try { repositoryId = (await github.rest.repos.get({ owner, repo })).data.node_id } catch (error) { - throw new Error(`Reading ${owner}/${repo} failed (HTTP ${error?.status}): ${error?.message}. ${unchanged}`) + throw new Error(`Reading ${owner}/${repo} failed (HTTP ${error?.status}): ${error?.message}. ${nothingMerged}`) } const refUpdates = moves.map(({ branch, from, to }) => ({ name: `refs/heads/${branch}`, beforeOid: from, afterOid: to })) - let failure + let reason try { await github.graphql(UPDATE_REFS, { input: { repositoryId, refUpdates } }) return } catch (error) { - failure = failureOf(error) + reason = failureReason(error) } const names = moves.map(({ branch }) => branch) - const several = names.length > 1 - const both = `${names.join(' and ')}${several ? ' together' : ''}` - const short = (sha) => sha.slice(0, 7) - const tips = await tipsAfter({ github, owner, repo, moves }) + const plural = names.length > 1 + const branchList = names.join(' and ') + const branchListTogether = plural ? `${branchList} together` : branchList + const shortSha = (sha) => sha.slice(0, 7) + const states = await readBranchStates({ github, owner, repo, moves }) // The request failed and the update went through: a connection lost after GitHub applied it. A // branch that received a commit since then holds its merge commit too. - if (tips.every(({ holds }) => holds === true)) { + if (states.every(({ hasMergeCommit }) => hasMergeCommit === true)) { core.warning( - `Moving ${both} failed (${failure}), but the update was applied: ` + - `${several ? `${names.join(' and ')} hold their merge commits` : `${names[0]} holds its merge commit`}.` + `Moving ${branchListTogether} failed (${reason}), but the update was applied: ` + + `${branchList} ${plural ? 'hold their merge commits' : 'holds its merge commit'}.` ) return @@ -608,41 +611,41 @@ async function moveBranches({ github, core, owner, repo, moves, unchanged }) { // A branch that could not be read or compared, or one that holds its merge commit while another // does not. - if (tips.some(({ holds }) => holds !== false)) { - const found = tips.map(({ branch, now, holds }) => { - if (now === null) { + if (states.some(({ hasMergeCommit }) => hasMergeCommit !== false)) { + const descriptions = states.map(({ branch, currentTip, hasMergeCommit }) => { + if (currentTip === null) { return `${branch} could not be read` } - if (holds === null) { - return `${branch} is at ${short(now)}, which could not be compared with its merge commit` + if (hasMergeCommit === null) { + return `${branch} is at ${shortSha(currentTip)}, which could not be compared with its merge commit` } - return holds ? `${branch} holds its merge commit` : `${branch} is at ${short(now)}` + return hasMergeCommit ? `${branch} holds its merge commit` : `${branch} is at ${shortSha(currentTip)}` }) throw new Error( - `Moving ${both} failed (${failure}), and what the branches hold now could not be confirmed: ` + - `${found.join(', ')}. Check them before running this step again.` + `Moving ${branchListTogether} failed (${reason}), and what the branches hold now could not be confirmed: ` + + `${descriptions.join(', ')}. Check them before running this step again.` ) } // From here on no branch holds its merge commit: that was checked, not taken from the tip being // another commit. - const moved = tips.filter(({ now, from }) => now !== from) + const moved = states.filter(({ currentTip, from }) => currentTip !== from) if (moved.length > 0) { - const which = moved - .map(({ branch, from, now }) => `${branch} (at ${short(now)}, the merge was made on ${short(from)})`) + const movedList = moved + .map(({ branch, from, currentTip }) => `${branch} (at ${shortSha(currentTip)}, the merge was made on ${shortSha(from)})`) .join(' and ') throw new Error( - `${which} received a commit while the merges were being made, so GitHub moved ` + - `${several ? 'neither branch' : 'nothing'} (${failure}). Nothing was merged. Run this step again.` + `${movedList} received a commit while the merges were being made, so GitHub moved ` + + `${plural ? 'neither branch' : 'nothing'} (${reason}). Nothing was merged. Run this step again.` ) } throw new Error( - `GitHub did not move ${both}: ${failure}. ${unchanged} No commit reached ` + - `${several ? 'them' : 'it'} while the merges were being made: when the answer names a rule or a ` + + `GitHub did not move ${branchListTogether}: ${reason}. ${nothingMerged} No commit reached ` + + `${plural ? 'them' : 'it'} while the merges were being made: when the answer names a rule or a ` + 'permission, change that before running this step again.' ) } @@ -658,7 +661,7 @@ async function moveBranches({ github, core, owner, repo, moves, unchanged }) { * commit in the history. A repository without a develop branch leaves `develop-branch` empty, and * only the first merge is made. * - * The two merges are written together or not at all. Each is made on a scratch branch, and the + * The two merges are written together or not at all. Each is made on a temporary branch, and the * base and develop move to their merge commits in one update once both exist. Written one after * the other, a conflict on develop would leave the release on the base with no tag and no build, * where a hotfix branch cut from the base would ship it. @@ -729,41 +732,41 @@ async function runMerge({ github, core, env }) { core.info(`${version} is already published (${releaseUrl}) from ${branch}: nothing to publish again.`) } - // Until the move at the end, the only branches written are the scratch ones, named after the + // Until the move at the end, the only branches written are the temporary ones, named after the // run so that no two runs share one. A failure from here on that found the base and develop where // they were closes with this sentence. - const unchanged = `Nothing was merged: ${develop === null ? `${base} is as it was` : `${base} and ${develop} are as they were`}.` - const scratchPrefix = `release-merge/${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` - const scratch = [] - const mergeOnScratch = async ({ into, tip, head, label, made = false, ...merge }) => { + const nothingMerged = `Nothing was merged: ${develop === null ? `${base} is as it was` : `${base} and ${develop} are as they were`}.` + const tempPrefix = `release-merge/${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` + const tempBranches = [] + const mergeOnTempBranch = async ({ into, tip, head, message, label, headIsNew = false }) => { // A tip that already holds the head has nothing to merge, which is what a retry after a late - // failure finds for both branches. Asked before the scratch branch is created, so that run + // failure finds for both branches. Asked before the temporary branch is created, so that run // writes nothing at all. A head this run has just made is on no branch yet: nothing to ask. - if (!made && (await commitContainsBranch({ github, owner, repo, branch: label, ref: head, sha: tip }))) { + if (!headIsNew && (await commitContainsBranch({ github, owner, repo, branch: label, ref: head, sha: tip }))) { core.info(`${into} already has ${label}: nothing to merge.`) return { merged: false, sha: tip } } - const name = `${scratchPrefix}/${into}` - await createScratch({ github, owner, repo, name, sha: tip, unchanged }) - scratch.push(name) + const tempBranch = `${tempPrefix}/${into}` + await createTempBranch({ github, owner, repo, name: tempBranch, sha: tip, nothingMerged }) + tempBranches.push(tempBranch) - return mergeInto({ github, core, owner, repo, into, scratch: name, tip, head, label, branch, unchanged, ...merge }) + return mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, branch, nothingMerged }) } let mergedBase let mergedDevelop = null let baseSha try { - const onBase = await mergeOnScratch({ + const baseMerge = await mergeOnTempBranch({ into: base, tip: baseTip, head: branchSha, message: mergeMessage(branch, base), label: branch, }) - mergedBase = onBase.merged + mergedBase = baseMerge.merged // base-sha is the commit the release is published at and built from. A merge that was made is // that merge commit, which holds the version the branch was just checked for. A retry that found @@ -773,7 +776,7 @@ async function runMerge({ github, core, env }) { // caller's deploy and publish each skip what they find done. So when the version is already // published, the retry builds the commit the tag was made from, what was published, and the base // moving on changes nothing; when it is not, the tip has to be the commit that merged the branch. - baseSha = onBase.sha + baseSha = baseMerge.sha if (!mergedBase) { if (tagged) { baseSha = tagSha @@ -783,7 +786,7 @@ async function runMerge({ github, core, env }) { `${base} already had ${branch} and has moved on since: its tip ${baseTip} is not the commit that merged ` + `${branch}, so a build from it would publish other changes under ${version}. There is nothing left ` + `to merge for ${version}; finish it by hand at the commit that merged ${branch} into ${base}, or ` + - `release another version. ${unchanged}` + `release another version. ${nothingMerged}` ) } } @@ -795,28 +798,28 @@ async function runMerge({ github, core, env }) { if (develop === null) { core.info(`develop-branch is empty: ${base} is not merged back into a develop branch.`) } else { - const onDevelop = await mergeOnScratch({ + const developMerge = await mergeOnTempBranch({ into: develop, tip: developTip, head: baseSha, message: `Merge ${base} back into ${develop} (${branch})`, label: base, - made: mergedBase, + headIsNew: mergedBase, }) - mergedDevelop = onDevelop.merged + mergedDevelop = developMerge.merged if (mergedDevelop) { - moves.push({ branch: develop, from: developTip, to: onDevelop.sha }) + moves.push({ branch: develop, from: developTip, to: developMerge.sha }) } } // Both merges exist: the branches move to them now, together. A retry that found a merge done // has nothing to move for that branch. if (moves.length > 0) { - await moveBranches({ github, core, owner, repo, moves, unchanged }) + await moveBranches({ github, core, owner, repo, moves, nothingMerged }) } } finally { - // After the move, not before: until then the scratch branches are what keeps the merge commits. - await deleteScratch({ github, core, owner, repo, names: scratch }) + // After the move, not before: until then the temporary branches are what keeps the merge commits. + await deleteTempBranches({ github, core, owner, repo, names: tempBranches }) } // One item of the list the deploy's actions write to the job summary, with each branch as a link. diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index faedb8fd..bbc74949 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -51,8 +51,8 @@ const TIP = { develop: 'cccccccccccccccccccccccccccccccccccccccc', } -/** The scratch branches of run 77, first attempt: where each merge is tried before a branch moves. */ -const SCRATCH = { +/** The temporary branches of run 77, first attempt: where each merge is tried before a branch moves. */ +const TEMP = { main: 'release-merge/77-1/main', develop: 'release-merge/77-1/develop', } @@ -225,7 +225,7 @@ function readAgain(rules, later) { /** * The rule under which a commit already holds another: what the action asks before it makes a - * scratch branch, and what a retry after a late failure finds for the base and for develop. + * temporary branch, and what a retry after a late failure finds for the base and for develop. * * @param {string} tip The commit asked about: the tip of the base or of develop. * @param {string} head What it holds; the release branch's tip when left out. @@ -238,15 +238,15 @@ function holds(tip, head = TIP['release/M26.test']) { /** * The rules of the common case: no tag, three branches, a prepared release branch that main does - * not hold yet, both merges made on their scratch branches, and the two branches moved together. + * not hold yet, both merges made on their temporary branches, and the two branches moved together. * * @param {object} options * @param {object[]} options.tag Rules for the tag and release reads; none for an untagged version. * @param {string[]} options.branches The branches the repository has. * @param {object} options.files Files of the release branch to change from the fixture. * @param {object[]} options.merges What the merge requests are answered with. - * @param {object} options.scratch What creating a scratch branch is answered with. - * @param {object} options.cleanup What deleting a scratch branch is answered with. + * @param {object} options.tempBranch What creating a temporary branch is answered with. + * @param {object} options.cleanup What deleting a temporary branch is answered with. * @param {object} options.move What the mutation that moves the branches is answered with. * @param {object[]} options.extra Any other rule. * @@ -257,7 +257,7 @@ function repositoryWith({ branches: names = Object.keys(TIP), files: contents = {}, merges: answers = [MERGE.main, MERGE.develop], - scratch = { status: 201, body: {} }, + tempBranch = { status: 201, body: {} }, cleanup = { status: 204 }, move = MOVE.done, extra = [], @@ -268,9 +268,9 @@ function repositoryWith({ ...files(contents), { method: 'GET', path: API.compare(TIP.main), body: { status: 'diverged' } }, merges(answers), - { method: 'POST', path: API.refs, ...scratch }, - { method: 'DELETE', path: API.ref(SCRATCH.main), ...cleanup }, - { method: 'DELETE', path: API.ref(SCRATCH.develop), ...cleanup }, + { method: 'POST', path: API.refs, ...tempBranch }, + { method: 'DELETE', path: API.ref(TEMP.main), ...cleanup }, + { method: 'DELETE', path: API.ref(TEMP.develop), ...cleanup }, { method: 'GET', path: API.repository, body: { node_id: REPOSITORY_ID } }, { method: 'POST', path: API.graphql, ...move }, ...extra, @@ -328,14 +328,14 @@ function written(requests) { return requested(requests.filter((request) => request.method !== 'GET')) } -/** The writes of a run that tried both merges and moved nothing: the scratch branches come and go. */ -const SCRATCH_ONLY = [ +/** The writes of a run that tried both merges and moved nothing: the temporary branches come and go. */ +const TEMP_ONLY = [ `POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.refs}`, `POST ${API.merges}`, - `DELETE ${API.ref(SCRATCH.main)}`, - `DELETE ${API.ref(SCRATCH.develop)}`, + `DELETE ${API.ref(TEMP.main)}`, + `DELETE ${API.ref(TEMP.develop)}`, ] describe('release-merge', () => { @@ -466,7 +466,7 @@ describe('release-merge', () => { }) describe('runMerge', () => { - it('tries both merges on scratch branches, then moves the base and develop together in one update', async (t) => { + it('tries both merges on temporary branches, then moves the base and develop together in one update', async (t) => { const { github, requests } = await octokitFor(repositoryWith()) const { core, outputs, summary } = await coreWithOutputs(t) @@ -483,7 +483,7 @@ describe('release-merge', () => { // Main is asked whether it already holds the release branch. Develop is not asked about the // commit made for main a moment later: no branch holds that one yet. `GET ${API.compare(TIP.main)}`, - // Each merge is made on a scratch branch at the tip that was read: nothing reaches main or + // Each merge is made on a temporary branch at the tip that was read: nothing reaches main or // develop until both merges exist. `POST ${API.refs}`, `POST ${API.merges}`, @@ -491,23 +491,23 @@ describe('release-merge', () => { `POST ${API.merges}`, `GET ${API.repository}`, `POST ${API.graphql}`, - // The scratch branches go last, so the merge commits are reachable when the branches move. - `DELETE ${API.ref(SCRATCH.main)}`, - `DELETE ${API.ref(SCRATCH.develop)}`, + // The temporary branches go last, so the merge commits are reachable when the branches move. + `DELETE ${API.ref(TEMP.main)}`, + `DELETE ${API.ref(TEMP.develop)}`, ]) assert.deepStrictEqual( requests.filter((request) => request.method === 'POST' && request.path === API.refs).map((request) => request.body), [ - { ref: `refs/heads/${SCRATCH.main}`, sha: TIP.main }, - { ref: `refs/heads/${SCRATCH.develop}`, sha: TIP.develop }, + { ref: `refs/heads/${TEMP.main}`, sha: TIP.main }, + { ref: `refs/heads/${TEMP.develop}`, sha: TIP.develop }, ] ) assert.deepStrictEqual(mergesAsked(requests), [ // The tip the version check read, not the branch by name: what was checked is what is merged. - // The message names main, the branch the commit is for, not the scratch branch. - { base: SCRATCH.main, head: TIP['release/M26.test'], commit_message: 'Merge release/M26.test into main' }, + // The message names main, the branch the commit is for, not the temporary branch. + { base: TEMP.main, head: TIP['release/M26.test'], commit_message: 'Merge release/M26.test into main' }, // The commit just made for main, not main by name: develop gets exactly what is released. - { base: SCRATCH.develop, head: MERGED.main, commit_message: 'Merge main back into develop (release/M26.test)' }, + { base: TEMP.develop, head: MERGED.main, commit_message: 'Merge main back into develop (release/M26.test)' }, ]) // One mutation for both branches, each from the tip the merge was made on to its merge commit: // GitHub moves both or neither, and a branch that is no longer at that tip is refused. @@ -655,7 +655,7 @@ describe('release-merge', () => { assert.ok(!requested(requests).some((line) => line.startsWith(`GET ${API.commit('')}`)), requested(requests).join('\n')) // Develop is asked about that commit, which it already has. assert.ok(requested(requests).includes(`GET ${API.compare(TIP.develop, MERGED.main)}`), requested(requests).join('\n')) - // Neither branch has anything to receive: no scratch branch is made, no merge is asked for + // Neither branch has anything to receive: no temporary branch is made, no merge is asked for // and nothing is moved. The run only reads. assert.deepStrictEqual(written(requests), []) }) @@ -678,8 +678,8 @@ describe('release-merge', () => { await runMerge({ github, core, env: mergeEnv() }) assert.deepStrictEqual(movesAsked(requests), [[{ name: 'refs/heads/develop', beforeOid: TIP.develop, afterOid: MERGED.develop }]]) - // One scratch branch, for the one merge there is to make. - assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.graphql}`, `DELETE ${API.ref(SCRATCH.develop)}`]) + // One temporary branch, for the one merge there is to make. + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.graphql}`, `DELETE ${API.ref(TEMP.develop)}`]) assert.strictEqual(outputs()['base-sha'], MERGED.main) assert.match(summary(), /\[main\]\(.*\) already had \[release\/M26\.test\]\(.*\), merged \[main\]\(.*\) into \[develop\]\(.*\)\.$/m) }) @@ -735,7 +735,7 @@ describe('release-merge', () => { } }) - it('reads a merge GitHub answers as already merged as nothing to merge, and deletes the scratch branch', async (t) => { + it('reads a merge GitHub answers as already merged as nothing to merge, and deletes the temporary branch', async (t) => { // The comparison said main lacks the branch and the merge endpoint then answers 204, its // answer for a base that already contains the head. The two agree in practice; if they ever // do not, the 204 is not read as a merge commit. @@ -753,7 +753,7 @@ describe('release-merge', () => { assert.match(logged(), /main already has release\/M26\.test: nothing to merge/) assert.strictEqual(outputs()['base-sha'], TIP.main) - assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(SCRATCH.main)}`]) + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(TEMP.main)}`]) }) it('sets no output when the step fails after the merges, so a caller reading published under continue-on-error skips its writes', async (t) => { @@ -840,8 +840,8 @@ describe('release-merge', () => { // The release branch itself is what was merged, so there is no other branch to merge by hand. assert.doesNotMatch(logged(), /already holds/) assert.strictEqual(outputs().conflict, 'main') - // Develop was never tried, and the one scratch branch is gone. - assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(SCRATCH.main)}`]) + // Develop was never tried, and the one temporary branch is gone. + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(TEMP.main)}`]) }) it('leaves the base as it was when the merge back into develop conflicts, and names develop', async (t) => { @@ -860,9 +860,9 @@ describe('release-merge', () => { /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. If develop already holds release\/M26\.test, the conflict is with a commit of main that develop lacks: merge main into develop as well\. Nothing was merged: main and develop are as they were\.$/m ) assert.strictEqual(outputs().conflict, 'develop') - // The merge for main was made, on its scratch branch only: main is never asked to move, so + // The merge for main was made, on its temporary branch only: main is never asked to move, so // the release is not on it while develop waits for a person. - assert.deepStrictEqual(written(requests), SCRATCH_ONLY) + assert.deepStrictEqual(written(requests), TEMP_ONLY) assert.deepStrictEqual(movesAsked(requests), []) assert.ok(!requested(requests).includes(`GET ${API.repository}`), requested(requests).join('\n')) }) @@ -886,7 +886,7 @@ describe('release-merge', () => { assert.doesNotMatch(logged(), /MERGE_CONFLICT/) assert.strictEqual(outputs().conflict, undefined) - assert.deepStrictEqual(written(requests).at(-1), `DELETE ${API.ref(SCRATCH.main)}`) + assert.deepStrictEqual(written(requests).at(-1), `DELETE ${API.ref(TEMP.main)}`) }) it('fails with neither branch moved, naming the branch that received a commit, and says to run again', async (t) => { @@ -898,7 +898,7 @@ describe('release-merge', () => { readAgain( repositoryWith({ move: MOVE.moved, - // The commit that reached main does not hold the merge commit, which is on the scratch + // The commit that reached main does not hold the merge commit, which is on the temporary // branch only: the update was not applied. extra: [{ method: 'GET', path: API.compare(later, MERGED.main), body: { status: 'diverged' } }], }), @@ -921,13 +921,13 @@ describe('release-merge', () => { `GET ${API.branch('main')}`, `GET ${API.compare(later, MERGED.main)}`, `GET ${API.branch('develop')}`, - `DELETE ${API.ref(SCRATCH.main)}`, - `DELETE ${API.ref(SCRATCH.develop)}`, + `DELETE ${API.ref(TEMP.main)}`, + `DELETE ${API.ref(TEMP.develop)}`, ]) }) it('says to change the rule, not to run again, when GitHub does not move the branches and neither received a commit', async (t) => { - // A base that only takes pull requests: the merges are made on the scratch branches, and the + // A base that only takes pull requests: the merges are made on the temporary branches, and the // update is where the rule answers. Running the step again would get the same answer. const { github, requests } = await octokitFor(repositoryWith({ move: MOVE.protected })) const { core, outputs, logged } = await coreWithOutputs(t) @@ -939,7 +939,7 @@ describe('release-merge', () => { assert.doesNotMatch(logged(), /MERGE_CONFLICT/) assert.deepStrictEqual(outputs(), {}) - assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(SCRATCH.main)}`, `DELETE ${API.ref(SCRATCH.develop)}`]) + assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(TEMP.main)}`, `DELETE ${API.ref(TEMP.develop)}`]) }) it('goes on with a warning when the request for the update failed and the branches hold their merge commits', async (t) => { @@ -959,7 +959,7 @@ describe('release-merge', () => { assert.strictEqual(movesAsked(requests).length, 1) assert.strictEqual(outputs()['base-sha'], MERGED.main) assert.strictEqual(outputs().conflict, '') - assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(SCRATCH.main)}`, `DELETE ${API.ref(SCRATCH.develop)}`]) + assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(TEMP.main)}`, `DELETE ${API.ref(TEMP.develop)}`]) }) it('takes the update as applied when the branch holds its merge commit under a commit it received since', async (t) => { @@ -990,7 +990,7 @@ describe('release-merge', () => { `POST ${API.graphql}`, `GET ${API.branch('main')}`, `GET ${API.compare(later, MERGED.main)}`, - `DELETE ${API.ref(SCRATCH.main)}`, + `DELETE ${API.ref(TEMP.main)}`, ]) }) @@ -1073,7 +1073,7 @@ describe('release-merge', () => { assert.deepStrictEqual(outputs(), {}) }) - it('fails before the update when the repository cannot be read, with both scratch branches deleted', async (t) => { + it('fails before the update when the repository cannot be read, with both temporary branches deleted', async (t) => { const { github, requests } = await octokitFor( repositoryWith().map((rule) => (rule.path === API.repository ? { ...rule, status: 500, body: { message: 'boom' } } : rule)) ) @@ -1084,23 +1084,23 @@ describe('release-merge', () => { /Reading stellarwp\/plugin failed \(HTTP 500\): boom\. Nothing was merged: main and develop are as they were\./ ) - // Both merges were made, on the scratch branches only: no update is asked for. - assert.deepStrictEqual(written(requests), SCRATCH_ONLY) + // Both merges were made, on the temporary branches only: no update is asked for. + assert.deepStrictEqual(written(requests), TEMP_ONLY) assert.deepStrictEqual(movesAsked(requests), []) assert.deepStrictEqual(outputs(), {}) }) - it('fails before any merge when the scratch branch cannot be created, naming the token, the rules and a branch in the way', async (t) => { + it('fails before any merge when the temporary branch cannot be created, naming the token, the rules and a branch in the way', async (t) => { // What GitHub answers when a ruleset restricts who creates branches, and also when a branch // named release-merge is in the way: the same words for both, so the message gives both causes. const { github, requests } = await octokitFor( - repositoryWith({ scratch: { status: 422, body: { message: 'Reference update failed' } } }) + repositoryWith({ tempBranch: { status: 422, body: { message: 'Reference update failed' } } }) ) const { core, outputs } = await coreWithOutputs(t) await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /Creating the scratch branch release-merge\/77-1\/main failed \(HTTP 422\): Reference update failed\. Each merge is tried on a scratch branch first, so the token and the repository's rules have to allow creating and deleting it, and the repository cannot have a branch named release-merge\. Nothing was merged: main and develop are as they were\./ + /Creating the temporary branch release-merge\/77-1\/main failed \(HTTP 422\): Reference update failed\. Each merge is tried on a temporary branch first, so the token and the repository's rules have to allow creating and deleting it, and the repository cannot have a branch named release-merge\. Nothing was merged: main and develop are as they were\./ ) // Nothing was created, so there is nothing to merge on and nothing to delete. @@ -1108,12 +1108,12 @@ describe('release-merge', () => { assert.deepStrictEqual(outputs(), {}) }) - it('says to delete the branch, not to change the token, when a scratch branch of that name already exists', async (t) => { + it('says to delete the branch, not to change the token, when a temporary branch of that name already exists', async (t) => { // A branch another run of this step left behind under the same run and attempt. The second // one is refused here, so the first, which holds the merge commit for main by then, is deleted. const { github, requests } = await octokitFor( repositoryWith({ - scratch: { responses: [{ status: 201, body: {} }, { status: 422, body: { message: 'Reference already exists' } }] }, + tempBranch: { responses: [{ status: 201, body: {} }, { status: 422, body: { message: 'Reference already exists' } }] }, }) ) const { core, outputs } = await coreWithOutputs(t) @@ -1121,27 +1121,27 @@ describe('release-merge', () => { await assert.rejects(runMerge({ github, core, env: mergeEnv() }), (error) => { assert.match( error.message, - /^Creating the scratch branch release-merge\/77-1\/develop failed \(HTTP 422\): Reference already exists\. A branch of that name is left from another run of this step: delete it, then run this step again\. Nothing was merged: main and develop are as they were\.$/ + /^Creating the temporary branch release-merge\/77-1\/develop failed \(HTTP 422\): Reference already exists\. A branch of that name is left from another run of this step: delete it, then run this step again\. Nothing was merged: main and develop are as they were\.$/ ) assert.doesNotMatch(error.message, /token/) return true }) - assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.refs}`, `DELETE ${API.ref(SCRATCH.main)}`]) + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `POST ${API.refs}`, `DELETE ${API.ref(TEMP.main)}`]) assert.deepStrictEqual(movesAsked(requests), []) assert.deepStrictEqual(outputs(), {}) }) - it('warns and still succeeds when a scratch branch cannot be deleted after the branches moved', async (t) => { + it('warns and still succeeds when a temporary branch cannot be deleted after the branches moved', async (t) => { const { github, requests } = await octokitFor(repositoryWith({ cleanup: { status: 500, body: { message: 'boom' } } })) const { core, outputs, logged } = await coreWithOutputs(t) await runMerge({ github, core, env: mergeEnv() }) // Both deletions are tried, and the release is merged whatever they answer. - assert.match(logged(), /::warning::.*scratch branch release-merge\/77-1\/main.*boom.*Delete it by hand/) - assert.match(logged(), /::warning::.*scratch branch release-merge\/77-1\/develop/) + assert.match(logged(), /::warning::.*temporary branch release-merge\/77-1\/main.*boom.*Delete it by hand/) + assert.match(logged(), /::warning::.*temporary branch release-merge\/77-1\/develop/) assert.strictEqual(movesAsked(requests).length, 1) assert.strictEqual(outputs()['base-sha'], MERGED.main) }) From 356d6351895a756cf45d5c12a439b1723e79c16c Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 12:53:19 -0300 Subject: [PATCH 07/13] release-merge: reword the comments so each one reads on the first pass The comments this branch added gave the verb to the wrong thing ("a conflict prints the marker"), hid a clause in the subject ("a tip that is neither the one the merge was made on nor the merge commit is compared"), and ran several steps into one sentence. They now say who acts, keep "that" and "if", and take one step per sentence. Three test names say the case in plainer words. No code changes. SOFT-5829 --- .github/actions/release-merge/action.yml | 14 +- .../actions/release-merge/release-merge.js | 198 ++++++++++-------- .../release-merge/release-merge.test.js | 128 ++++++----- 3 files changed, 191 insertions(+), 149 deletions(-) diff --git a/.github/actions/release-merge/action.yml b/.github/actions/release-merge/action.yml index e5996bc2..056c0f0a 100644 --- a/.github/actions/release-merge/action.yml +++ b/.github/actions/release-merge/action.yml @@ -4,9 +4,9 @@ # publishing the GitHub release is the release-publish action's, at the commit this one leaves on the # base (`base-sha`). # -# The two merges are written together or not at all: each is made on a temporary branch, and the -# base and develop then move to their merge commits in one update. A conflict in either merge -# leaves both branches as they were. +# The action writes the two merges together or not at all. It makes each merge on a temporary +# branch, then moves the base and develop to their merge commits in one update. A conflict in +# either merge leaves both branches as they were. # # The checks and the merges live in release-merge.js, run by actions/github-script, so there is no # dependency to install and nothing to build. Everything goes through the GitHub API: the branch, @@ -54,10 +54,10 @@ inputs: token: description: >- Token the merges are made with, and the branch, tag, release and file reads. It needs - `contents: write` on the repository, so the job needs that permission too, and it has to be - allowed to create and delete the temporary branches under `release-merge/`. GitHub makes the - merge commits under its identity and signs them. It only ever reaches github-script's client, - never git or the log. + `contents: write` on the repository, so the job needs that permission too. The repository's + rules also have to let it create and delete the temporary branches under `release-merge/`. + GitHub makes the merge commits under its identity and signs them. It only ever reaches + github-script's client, never git or the log. required: false default: ${{ github.token }} diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index d3bd8465..7103fdca 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -6,13 +6,16 @@ * after this action and its build, at the commit this one leaves on the base (`base-sha`). * * runMerge checks the inputs and the version against the repository's tags, then asks GitHub for - * the two merges. Each is made on a temporary branch first, and the base and develop then move to - * their merge commits in one update, both or neither: a conflict in either merge leaves the two as - * they were. Everything goes through the Octokit github-script supplies: the action reads and - * writes no checkout, so a test runs it against a recorded client and asserts the requests. + * the two merges. It makes each merge on a temporary branch first. When both merge commits exist, + * it moves the base and develop to them in one update, and GitHub moves both branches or neither. + * A conflict in either merge leaves the base and develop as they were. * - * Every step is safe to run again after a late failure: a branch that already holds the release - * has nothing to merge, and a run that finds both that way writes nothing, temporary branches included. + * Everything goes through the Octokit github-script supplies: the action reads and writes no + * checkout, so a test runs it against a recorded client and asserts the requests. + * + * Every step is safe to run again after a late failure. A branch that already holds the release + * has nothing to merge. A run that finds the base and develop in that state writes nothing: it + * creates no temporary branch either. */ const { validateVersion: validateReleaseVersion } = require('../../lib/versions.js') @@ -289,8 +292,11 @@ async function assertBranchHoldsVersion({ github, owner, repo, branch, ref, vers } /** - * Whether a commit contains a branch: the tag of a published version against the release branch, - * and the tip of the base or of develop against what is about to be merged into it. + * Whether one commit contains another. runMerge asks two questions with it: + * + * - Does the tag of a published version contain the release branch? + * - Does the tip of the base, or of develop, already contain the commit that is about to be merged + * into it? * * @param {object} options * @param {object} options.github The Octokit github-script supplies. @@ -344,10 +350,12 @@ async function commitMergesBranch({ github, owner, repo, branch, ref, sha }) { } /** - * Creates the temporary branch a merge is tried on, at the tip of the branch the merge is for. + * Creates the temporary branch that a merge is made on. The temporary branch starts at the tip of + * the branch that the merge is for. * - * The merge endpoint has no dry run: it writes the merge commit to the branch it is given. Given - * the base or develop itself, the first merge would be on the remote before the second was tried. + * The merge endpoint has no dry run: it writes the merge commit to the branch that it is given. If + * the action gave it the base and then develop, the base would hold its merge before the merge + * into develop was tried. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. @@ -355,7 +363,8 @@ async function commitMergesBranch({ github, owner, repo, branch, ref, sha }) { * @param {string} options.repo The repository name. * @param {string} options.name The temporary branch to create. * @param {string} options.sha The commit to create it at. - * @param {string} options.nothingMerged The sentence that closes the message: no branch was written. + * @param {string} options.nothingMerged The sentence that closes the error message. It says that + * the base and develop were not written. * * @throws {Error} When GitHub refuses to create the branch. * @@ -367,9 +376,11 @@ async function createTempBranch({ github, owner, repo, name, sha, nothingMerged } catch (error) { const whatFailed = `Creating the temporary branch ${name} failed (HTTP ${error?.status}): ${error?.message}.` - // "Reference already exists" is GitHub's answer for a name that is taken, which a token or a - // rule has nothing to do with. Any other answer, "Reference update failed" for a rule that - // restricts who creates branches and for a branch named release-merge alike, gets both causes. + // GitHub answers "Reference already exists" when a branch of that name exists. The token and + // the rules are not the cause, so that message does not mention them. GitHub answers + // "Reference update failed" both when a rule restricts who creates branches and when the + // repository has a branch named release-merge. The message for any other answer gives these + // two causes. if (error?.status === 422 && /already exists/i.test(String(error?.message))) { throw new Error( `${whatFailed} A branch of that name is left from another run of this step: delete it, then run this ` + @@ -385,10 +396,10 @@ async function createTempBranch({ github, owner, repo, name, sha, nothingMerged } /** - * Deletes the temporary branches of a run, whatever the run did with them. + * Deletes the temporary branches that the run created, whether the run passed or failed. * - * A branch that cannot be deleted is a warning, not a failure: by then the release is merged or - * the run has already failed with its own message, and nothing reads a temporary branch. + * If a temporary branch cannot be deleted, the step warns and goes on. By then the release is + * merged, or the run has already failed with its own message. Nothing reads a temporary branch. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. @@ -413,13 +424,17 @@ async function deleteTempBranches({ github, core, owner, repo, names }) { } /** - * Makes one merge commit, on a temporary branch at the tip of the branch the commit is for. That - * branch is not written here: moveBranches moves it to the commit once every merge is made. + * Makes one merge commit, on a temporary branch that starts at the tip of `into`, the branch that + * the merge is for. This function does not write `into`: moveBranches moves it to the merge commit + * once every merge is made. + * + * GitHub makes the merge commit under the token's identity and signs it. On a conflict, the + * function prints the MERGE_CONFLICT marker on a line of its own, where the release dashboard looks + * for it, and throws. * - * GitHub makes the merge commit under the token's identity and signs it. A conflict prints the - * MERGE_CONFLICT marker on a line of its own, where the release dashboard looks for it, and fails. - * A tip that already holds the head is found before this, by runMerge, so no temporary branch is made - * for it; the 204 the endpoint answers in that case is read the same way all the same. + * runMerge checks first whether `tip` already holds `head`, and makes no temporary branch when it + * does. The endpoint answers 204 in that case, and this function also reads a 204 as nothing to + * merge. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. @@ -427,20 +442,21 @@ async function deleteTempBranches({ github, core, owner, repo, names }) { * @param {string} options.owner The repository owner. * @param {string} options.repo The repository name. * @param {string} options.into The branch the merge is for, e.g. `main`. - * @param {string} options.tempBranch The temporary branch the merge is made on, at `tip`. - * @param {string} options.tip The commit `into` was read at. + * @param {string} options.tempBranch The temporary branch that the merge is made on. It starts + * at `tip`. + * @param {string} options.tip The commit that `into` pointed at when it was read. * @param {string} options.head What is merged: a commit hash. * @param {string} options.message The merge commit message. * @param {string} options.label How to name `head` in the log, e.g. `release/M26.test`. - * @param {string} options.branch The release branch: what a person merges into `into` to - * resolve a conflict, whichever merge it was. - * @param {string} options.nothingMerged The sentence that closes a failure message: no branch was - * written. + * @param {string} options.branch The release branch. On a conflict in either merge, the + * message tells a person to merge this branch into `into`. + * @param {string} options.nothingMerged The sentence that closes a failure message. It says that + * the base and develop were not written. * * @throws {Error} When the merge conflicts or GitHub refuses it. * * @returns {Promise<{merged: boolean, sha: string}>} Whether a merge commit was made, and the commit - * `into` is to hold: the merge commit, or `tip` when there was nothing to merge. + * that `into` should point at: the merge commit, or `tip` when there was nothing to merge. */ async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, branch, nothingMerged }) { let response @@ -448,9 +464,10 @@ async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, hea response = await github.rest.repos.merge({ owner, repo, base: tempBranch, head, commit_message: message }) } catch (error) { if (isConflict(error)) { - // The merge back takes the base, not the release branch, so it can also conflict with a - // commit that only the base has. Merging the release branch by hand shows no conflict then, - // and the next run stops on this line again: the second sentence is the way out. + // The merge into develop takes the base, not the release branch, so it can also conflict + // with a commit that only the base has. In that case, merging the release branch into + // develop by hand shows no conflict, and the next run stops on this line again. The second + // sentence tells the reader to merge the base into develop as well. const howToResolve = `Merge ${branch} into ${into} by hand, resolve the conflict and push, then run this step again.` + (label === branch @@ -477,9 +494,10 @@ async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, hea } /** - * The mutation that moves several branches at once: GitHub applies every update or none. The REST - * API moves one ref per request, so two requests could move the base and then be refused for - * develop, which is the state the temporary branches exist to avoid. + * The mutation that moves several branches at once: GitHub applies every update or none. + * + * The REST API moves one ref per request. With two requests, GitHub could move the base and then + * refuse develop, which is the state that the temporary branches exist to avoid. */ const UPDATE_REFS = `mutation ($input: UpdateRefsInput!) { updateRefs(input: $input) { @@ -490,8 +508,9 @@ const UPDATE_REFS = `mutation ($input: UpdateRefsInput!) { /** * What GitHub answered to a failed update, on one line. * - * A refusal comes as GraphQL errors, which Octokit joins with a line break before each. A request - * that got no usable answer has a status and a message instead. + * When GitHub refuses the update, it answers with GraphQL errors, and Octokit joins them with a + * line break before each one. When the request gets no usable answer, the error has a status and a + * message instead. * * @param {Error} error The Octokit error. * @@ -506,15 +525,17 @@ function failureReason(error) { } /** - * Reads where the branches of a failed update are now, and whether each holds its merge commit. + * After a failed update, reads the tip of each branch and whether the branch holds its merge + * commit. * - * GitHub's answer does not say: a branch that moved gets the same "Something went wrong" as a - * failure on GitHub's side, and a request that got no answer may have been applied. + * GitHub's answer does not say what happened. For a branch that moved, GitHub answers "Something + * went wrong", the same text as for a failure on its own side. And GitHub may have applied a + * request that got no answer. * - * A tip that is neither the one the merge was made on nor the merge commit is compared with the - * merge commit. A tip that descends from it means the update was applied and the branch received a - * commit afterwards; read as "the branch moved before the update", the message would say nothing - * was merged about a branch that holds the release. + * If the tip is neither the commit that the merge was made on nor the merge commit, the function + * compares the tip with the merge commit. If the tip descends from the merge commit, GitHub applied + * the update and the branch received a commit afterwards. Without this comparison, the message + * would say that nothing was merged, for a branch that holds the release. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. @@ -522,9 +543,9 @@ function failureReason(error) { * @param {string} options.repo The repository name. * @param {object[]} options.moves `{branch, from, to}` per branch. * - * @returns {Promise} Each move with `currentTip`, the branch's tip or null when it could - * not be read, and `hasMergeCommit`: whether that tip is the merge commit or descends from - * it, or null when that could not be found out. + * @returns {Promise} Each move, with two more fields. `currentTip` is the branch's tip, + * or null if the tip could not be read. `hasMergeCommit` says whether `currentTip` is the + * merge commit or descends from it, and is null if that could not be checked. */ async function readBranchStates({ github, owner, repo, moves }) { const states = [] @@ -538,7 +559,7 @@ async function readBranchStates({ github, owner, repo, moves }) { (currentTip !== move.from && (await commitContainsBranch({ github, owner, repo, branch: move.branch, ref: move.to, sha: currentTip }))) } catch { - // Left as null: the message says what could not be read or compared. + // The values stay null, and the message says what could not be read or compared. } states.push({ ...move, currentTip, hasMergeCommit }) } @@ -550,28 +571,31 @@ async function readBranchStates({ github, owner, repo, moves }) { * Moves the base and develop to their merge commits in one update, so the remote never holds one * merge without the other. * - * Each branch is moved from the tip its merge was made on. A branch that is no longer at that tip - * is refused, and GitHub then moves none of them; the merge commit descends from the tip, so the - * move is a fast-forward and never needs to be forced. + * The update moves each branch from the tip that its merge was made on. If a branch is no longer + * at that tip, GitHub refuses the update and moves no branch. The merge commit descends from the + * tip, so the move is a fast-forward and never needs to be forced. * - * When the update fails, the branches are read again and the message says what they hold: a - * failure only states what was checked, and each case asks something different of the reader. + * When the update fails, the function reads the branches again, and the message says what they + * hold. The message states only what was checked, because each case needs a different action from + * the reader. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. * @param {object} options.core The @actions/core toolkit. * @param {string} options.owner The repository owner. * @param {string} options.repo The repository name. - * @param {object[]} options.moves `{branch, from, to}` per branch: the tip it was read at - * and the merge commit made on that tip. - * @param {string} options.nothingMerged The sentence that closes a message when no branch was - * written. - * - * @throws {Error} When the repository cannot be read, a branch received a commit meanwhile, GitHub - * does not move the branches, or the update failed and what they hold cannot be confirmed. - * - * @returns {Promise} Resolves once the branches are moved, also when the request failed and - * the branches hold their merge commits all the same. + * @param {object[]} options.moves `{branch, from, to}` per branch. `from` is the tip that + * the branch was read at, and `to` is the merge commit made + * on that tip. + * @param {string} options.nothingMerged The sentence that closes a message when the base and + * develop were not written. + * + * @throws {Error} When the repository cannot be read, a branch received a commit while the merges + * were being made, GitHub does not move the branches, or the update failed and what + * the branches hold cannot be confirmed. + * + * @returns {Promise} Resolves once the branches are moved. That includes a request that + * failed while the branches hold their merge commits. */ async function moveBranches({ github, core, owner, repo, moves, nothingMerged }) { let repositoryId @@ -598,8 +622,8 @@ async function moveBranches({ github, core, owner, repo, moves, nothingMerged }) const shortSha = (sha) => sha.slice(0, 7) const states = await readBranchStates({ github, owner, repo, moves }) - // The request failed and the update went through: a connection lost after GitHub applied it. A - // branch that received a commit since then holds its merge commit too. + // The request failed, and GitHub applied the update: the connection was lost after that. A + // branch that received a commit after the update still holds its merge commit. if (states.every(({ hasMergeCommit }) => hasMergeCommit === true)) { core.warning( `Moving ${branchListTogether} failed (${reason}), but the update was applied: ` + @@ -609,8 +633,8 @@ async function moveBranches({ github, core, owner, repo, moves, nothingMerged }) return } - // A branch that could not be read or compared, or one that holds its merge commit while another - // does not. + // At least one branch could not be read or compared, or one branch holds its merge commit and + // the other does not. if (states.some(({ hasMergeCommit }) => hasMergeCommit !== false)) { const descriptions = states.map(({ branch, currentTip, hasMergeCommit }) => { if (currentTip === null) { @@ -629,8 +653,8 @@ async function moveBranches({ github, core, owner, repo, moves, nothingMerged }) ) } - // From here on no branch holds its merge commit: that was checked, not taken from the tip being - // another commit. + // From here on, no branch holds its merge commit. The comparison checked that; it is not + // inferred from a tip that changed. const moved = states.filter(({ currentTip, from }) => currentTip !== from) if (moved.length > 0) { const movedList = moved @@ -661,10 +685,10 @@ async function moveBranches({ github, core, owner, repo, moves, nothingMerged }) * commit in the history. A repository without a develop branch leaves `develop-branch` empty, and * only the first merge is made. * - * The two merges are written together or not at all. Each is made on a temporary branch, and the - * base and develop move to their merge commits in one update once both exist. Written one after - * the other, a conflict on develop would leave the release on the base with no tag and no build, - * where a hotfix branch cut from the base would ship it. + * The action writes the two merges together or not at all. It makes each merge on a temporary + * branch. Once both merge commits exist, it moves the base and develop to them in one update. If + * the merges were written one after the other, a conflict on develop would leave the release on + * the base with no tag and no build. A hotfix branch cut from the base would then ship that code. * * Nothing is read from or written to a checkout: the caller checks out `base-sha` afterwards for * the steps that build the release from it. @@ -710,8 +734,8 @@ async function runMerge({ github, core, env }) { ) } - // The tips of the base and develop are read once too: each merge is made on its tip, and the - // branch moves from that tip to the merge commit or not at all. + // The tips of the base and develop are read once as well. Each merge is made on the tip that + // was read, and the update moves the branch from that tip to the merge commit, or does not move it. const baseTip = await readBranchTip({ github, owner, repo, branch: base }) const developTip = develop === null ? null : await readBranchTip({ github, owner, repo, branch: develop }) @@ -732,16 +756,17 @@ async function runMerge({ github, core, env }) { core.info(`${version} is already published (${releaseUrl}) from ${branch}: nothing to publish again.`) } - // Until the move at the end, the only branches written are the temporary ones, named after the - // run so that no two runs share one. A failure from here on that found the base and develop where - // they were closes with this sentence. + // Until the update at the end, the action writes only the temporary branches. Their names carry + // the run id and the attempt, so two runs never share a temporary branch. From here on, a failure + // message closes with this sentence when the base and develop are where they were. const nothingMerged = `Nothing was merged: ${develop === null ? `${base} is as it was` : `${base} and ${develop} are as they were`}.` const tempPrefix = `release-merge/${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` const tempBranches = [] const mergeOnTempBranch = async ({ into, tip, head, message, label, headIsNew = false }) => { - // A tip that already holds the head has nothing to merge, which is what a retry after a late - // failure finds for both branches. Asked before the temporary branch is created, so that run - // writes nothing at all. A head this run has just made is on no branch yet: nothing to ask. + // If the tip already holds the head, there is nothing to merge. A retry after a late failure + // finds that for the base and for develop. The action checks this before it creates the + // temporary branch, so that retry writes nothing. When this run has just made the head, no + // branch holds it yet, so the check is skipped. if (!headIsNew && (await commitContainsBranch({ github, owner, repo, branch: label, ref: head, sha: tip }))) { core.info(`${into} already has ${label}: nothing to merge.`) @@ -812,13 +837,14 @@ async function runMerge({ github, core, env }) { } } - // Both merges exist: the branches move to them now, together. A retry that found a merge done - // has nothing to move for that branch. + // Every merge commit exists, so the branches move to them now, in one update. A branch that + // already held its merge is not part of the update. if (moves.length > 0) { await moveBranches({ github, core, owner, repo, moves, nothingMerged }) } } finally { - // After the move, not before: until then the temporary branches are what keeps the merge commits. + // The temporary branches are deleted after the update, not before. Until the update, they are + // the only branches that hold the merge commits. await deleteTempBranches({ github, core, owner, repo, names: tempBranches }) } diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index bbc74949..8eda6759 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -51,13 +51,13 @@ const TIP = { develop: 'cccccccccccccccccccccccccccccccccccccccc', } -/** The temporary branches of run 77, first attempt: where each merge is tried before a branch moves. */ +/** The temporary branches of run 77, first attempt. Each merge is made on one of them before a branch moves. */ const TEMP = { main: 'release-merge/77-1/main', develop: 'release-merge/77-1/develop', } -/** The repository's node ID, which the mutation that moves the branches is addressed with. */ +/** The repository's node ID. The mutation that moves the branches takes it to name the repository. */ const REPOSITORY_ID = 'R_kgDOplugin' /** The merge commits GitHub answers with: the release on main, then main on develop. */ @@ -169,9 +169,14 @@ function merges(responses) { } /** - * What the mutation that moves the branches answers, as GitHub sent each one to a real update: done, - * a branch that is no longer at the tip it was read at, a base that only takes pull requests, and a - * request that got no usable answer. The second names neither the branch nor the reason. + * The answers to the mutation that moves the branches. The first three are what GitHub sent to real + * updates: + * + * - `done`: the update was applied. + * - `moved`: a branch is no longer at the tip that it was read at. GitHub names neither the branch + * nor the reason. + * - `protected`: the base only takes pull requests. + * - `unanswered`: the request got no usable answer. */ const MOVE = { done: { body: { data: { updateRefs: { clientMutationId: null } } } }, @@ -202,11 +207,12 @@ const MOVE = { } /** - * Makes a branch answer differently from its second read on: what the action finds when it reads - * the branches again after an update that failed. + * Makes a branch answer differently from its second read on. The second read is the one that the + * action makes after a failed update. * * @param {object[]} rules The rules, with one per branch as `branches` builds them. - * @param {object} later Branch name to the tip it is at by then, or to a whole response. + * @param {object} later Maps a branch name to the tip that the second read returns, or to a whole + * response. * * @returns {object[]} The rules, with those branches answering their first read as before. */ @@ -224,11 +230,11 @@ function readAgain(rules, later) { } /** - * The rule under which a commit already holds another: what the action asks before it makes a - * temporary branch, and what a retry after a late failure finds for the base and for develop. + * The rule that answers that one commit already holds another. The action asks this before it makes + * a temporary branch, and a retry after a late failure gets this answer for the base and for develop. * * @param {string} tip The commit asked about: the tip of the base or of develop. - * @param {string} head What it holds; the release branch's tip when left out. + * @param {string} head The commit that `tip` holds. Defaults to the release branch's tip. * * @returns {object} The rule. */ @@ -300,7 +306,7 @@ function mergesAsked(requests) { } /** - * The branches each request to move them named, in order: one entry per mutation sent. + * The branch updates in each mutation that was sent, in order: one entry per mutation. * * @param {object[]} requests The requests recordingFetch recorded. * @@ -328,7 +334,7 @@ function written(requests) { return requested(requests.filter((request) => request.method !== 'GET')) } -/** The writes of a run that tried both merges and moved nothing: the temporary branches come and go. */ +/** The writes of a run that made both merges and moved nothing: it creates the temporary branches, then deletes them. */ const TEMP_ONLY = [ `POST ${API.refs}`, `POST ${API.merges}`, @@ -480,8 +486,8 @@ describe('release-merge', () => { `GET ${API.file('.puprc')}`, `GET ${API.file('plugin.php')}`, `GET ${API.file('readme.txt')}`, - // Main is asked whether it already holds the release branch. Develop is not asked about the - // commit made for main a moment later: no branch holds that one yet. + // The action asks GitHub whether main already holds the release branch. It does not ask + // whether develop holds the commit just made for main: no branch holds that commit yet. `GET ${API.compare(TIP.main)}`, // Each merge is made on a temporary branch at the tip that was read: nothing reaches main or // develop until both merges exist. @@ -491,7 +497,8 @@ describe('release-merge', () => { `POST ${API.merges}`, `GET ${API.repository}`, `POST ${API.graphql}`, - // The temporary branches go last, so the merge commits are reachable when the branches move. + // The temporary branches are deleted last, so the merge commits are still on a branch when + // main and develop move. `DELETE ${API.ref(TEMP.main)}`, `DELETE ${API.ref(TEMP.develop)}`, ]) @@ -504,13 +511,14 @@ describe('release-merge', () => { ) assert.deepStrictEqual(mergesAsked(requests), [ // The tip the version check read, not the branch by name: what was checked is what is merged. - // The message names main, the branch the commit is for, not the temporary branch. + // The message names main, the branch that the commit is for, not the temporary branch. { base: TEMP.main, head: TIP['release/M26.test'], commit_message: 'Merge release/M26.test into main' }, // The commit just made for main, not main by name: develop gets exactly what is released. { base: TEMP.develop, head: MERGED.main, commit_message: 'Merge main back into develop (release/M26.test)' }, ]) - // One mutation for both branches, each from the tip the merge was made on to its merge commit: - // GitHub moves both or neither, and a branch that is no longer at that tip is refused. + // One mutation moves both branches, each from the tip that its merge was made on to its merge + // commit. GitHub moves both or neither, and refuses the update when a branch is no longer at + // that tip. assert.deepStrictEqual(movesAsked(requests), [ [ { name: 'refs/heads/main', beforeOid: TIP.main, afterOid: MERGED.main }, @@ -638,7 +646,8 @@ describe('release-merge', () => { { method: 'GET', path: API.tag, body: { ref: 'refs/tags/4.17.0', object: { sha: MERGED.main, type: 'commit' } } }, PUBLISHED[1], ], - // One answer for two questions: the tag contains the branch, and so does main, at the same commit. + // The tag and main are at the same commit, so one rule answers two questions: the tag + // contains the branch, and main does too. extra: [holds(MERGED.main), holds(TIP.develop, MERGED.main)], }).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: MERGED.main } } } : rule)) ) @@ -653,7 +662,7 @@ describe('release-merge', () => { // published. Nothing else is read about main's tip. assert.strictEqual(outputs()['base-sha'], MERGED.main) assert.ok(!requested(requests).some((line) => line.startsWith(`GET ${API.commit('')}`)), requested(requests).join('\n')) - // Develop is asked about that commit, which it already has. + // The action asks whether develop holds that commit, and develop does. assert.ok(requested(requests).includes(`GET ${API.compare(TIP.develop, MERGED.main)}`), requested(requests).join('\n')) // Neither branch has anything to receive: no temporary branch is made, no merge is asked for // and nothing is moved. The run only reads. @@ -661,8 +670,8 @@ describe('release-merge', () => { }) it('moves only develop on a retry that finds the release on the base and not yet on develop', async (t) => { - // What a deploy stopped by a develop conflict left before the two merges were written - // together: the release on main, published or not, and develop without it. + // The state that an earlier version of the action left after a conflict on develop: the + // release is on main, published or not, and develop does not have it. const { github, requests } = await octokitFor( repositoryWith({ merges: [MERGE.develop], @@ -696,8 +705,9 @@ describe('release-merge', () => { await runMerge({ github, core, env: mergeEnv() }) - // The tip that was read is the one checked, once main is found to hold the branch and before - // develop is asked about that same commit; main is not read again, and no version file at the base. + // The action finds that main holds the branch, checks that the tip it read is the commit that + // merged the branch, then asks about develop. It does not read main again, and it reads no + // version file at the base. assert.deepStrictEqual(requested(requests).slice(-3), [ `GET ${API.compare(MERGED.main)}`, `GET ${API.commit(MERGED.main)}`, @@ -729,16 +739,17 @@ describe('release-merge', () => { `parents ${parents.length}` ) - // Refused before develop is asked about, with nothing written and no output set. + // The run is refused before the action asks about develop. Nothing is written and no + // output is set. assert.deepStrictEqual(written(requests), []) assert.deepStrictEqual(outputs(), {}) } }) - it('reads a merge GitHub answers as already merged as nothing to merge, and deletes the temporary branch', async (t) => { - // The comparison said main lacks the branch and the merge endpoint then answers 204, its - // answer for a base that already contains the head. The two agree in practice; if they ever - // do not, the 204 is not read as a merge commit. + it('treats a 204 from the merge endpoint as nothing to merge, and deletes the temporary branch', async (t) => { + // The comparison said that main lacks the branch, and the merge endpoint then answers 204, + // which means that the base already contains the head. The two answers agree in practice. If + // they ever disagree, the action must not read the 204 as a merge commit. const other = '8888888888888888888888888888888888888888' const { github, requests } = await octokitFor( repositoryWith({ @@ -840,7 +851,7 @@ describe('release-merge', () => { // The release branch itself is what was merged, so there is no other branch to merge by hand. assert.doesNotMatch(logged(), /already holds/) assert.strictEqual(outputs().conflict, 'main') - // Develop was never tried, and the one temporary branch is gone. + // The merge into develop was never tried, and the one temporary branch was deleted. assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(TEMP.main)}`]) }) @@ -853,15 +864,15 @@ describe('release-merge', () => { /main into develop stopped on a conflict\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. If develop already holds release\/M26\.test, the conflict is with a commit of main that develop lacks: merge main into develop as well\. Nothing was merged: main and develop are as they were\./ ) - // The merge back can conflict with a commit only main has. Merging the release branch by hand - // shows no conflict then, so the line also says what to do when that was already done. + // The merge into develop can conflict with a commit that only main has. In that case, merging + // the release branch by hand shows no conflict, so the line also says what to do next. assert.match( logged(), /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. If develop already holds release\/M26\.test, the conflict is with a commit of main that develop lacks: merge main into develop as well\. Nothing was merged: main and develop are as they were\.$/m ) assert.strictEqual(outputs().conflict, 'develop') - // The merge for main was made, on its temporary branch only: main is never asked to move, so - // the release is not on it while develop waits for a person. + // The merge for main was made on its temporary branch only. The action never asks GitHub to + // move main, so main does not hold the release while develop waits for a person. assert.deepStrictEqual(written(requests), TEMP_ONLY) assert.deepStrictEqual(movesAsked(requests), []) assert.ok(!requested(requests).includes(`GET ${API.repository}`), requested(requests).join('\n')) @@ -891,8 +902,8 @@ describe('release-merge', () => { it('fails with neither branch moved, naming the branch that received a commit, and says to run again', async (t) => { // A commit reached main after its tip was read. The merge commit was made on the old tip, so - // GitHub refuses the update, and with it the one for develop. Its answer names no branch and - // no reason, so the action reads the two again to say which one moved. + // GitHub refuses the update for main, and the update for develop with it. GitHub's answer + // names no branch and no reason, so the action reads both branches again to say which one moved. const later = '9999999999999999999999999999999999999999' const { github, requests } = await octokitFor( readAgain( @@ -927,8 +938,8 @@ describe('release-merge', () => { }) it('says to change the rule, not to run again, when GitHub does not move the branches and neither received a commit', async (t) => { - // A base that only takes pull requests: the merges are made on the temporary branches, and the - // update is where the rule answers. Running the step again would get the same answer. + // The base only takes pull requests. The merges are made on the temporary branches, so the + // rule refuses the update, not the merges. Running the step again would get the same answer. const { github, requests } = await octokitFor(repositoryWith({ move: MOVE.protected })) const { core, outputs, logged } = await coreWithOutputs(t) @@ -943,8 +954,8 @@ describe('release-merge', () => { }) it('goes on with a warning when the request for the update failed and the branches hold their merge commits', async (t) => { - // The connection dropped after GitHub applied the update. Read again, both branches are at - // their merge commits: the release is merged, and the caller builds it. + // The connection dropped after GitHub applied the update. The action reads both branches + // again and finds them at their merge commits: the release is merged, and the caller builds it. const { github, requests } = await octokitFor( readAgain(repositoryWith({ move: MOVE.unanswered }), { main: MERGED.main, develop: MERGED.develop }) ) @@ -962,11 +973,12 @@ describe('release-merge', () => { assert.deepStrictEqual(written(requests).slice(-2), [`DELETE ${API.ref(TEMP.main)}`, `DELETE ${API.ref(TEMP.develop)}`]) }) - it('takes the update as applied when the branch holds its merge commit under a commit it received since', async (t) => { + it('takes the update as applied when the branch received a commit on top of its merge commit', async (t) => { // The update moved main to its merge commit, the answer was lost, and a commit reached main - // before it was read again. Its tip is neither the old one nor the merge commit, and it - // descends from the merge commit: the release is on main. "Nothing was merged" would be false, - // and a retry would find main moved on and ask for the release to be finished by hand. + // before the action read it again. Main's tip is neither the old tip nor the merge commit, and + // it descends from the merge commit: the release is on main. "Nothing was merged" would be + // false. A retry would then find that main moved on, and ask a person to finish the release + // by hand. const later = '9999999999999999999999999999999999999999' const { github, requests } = await octokitFor( readAgain( @@ -984,7 +996,8 @@ describe('release-merge', () => { await runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }) assert.match(logged(), /::warning::Moving main failed \(HTTP 502: Server Error\), but the update was applied: main holds its merge commit\./) - // The merge commit, not main's tip: what the caller builds is the release, without the later commit. + // base-sha is the merge commit, not main's tip: the caller builds the release without the + // later commit. assert.strictEqual(outputs()['base-sha'], MERGED.main) assert.deepStrictEqual(requested(requests).slice(-4), [ `POST ${API.graphql}`, @@ -1012,8 +1025,9 @@ describe('release-merge', () => { }) it('does not say nothing was merged when a moved branch cannot be compared with its merge commit', async (t) => { - // No answer to the update, main is at another commit, and the comparison that would tell - // whether that commit holds the merge commit fails. Either can be true. + // The update got no answer, and main is at another commit. The comparison that would tell + // whether that commit holds the merge commit fails, so the update may or may not have been + // applied. const later = '9999999999999999999999999999999999999999' const { github } = await octokitFor( readAgain( @@ -1054,8 +1068,9 @@ describe('release-merge', () => { }) it('says what each branch holds, and not that nothing was merged, when a failed update cannot be confirmed', async (t) => { - // No answer to the update, and develop cannot be read afterwards: main holds the release and - // nobody knows about develop. The message must not claim either way. + // The update got no answer, and develop cannot be read afterwards. Main holds the release, + // and the state of develop is unknown. The message must not say that the release was merged, + // nor that it was not. const { github } = await octokitFor( readAgain(repositoryWith({ move: MOVE.unanswered }), { main: MERGED.main, develop: { status: 500, body: { message: 'boom' } } }) ) @@ -1084,15 +1099,15 @@ describe('release-merge', () => { /Reading stellarwp\/plugin failed \(HTTP 500\): boom\. Nothing was merged: main and develop are as they were\./ ) - // Both merges were made, on the temporary branches only: no update is asked for. + // Both merges were made on the temporary branches only, and no update is asked for. assert.deepStrictEqual(written(requests), TEMP_ONLY) assert.deepStrictEqual(movesAsked(requests), []) assert.deepStrictEqual(outputs(), {}) }) - it('fails before any merge when the temporary branch cannot be created, naming the token, the rules and a branch in the way', async (t) => { - // What GitHub answers when a ruleset restricts who creates branches, and also when a branch - // named release-merge is in the way: the same words for both, so the message gives both causes. + it('fails before any merge when the temporary branch cannot be created, naming the token, the rules and a branch named release-merge', async (t) => { + // GitHub gives this answer when a ruleset restricts who creates branches, and also when the + // repository has a branch named release-merge. The message gives both causes. const { github, requests } = await octokitFor( repositoryWith({ tempBranch: { status: 422, body: { message: 'Reference update failed' } } }) ) @@ -1109,8 +1124,9 @@ describe('release-merge', () => { }) it('says to delete the branch, not to change the token, when a temporary branch of that name already exists', async (t) => { - // A branch another run of this step left behind under the same run and attempt. The second - // one is refused here, so the first, which holds the merge commit for main by then, is deleted. + // Another run of this step left a branch with the same run id and attempt. Here GitHub refuses + // the second temporary branch. The first one holds the merge commit for main by then, and the + // action deletes it. const { github, requests } = await octokitFor( repositoryWith({ tempBranch: { responses: [{ status: 201, body: {} }, { status: 422, body: { message: 'Reference already exists' } }] }, From 8c85c1941a4aa82d9702cc4722f63c336159a738 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 13:51:50 -0300 Subject: [PATCH 08/13] release-merge: resolve a conflict in the order of a release merged by hand On a conflict in the merge back, the message said to merge the release branch into develop. For a hotfix cut from the base, that puts the fix on develop before it is on the base, the opposite of how a release is merged by hand. It also missed a conflict with a commit that only the base has, which took a second merge by hand. The message now gives one instruction for a conflict in either merge: merge the release branch into the base, then the base into develop, resolve, push both, and run the step again. The merge of the base into develop shows every conflict at once. A merge by hand of a hotfix branch that is directly ahead of the base is a fast-forward, with no merge commit. The retry refused that base as one that had moved on. It now accepts a base whose tip is the release branch's tip: that tip holds nothing but the release. SOFT-5829 --- .github/actions/release-merge/README.md | 58 ++++++++++++------- .github/actions/release-merge/action.yml | 4 +- .../actions/release-merge/release-merge.js | 38 +++++++----- .../release-merge/release-merge.test.js | 50 ++++++++++++---- 4 files changed, 99 insertions(+), 51 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 1a0a0fb5..980bf49e 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -51,8 +51,9 @@ In this order, stopping at the first failure: 3. Makes the merge of the release branch into the base branch, a merge commit `Merge release/… into main` (or `into master`), on a temporary branch: the base is not written yet. When the base already holds the branch, a retry, nothing is merged: the commit to build is then the one the - tag points at when the version is already published, and otherwise the base's tip, which has to - be the commit that merged the branch. A base that has moved on since, by any commit, is refused. + tag points at when the version is already published, and otherwise the base's tip. That tip has + to be the commit that merged the branch, or the branch's own tip when the base was + fast-forwarded to it. A base that has moved on since, by any commit, is refused. See [Versions it refuses](#versions-it-refuses). 4. Makes the merge of that **merged commit** into develop, a merge commit `Merge main back into develop (release/…)`, on a second temporary branch. The base branch rather than the release @@ -109,13 +110,13 @@ does: - A branch that already holds the other has nothing to merge. The action asks GitHub that before it makes the temporary branch, so a run that finds both branches that way writes nothing: no temporary branch, no merge, no update. What the caller then builds and publishes is the commit the tag - points at, or the base's tip when there is no tag yet, which has to be the commit that merged - the branch; see [Versions it refuses](#versions-it-refuses). + points at, or the base's tip when there is no tag yet. That tip has to be the commit that merged + the branch, or the branch's own tip; see [Versions it refuses](#versions-it-refuses). - A version already published counts as a retry only when its tag contains the release branch, that is, when it was published from this branch. See [Versions it refuses](#versions-it-refuses). release-publish then skips the release. -- A conflict resolved by hand, then pushed, is not found again: the branch then holds the release - branch, and the next run goes on from there. The one case that takes a second merge by hand is in +- A conflict resolved by hand, then pushed, is not found again. The base and develop then hold the + release, and the next run goes on from there. See [When a merge conflicts](#when-a-merge-conflicts). - A release that is on the base and not on develop, which a deploy stopped by a conflict left before the two merges were written together, gets the merge it lacks: only develop moves. @@ -129,24 +130,36 @@ GitHub refuses the merge and the step fails with the branch named. Neither the b written, whichever merge conflicted: a conflict merging the base into develop does not leave the release on the base. -To resolve it, merge the release branch into the branch named, by hand, resolve the conflict, push, -and run the step again. That is the release branch for a conflict on develop too: the base does not -hold the release yet, so there is nothing else to merge from. The next run finds the release -branch in the branch resolved by hand, with nothing left to conflict on. The API does not say which -files conflict; the merge done by hand shows them. +To resolve it, merge the release by hand, in the order of a release that is merged by hand: -A conflict on develop can also be with a commit the base has and develop lacks, a hotfix that never -went back to develop. Merging the release branch into develop then shows no conflict, and the next -run stops on develop again: merge the base into develop as well, resolve, push, and run the step -again. The line for develop says both steps. +1. Merge the release branch into the base. +2. Merge the base into develop. +3. Resolve the conflict where it shows, and push both branches. +4. Run the step again. + +The next run finds the release on both branches and merges nothing. It goes on with the release, +and the caller builds and publishes the commit that the merge by hand left on the base. The steps +are the same for a conflict on the base and for a conflict on develop. They are also the same for a +hotfix cut from the base and for a release cut from develop. The API does not say which files +conflict; the merge done by hand shows them. + +The merge into the base can be a merge commit or a fast-forward. A fast-forward is what git does +for a hotfix branch that is directly ahead of the base. A squash or a rebase does not put the +commits of the release branch on the base, so the next run would merge the branch again. + +If step 2 merged the release branch into develop, not the base, the next run merges the base into +develop itself. + +Between the push and the next run, the base holds the release with no tag and no build. Run the +step again right after the push. The log carries this line, on one line: ``` -MERGE_CONFLICT: merging main into develop conflicted. Merge release/M26.test into develop by hand, -resolve the conflict and push, then run this step again. If develop already holds release/M26.test, -the conflict is with a commit of main that develop lacks: merge main into develop as well. Nothing -was merged: main and develop are as they were. +MERGE_CONFLICT: merging main into develop conflicted. Merge release/M26.test into main by hand, +then main into develop, resolve the conflict and push both branches. Then run this step again: it +finds the merges done and goes on with the release. Nothing was merged: main and develop are as +they were. ``` and the `conflict` output names the branch. The [release @@ -190,9 +203,10 @@ other reason. The message says which, and whether running the step again is enou failed after the deploy, then another release or a commit pushed straight to the base, then a retry of this one would build those changes and publish them under this version: a deploy skips the tag it finds on wordpress.org and zips the current trunk, and release-publish tags whatever - commit it is given. So when the version has no tag yet, the base's tip has to be the commit that - merged the branch, the merge commit whose second parent is the branch's tip; a tip that is any - other commit, bump or not, is refused. Finish this release by hand at the commit that merged the + commit it is given. So when the version has no tag yet, the base's tip has to be one of two + commits: the commit that merged the branch (the merge commit whose second parent is the branch's + tip), or the branch's own tip, when the base was fast-forwarded to it. A tip that is any other + commit, bump or not, is refused. Finish this release by hand at the commit that merged the branch into the base, or release another version. A version already published is not refused (the `already-published` output says so, for the caller's own steps): its retry builds the commit the tag points at, what was published, and deploy and publish skip diff --git a/.github/actions/release-merge/action.yml b/.github/actions/release-merge/action.yml index 056c0f0a..0ff450a2 100644 --- a/.github/actions/release-merge/action.yml +++ b/.github/actions/release-merge/action.yml @@ -37,8 +37,8 @@ inputs: whose tag already exists, unless that tag's release was published from this branch, which makes the run a retry. On a retry nothing is merged, and the commit the caller then builds and publishes is the one the tag points at, or, before the tag exists, the base's tip, which has to - be the commit that merged the branch. The release itself is published by release-publish, after - this action. + be the commit that merged the branch or the branch's own tip. The release itself is published + by release-publish, after this action. required: true base-branch: description: 'The branch the release is merged into: `main`, or `master` where that is the name.' diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index 7103fdca..d86e1b09 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -448,8 +448,8 @@ async function deleteTempBranches({ github, core, owner, repo, names }) { * @param {string} options.head What is merged: a commit hash. * @param {string} options.message The merge commit message. * @param {string} options.label How to name `head` in the log, e.g. `release/M26.test`. - * @param {string} options.branch The release branch. On a conflict in either merge, the - * message tells a person to merge this branch into `into`. + * @param {string} options.howToResolve The sentences that tell a person how to resolve a + * conflict by hand. They are the same for either merge. * @param {string} options.nothingMerged The sentence that closes a failure message. It says that * the base and develop were not written. * @@ -458,23 +458,12 @@ async function deleteTempBranches({ github, core, owner, repo, names }) { * @returns {Promise<{merged: boolean, sha: string}>} Whether a merge commit was made, and the commit * that `into` should point at: the merge commit, or `tip` when there was nothing to merge. */ -async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, branch, nothingMerged }) { +async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, howToResolve, nothingMerged }) { let response try { response = await github.rest.repos.merge({ owner, repo, base: tempBranch, head, commit_message: message }) } catch (error) { if (isConflict(error)) { - // The merge into develop takes the base, not the release branch, so it can also conflict - // with a commit that only the base has. In that case, merging the release branch into - // develop by hand shows no conflict, and the next run stops on this line again. The second - // sentence tells the reader to merge the base into develop as well. - const howToResolve = - `Merge ${branch} into ${into} by hand, resolve the conflict and push, then run this step again.` + - (label === branch - ? '' - : ` If ${into} already holds ${branch}, the conflict is with a commit of ${label} that ${into} lacks: ` + - `merge ${label} into ${into} as well.`) - // core.info prints a plain line: the marker has to open an output line to count, see the README. core.setOutput('conflict', into) core.info(`MERGE_CONFLICT: merging ${label} into ${into} conflicted. ${howToResolve} ${nothingMerged}`) @@ -760,6 +749,16 @@ async function runMerge({ github, core, env }) { // the run id and the attempt, so two runs never share a temporary branch. From here on, a failure // message closes with this sentence when the base and develop are where they were. const nothingMerged = `Nothing was merged: ${develop === null ? `${base} is as it was` : `${base} and ${develop} are as they were`}.` + // A conflict is resolved the way a release is merged by hand: the release branch into the base, + // then the base into develop. The instruction is the same for a conflict in either merge, and for + // a hotfix cut from the base as for a release cut from develop. The next run then finds each + // branch holding what would be merged into it. + const howToResolve = + develop === null + ? `Merge ${branch} into ${base} by hand, resolve the conflict and push. Then run this step again: ` + + 'it finds the merge done and goes on with the release.' + : `Merge ${branch} into ${base} by hand, then ${base} into ${develop}, resolve the conflict and push ` + + 'both branches. Then run this step again: it finds the merges done and goes on with the release.' const tempPrefix = `release-merge/${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` const tempBranches = [] const mergeOnTempBranch = async ({ into, tip, head, message, label, headIsNew = false }) => { @@ -777,7 +776,7 @@ async function runMerge({ github, core, env }) { await createTempBranch({ github, owner, repo, name: tempBranch, sha: tip, nothingMerged }) tempBranches.push(tempBranch) - return mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, branch, nothingMerged }) + return mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, howToResolve, nothingMerged }) } let mergedBase @@ -801,12 +800,19 @@ async function runMerge({ github, core, env }) { // caller's deploy and publish each skip what they find done. So when the version is already // published, the retry builds the commit the tag was made from, what was published, and the base // moving on changes nothing; when it is not, the tip has to be the commit that merged the branch. + // + // A base that was fast-forwarded to the release branch has no merge commit: its tip is the + // branch's tip. That is what a merge by hand leaves for a hotfix cut from the base, and that tip + // holds nothing but the release, so the retry builds it. baseSha = baseMerge.sha if (!mergedBase) { if (tagged) { baseSha = tagSha core.info(`${version} builds at ${tagSha}, the commit it was published from.`) - } else if (!(await commitMergesBranch({ github, owner, repo, branch, ref: branchSha, sha: baseTip }))) { + } else if ( + baseTip !== branchSha && + !(await commitMergesBranch({ github, owner, repo, branch, ref: branchSha, sha: baseTip })) + ) { throw new Error( `${base} already had ${branch} and has moved on since: its tip ${baseTip} is not the commit that merged ` + `${branch}, so a build from it would publish other changes under ${version}. There is nothing left ` + diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index 8eda6759..41c9c39b 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -717,6 +717,26 @@ describe('release-merge', () => { assert.strictEqual(outputs()['base-sha'], MERGED.main) }) + it('goes on with an unpublished retry whose base was fast-forwarded to the release branch', async (t) => { + // What a merge by hand leaves for a hotfix cut from main: git moves main to the branch's tip + // with no merge commit. That tip holds nothing but the release, so the retry builds it, and + // no commit is read to look for a merge. + const tip = TIP['release/M26.test'] + const { github, requests } = await octokitFor( + repositoryWith({ + extra: [{ method: 'GET', path: API.compare(tip), body: { status: 'identical' } }, holds(TIP.develop)], + }).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: tip } } } : rule)) + ) + const { core, outputs, logged } = await coreWithOutputs(t) + + await runMerge({ github, core, env: mergeEnv() }) + + assert.match(logged(), /main already has release\/M26\.test: nothing to merge/) + assert.strictEqual(outputs()['base-sha'], tip) + assert.ok(!requested(requests).some((line) => line.startsWith(`GET ${API.commit('')}`)), requested(requests).join('\n')) + assert.deepStrictEqual(written(requests), []) + }) + it('refuses an unpublished retry once the base has moved on, bump or not, so other changes are not published under this version', async (t) => { // 4.17.0 reached the deploy but its publish failed, and main moved on since: another release // was merged, or a commit was pushed straight to main. The caller's deploy would skip the @@ -843,13 +863,16 @@ describe('release-merge', () => { await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /release\/M26\.test into main stopped on a conflict\. Merge release\/M26\.test into main by hand, resolve the conflict and push, then run this step again\. Nothing was merged: main and develop are as they were\./ + /release\/M26\.test into main stopped on a conflict\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\./ ) - // The marker opens a line of its own: that is what the dashboard's detector anchors on. - assert.match(logged(), /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand.*main and develop are as they were\.$/m) - // The release branch itself is what was merged, so there is no other branch to merge by hand. - assert.doesNotMatch(logged(), /already holds/) + // The marker opens a line of its own: that is what the dashboard's detector anchors on. The + // instruction is the order of a release merged by hand: the release branch into main, then + // main into develop. + assert.match( + logged(), + /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\.$/m + ) assert.strictEqual(outputs().conflict, 'main') // The merge into develop was never tried, and the one temporary branch was deleted. assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(TEMP.main)}`]) @@ -861,14 +884,15 @@ describe('release-merge', () => { await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /main into develop stopped on a conflict\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. If develop already holds release\/M26\.test, the conflict is with a commit of main that develop lacks: merge main into develop as well\. Nothing was merged: main and develop are as they were\./ + /main into develop stopped on a conflict\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\./ ) - // The merge into develop can conflict with a commit that only main has. In that case, merging - // the release branch by hand shows no conflict, so the line also says what to do next. + // The instruction is the same as for a conflict on main: the release branch into main first, + // then main into develop. The merge of main into develop shows every conflict, also one with + // a commit that only main has. assert.match( logged(), - /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into develop by hand, resolve the conflict and push, then run this step again\. If develop already holds release\/M26\.test, the conflict is with a commit of main that develop lacks: merge main into develop as well\. Nothing was merged: main and develop are as they were\.$/m + /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\.$/m ) assert.strictEqual(outputs().conflict, 'develop') // The merge for main was made on its temporary branch only. The action never asks GitHub to @@ -878,12 +902,16 @@ describe('release-merge', () => { assert.ok(!requested(requests).includes(`GET ${API.repository}`), requested(requests).join('\n')) }) - it('names only the base as unchanged when there is no develop branch', async (t) => { + it('names only the base, in the instruction and as unchanged, when there is no develop branch', async (t) => { const { github } = await octokitFor(repositoryWith({ branches: ['release/M26.test', 'main'], merges: [MERGE.conflict] })) const { core, logged } = await coreWithOutputs(t) await assert.rejects(runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }), /Nothing was merged: main is as it was\./) - assert.match(logged(), /^MERGE_CONFLICT: .*Nothing was merged: main is as it was\.$/m) + assert.match( + logged(), + /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand, resolve the conflict and push\. Then run this step again: it finds the merge done and goes on with the release\. Nothing was merged: main is as it was\.$/m + ) + assert.doesNotMatch(logged(), /develop/) }) it('fails without the marker when GitHub refuses a merge for any other reason', async (t) => { From cdb039d980757216be07803a719ad329ce2791bb Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 14:11:46 -0300 Subject: [PATCH 09/13] release-merge: say when a merge by hand is a fast-forward The README and a comment gave the hotfix as the case where a merge by hand fast-forwards the base. It also happens for a release cut from a develop that the base was merged back into: the branch already contains the base, so git moves the base to the branch's tip with no merge commit. Text only. SOFT-5829 --- .github/actions/release-merge/README.md | 5 +++-- .github/actions/release-merge/release-merge.js | 5 +++-- tests/actions/release-merge/release-merge.test.js | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 980bf49e..71e8b92c 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -144,8 +144,9 @@ hotfix cut from the base and for a release cut from develop. The API does not sa conflict; the merge done by hand shows them. The merge into the base can be a merge commit or a fast-forward. A fast-forward is what git does -for a hotfix branch that is directly ahead of the base. A squash or a rebase does not put the -commits of the release branch on the base, so the next run would merge the branch again. +when the release branch already contains the base: a hotfix cut from the base, or a release cut +from a develop that the base was merged back into. A squash or a rebase does not put the commits +of the release branch on the base, so the next run would merge the branch again. If step 2 merged the release branch into develop, not the base, the next run merges the base into develop itself. diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index d86e1b09..ad1bdc40 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -802,8 +802,9 @@ async function runMerge({ github, core, env }) { // moving on changes nothing; when it is not, the tip has to be the commit that merged the branch. // // A base that was fast-forwarded to the release branch has no merge commit: its tip is the - // branch's tip. That is what a merge by hand leaves for a hotfix cut from the base, and that tip - // holds nothing but the release, so the retry builds it. + // branch's tip. A merge by hand leaves that when the branch already contains the base, as a + // hotfix cut from the base does, and a release cut from a develop that the base was merged back + // into. That tip holds nothing but the release, so the retry builds it. baseSha = baseMerge.sha if (!mergedBase) { if (tagged) { diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index 41c9c39b..8796d159 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -718,9 +718,9 @@ describe('release-merge', () => { }) it('goes on with an unpublished retry whose base was fast-forwarded to the release branch', async (t) => { - // What a merge by hand leaves for a hotfix cut from main: git moves main to the branch's tip - // with no merge commit. That tip holds nothing but the release, so the retry builds it, and - // no commit is read to look for a merge. + // What a merge by hand leaves when the branch already contains main, as a hotfix cut from + // main does: git moves main to the branch's tip with no merge commit. That tip holds nothing + // but the release, so the retry builds it, and no commit is read to look for a merge. const tip = TIP['release/M26.test'] const { github, requests } = await octokitFor( repositoryWith({ From 6b13c86d274faf3563891dbcc91123b5973f30e5 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 14:28:11 -0300 Subject: [PATCH 10/13] release-merge: say to restore the release branch that a merge by hand deletes To resolve a conflict, the release owner merges the release branch into the base by hand. That marks the branch's pull request as merged, and a repository with "Automatically delete head branches" on then deletes the release branch. The next run is started on that branch and has nothing to start on. The conflict message and the README now say to restore the branch on its pull request before running the step again. SOFT-5829 --- .github/actions/release-merge/README.md | 17 +++++++++++++---- .github/actions/release-merge/release-merge.js | 13 ++++++++++--- .../actions/release-merge/release-merge.test.js | 13 +++++++------ 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 71e8b92c..cb7c0e5e 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -135,7 +135,8 @@ To resolve it, merge the release by hand, in the order of a release that is merg 1. Merge the release branch into the base. 2. Merge the base into develop. 3. Resolve the conflict where it shows, and push both branches. -4. Run the step again. +4. If GitHub deleted the release branch after the push, restore it on its pull request. +5. Run the step again. The next run finds the release on both branches and merges nothing. It goes on with the release, and the caller builds and publishes the commit that the merge by hand left on the base. The steps @@ -151,6 +152,13 @@ of the release branch on the base, so the next run would merge the branch again. If step 2 merged the release branch into develop, not the base, the next run merges the base into develop itself. +Step 4 is for a repository that has "Automatically delete head branches" on. The push marks the +pull request of the release branch as merged, and GitHub then deletes the branch. A run that is +started on the release branch, as a deploy from the release dashboard is, has no branch to start +on. Click **Restore branch** on the pull request +([GitHub docs](https://docs.github.com/en/repositories/configuring-branches-and-merges-in-your-repository/managing-branches-in-your-repository/deleting-and-restoring-branches-in-a-pull-request)): +the branch comes back at its last commit. + Between the push and the next run, the base holds the release with no tag and no build. Run the step again right after the push. @@ -158,9 +166,10 @@ The log carries this line, on one line: ``` MERGE_CONFLICT: merging main into develop conflicted. Merge release/M26.test into main by hand, -then main into develop, resolve the conflict and push both branches. Then run this step again: it -finds the merges done and goes on with the release. Nothing was merged: main and develop are as -they were. +then main into develop, resolve the conflict and push both branches. If GitHub then deletes +release/M26.test, because its pull request is marked as merged, restore the branch on the pull +request. Then run this step again: it finds the merges done and goes on with the release. Nothing +was merged: main and develop are as they were. ``` and the `conflict` output names the branch. The [release diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index ad1bdc40..1a70a734 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -753,12 +753,19 @@ async function runMerge({ github, core, env }) { // then the base into develop. The instruction is the same for a conflict in either merge, and for // a hotfix cut from the base as for a release cut from develop. The next run then finds each // branch holding what would be merged into it. + // + // The merge by hand marks the pull request of the release branch as merged. A repository with + // "Automatically delete head branches" on then deletes the release branch, and a run started on + // that branch has nothing to start on. So the instruction says to restore the branch first. + const restore = + `If GitHub then deletes ${branch}, because its pull request is marked as merged, restore the branch ` + + 'on the pull request.' const howToResolve = develop === null - ? `Merge ${branch} into ${base} by hand, resolve the conflict and push. Then run this step again: ` + - 'it finds the merge done and goes on with the release.' + ? `Merge ${branch} into ${base} by hand, resolve the conflict and push. ${restore} Then run this step ` + + 'again: it finds the merge done and goes on with the release.' : `Merge ${branch} into ${base} by hand, then ${base} into ${develop}, resolve the conflict and push ` + - 'both branches. Then run this step again: it finds the merges done and goes on with the release.' + `both branches. ${restore} Then run this step again: it finds the merges done and goes on with the release.` const tempPrefix = `release-merge/${env.GITHUB_RUN_ID}-${env.GITHUB_RUN_ATTEMPT}` const tempBranches = [] const mergeOnTempBranch = async ({ into, tip, head, message, label, headIsNew = false }) => { diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index 8796d159..53af98eb 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -863,15 +863,16 @@ describe('release-merge', () => { await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /release\/M26\.test into main stopped on a conflict\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\./ + /release\/M26\.test into main stopped on a conflict\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. If GitHub then deletes release\/M26\.test, because its pull request is marked as merged, restore the branch on the pull request\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\./ ) // The marker opens a line of its own: that is what the dashboard's detector anchors on. The // instruction is the order of a release merged by hand: the release branch into main, then - // main into develop. + // main into develop. It also says to restore the release branch when GitHub deletes it, which + // a repository that deletes the branch of a merged pull request does after the merge by hand. assert.match( logged(), - /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\.$/m + /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. If GitHub then deletes release\/M26\.test, because its pull request is marked as merged, restore the branch on the pull request\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\.$/m ) assert.strictEqual(outputs().conflict, 'main') // The merge into develop was never tried, and the one temporary branch was deleted. @@ -884,7 +885,7 @@ describe('release-merge', () => { await assert.rejects( runMerge({ github, core, env: mergeEnv() }), - /main into develop stopped on a conflict\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\./ + /main into develop stopped on a conflict\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. If GitHub then deletes release\/M26\.test, because its pull request is marked as merged, restore the branch on the pull request\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\./ ) // The instruction is the same as for a conflict on main: the release branch into main first, @@ -892,7 +893,7 @@ describe('release-merge', () => { // a commit that only main has. assert.match( logged(), - /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\.$/m + /^MERGE_CONFLICT: merging main into develop conflicted\. Merge release\/M26\.test into main by hand, then main into develop, resolve the conflict and push both branches\. If GitHub then deletes release\/M26\.test, because its pull request is marked as merged, restore the branch on the pull request\. Then run this step again: it finds the merges done and goes on with the release\. Nothing was merged: main and develop are as they were\.$/m ) assert.strictEqual(outputs().conflict, 'develop') // The merge for main was made on its temporary branch only. The action never asks GitHub to @@ -909,7 +910,7 @@ describe('release-merge', () => { await assert.rejects(runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }), /Nothing was merged: main is as it was\./) assert.match( logged(), - /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand, resolve the conflict and push\. Then run this step again: it finds the merge done and goes on with the release\. Nothing was merged: main is as it was\.$/m + /^MERGE_CONFLICT: merging release\/M26\.test into main conflicted\. Merge release\/M26\.test into main by hand, resolve the conflict and push\. If GitHub then deletes release\/M26\.test, because its pull request is marked as merged, restore the branch on the pull request\. Then run this step again: it finds the merge done and goes on with the release\. Nothing was merged: main is as it was\.$/m ) assert.doesNotMatch(logged(), /develop/) }) From 1b1ca205e7cfd9a1830c2030e6e8be56d3e0d87c Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 14:59:13 -0300 Subject: [PATCH 11/13] release-merge: refuse a merge made on a commit other than the tip that was read The merge endpoint takes the temporary branch by name. Someone who can push to the repository could push a commit to that branch between its creation and the merge. GitHub then made the merge on their commit. The merge commit still descends from the tip the action read, so the update that moves the base accepted it, and their commit reached the base with the release. The action now checks that the first parent of each merge commit is the tip it read. When it is not, or when the answer has no parents, the step fails before it moves a branch and says to run it again. SOFT-5829 --- .github/actions/release-merge/README.md | 6 ++ .../actions/release-merge/release-merge.js | 37 ++++++++++- .../release-merge/release-merge.test.js | 64 ++++++++++++++++++- 3 files changed, 103 insertions(+), 4 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index cb7c0e5e..8c516537 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -82,6 +82,12 @@ every update or none. Each branch moves from the tip its merge was made on to th fast-forward. A branch that received a commit in the meantime is no longer at that tip: GitHub refuses the update, neither branch moves, and the step fails with a message to run it again. +The merge endpoint takes the temporary branch by name. A commit that someone else pushes to that +branch before the merge would end up under the merge commit, and the update would carry it to the +base. So the step checks that each merge commit was made on the tip the action read: its first +parent is that tip. When it is not, the step fails before it moves a branch, with a message to run +it again. + GitHub's answer to an update it does not make gives no reason for a branch that moved, so the step reads the two branches again before it fails, and the message says what it found: diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index 1a70a734..e74f6c32 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -423,6 +423,17 @@ async function deleteTempBranches({ github, core, owner, repo, names }) { } } +/** + * A commit hash as a message shows it: its first seven characters. + * + * @param {string} sha The commit hash. + * + * @returns {string} The short hash. + */ +function shortSha(sha) { + return sha.slice(0, 7) +} + /** * Makes one merge commit, on a temporary branch that starts at the tip of `into`, the branch that * the merge is for. This function does not write `into`: moveBranches moves it to the merge commit @@ -436,6 +447,9 @@ async function deleteTempBranches({ github, core, owner, repo, names }) { * does. The endpoint answers 204 in that case, and this function also reads a 204 as nothing to * merge. * + * The endpoint takes the temporary branch by name, so the merge is made on whatever commit that + * branch points at. The function checks that this commit is `tip`, and throws when it is not. + * * @param {object} options * @param {object} options.github The Octokit github-script supplies. * @param {object} options.core The @actions/core toolkit. @@ -453,7 +467,7 @@ async function deleteTempBranches({ github, core, owner, repo, names }) { * @param {string} options.nothingMerged The sentence that closes a failure message. It says that * the base and develop were not written. * - * @throws {Error} When the merge conflicts or GitHub refuses it. + * @throws {Error} When the merge conflicts, GitHub refuses it, or it was not made on `tip`. * * @returns {Promise<{merged: boolean, sha: string}>} Whether a merge commit was made, and the commit * that `into` should point at: the merge commit, or `tip` when there was nothing to merge. @@ -479,6 +493,26 @@ async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, hea return { merged: false, sha: tip } } + // The first parent of the merge commit is the commit that the merge was made on. Anyone who can + // push to the repository can push to the temporary branch before the merge. The merge commit then + // descends from their commit and from `tip`, so the update at the end, which only checks that + // `into` is still at `tip`, would move `into` to it and carry their commit along. + const madeOn = response.data.parents?.[0]?.sha + if (madeOn === undefined) { + throw new Error( + `GitHub's answer to the merge of ${label} into ${into} does not name the commit that the merge was made on, ` + + `so this step cannot confirm that it was made on ${shortSha(tip)}, the commit that ${into} was read at. ` + + `${nothingMerged} Run this step again.` + ) + } + if (madeOn !== tip) { + throw new Error( + `The merge of ${label} into ${into} was made on ${shortSha(madeOn)}, not on ${shortSha(tip)}, the commit that ` + + `${into} was read at: the temporary branch ${tempBranch} received a commit after this step created it. ` + + `${nothingMerged} Run this step again.` + ) + } + return { merged: true, sha: response.data.sha } } @@ -608,7 +642,6 @@ async function moveBranches({ github, core, owner, repo, moves, nothingMerged }) const plural = names.length > 1 const branchList = names.join(' and ') const branchListTogether = plural ? `${branchList} together` : branchList - const shortSha = (sha) => sha.slice(0, 7) const states = await readBranchStates({ github, owner, repo, moves }) // The request failed, and GitHub applied the update: the connection was lost after that. A diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index 53af98eb..d54b4ce5 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -148,10 +148,23 @@ function files(contents = {}, ref = TIP['release/M26.test']) { })) } +/** + * The body of a merge commit as the merge endpoint answers it. The first parent is the commit that + * the merge was made on, the second is the commit that was merged. + * + * @param {string} sha The merge commit. + * @param {string[]} parents Its parents' hashes. + * + * @returns {object} The body. + */ +function mergeCommit(sha, parents) { + return { sha, parents: parents.map((parent) => ({ sha: parent })) } +} + /** What the merge endpoint answers: a merge commit, nothing to merge, or a refusal. */ const MERGE = { - main: { status: 201, body: { sha: MERGED.main } }, - develop: { status: 201, body: { sha: MERGED.develop } }, + main: { status: 201, body: mergeCommit(MERGED.main, [TIP.main, TIP['release/M26.test']]) }, + develop: { status: 201, body: mergeCommit(MERGED.develop, [TIP.develop, MERGED.main]) }, done: { status: 204 }, conflict: { status: 409, body: { message: 'Merge conflict' } }, moved: { status: 409, body: { message: 'Base branch was modified. Review and try the merge again.' } }, @@ -929,6 +942,53 @@ describe('release-merge', () => { assert.deepStrictEqual(written(requests).at(-1), `DELETE ${API.ref(TEMP.main)}`) }) + it('fails with neither branch moved when a merge was made on a commit that is not the tip that was read', async (t) => { + // The merge is asked for by the name of the temporary branch. Someone else pushed a commit to + // that branch after the action created it, so GitHub made the merge on their commit. Moving + // the branch to that merge commit would be a fast-forward, and GitHub would accept it. + const intruder = '9999999999999999999999999999999999999999' + const onMain = { status: 201, body: mergeCommit(MERGED.main, [intruder, TIP['release/M26.test']]) } + const onDevelop = { status: 201, body: mergeCommit(MERGED.develop, [intruder, MERGED.main]) } + + for (const [into, answers, madeBeforeFailing] of [ + ['main', [onMain], [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(TEMP.main)}`]], + ['develop', [MERGE.main, onDevelop], TEMP_ONLY], + ]) { + const { github, requests } = await octokitFor(repositoryWith({ merges: answers })) + const { core, outputs, logged } = await coreWithOutputs(t) + const label = into === 'main' ? 'release/M26\\.test' : 'main' + + await assert.rejects( + runMerge({ github, core, env: mergeEnv() }), + new RegExp( + `The merge of ${label} into ${into} was made on 9999999, not on ${TIP[into].slice(0, 7)}, the commit that ${into} was read at: ` + + `the temporary branch release-merge/77-1/${into} received a commit after this step created it\\. ` + + 'Nothing was merged: main and develop are as they were\\. Run this step again\\.' + ), + into + ) + + // No branch is moved, and every temporary branch is deleted. + assert.deepStrictEqual(movesAsked(requests), [], into) + assert.deepStrictEqual(written(requests), madeBeforeFailing, into) + assert.doesNotMatch(logged(), /MERGE_CONFLICT/, into) + assert.strictEqual(outputs().conflict, undefined, into) + } + }) + + it('fails with neither branch moved when the answer to a merge does not name the commit that it was made on', async (t) => { + const { github, requests } = await octokitFor(repositoryWith({ merges: [{ status: 201, body: { sha: MERGED.main } }] })) + const { core } = await coreWithOutputs(t) + + await assert.rejects( + runMerge({ github, core, env: mergeEnv() }), + /GitHub's answer to the merge of release\/M26\.test into main does not name the commit that the merge was made on, so this step cannot confirm that it was made on bbbbbbb, the commit that main was read at\. Nothing was merged: main and develop are as they were\. Run this step again\./ + ) + + assert.deepStrictEqual(movesAsked(requests), []) + assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(TEMP.main)}`]) + }) + it('fails with neither branch moved, naming the branch that received a commit, and says to run again', async (t) => { // A commit reached main after its tip was read. The merge commit was made on the old tip, so // GitHub refuses the update for main, and the update for develop with it. GitHub's answer From cabf47577a1c05507f7972be37bf751c35f2afb3 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 15:21:13 -0300 Subject: [PATCH 12/13] release-merge: fail when a temporary branch already has what is merged The action asks for a merge only after it knows that the tip does not hold the head. A 204 from the merge endpoint therefore says that someone else put the head on the temporary branch. The action read that 204 as nothing to merge. For the merge into develop, it then moved the base alone and passed, with develop left without the merge back. A 204 now fails the step before it moves a branch, with a message to run it again. The case with nothing to merge is still found in runMerge, before a temporary branch exists. SOFT-5829 --- .github/actions/release-merge/README.md | 4 +- .../actions/release-merge/release-merge.js | 38 ++++++++++------ .../release-merge/release-merge.test.js | 45 ++++++++++++------- 3 files changed, 55 insertions(+), 32 deletions(-) diff --git a/.github/actions/release-merge/README.md b/.github/actions/release-merge/README.md index 8c516537..820ca9f0 100644 --- a/.github/actions/release-merge/README.md +++ b/.github/actions/release-merge/README.md @@ -86,7 +86,9 @@ The merge endpoint takes the temporary branch by name. A commit that someone els branch before the merge would end up under the merge commit, and the update would carry it to the base. So the step checks that each merge commit was made on the tip the action read: its first parent is that tip. When it is not, the step fails before it moves a branch, with a message to run -it again. +it again. The step fails the same way when GitHub answers that the temporary branch already holds +what is merged: the action only asks for a merge that the tip does not hold, so that answer means +the temporary branch moved. GitHub's answer to an update it does not make gives no reason for a branch that moved, so the step reads the two branches again before it fails, and the message says what it found: diff --git a/.github/actions/release-merge/release-merge.js b/.github/actions/release-merge/release-merge.js index e74f6c32..a8e808c3 100644 --- a/.github/actions/release-merge/release-merge.js +++ b/.github/actions/release-merge/release-merge.js @@ -444,11 +444,12 @@ function shortSha(sha) { * for it, and throws. * * runMerge checks first whether `tip` already holds `head`, and makes no temporary branch when it - * does. The endpoint answers 204 in that case, and this function also reads a 204 as nothing to - * merge. + * does. So this function is only called when `tip` does not hold `head`. * * The endpoint takes the temporary branch by name, so the merge is made on whatever commit that - * branch points at. The function checks that this commit is `tip`, and throws when it is not. + * branch points at. The function checks that this commit is `tip`, and throws when it is not. It + * also throws when the endpoint answers 204, which says that the temporary branch already holds + * `head`: `tip` does not, so the branch is no longer at `tip`. * * @param {object} options * @param {object} options.github The Octokit github-script supplies. @@ -469,8 +470,7 @@ function shortSha(sha) { * * @throws {Error} When the merge conflicts, GitHub refuses it, or it was not made on `tip`. * - * @returns {Promise<{merged: boolean, sha: string}>} Whether a merge commit was made, and the commit - * that `into` should point at: the merge commit, or `tip` when there was nothing to merge. + * @returns {Promise} The merge commit, which `into` should point at. */ async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, howToResolve, nothingMerged }) { let response @@ -487,16 +487,24 @@ async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, hea throw new Error(`Merging ${label} into ${into} failed (HTTP ${error?.status}): ${error?.message}. ${nothingMerged}`) } + // Anyone who can push to the repository can push to the temporary branch before the merge. The + // two checks below fail the step when the merge was not made on `tip`. + // + // A 204 says that the temporary branch already holds `head`. `tip` does not, so someone else + // put `head` on the temporary branch. Read as nothing to merge, the 204 would leave `into` out + // of the update at the end while the other branch moves. if (response.status === 204) { - core.info(`${into} already has ${label}: nothing to merge.`) - - return { merged: false, sha: tip } + throw new Error( + `GitHub answered that the temporary branch ${tempBranch} already has ${label}, but ${into} did not have it at ` + + `${shortSha(tip)}, the commit that ${into} was read at: the temporary branch received a commit after this step ` + + `created it. ${nothingMerged} Run this step again.` + ) } - // The first parent of the merge commit is the commit that the merge was made on. Anyone who can - // push to the repository can push to the temporary branch before the merge. The merge commit then - // descends from their commit and from `tip`, so the update at the end, which only checks that - // `into` is still at `tip`, would move `into` to it and carry their commit along. + // The first parent of the merge commit is the commit that the merge was made on. When it is + // someone else's commit, the merge commit still descends from `tip`, so the update at the end, + // which only checks that `into` is still at `tip`, would move `into` to it and carry their + // commit along. const madeOn = response.data.parents?.[0]?.sha if (madeOn === undefined) { throw new Error( @@ -513,7 +521,7 @@ async function mergeInto({ github, core, owner, repo, into, tempBranch, tip, hea ) } - return { merged: true, sha: response.data.sha } + return response.data.sha } /** @@ -816,7 +824,9 @@ async function runMerge({ github, core, env }) { await createTempBranch({ github, owner, repo, name: tempBranch, sha: tip, nothingMerged }) tempBranches.push(tempBranch) - return mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, howToResolve, nothingMerged }) + const sha = await mergeInto({ github, core, owner, repo, into, tempBranch, tip, head, message, label, howToResolve, nothingMerged }) + + return { merged: true, sha } } let mergedBase diff --git a/tests/actions/release-merge/release-merge.test.js b/tests/actions/release-merge/release-merge.test.js index d54b4ce5..abb56d33 100644 --- a/tests/actions/release-merge/release-merge.test.js +++ b/tests/actions/release-merge/release-merge.test.js @@ -779,25 +779,36 @@ describe('release-merge', () => { } }) - it('treats a 204 from the merge endpoint as nothing to merge, and deletes the temporary branch', async (t) => { - // The comparison said that main lacks the branch, and the merge endpoint then answers 204, - // which means that the base already contains the head. The two answers agree in practice. If - // they ever disagree, the action must not read the 204 as a merge commit. - const other = '8888888888888888888888888888888888888888' - const { github, requests } = await octokitFor( - repositoryWith({ - branches: ['release/M26.test', 'main'], - merges: [MERGE.done], - extra: [commit(TIP.main, [other, TIP['release/M26.test']])], - }) - ) - const { core, outputs, logged } = await coreWithOutputs(t) + it('fails with neither branch moved when the merge endpoint answers that a temporary branch already has what is merged', async (t) => { + // The action only asks for a merge after it knows that the tip does not hold the head: the + // comparison said so, or the head is the merge commit that this run has just made. A 204 + // then means that someone else moved the temporary branch to a commit that holds the head. + // Read as nothing to merge, it would leave develop out of the update while main moves. + for (const [into, answers, madeBeforeFailing] of [ + ['main', [MERGE.done], [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(TEMP.main)}`]], + ['develop', [MERGE.main, MERGE.done], TEMP_ONLY], + ]) { + const { github, requests } = await octokitFor(repositoryWith({ merges: answers })) + const { core, outputs, logged } = await coreWithOutputs(t) + const label = into === 'main' ? 'release/M26\\.test' : 'main' - await runMerge({ github, core, env: mergeEnv({ INPUT_DEVELOP_BRANCH: '' }) }) + await assert.rejects( + runMerge({ github, core, env: mergeEnv() }), + new RegExp( + `GitHub answered that the temporary branch release-merge/77-1/${into} already has ${label}, but ${into} did not have it at ` + + `${TIP[into].slice(0, 7)}, the commit that ${into} was read at: the temporary branch received a commit after this step ` + + 'created it\\. Nothing was merged: main and develop are as they were\\. Run this step again\\.' + ), + into + ) - assert.match(logged(), /main already has release\/M26\.test: nothing to merge/) - assert.strictEqual(outputs()['base-sha'], TIP.main) - assert.deepStrictEqual(written(requests), [`POST ${API.refs}`, `POST ${API.merges}`, `DELETE ${API.ref(TEMP.main)}`]) + // No branch is moved, not even main when its merge was made, and every temporary branch is + // deleted. The log does not say that the branch already had the merge. + assert.deepStrictEqual(movesAsked(requests), [], into) + assert.deepStrictEqual(written(requests), madeBeforeFailing, into) + assert.doesNotMatch(logged(), /nothing to merge/, into) + assert.deepStrictEqual(outputs(), {}, into) + } }) it('sets no output when the step fails after the merges, so a caller reading published under continue-on-error skips its writes', async (t) => { From b0a001422649a6ee54de08c7749943f6446f0642 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 3 Dec 2026 15:51:52 -0300 Subject: [PATCH 13/13] release-prep: show the commit and the branch as links in the job summary The summary of the prepare job wrote the commit and the branch in code spans. The three actions of a deploy show them as links since their summary became one list. release-prep now uses the same helpers, in the line for a commit that was made and in the line for nothing to commit. The file names stay code spans. SOFT-5829 --- .github/actions/release-prep/release-prep.js | 8 +++++--- .../actions/release-prep/release-prep.test.js | 18 ++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/.github/actions/release-prep/release-prep.js b/.github/actions/release-prep/release-prep.js index 2755bd0a..0c5a4cd5 100644 --- a/.github/actions/release-prep/release-prep.js +++ b/.github/actions/release-prep/release-prep.js @@ -15,7 +15,7 @@ const { execFileSync } = require('node:child_process') // The three-or-four-parts pattern lives with the other version rules, shared with release-merge. const { compareVersions, STELLARWP_VERSION_PATTERN } = require('../../lib/versions.js') -const { readRepository, tagExists } = require('../../lib/github.js') +const { readRepository, tagExists, repositoryUrl, branchLink, commitLink } = require('../../lib/github.js') const { runOrFail } = require('../../lib/exec.js') // How pup is resolved, downloaded and run lives with the other actions that run it. const { @@ -612,6 +612,8 @@ async function runCommit({ github, core, exec, env, cwd = process.cwd() }) { const version = readVersion(env.INPUT_VERSION) const message = commitMessage(branch, version) const { owner, repo } = readRepository(env, 'Nothing was committed.') + // The job summary shows the branch and the commit as links, as the deploy's actions do. + const url = repositoryUrl(env, { owner, repo }) await runOrFail(exec, 'git', ['add', '--all'], { cwd }, 'Staging the changes') @@ -630,7 +632,7 @@ async function runCommit({ github, core, exec, env, cwd = process.cwd() }) { core.setOutput('commit', '') core.setOutput('files', '') await core.summary - .addRaw(`Nothing to commit: \`${branch}\` already holds ${version}.`, true) + .addRaw(`Nothing to commit: ${branchLink(url, branch)} already holds ${version}.`, true) .write() return @@ -706,7 +708,7 @@ async function runCommit({ github, core, exec, env, cwd = process.cwd() }) { core.setOutput('files', files.join('\n')) // A Markdown list rather than addList, whose HTML list would show the backticks literally. await core.summary - .addRaw(`Committed \`${commit.sha.slice(0, 7)}\` "${message}" to \`${branch}\`:\n\n`) + .addRaw(`Committed ${commitLink(url, commit.sha)} "${message}" to ${branchLink(url, branch)}:\n\n`) .addRaw(files.map((file) => `- \`${file}\``).join('\n'), true) .write() } diff --git a/tests/actions/release-prep/release-prep.test.js b/tests/actions/release-prep/release-prep.test.js index be49bc95..d6e0587f 100644 --- a/tests/actions/release-prep/release-prep.test.js +++ b/tests/actions/release-prep/release-prep.test.js @@ -1227,7 +1227,15 @@ describe('release-prep', () => { commit: 'commit-new', files: 'bin/tool.sh\nchangelog.txt\nchangelog/entry.yaml\nlogo.png\nplugin.php', }) - assert.match(summary(), /Committed `commit-` "Prepare release\/M26\.jigglypuff \(4\.17\.0\)" to `release\/M26\.jigglypuff`/) + // The commit and the branch are links, as in the summary of the deploy's actions. The files stay + // code spans. + assert.ok( + summary().includes( + 'Committed [commit-](https://github.com/stellarwp/plugin/commit/commit-new) "Prepare release/M26.jigglypuff (4.17.0)" ' + + 'to [release/M26.jigglypuff](https://github.com/stellarwp/plugin/tree/release/M26.jigglypuff):' + ), + summary() + ) assert.match(summary(), /^- `bin\/tool\.sh`\n- `changelog\.txt`\n- `changelog\/entry\.yaml`\n- `logo\.png`\n- `plugin\.php`$/m) assert.equal(git(work, 'rev-parse', 'HEAD'), head, 'the checkout itself stays where it was') }) @@ -1268,7 +1276,7 @@ describe('release-prep', () => { it('neither commits nor calls the API when nothing changed, and still succeeds', async (t) => { // A retry of the same version finds the branch already prepared. - const { core, outputs } = await coreWithOutputs(t) + const { core, outputs, summary } = await coreWithOutputs(t) const { exec } = await recordedRealExec() const { github, requests } = await octokitFor(apiAccepts()) const work = checkoutOn('release/M26.jigglypuff') @@ -1277,6 +1285,12 @@ describe('release-prep', () => { assert.deepEqual(requests, []) assert.deepEqual(outputs(), { commit: '', files: '' }) + assert.ok( + summary().includes( + 'Nothing to commit: [release/M26.jigglypuff](https://github.com/stellarwp/plugin/tree/release/M26.jigglypuff) already holds 4.17.0.' + ), + summary() + ) }) it('fails when the branch moved after the checkout, without forcing the ref', async (t) => {