mirror of
https://github.com/PaulHatch/semantic-version.git
synced 2026-04-06 16:16:54 +00:00
Add "patch pattern" support for bump each commit versioning
This commit is contained in:
parent
59b55a49a0
commit
cc7cc19f01
9 changed files with 165 additions and 11 deletions
|
|
@ -28,4 +28,6 @@ export class ActionConfig {
|
|||
public userFormatType: string = "csv";
|
||||
/** Prevents pre-v1.0.0 version from automatically incrementing the major version. If enabled, when the major version is 0, major releases will be treated as minor and minor as patch. Note that the versionType output is unchanged. */
|
||||
public enablePrereleaseMode: boolean = false;
|
||||
/** If bump_each_commit is also set to true, setting this value will cause the version to increment only if the pattern specified is matched. */
|
||||
public bumpEachCommitPatchPattern: string = "";
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ export async function runAction(configurationProvider: ConfigurationProvider): P
|
|||
const commitsProvider = configurationProvider.GetCommitsProvider();
|
||||
const versionClassifier = configurationProvider.GetVersionClassifier();
|
||||
const versionFormatter = configurationProvider.GetVersionFormatter();
|
||||
const tagFormmater = configurationProvider.GetTagFormatter();
|
||||
const tagFormatter = configurationProvider.GetTagFormatter();
|
||||
const userFormatter = configurationProvider.GetUserFormatter();
|
||||
|
||||
if (await currentCommitResolver.IsEmptyRepoAsync()) {
|
||||
|
|
@ -23,7 +23,7 @@ export async function runAction(configurationProvider: ConfigurationProvider): P
|
|||
versionInfo.increment,
|
||||
versionInfo.type,
|
||||
versionFormatter.Format(versionInfo),
|
||||
tagFormmater.Format(versionInfo),
|
||||
tagFormatter.Format(versionInfo),
|
||||
versionInfo.changed,
|
||||
versionInfo.isTagged,
|
||||
userFormatter.Format('author', []),
|
||||
|
|
@ -34,7 +34,7 @@ export async function runAction(configurationProvider: ConfigurationProvider): P
|
|||
}
|
||||
|
||||
const currentCommit = await currentCommitResolver.ResolveAsync();
|
||||
const lastRelease = await lastReleaseResolver.ResolveAsync(currentCommit, tagFormmater);
|
||||
const lastRelease = await lastReleaseResolver.ResolveAsync(currentCommit, tagFormatter);
|
||||
const commitSet = await commitsProvider.GetCommitsAsync(lastRelease.hash, currentCommit);
|
||||
const classification = await versionClassifier.ClassifyAsync(lastRelease, commitSet);
|
||||
|
||||
|
|
@ -65,7 +65,7 @@ export async function runAction(configurationProvider: ConfigurationProvider): P
|
|||
versionInfo.increment,
|
||||
versionInfo.type,
|
||||
versionFormatter.Format(versionInfo),
|
||||
tagFormmater.Format(versionInfo),
|
||||
tagFormatter.Format(versionInfo),
|
||||
versionInfo.changed,
|
||||
versionInfo.isTagged,
|
||||
userFormatter.Format('author', authors),
|
||||
|
|
|
|||
|
|
@ -805,6 +805,89 @@ test('Patch increments each time when bump each commit is set', async () => {
|
|||
|
||||
}, timeout);
|
||||
|
||||
test('Patch does not increment on bump each commit if a patch pattern is set', async () => {
|
||||
const repo = createTestRepo({ tagPrefix: '', versionFormat: "${major}.${minor}.${patch}+${increment}", bumpEachCommit: true, bumpEachCommitPatchPattern: '(PATCH)' });
|
||||
|
||||
repo.makeCommit('Initial Commit');
|
||||
repo.exec('git tag 0.0.1');
|
||||
const firstResult = await repo.runAction();
|
||||
repo.makeCommit('Second Commit');
|
||||
const secondResult = await repo.runAction();
|
||||
repo.makeCommit('Third Commit');
|
||||
const thirdResult = await repo.runAction();
|
||||
repo.makeCommit('fourth Commit');
|
||||
const fourthResult = await repo.runAction();
|
||||
|
||||
|
||||
expect(firstResult.formattedVersion).toBe('0.0.1+0');
|
||||
expect(secondResult.formattedVersion).toBe('0.0.1+1');
|
||||
expect(thirdResult.formattedVersion).toBe('0.0.1+2');
|
||||
expect(fourthResult.formattedVersion).toBe('0.0.1+3');
|
||||
|
||||
}, timeout);
|
||||
|
||||
test('Patch pattern increment is correct on empty repo', async () => {
|
||||
const repo = createTestRepo({ tagPrefix: '', versionFormat: "${major}.${minor}.${patch}+${increment}", bumpEachCommit: true, bumpEachCommitPatchPattern: '(PATCH)' });
|
||||
|
||||
|
||||
const initialResult = await repo.runAction();
|
||||
repo.makeCommit('Initial Commit');
|
||||
const firstResult = await repo.runAction();
|
||||
repo.makeCommit('Second Commit');
|
||||
const secondResult = await repo.runAction();
|
||||
repo.makeCommit('Third Commit');
|
||||
const thirdResult = await repo.runAction();
|
||||
repo.makeCommit('fourth Commit');
|
||||
const fourthResult = await repo.runAction();
|
||||
|
||||
expect(initialResult.formattedVersion).toBe('0.0.0+0');
|
||||
expect(firstResult.formattedVersion).toBe('0.0.1+0');
|
||||
expect(secondResult.formattedVersion).toBe('0.0.1+1');
|
||||
expect(thirdResult.formattedVersion).toBe('0.0.1+2');
|
||||
expect(fourthResult.formattedVersion).toBe('0.0.1+3');
|
||||
|
||||
}, timeout);
|
||||
|
||||
test('Patch pattern increment is correct/matches non-bumped on empty repo', async () => {
|
||||
const repo = createTestRepo({ tagPrefix: '', versionFormat: "${major}.${minor}.${patch}+${increment}", bumpEachCommit: true, bumpEachCommitPatchPattern: '(PATCH)' });
|
||||
|
||||
|
||||
repo.makeCommit('Initial Commit');
|
||||
const firstResult = await repo.runAction();
|
||||
repo.makeCommit('Second Commit');
|
||||
const secondResult = await repo.runAction();
|
||||
repo.makeCommit('Third Commit');
|
||||
const thirdResult = await repo.runAction();
|
||||
repo.makeCommit('fourth Commit');
|
||||
const fourthResult = await repo.runAction();
|
||||
|
||||
expect(firstResult.formattedVersion).toBe('0.0.1+0');
|
||||
expect(secondResult.formattedVersion).toBe('0.0.1+1');
|
||||
expect(thirdResult.formattedVersion).toBe('0.0.1+2');
|
||||
expect(fourthResult.formattedVersion).toBe('0.0.1+3');
|
||||
|
||||
}, timeout);
|
||||
|
||||
test('Patch pattern applied when present', async () => {
|
||||
const repo = createTestRepo({ tagPrefix: '', versionFormat: "${major}.${minor}.${patch}+${increment}", bumpEachCommit: true, bumpEachCommitPatchPattern: '(PATCH)' });
|
||||
|
||||
|
||||
repo.makeCommit('Initial Commit');
|
||||
const firstResult = await repo.runAction();
|
||||
repo.makeCommit('Second Commit');
|
||||
const secondResult = await repo.runAction();
|
||||
repo.makeCommit('Third Commit (PATCH)');
|
||||
const thirdResult = await repo.runAction();
|
||||
repo.makeCommit('fourth Commit');
|
||||
const fourthResult = await repo.runAction();
|
||||
|
||||
expect(firstResult.formattedVersion).toBe('0.0.1+0');
|
||||
expect(secondResult.formattedVersion).toBe('0.0.1+1');
|
||||
expect(thirdResult.formattedVersion).toBe('0.0.2+0');
|
||||
expect(fourthResult.formattedVersion).toBe('0.0.2+3');
|
||||
|
||||
}, timeout);
|
||||
|
||||
test('Current commit is provided', async () => {
|
||||
const repo = createTestRepo({ tagPrefix: '', versionFormat: "${major}.${minor}.${patch}" });
|
||||
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ export async function run() {
|
|||
searchCommitBody: core.getInput('search_commit_body') === 'true',
|
||||
userFormatType: core.getInput('user_format_type'),
|
||||
enablePrereleaseMode: core.getInput('enable_prerelease_mode') === 'true',
|
||||
bumpEachCommitPatchPattern: core.getInput('bump_each_commit_patch_pattern')
|
||||
};
|
||||
|
||||
if (config.versionFormat === '' && core.getInput('format') !== '') {
|
||||
|
|
|
|||
|
|
@ -8,9 +8,14 @@ import { VersionType } from "./VersionType";
|
|||
|
||||
export class BumpAlwaysVersionClassifier extends DefaultVersionClassifier {
|
||||
|
||||
protected patchPattern: (commit: CommitInfo) => boolean;
|
||||
|
||||
constructor(config: ActionConfig) {
|
||||
super(config);
|
||||
// Placeholder for consistency
|
||||
|
||||
this.patchPattern = !config.bumpEachCommitPatchPattern ?
|
||||
_ => true :
|
||||
this.parsePattern(config.bumpEachCommitPatchPattern, "", config.searchCommitBody);
|
||||
}
|
||||
|
||||
public override async ClassifyAsync(lastRelease: ReleaseInformation, commitSet: CommitInfoSet): Promise<VersionClassification> {
|
||||
|
|
@ -21,6 +26,7 @@ export class BumpAlwaysVersionClassifier extends DefaultVersionClassifier {
|
|||
|
||||
let { major, minor, patch } = lastRelease;
|
||||
let type = VersionType.None;
|
||||
let increment = 0;
|
||||
|
||||
if (commitSet.commits.length === 0) {
|
||||
return new VersionClassification(type, 0, false, major, minor, patch);
|
||||
|
|
@ -32,16 +38,25 @@ export class BumpAlwaysVersionClassifier extends DefaultVersionClassifier {
|
|||
minor = 0;
|
||||
patch = 0;
|
||||
type = VersionType.Major;
|
||||
increment = 0;
|
||||
} else if (this.minorPattern(commit)) {
|
||||
minor += 1;
|
||||
patch = 0;
|
||||
type = VersionType.Minor;
|
||||
increment = 0;
|
||||
} else {
|
||||
patch += 1;
|
||||
type = VersionType.Patch;
|
||||
if (this.patchPattern(commit) ||
|
||||
(major === 0 && minor === 0 && patch === 0 && commitSet.commits.length > 0)) {
|
||||
patch += 1;
|
||||
type = VersionType.Patch;
|
||||
increment = 0;
|
||||
} else {
|
||||
type = VersionType.None;
|
||||
increment++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new VersionClassification(type, 0, true, major, minor, patch);
|
||||
return new VersionClassification(type, increment, true, major, minor, patch);
|
||||
}
|
||||
}
|
||||
|
|
@ -20,8 +20,10 @@ export class DefaultVersionClassifier implements VersionClassifier {
|
|||
}
|
||||
|
||||
protected parsePattern(pattern: string, flags: string, searchBody: boolean): (pattern: CommitInfo) => boolean {
|
||||
if (pattern.startsWith('/') && pattern.endsWith('/')) {
|
||||
var regex = new RegExp(pattern.slice(1, -1), flags);
|
||||
if (/^\/.+\/[i]*$/.test(pattern)) {
|
||||
const regexEnd = pattern.lastIndexOf('/');
|
||||
const parsedFlags = pattern.slice(pattern.lastIndexOf('/') + 1);
|
||||
const regex = new RegExp(pattern.slice(1, regexEnd), parsedFlags || flags);
|
||||
return searchBody ?
|
||||
(commit: CommitInfo) => regex.test(commit.subject) || regex.test(commit.body) :
|
||||
(commit: CommitInfo) => regex.test(commit.subject);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue