Skip to content

Commit

Permalink
feat(last-tag): Add before, after options
Browse files Browse the repository at this point in the history
  • Loading branch information
Alorel committed Sep 21, 2023
1 parent 8bec46e commit e808776
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions src/lib/semver.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/modules/last-tag/action.yml
Original file line number Diff line number Diff line change
@@ -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
44 changes: 42 additions & 2 deletions src/modules/last-tag/index.mts
Original file line number Diff line number Diff line change
@@ -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<Inputs>({
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<ReleaseOutputName, Valueify<ReleaseOutputName>>()
.set(ReleaseOutputName.LastTag, lastTag.toString())
Expand Down

0 comments on commit e808776

Please sign in to comment.