diff --git a/README.md b/README.md index 1e517e8..ef370cb 100644 --- a/README.md +++ b/README.md @@ -133,9 +133,16 @@ Same as primary action: `changelog`, `release-type`, `commit-count`, `relevant-c This action lives under `/last-tag`. Same step as the primary action uses. -Inputs: none +##Inputs -Outputs: `last-tag`, if resolved. +| Key | Required | Default | Description | +|----------|----------|---------|----------------------------------------------------------------------------------| +| `before` | :x: | | If specified, the last tag must come before this tag sorted by semantic version. | +| `after` | :x: | | If specified, the last tag must come after this tag sorted by semantic version. | + +## Outputs + +`last-tag`, if resolved. # Resolve the next tag to release diff --git a/src/lib/semver.mts b/src/lib/semver.mts index 7fcdf2e..ea0c121 100644 --- a/src/lib/semver.mts +++ b/src/lib/semver.mts @@ -142,6 +142,18 @@ class SemVer { return this; } + public isEqual(other: SemVer): boolean { + return SemVer.cmp(this, other) === 0; + } + + public isGreaterThan(other: SemVer): boolean { + return SemVer.cmp(this, other) === -1; + } + + public isLowerThan(other: SemVer): boolean { + return SemVer.cmp(this, other) === 1; + } + /** Clone & ensure the prefix & all major/minor/patch versions are set */ public materialise(): SemVer { return new SemVer(this.major, this.minor, this.patch); diff --git a/src/modules/last-tag/action.yml b/src/modules/last-tag/action.yml index b83b46c..c2c2865 100644 --- a/src/modules/last-tag/action.yml +++ b/src/modules/last-tag/action.yml @@ -1,5 +1,10 @@ name: 'Semantic Release Lite: Last tag' description: Gets the last release's tag, if any +inputs: + after: + description: If specified, the last tag must come after this tag sorted by semantic version. + before: + description: If specified, the last tag must come before this tag sorted by semantic version. runs: using: node20 main: last-tag.js diff --git a/src/modules/last-tag/index.mts b/src/modules/last-tag/index.mts index d85c4a0..47a8f1e 100644 --- a/src/modules/last-tag/index.mts +++ b/src/modules/last-tag/index.mts @@ -1,11 +1,51 @@ -import {info, isDebug, setFailed} from '@actions/core'; +import {getInput, info, isDebug, setFailed} from '@actions/core'; +import InputMgr from '../../lib/input-mgr.mjs'; import OutputMgr from '../../lib/output-mgr.mjs'; import {SemVer} from '../../lib/semver.mjs'; import {ReleaseOutputName} from '../../output-mgr.mjs'; import Valueify = OutputMgr.Valueify; +interface Inputs { + after?: SemVer; + + before?: SemVer; +} + (async function getLastTag() { - const lastTag = await SemVer.resolveLastRelease(); + function loadInput(name: keyof Inputs): () => SemVer | undefined { + return function actualInputLoader() { + const str = getInput(name); + if (!str) { + return; + } + + const semVer = SemVer.parse(str); + if (semVer) { + return semVer; + } + + throw new Error('Invalid SemVer string'); + }; + } + + const inputs = new InputMgr({ + after: loadInput('after'), + before: loadInput('before'), + }); + inputs.load(); + + let tags = await SemVer.resolveReleases(); + if (inputs.after) { + if (inputs.before) { + tags = tags.filter(tag => tag.isGreaterThan(inputs.after!) && tag.isLowerThan(inputs.before!)); + } else { + tags = tags.filter(tag => tag.isGreaterThan(inputs.after!)); + } + } else if (inputs.before) { + tags = tags.filter(tag => tag.isLowerThan(inputs.before!)); + } + + const lastTag = tags[0]; if (lastTag) { new OutputMgr>() .set(ReleaseOutputName.LastTag, lastTag.toString())