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
95 changes: 21 additions & 74 deletions .github/workflows/__test-action-release-prepare-and-plan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
name: Tests for "release/plan" action
runs-on: ubuntu-latest
permissions:
contents: write
contents: read
pull-requests: read
steps:
- name: Arrange - Checkout
Expand Down Expand Up @@ -63,10 +63,11 @@ jobs:
uses: $/actions/release/plan
with:
working-directory: actions/release/get-configuration
release-config: |
release-config: &plan-configuration |
tag-prefix: configured-
tag-template: configured-$RESOLVED_VERSION
name-template: Configured $RESOLVED_VERSION
tag-template: configured-$RESOLVED_VERSION-final
name-template: Configured $RESOLVED_VERSION / protocol 1.0.0
version-template: $MAJOR.$MINOR

- name: Assert - Configured release identity
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
Expand All @@ -77,82 +78,28 @@ jobs:
with:
script: |
const assert = require('node:assert/strict');
assert.match(process.env.RELEASE_TAG, /^configured-\d+\.\d+\.\d+$/);
assert.match(process.env.RELEASE_NAME, /^Configured \d+\.\d+\.\d+$/);
assert.match(process.env.RELEASE_TAG, /^configured-\d+\.\d+-final$/);
assert.match(process.env.RELEASE_NAME, /^Configured \d+\.\d+ \/ protocol 1\.0\.0$/);
assert.ok(['true', 'false'].includes(process.env.HAS_CHANGES));

- name: Arrange - Create colliding tag
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
RELEASE_TAG: ${{ steps.drafter-plan.outputs.tag }}
TARGET_SHA: ${{ github.sha }}
with:
github-token: ${{ github.token }}
script: |
const releaseTag = (process.env.RELEASE_TAG || '').trim();
const targetSha = (process.env.TARGET_SHA || '').trim();

if (!releaseTag) {
throw new Error('Expected planned release tag before creating a colliding tag');
}

if (!targetSha) {
throw new Error('Expected target sha before creating a colliding tag');
}

await github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `refs/tags/${releaseTag}`,
sha: targetSha,
});

- name: Act - Plan drafter release with existing tag
id: existing-tag-plan
continue-on-error: true
- name: Act - Force a read-only release plan
id: forced-plan
uses: $/actions/release/plan
with:
working-directory: actions/release/get-configuration

- name: Assert - Existing remote tag blocks release plan
force: "true"
release-config: *plan-configuration
- name: Assert - Forced plan preserves the configured identity
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
RELEASE_TAG: ${{ steps.drafter-plan.outputs.tag }}
STEP_OUTCOME: ${{ steps.existing-tag-plan.outcome }}
HAS_CHANGES: ${{ steps.forced-plan.outputs.has-changes }}
RELEASE_TAG: ${{ steps.forced-plan.outputs.tag }}
RELEASE_NAME: ${{ steps.forced-plan.outputs.name }}
EXPECTED_TAG: ${{ steps.configured-plan.outputs.tag }}
EXPECTED_NAME: ${{ steps.configured-plan.outputs.name }}
with:
script: |
const assert = require('node:assert');

const releaseTag = (process.env.RELEASE_TAG || '').trim();
const stepOutcome = (process.env.STEP_OUTCOME || '').trim();

assert.ok(releaseTag, 'Expected colliding tag to be set');
assert.equal(stepOutcome, 'failure', `Expected plan action to fail when tag already exists, got "${stepOutcome}"`);

- name: Cleanup - Delete colliding tag
if: always()
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
RELEASE_TAG: ${{ steps.drafter-plan.outputs.tag }}
with:
github-token: ${{ github.token }}
script: |
const releaseTag = (process.env.RELEASE_TAG || '').trim();

if (!releaseTag) {
return;
}

try {
await github.rest.git.deleteRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: `tags/${releaseTag}`,
});
} catch (error) {
if (error.status === 404) {
return;
}

throw error;
}
const assert = require('node:assert/strict');
assert.equal(process.env.HAS_CHANGES, 'true');
assert.equal(process.env.RELEASE_TAG, process.env.EXPECTED_TAG);
assert.equal(process.env.RELEASE_NAME, process.env.EXPECTED_NAME);
18 changes: 14 additions & 4 deletions .github/workflows/__test-action-release-verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,27 @@ jobs:
assert.equal(JSON.parse(process.env.RELEASES)[0].published, false);
}
NODE
- name: Act - Require a missing prepared tag
if: github.event_name != 'pull_request'
- name: Act - Verify source-only release without artifacts
id: source
uses: $/actions/release/verify
with:
tags: '["verify-${{ github.run_id }}-${{ github.run_attempt }}"]'
- name: Assert - Source-only release is ready
env:
RELEASES: ${{ steps.source.outputs.releases }}
run: |
node <<'NODE'
const assert = require('node:assert/strict');
assert.equal(JSON.parse(process.env.RELEASES)[0].published, false);
NODE
- name: Act - Require a missing source tag
id: missing-tag
continue-on-error: true
uses: $/actions/release/verify
with:
artifact-ids: '["${{ steps.artifact.outputs.artifact-id }}"]'
tags: '["verify-${{ github.run_id }}-${{ github.run_attempt }}"]'
require-tags: "true"
- name: Assert - Missing tag fails verification
if: github.event_name != 'pull_request'
env:
OUTCOME: ${{ steps.missing-tag.outcome }}
run: test "$OUTCOME" = failure
22 changes: 16 additions & 6 deletions actions/release/plan/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ Detect release changes and plan a release identity without creating a Git tag or
## Usage

