Skip to content

Commit

Permalink
feat: add support for version override
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin-de-Jong committed Aug 31, 2023
1 parent 01449a4 commit 834d7c6
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 70 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ jobs:
| `files` | NO | Multiline list of files (paths) to upload as a [GitHub Release asset](./docs/asset-management.md) |
| `increment-type` | NO | Enforce a specific increment type, please refer to the [Versioning Strategies](./docs/versioning-strategies.md) for more details |
| `release-notes` | NO | Path towards a file containing the release notes to include in the GitHub release (Markdown format recommended) |
| `version` | NO | Version to associate with the GitHub Release, can be OPTIONALLY set to override automatic detection or the `increment-type` parameter |
| `versioning` | NO | [Versioning strategy](#versioning-strategies) to apply. MUST be one of `semver` or `calver`. Default: `semver` |

## Outputs
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ inputs:
description: 'Increment type to apply to the version'
required: false

version:
description: 'Version to associate with the GitHub Release'
required: false

create-release:
description: 'Create the GitHub Release, can be set to `false` to perform a dry run (i.e. determine the previous and incremented version)'
required: false
Expand Down
76 changes: 44 additions & 32 deletions lib/main/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 51 additions & 36 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,50 +41,65 @@ export async function run(): Promise<void> {

const branch = branching.getBranch();
const versionScheme = versioning.getVersionScheme();
const versionOverride = core.getInput("version");

let newVersion: versioning.Version;
const commits: commitLib.IConventionalCommit[] = [];

core.startGroup("🔍 Determining increment type");
const latestRelease = await releasing.getLatestRelease(branch, versionScheme);
let latestRef: string;
let latestVersion: versioning.Version;

if (latestRelease) {
core.info(`ℹ️ Latest release: ${latestRelease.tag_name}`);
latestRef = latestRelease.tag_name;
latestVersion = versionScheme.createVersion(latestRef);
if (versionOverride) {
core.info(`ℹ️ Using version override: ${versionOverride}`);
newVersion = versionScheme.createVersion(versionOverride);
core.setOutput("incremented-version", newVersion.toString());
} else {
// NOTE: Postponed this expensive request for the case where there is no release determined yet
const initialCommit = await releasing.getInitialCommit();

core.info(`ℹ️ Creating release based on commit SHA: ${initialCommit.hash}`);
latestRef = initialCommit.hash;
latestVersion = versionScheme.initialVersion();
}
let latestRef: string;
let latestVersion: versioning.Version;

const latestRelease = await releasing.getLatestRelease(branch, versionScheme);

if (latestRelease) {
core.info(`ℹ️ Latest release: ${latestRelease.tag_name}`);
latestRef = latestRelease.tag_name;
latestVersion = versionScheme.createVersion(latestRef);
} else {
// NOTE: Postponed this expensive request for the case where there is no release determined yet
const initialCommit = await releasing.getInitialCommit();

core.info(`ℹ️ Creating release based on commit SHA: ${initialCommit.hash}`);
latestRef = initialCommit.hash;
latestVersion = versionScheme.initialVersion();
}

const increments: versioning.VersionIncrement[] = [];
const commits: commitLib.IConventionalCommit[] = [];
if (core.getInput("increment-type")) {
increments.push(...core.getInput("increment-type").split("|").map(inc => versioning.getIncrementType(versionScheme, inc)));
} else {
const delta = await releasing.getChangesSince(latestRef);
core.info(`ℹ️ Changes since: ${delta.length} commits`);

commits.push(...filterConventionalCommits(delta));
core.info(`ℹ️ Conventional Commits since: ${commits.length} commits`);

const increment = versionScheme.determineIncrementType(commits)
if (increment === undefined) {
core.info("⚠️ No increment required, skipping...");
core.endGroup();
return;
const increments: versioning.VersionIncrement[] = [];
if (core.getInput("increment-type")) {
increments.push(
...core
.getInput("increment-type")
.split("|")
.map(inc => versioning.getIncrementType(versionScheme, inc))
);
} else {
const delta = await releasing.getChangesSince(latestRef);
core.info(`ℹ️ Changes since: ${delta.length} commits`);

commits.push(...filterConventionalCommits(delta));
core.info(`ℹ️ Conventional Commits since: ${commits.length} commits`);

const increment = versionScheme.determineIncrementType(commits);
if (increment === undefined) {
core.info("⚠️ No increment required, skipping...");
core.endGroup();
return;
}
increments.push(increment);
}
increments.push(increment);

core.setOutput("previous-version", latestVersion.toString());
newVersion = versioning.incrementVersion(latestVersion, increments);
core.setOutput("incremented-version", newVersion.toString());
}
core.endGroup();

core.setOutput("previous-version", latestVersion.toString());
const newVersion = versioning.incrementVersion(latestVersion, increments);
core.setOutput("incremented-version", newVersion.toString());

if (!core.getBooleanInput("create-release")) {
return;
}
Expand Down
8 changes: 6 additions & 2 deletions src/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export function incrementVersion(version: Version, incrementType: VersionIncreme

return newVersion;
}

let newVersion = new CalVer(version.format, version);
let previousIncrement = "";
for (const increment of incrementType) {
Expand Down Expand Up @@ -216,7 +216,11 @@ export function incrementVersion(version: Version, incrementType: VersionIncreme
let newVersion = new SemVer(version);
let previousIncrement = "";
for (const increment of incrementType) {
newVersion = incrementSemVer(newVersion, increment as SemVerIncrement, ["PRERELEASE", "BUILD"].includes(previousIncrement));
newVersion = incrementSemVer(
newVersion,
increment as SemVerIncrement,
["PRERELEASE", "BUILD"].includes(previousIncrement)
);
previousIncrement = increment as SemVerIncrement;
if (newVersion.isGreaterThan(version)) {
break;
Expand Down

0 comments on commit 834d7c6

Please sign in to comment.