Add "patch pattern" support for bump each commit versioning

This commit is contained in:
Paul Hatcherian 2023-08-09 08:55:19 -04:00
parent 59b55a49a0
commit cc7cc19f01
9 changed files with 165 additions and 11 deletions

View file

@ -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);
}
}

View file

@ -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);