Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions .github/actions/release-merge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ 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. Merges the release branch into the base branch as a merge commit, `Merge release/… into main`
(or `into master`).
(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
Expand All @@ -69,7 +72,9 @@ 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 merges as nothing to merge: GitHub answers that the base
already contains the head, and nothing is written.
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 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.
Expand Down Expand Up @@ -127,6 +132,17 @@ fails the step without the marker; running the step again is enough.
out, so the action refuses, with the branches untouched. Give the release a new version and
prepare the branch again. A tag that does contain the branch is a retry of this release, and the
action goes on.
- **An unpublished retry after the base has moved on.** When the base already holds the release
branch, nothing is merged, and what the caller builds and publishes is `base-sha`. A publish that
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
branch into the base, or release another version. A version already published is not refused:
its retry builds the commit the tag points at, what was published, and deploy and publish skip
what they find.

The branches are checked too: each has to exist in the repository, the base and develop have to be
two different branches (when there is a develop), and the release branch cannot be either of them.
Expand Down
11 changes: 7 additions & 4 deletions .github/actions/release-merge/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ inputs:
paths.versions is read, and a different version in any of them is refused. A pre-release such as
4.17.0-beta.1 and an empty value are refused before anything is merged, and so is a version
whose tag already exists, unless that tag's release was published from this branch, which makes
the run a retry. The release itself is published by release-publish, after this action.
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.
required: true
base-branch:
description: 'The branch the release is merged into: `main`, or `master` where that is the name.'
Expand Down Expand Up @@ -62,9 +65,9 @@ outputs:
value: ${{ steps.merge.outputs.branch }}
base-sha:
description: >-
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 already done. The commit to check out and build from; pass it to
release-publish as `target`.
The commit that holds the release: the merge commit, or on a retry that found the merge already
done, the commit the tag points at, or the base's tip before the tag exists. The commit to check
out and build from; pass it to release-publish as `target`.
value: ${{ steps.merge.outputs.base-sha }}
conflict:
description: >-
Expand Down
57 changes: 53 additions & 4 deletions .github/actions/release-merge/release-merge.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,33 @@ async function commitContainsBranch({ github, owner, repo, branch, ref, sha }) {
}
}

/**
* Whether a commit is the one that merged the branch: one of its parents is the branch's tip. The
* API never fast-forwards, so the merge of a release branch into the base is always a merge commit
* with the branch's tip as a parent; a commit pushed on top of it, bump or not, has other parents.
*
* @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.branch The branch, for the message.
* @param {string} options.ref The branch's tip as it was read, a hash.
* @param {string} options.sha The commit to read, a hash: the base's tip as it was read.
*
* @throws {Error} When the read fails.
*
* @returns {Promise<boolean>} True when `ref` is one of the commit's parents.
*/
async function commitMergesBranch({ github, owner, repo, branch, ref, sha }) {
try {
const { data } = await github.rest.repos.getCommit({ owner, repo, ref: sha })

return (data.parents ?? []).some((parent) => parent.sha === ref)
} catch (error) {
throw new Error(`Reading ${sha} to check it merged ${branch} failed (HTTP ${error.status}): ${error.message}. Nothing was merged.`)
}
}

/**
* Merges one branch, or commit, into another as a merge commit, through the API.
*
Expand Down Expand Up @@ -447,7 +474,7 @@ async function runMerge({ github, core, env }) {
core.info(`${version} is already published (${releaseUrl}) from ${branch}: nothing to publish again.`)
}

const { merged: mergedBase, sha: baseSha } = await mergeInto({
const { merged: mergedBase, sha: mergedSha } = await mergeInto({
github,
core,
owner,
Expand All @@ -458,9 +485,31 @@ async function runMerge({ github, core, env }) {
label: branch,
})

// base-sha is the commit the release is published at and built from: the merge commit, or the
// base's tip on a retry that found the merge done. 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.
// 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) {
Comment thread
larodiel marked this conversation as resolved.
Resolved
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.'
)
}
}

// 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 mergedDevelop = null
if (develop === null) {
core.info(`develop-branch is empty: ${base} is not merged back into a develop branch.`)
Expand Down
105 changes: 100 additions & 5 deletions tests/actions/release-merge/release-merge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,23 @@ const API = {
// The files are read at the release branch's tip, the commit, not at the branch by name.
file: (name, ref = TIP['release/M26.test']) => `${REPO}/contents/${name}?ref=${encodeURIComponent(ref)}`,
compare: (sha, ref = TIP['release/M26.test']) => `${REPO}/compare/${encodeURIComponent(`${ref}...${sha}`)}`,
commit: (sha) => `${REPO}/commits/${sha}`,
merges: `${REPO}/merges`,
}

/**
* The rule for a commit read, answering with its parents: a merge commit of the release branch
* into main has main's old tip and the branch's tip; any other commit has other parents.
*
* @param {string} sha The commit.
* @param {string[]} parents Its parents' hashes.
*
* @returns {object} The rule.
*/
function commit(sha, parents) {
return { method: 'GET', path: API.commit(sha), body: { sha, parents: parents.map((parent) => ({ sha: parent })) } }
}

