Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds "semver" versioning-strategy #2451

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion __snapshots__/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ Options:
generated? [boolean] [default: false]
--versioning-strategy strategy used for bumping versions
[choices: "always-bump-major", "always-bump-minor", "always-bump-patch",
"default", "prerelease", "service-pack"] [default: "default"]
"default", "prerelease", "semver", "service-pack"] [default: "default"]
--changelog-path where can the CHANGELOG be found in the
project? [string] [default: "CHANGELOG.md"]
--changelog-type type of changelog to build
Expand Down
2 changes: 2 additions & 0 deletions src/factories/versioning-strategy-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {ServicePackVersioningStrategy} from '../versioning-strategies/service-pa
import {GitHub} from '../github';
import {ConfigurationError} from '../errors';
import {PrereleaseVersioningStrategy} from '../versioning-strategies/prerelease';
import {SemverVersioningStrategy} from '../versioning-strategies/semver';

export type VersioningStrategyType = string;

Expand All @@ -44,6 +45,7 @@ const versioningTypes: Record<string, VersioningStrategyBuilder> = {
'always-bump-major': options => new AlwaysBumpMajor(options),
'service-pack': options => new ServicePackVersioningStrategy(options),
prerelease: options => new PrereleaseVersioningStrategy(options),
semver: options => new SemverVersioningStrategy(options),
};

