Feat: support flags when using regexp

This commit is contained in:
DELMAS Nicolas 2022-09-23 15:44:55 +02:00
parent 8a595f42df
commit 472c83c1ce
11 changed files with 52 additions and 10 deletions

View file

@ -8,8 +8,12 @@ export class ActionConfig {
public useBranches: boolean = false;
/** A string which, if present in a git commit, indicates that a change represents a major (breaking) change. Wrap with '/' to match using a regular expression. */
public majorPattern: string = "(MAJOR)";
/** A string which indicates the flags used by the `majorPattern` regular expression. */
public majorFlags: string = "";
/** A string which, if present in a git commit, indicates that a change represents a minor (feature) change. Wrap with '/' to match using a regular expression. */
public minorPattern: string = "(MINOR)";
/** A string which indicates the flags used by the `minorPattern` regular expression. */
public minorFlags: string = "";
/** Pattern to use when formatting output version */
public versionFormat: string = '${major}.${minor}.${patch}';
/** Path to check for changes. If any changes are detected in the path the 'changed' output will true. Enter multiple paths separated by spaces. */

View file

@ -472,6 +472,22 @@ test('Regular expressions can be used as minor tag', async () => {
expect((await repo.runAction()).formattedVersion).toBe('0.1.0+0');
}, 15000);
test('Regular expressions and flags can be used as major tag', async () => {
const repo = createTestRepo({ tagPrefix: '', majorPattern: '/s[a-z]+value/', majorFlags: 'i' }); // 0.0.1
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit SomeValue'); // 1.0.0+0
expect((await repo.runAction()).formattedVersion).toBe('1.0.0+0');
}, 15000);
test('Regular expressions and flags can be used as minor tag', async () => {
const repo = createTestRepo({ tagPrefix: '', minorPattern: '/s[a-z]+value/', minorFlags: 'i' }); // 0.0.1
repo.makeCommit('Initial Commit'); // 0.0.1+0
repo.makeCommit('Second Commit SomeValue'); // 0.0.1+1
expect((await repo.runAction()).formattedVersion).toBe('0.1.0+0');
}, 15000);
test('Tag prefix can include forward slash', async () => {
const repo = createTestRepo({ tagPrefix: 'version/' }); // 0.0.0

View file

@ -39,6 +39,8 @@ export async function run() {
useBranches: core.getInput('use_branches') === 'true',
majorPattern: core.getInput('major_pattern'),
minorPattern: core.getInput('minor_pattern'),
majorFlags: core.getInput('major_regexp_flags'),
minorFlags: core.getInput('minor_regexp_flags'),
versionFormat: core.getInput('version_format'),
changePath: core.getInput('change_path'),
namespace: core.getInput('namespace'),

View file

@ -13,13 +13,13 @@ export class DefaultVersionClassifier implements VersionClassifier {
constructor(config: ActionConfig) {
const searchBody = config.searchCommitBody;
this.majorPattern = this.parsePattern(config.majorPattern, searchBody);
this.minorPattern = this.parsePattern(config.minorPattern, searchBody);
this.majorPattern = this.parsePattern(config.majorPattern, config.majorFlags, searchBody);
this.minorPattern = this.parsePattern(config.minorPattern, config.minorFlags, searchBody);
}
protected parsePattern(pattern: string, searchBody: boolean): (pattern: CommitInfo) => boolean {
protected parsePattern(pattern: string, flags: string, searchBody: boolean): (pattern: CommitInfo) => boolean {
if (pattern.startsWith('/') && pattern.endsWith('/')) {
var regex = new RegExp(pattern.slice(1, -1));
var regex = new RegExp(pattern.slice(1, -1), flags);
return searchBody ?
(commit: CommitInfo) => regex.test(commit.subject) || regex.test(commit.body) :
(commit: CommitInfo) => regex.test(commit.subject);