@@ -25,11 +25,11 @@ import * as core from '@actions/core'
25
25
* error message and the messages.
26
26
*/
27
27
export interface ICheckerArguments {
28
- pattern : string
28
+ pattern : null | string
29
29
flags : string
30
30
error : string
31
31
messages : string [ ]
32
- debugRegex : null | ( string | string [ ] ) [ ] ,
32
+ progressivePattern : null | ( string | string [ ] ) [ ] ,
33
33
}
34
34
35
35
/**
@@ -41,52 +41,75 @@ export interface ICheckerArguments {
41
41
export async function checkCommitMessages (
42
42
args : ICheckerArguments
43
43
) : 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.` )
83
60
}
84
- }
85
61
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
+ }
90
113
}
91
114
92
115
/**
@@ -108,7 +131,7 @@ function checkMessage(
108
131
/*
109
132
* Debugs until which characters does a regex matches.
110
133
*/
111
- const debugRegexMatching = ( regexes : ( string | string [ ] ) [ ] , str : string ) : string => {
134
+ const debugRegexMatching = ( regexes : ( string | string [ ] ) [ ] , str : string ) : string | null => {
112
135
str = str . replaceAll ( '\r' , '' ) ;
113
136
let matchesUntil = 0 ;
114
137
const copyStr = str ;
@@ -140,7 +163,7 @@ const debugRegexMatching = (regexes: (string | string[])[], str: string): string
140
163
} while ( regexes . length > 0 ) ;
141
164
142
165
if ( str . length === 0 && regexes . length === 0 ) {
143
- return "The regex should work." ;
166
+ return null ;
144
167
} else {
145
168
const paddingLeft = Math . max ( matchesUntil - 10 , 0 ) ;
146
169
const paddingRight = Math . min ( matchesUntil + 10 , copyStr . length ) ;
0 commit comments