Skip to content

Commit c6ec261

Browse files
committed
feat!: allow only matching my progressive pattern
1 parent 54ebcd6 commit c6ec261

File tree

3 files changed

+82
-58
lines changed

3 files changed

+82
-58
lines changed

action.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@ description: 'Check the commit message against a regex pattern'
33
author: 'acksly'
44
inputs:
55
pattern:
6-
description: 'A regex pattern to check if a commit message is valid.'
7-
required: true
6+
description: 'A regex pattern to check if a commit message is valid. Either `progressivePattern` or `pattern` should be present.'
7+
default: ''
8+
required: false
9+
progressivePattern:
10+
description: 'A set of regex to progressively check matching strings. Either `progressivePattern` or `pattern` should be present.'
11+
required: false
12+
default: ''
813
flags:
914
description: 'Expression flags change how the expression is interpreted.'
1015
required: false
@@ -28,10 +33,6 @@ inputs:
2833
description: 'you must provide GITHUB_TOKEN to this input if checkAllCommitMessages is true'
2934
required: false
3035
default: 'false'
31-
debugRegex:
32-
description: 'A set of regex to debug non matching strings'
33-
required: false
34-
default: ''
3536
runs:
3637
using: node16
3738
main: dist/index.js

src/commit-message-checker.ts

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import * as core from '@actions/core'
2525
* error message and the messages.
2626
*/
2727
export interface ICheckerArguments {
28-
pattern: string
28+
pattern: null | string
2929
flags: string
3030
error: string
3131
messages: string[]
32-
debugRegex: null | (string | string[])[],
32+
progressivePattern: null | (string | string[])[],
3333
}
3434