export function buildVersioningStrategy(
Expand Down
86 changes: 86 additions & 0 deletions src/versioning-strategies/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as semver from 'semver';
import {Version} from '../version';
import {VersionUpdater} from '../versioning-strategy';
import {ConventionalCommit} from '../commit';

const inc = (version: string, release: semver.ReleaseType, _preid?: string) => {
const parsed = new semver.SemVer(version);
const implicitPreid =
parsed.prerelease.length > 1 ? parsed.prerelease[0]?.toString() : undefined;
const preid = _preid || implicitPreid;
const next = new semver.SemVer(version).inc(release, preid);
if (!parsed.prerelease.length) {
return next.format();
}
const isFreshMajor = parsed.minor === 0 && parsed.patch === 0;
const isFreshMinor = parsed.patch === 0;
const shouldPrerelease =
(release === 'premajor' && isFreshMajor) ||
(release === 'preminor' && isFreshMinor) ||
release === 'prepatch';
if (shouldPrerelease) {
return semver.inc(version, 'prerelease', preid);
}
return next.format();
};

const parseCommits = (commits: ConventionalCommit[]) => {
let release: semver.ReleaseType = 'patch';
for (const commit of commits) {
if (commit.breaking) {
release = 'major';
break;
} else if (['feat', 'feature'].includes(commit.type)) {
release = 'minor';
}
}
return release;
};

type SemverOptions = {prerelease?: boolean; prereleaseType?: string};

class SemverVersioningStrategyNested {
constructor(
public options: SemverOptions,
public version: Version,
public commits: ConventionalCommit[]
) {}

bump() {
return new SemverVersioningStrategy(this.options).bump(
this.version,
this.commits
);
}
}

class SemverVersioningStrategy {
options: SemverOptions;

constructor(options: {prerelease?: boolean; prereleaseType?: string}) {
this.options = options;
}

determineReleaseType(
version: Version,
commits: ConventionalCommit[]
): VersionUpdater {
return new SemverVersioningStrategyNested(this.options, version, commits);
}

bump(currentVersion: Version, commits: ConventionalCommit[]): Version {
const prerelease = this.options.prerelease;
const tag = this.options.prereleaseType;
const releaseType = parseCommits(commits);
const addPreIfNeeded: semver.ReleaseType = prerelease
? `pre${releaseType}`
: releaseType;
const version = inc(currentVersion.toString(), addPreIfNeeded, tag);
if (!version) {
throw new Error('Could not bump version');
}
return Version.parse(version);
}
}

export {SemverVersioningStrategy};
101 changes: 101 additions & 0 deletions test/versioning-strategies/semver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {describe, it} from 'mocha';
import {SemverVersioningStrategy} from '../../src/versioning-strategies/semver';
import {ConventionalCommit} from '../../src';
import {Version} from '../../src/version';
import {expect} from 'chai';

const commit: ConventionalCommit = {
type: 'chore',
breaking: false,
notes: [],
references: [],
scope: '',
bareMessage: '',
sha: '',
message: '',
};

const g = (v: Partial<ConventionalCommit>) => ({...commit, ...v});

const COMMITS = {
major: [{type: 'feat'}, {}, {}, {breaking: true}].map(g),
minor: [{}, {}, {type: 'feat'}].map(g),
patch: [{}, {type: 'chore'}, {type: 'fix'}].map(g),
};

const checks = [
// Normal releases
['2.0.0', 'major', false, undefined, '3.0.0'],
['2.0.0', 'minor', false, undefined, '2.1.0'],
['2.0.0', 'patch', false, undefined, '2.0.1'],
// premajor -> normal
['2.0.0-pre.1', 'major', false, undefined, '2.0.0'],
['2.0.0-pre.5', 'minor', false, undefined, '2.0.0'],
['2.0.0-pre.4', 'patch', false, undefined, '2.0.0'],
// preminor -> normal
['2.1.0-pre.1', 'major', false, undefined, '3.0.0'],
['2.1.0-pre.5', 'minor', false, undefined, '2.1.0'],
['2.1.0-pre.4', 'patch', false, undefined, '2.1.0'],
// prepatch -> normal
['2.0.1-pre.1', 'major', false, undefined, '3.0.0'],
['2.0.1-pre.5', 'minor', false, undefined, '2.1.0'],
['2.0.1-pre.4', 'patch', false, undefined, '2.0.1'],
// Prereleases
['2.0.0', 'major', true, 'pre', '3.0.0-pre.0'],
['2.0.0', 'minor', true, 'pre', '2.1.0-pre.0'],
['2.0.0', 'patch', true, 'pre', '2.0.1-pre.0'],
// premajor - prereleases
['2.0.0-pre.1', 'major', true, undefined, '2.0.0-pre.2'],
['2.0.0-pre.1', 'minor', true, undefined, '2.0.0-pre.2'],
['2.0.0-pre.1', 'patch', true, undefined, '2.0.0-pre.2'],
// preminor - prereleases
['2.1.0-pre.1', 'major', true, undefined, '3.0.0-pre.0'],
['2.1.0-pre.1', 'minor', true, undefined, '2.1.0-pre.2'],
['2.1.0-pre.1', 'patch', true, undefined, '2.1.0-pre.2'],
// prepatch - prereleases
['2.0.1-pre.1', 'major', true, undefined, '3.0.0-pre.0'],
['2.0.1-pre.1', 'minor', true, undefined, '2.1.0-pre.0'],
['2.0.1-pre.1', 'patch', true, undefined, '2.0.1-pre.2'],
// different prerelease identifiers
['2.0.0-beta.1', 'major', true, undefined, '2.0.0-beta.2'],
['2.0.0-alpha.1', 'major', true, undefined, '2.0.0-alpha.2'],
['2.0.0-rc.1', 'major', true, undefined, '2.0.0-rc.2'],
['2.0.0-0', 'major', true, undefined, '2.0.0-1'],
// leaves prerelease
['2.0.0-beta.1', 'major', false, undefined, '2.0.0'],
['2.0.0-alpha.1', 'major', false, undefined, '2.0.0'],
['2.0.0-rc.1', 'major', false, undefined, '2.0.0'],
['2.0.0-0', 'major', false, undefined, '2.0.0'],
] satisfies [
string,
keyof typeof COMMITS,
boolean,
string | undefined,
string
][];

describe('SemverVersioningStrategy', () => {
for (const [
version,
commits,
prerelease,
prereleaseType,
expected,
] of checks) {
const name = [version, commits, prerelease, prereleaseType, expected];
const id = name
.map(v => (typeof v === 'undefined' ? 'undefined' : v))
.join(',');
it(`${id}`, () => {
const r = new SemverVersioningStrategy({prerelease, prereleaseType}).bump(
Version.parse(version),
COMMITS[commits]
);
expect(
`${r.major}.${r.minor}.${r.patch}${
r.preRelease ? `-${r.preRelease}` : ''
}`
).to.equal(expected, id);
});
}
});