const RELEASE_URL = 'https://github.com/stellarwp/plugin/releases/tag/4.17.0'

/** Rules under which the version is tagged and its release published, as a retry finds. */
Expand All @@ -93,14 +107,16 @@ function branches(names = Object.keys(TIP)) {
}

/**
* Rules for the files the action reads from the release branch, as the contents endpoint sends
* them. A file set to null is absent: no rule, so the read answers 404.
* Rules for the files the action reads from a branch, as the contents endpoint sends them. A file
* set to null is absent: no rule, so the read answers 404.
*
* @param {object} contents File name to contents; the fixture's defaults hold 4.17.0 everywhere.
* @param {string} ref The commit the files are read at: the release branch's tip, or the
* base's tip on a retry that found the merge done.
*
* @returns {object[]} One rule per present file.
*/
function files(contents = {}) {
function files(contents = {}, ref = TIP['release/M26.test']) {
const all = {
'.puprc': JSON.stringify(PUPRC),
'plugin.php': 'Version: 4.17.0\n',
Expand All @@ -112,7 +128,7 @@ function files(contents = {}) {
.filter(([, body]) => body !== null)
.map(([name, body]) => ({
method: 'GET',
path: API.file(name),
path: API.file(name, ref),
body: { type: 'file', path: name, encoding: 'base64', content: Buffer.from(body).toString('base64') },
}))
}
Expand Down Expand Up @@ -458,12 +474,91 @@ describe('release-merge', () => {
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, '')
// Nothing was merged, so the commit to publish at is the base's tip, the one holding the release.
// Nothing was merged, so the commit to publish at is the one the tag was made from: what was
// 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)
})

it('goes on with an unpublished retry whose base tip is the commit that merged the branch', async (t) => {
// The publish step failed after the deploy, so there is no tag, and main's tip is still the
// 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']])],
}).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 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), [
`POST ${API.merges}`,
`GET ${API.branch('main')}`,
`GET ${API.commit(MERGED.main)}`,
`POST ${API.merges}`,
`GET ${API.branch('develop')}`,
])
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) => {
// 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
// tag it finds and zip the current trunk, and its publish would tag main's tip: those
// changes under 4.17.0. The version is not what decides it, so a tip holding 4.17.0 is
// refused too.
const later = '9999999999999999999999999999999999999999'
const other = '8888888888888888888888888888888888888888'
for (const parents of [[MERGED.main, other], [MERGED.main]]) {
const { github, requests } = await octokitFor(
repositoryWith({
merges: [MERGE.done, MERGE.done],
extra: [commit(later, parents)],
}).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: later } } } : rule))
)
const { core, outputs } = await coreWithOutputs(t)

await assert.rejects(
runMerge({ github, core, env: mergeEnv() }),
/main already had release\/M26\.test and has moved on since: its tip 9999999999999999999999999999999999999999 is not the commit that merged release\/M26\.test.*finish it by hand at the commit that merged release\/M26\.test into main.*Nothing was merged/s,
`parents ${parents.length}`
)

// Refused before the develop merge, and the outputs are not set.
assert.strictEqual(mergesAsked(requests).length, 1)
assert.deepStrictEqual(outputs(), {})
}
})

it('builds a published retry from the tagged commit, so the base moving on changes nothing', async (t) => {
// 4.17.0 was published, then 4.18.0: main's tip is 4.18.0's merge commit. The retry is a
// no-op that rebuilds what was published, the commit the tag points at; deploy and publish
// skip what they find.
const later = '9999999999999999999999999999999999999999'
const { github, requests } = await octokitFor(
repositoryWith({
tag: PUBLISHED,
merges: [MERGE.done, MERGE.done],
extra: [{ method: 'GET', path: API.compare(TAG_COMMIT), body: { status: 'ahead' } }],
}).map((rule) => (rule.path === API.branch('main') ? { ...rule, body: { name: 'main', commit: { sha: later } } } : rule))
)
const { core, outputs, logged } = await coreWithOutputs(t)

await runMerge({ github, core, env: mergeEnv() })

assert.strictEqual(outputs()['base-sha'], TAG_COMMIT)
assert.strictEqual(mergesAsked(requests)[1].head, TAG_COMMIT, 'develop is asked for the published commit')
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')
})

it('says so when the release is already published from this branch, so the publish has nothing to do', async (t) => {
const { github, requests } = await octokitFor(
repositoryWith({
Expand Down
Loading