3535
/**
@@ -41,52 +41,75 @@ export interface ICheckerArguments {
4141
export async function checkCommitMessages(
4242
args: ICheckerArguments
4343
): Promise<void> {
44-
// Check arguments
45-
if (args.pattern.length === 0) {
46-
throw new Error(`PATTERN not defined.`)
47-
}
48-
49-
const regex = new RegExp('[^gimsuy]', 'g')
50-
let invalidChars
51-
let chars = ''
52-
while ((invalidChars = regex.exec(args.flags)) !== null) {
53-
chars += invalidChars[0]
54-
}
55-
if (chars !== '') {
56-
throw new Error(`FLAGS contains invalid characters "${chars}".`)
57-
}
58-
59-
if (args.error.length === 0) {
60-
throw new Error(`ERROR not defined.`)
61-
}
62-
63-
if (args.messages.length === 0) {
64-
throw new Error(`MESSAGES not defined.`)
65-
}
66-
67-
// Check messages
68-
let result = true
69-
70-
core.info(`Checking commit messages against "${args.pattern}"...`)
71-
72-
let debugRegexMsg = '';
73-
74-
for (const message of args.messages) {
75-
if (checkMessage(message.replaceAll('\r', ''), args.pattern, args.flags)) {
76-
core.info(`- OK: "${message}"`)
77-
} else {
78-
core.info(`- failed: "${message}"`)
79-
if (args.debugRegex !== null) {
80-
debugRegexMsg = '\n' + debugRegexMatching(args.debugRegex, message);
81-
}
82-
result = false;
44+
// Check arguments
45+
if (
46+
(
47+
args.pattern === null &&
48+
args.progressivePattern === null
49+
) || (
50+
args.pattern === null &&
51+
args.progressivePattern !== null &&
52+
args.progressivePattern.length !== 0
53+
) || (
54+
args.progressivePattern === null &&
55+
args.pattern !== null &&
56+
args.pattern.length !== 0
57+
)
58+
) {
59+
throw new Error(`PATTERN not defined.`)
8360
}
84-
}
8561

86-
// Throw error in case of failed test
87-
if (!result) {
88-
throw new Error(args.error + debugRegexMsg)
89-
}
62+
const regex = new RegExp('[^gimsuy]', 'g')
63+
let invalidChars
64+
let chars = ''
65+
while ((invalidChars = regex.exec(args.flags)) !== null) {
66+
chars += invalidChars[0]
67+
}
68+
if (chars !== '') {
69+
throw new Error(`FLAGS contains invalid characters "${chars}".`)
70+
}
71+
72+
if (args.error.length === 0) {
73+
throw new Error(`ERROR not defined.`)
74+
}
75+
76+
if (args.messages.length === 0) {
77+
throw new Error(`MESSAGES not defined.`)
78+
}
79+
80+
// Check messages
81+
let result = true
82+
83+
core.info(`Checking commit messages against "${args.pattern}"...`)
84+
85+
for (const message of args.messages) {
86+
if (args.pattern === null || args.pattern.length === 0) {
87+
const errorMessage = debugRegexMatching(args.progressivePattern as (string | string[])[], message);
88+
89+
if (errorMessage !== null) {
90+
core.info(`- failed: "${message}"`)
91+
args.error += '\n' + errorMessage;
92+
result = false;
93+
} else {
94+
core.info(`- OK: "${message}"`)
95+
}
96+
} else {
97+
if (checkMessage(message.replaceAll('\r', ''), args.pattern, args.flags)) {
98+
core.info(`- OK: "${message}"`)
99+
} else {
100+
core.info(`- failed: "${message}"`)
101+
if (args.progressivePattern !== null) {
102+
args.error += '\n' + (debugRegexMatching(args.progressivePattern, message) ?? 'Unexpected missmatch.');
103+
}
104+
result = false;
105+
}
106+
}
107+
}
108+
109+
// Throw error in case of failed test
110+
if (!result) {
111+
throw new Error(args.error)
112+
}
90113
}
91114

92115
/**
@@ -108,7 +131,7 @@ function checkMessage(
108131
/*
109132
* Debugs until which characters does a regex matches.
110133
*/
111-
const debugRegexMatching = (regexes: (string | string[])[], str: string): string => {
134+
const debugRegexMatching = (regexes: (string | string[])[], str: string): string | null => {
112135
str = str.replaceAll('\r', '');
113136
let matchesUntil = 0;
114137
const copyStr = str;
@@ -140,7 +163,7 @@ const debugRegexMatching = (regexes: (string | string[])[], str: string): string
140163
} while (regexes.length > 0);
141164

142165
if (str.length === 0 && regexes.length === 0) {
143-
return "The regex should work.";
166+
return null;
144167
} else {
145168
const paddingLeft = Math.max(matchesUntil - 10, 0);
146169
const paddingRight = Math.min(matchesUntil + 10, copyStr.length);

src/input-helper.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function getInputs(): Promise<ICheckerArguments> {
4141
core.debug('Get inputs...')
4242

4343
// Get pattern
44-
result.pattern = core.getInput('pattern', {required: true})
44+
result.pattern = core.getInput('pattern')
4545
core.debug(`pattern: ${result.pattern}`)
4646

4747
// Get flags
@@ -61,10 +61,10 @@ export async function getInputs(): Promise<ICheckerArguments> {
6161
core.debug(`excludeDescription: ${excludeDescriptionStr}`)
6262

6363
// Debug regex
64-
const debugRegex = core.getInput('debugRegex')
65-
core.debug(`debugRegex: ${debugRegex}`)
64+
const progressivePattern = core.getInput('progressivePattern')
65+
core.debug(`progressivePattern: ${progressivePattern}`)
6666

67-
if (debugRegex.length > 0) result.debugRegex = JSON.parse(debugRegex);
67+
if (progressivePattern.length > 0) result.progressivePattern = JSON.parse(progressivePattern);
6868

6969
// Get checkAllCommitMessages
7070
const checkAllCommitMessagesStr = core.getInput('checkAllCommitMessages')

0 commit comments

Comments
 (0)