```yaml
- uses: hoverkraft-tech/ci-github-publish/actions/release/plan@f06e0e72a76bfca57c34d08d03e5c1914275fd4c # 2.0.0
- uses: hoverkraft-tech/ci-github-publish/actions/release/plan@<commit-sha>
with:
# Optional partial Release Drafter configuration as a YAML or JSON mapping.
# Supplied top-level keys override repository settings, which inherit shared defaults unless they declare their own _extends chain.
# Mappings and lists are replaced. Pass identical overrides to release/plan and release/create. The _extends key is reserved.
release-config: ""

# Plan a release even without relevant changes, using the version, tag and name calculated by Release Drafter.
# Configured version rules and identity templates remain authoritative. Existing tag collisions still fail; tags are never created or moved.
# Callers decide when forcing is allowed, for example only on manual default-branch runs.
#
# Default: `false`
force: "false"

# Whether to plan the release as a prerelease
# Default: `false`
prerelease: "false"
Expand Down Expand Up @@ -91,6 +98,9 @@ Detect release changes and plan a release identity without creating a Git tag or
| **`release-config`** | Optional partial Release Drafter configuration as a YAML or JSON mapping. | **false** | - |
| | Supplied top-level keys override repository settings, which inherit shared defaults unless they declare their own _extends chain. | | |
| | Mappings and lists are replaced. Pass identical overrides to release/plan and release/create. The _extends key is reserved. | | |
| **`force`** | Plan a release even without relevant changes, using the version, tag and name calculated by Release Drafter. | **false** | `false` |
| | Configured version rules and identity templates remain authoritative. Existing tag collisions still fail; tags are never created or moved. | | |
| | Callers decide when forcing is allowed, for example only on manual default-branch runs. | | |
| **`prerelease`** | Whether to plan the release as a prerelease | **false** | `false` |
| **`working-directory`** | Working directory used to scope release automation in a monorepo. | **false** | - |
| | If specified, the action looks for `.github/release-configs/{slug}.yml`, where `slug` is derived from the working directory basename. | | |
Expand All @@ -117,11 +127,11 @@ Detect release changes and plan a release identity without creating a Git tag or

## Outputs

| **Output** | **Description** |
| ----------------- | ------------------------------------------------------------------------------------------------ |
| **`has-changes`** | Whether the release has relevant changes, or no previous matching release exists (true or false) |
| **`tag`** | The planned release tag, including when there are no relevant changes |
| **`name`** | The planned release name, including when there are no relevant changes |
| **Output** | **Description** |
| ----------------- | -------------------------------------------------------------------------------------------------------------- |
| **`has-changes`** | Whether there are relevant changes, no previous matching release, or an explicit force request (true or false) |
| **`tag`** | The planned release tag, including when there are no relevant changes |
| **`name`** | The planned release name, including when there are no relevant changes |

<!-- outputs:end -->

Expand Down
13 changes: 11 additions & 2 deletions actions/release/plan/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ inputs:
Supplied top-level keys override repository settings, which inherit shared defaults unless they declare their own _extends chain.
Mappings and lists are replaced. Pass identical overrides to release/plan and release/create. The _extends key is reserved.
default: ""
force:
description: |
Plan a release even without relevant changes, using the version, tag and name calculated by Release Drafter.
Configured version rules and identity templates remain authoritative. Existing tag collisions still fail; tags are never created or moved.
Callers decide when forcing is allowed, for example only on manual default-branch runs.
default: "false"
prerelease:
description: "Whether to plan the release as a prerelease"
required: false
Expand Down Expand Up @@ -51,7 +57,7 @@ inputs:

outputs:
has-changes:
description: "Whether the release has relevant changes, or no previous matching release exists (true or false)"
description: "Whether there are relevant changes, no previous matching release, or an explicit force request (true or false)"
value: ${{ steps.release-plan.outputs.has-changes }}
tag:
description: "The planned release tag, including when there are no relevant changes"
Expand Down Expand Up @@ -110,21 +116,24 @@ runs:
PLANNED_RELEASE_TAG: ${{ steps.prepare-release.outputs.tag_name }}
PLANNED_RELEASE_BODY: ${{ steps.prepare-release.outputs.body }}
CHANGE_DETECTION_MARKER: ${{ steps.prepare-change-detection.outputs.marker }}
FORCE: ${{ inputs.force }}
with:
github-token: ${{ inputs.github-token }}
script: |
const { resolvePlan } = require(`${process.env.ACTION_PATH}/index.js`);
try {
if (!["true", "false"].includes(process.env.FORCE)) throw new Error("force must be true or false.");
const plan = await resolvePlan({
body: process.env.PLANNED_RELEASE_BODY,
marker: process.env.CHANGE_DETECTION_MARKER,
force: process.env.FORCE === "true",
tag: process.env.PLANNED_RELEASE_TAG,
name: process.env.PLANNED_RELEASE_NAME,
github,
repo: context.repo,
});
core.info(plan.hasChanges
? 'Release planned with relevant changes or no previous release.'
? 'Release planned with changes, no previous release, or an explicit force request.'
: 'Release planned without relevant changes.');
core.setOutput('has-changes', plan.hasChanges ? 'true' : 'false');
core.setOutput('tag', plan.tag);
Expand Down
22 changes: 16 additions & 6 deletions actions/release/plan/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,25 @@ function prepareConfiguration({ configPath, workspace, runnerTemp }) {
};
}

async function assertTagDoesNotExist(github, repo, tag) {
async function tagExists(github, repo, tag) {
try {
await github.rest.git.getRef({ ...repo, ref: `tags/${tag}` });
} catch (error) {
if (error.status === 404) return;
if (error.status === 404) return false;
throw new Error(`Cannot check remote tag ${tag}: ${error.message}`);
}
throw new Error(`Remote tag already exists: ${tag}`);
return true;
}

async function resolvePlan({ body, marker, tag, name, github, repo }) {
async function resolvePlan({
body,
marker,
tag,
name,
github,
repo,
force = false,
}) {
const prefix = `${marker}:`;
const lineEnd = body?.indexOf("\n") ?? -1;
if (!marker || !body?.startsWith(prefix) || lineEnd < 0) {
Expand All @@ -72,9 +80,11 @@ async function resolvePlan({ body, marker, tag, name, github, repo }) {
if (!releaseTag) {
throw new Error("Cannot determine release tag. Configure Release Drafter.");
}
await assertTagDoesNotExist(github, repo, releaseTag);
if (await tagExists(github, repo, releaseTag)) {
throw new Error(`Remote tag already exists: ${releaseTag}`);
}
Comment thread
neilime marked this conversation as resolved.
Resolved
return {
hasChanges,
hasChanges: hasChanges || force,
tag: releaseTag,
name: name?.trim() || releaseTag,
};
Expand Down
65 changes: 65 additions & 0 deletions actions/release/plan/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,68 @@ test("unchanged releases still validate tag availability", async () => {
);
}
});

test("force plans an unchanged release without lowering the planned version", async () => {
const { input } = fixture({
body: `${marker}:v1.0.0\n${marker}:no-changes`,
force: true,
});
assert.deepEqual(await resolvePlan(input), {
hasChanges: true,
tag: "v1.1.0",
name: "Version 1.1.0",
});
});
test("force preserves custom tag and name formats from Release Drafter", async () => {
for (const tag of [
"release-42",
"v1.3",
"package-1.3-final",
"package-1.3.0-rc.2",
]) {
const name = `Release ${tag} / protocol 1.0.0`;
const { input, calls } = fixture({
body: `${marker}:v1.0.0\n${marker}:no-changes`,
tag,
name,
force: true,
});
assert.deepEqual(await resolvePlan(input), { hasChanges: true, tag, name });
assert.deepEqual(calls, [{ ...repo, ref: `tags/${tag}` }]);
}
});

test("force rejects collisions without inventing a different tag", async () => {
for (const changes of ["* A relevant change", `${marker}:no-changes`]) {
for (const tag of ["v1.0.0", "release-42", "v1.3", "package-1.3.0-rc.2"]) {
const { input, calls } = fixture({
force: true,
tag,
body: `${marker}:v1.0.0\n${changes}`,
});
input.github.rest.git.getRef = async (request) => {
calls.push(request);
if (request.ref === `tags/${tag}`) return { data: {} };
throw Object.assign(new Error("Not Found"), { status: 404 });
};
await assert.rejects(resolvePlan(input), {
message: `Remote tag already exists: ${tag}`,
});
assert.deepEqual(calls, [{ ...repo, ref: `tags/${tag}` }]);
}
}
});

test("forced planning does not bypass tag API failures", async () => {
const { input } = fixture({
force: true,
body: `${marker}:v1.0.0\n${marker}:no-changes`,
});
input.github.rest.git.getRef = async () => {
throw Object.assign(new Error("Forbidden"), { status: 403 });
};
await assert.rejects(
resolvePlan(input),
/Cannot check remote tag v1.1.0: Forbidden/,
);
});
Loading
Loading