Skip to content

Commit d273f9b

Browse files
committed
wip
1 parent 4c06ad2 commit d273f9b

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

src/commit-message-checker.ts

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export interface ICheckerArguments {
2929
flags: string
3030
error: string
3131
messages: string[]
32+
debugRegex: null | (string | string[])[],
3233
}
3334

3435
/**
@@ -73,7 +74,10 @@ export async function checkCommitMessages(
7374
core.info(`- OK: "${message}"`)
7475
} else {
7576
core.info(`- failed: "${message}"`)
76-
result = false
77+
if (args.debugRegex !== null) {
78+
core.info(debugRegexMatching(args.debugRegex, message));
79+
}
80+
result = false;
7781
}
7882
}
7983

@@ -98,3 +102,80 @@ function checkMessage(
98102
const regex = new RegExp(pattern, flags)
99103
return regex.test(message)
100104
}
105+
106+
/*
107+
* Debugs until which characters does a regex matches.
108+
*/
109+
const debugRegexMatching = (regexes: (string | string[])[], str: string): string => {
110+
str = str.replaceAll('\r', '');
111+
let matchesUntil = 0;
112+
const copyStr = str;
113+
let rgx;
114+
115+
do {
116+
if (Array.isArray(regexes[0])) {
117+
const previousLength = str.length;
118+
const [newString, failedAt] = optionalRemoval(regexes[0], str);
119+
str = newString;
120+
matchesUntil += previousLength - str.length;
121+
122+
if (failedAt !== null) {
123+
break;
124+
}
125+
} else {
126+
rgx = new RegExp("^" + regexes[0]);
127+
128+
if (rgx.test(str)) {
129+
const previousLength = str.length;
130+
str = str.replace(rgx, '');
131+
matchesUntil += previousLength - str.length;
132+
} else {
133+
break;
134+
}
135+
}
136+
137+
regexes = regexes.splice(1);
138+
} while (regexes.length > 0);
139+
140+
if (str.length === 0 && regexes.length === 0) {
141+
return "The regex should work.";
142+
} else {
143+
const paddingLeft = Math.max(matchesUntil - 10, 0);
144+
const paddingRight = Math.min(matchesUntil + 10, copyStr.length);
145+
const rightDots = paddingRight !== copyStr.length ? '…' : '';
146+
const leftDots = paddingLeft !== 0 ? '…' : '';
147+
148+
if (str.length > 0 && regexes.length === 0) {
149+
return `Trailing characters: "${str}"
150+
--------------------------------
151+
Context: "${leftDots}${copyStr.slice(paddingLeft, Math.min(copyStr.length, matchesUntil + 10)).replaceAll('\n', '␤')}${rightDots}"
152+
${" ".repeat(matchesUntil - paddingLeft)}^`;
153+
} else {
154+
return `The regex stopped matching at index: ${matchesUntil}.
155+
Expected: "${rgx}"
156+
Context: "${leftDots}${copyStr.slice(paddingLeft, paddingRight).replaceAll('\n', '␤')}${rightDots}"
157+
${" ".repeat(matchesUntil - paddingLeft)}^${"~".repeat(paddingRight - matchesUntil)}`;
158+
}
159+
}
160+
}
161+
162+
/// If the first member of the optionalRegexes is matching then all the other should match. Else we don't test the others.
163+
const optionalRemoval = (optionalRegexes: string[], str: string): [string, number | null] => {
164+
let rgx = new RegExp("^" + optionalRegexes[0]);
165+
166+
if (rgx.test(str)) {
167+
do {
168+
rgx = new RegExp("^" + optionalRegexes[0]);
169+
170+
if (rgx.test(str)) {
171+
str = str.replace(rgx, '');
172+
} else {
173+
return [str, 0];
174+
}
175+
optionalRegexes = optionalRegexes.splice(1);
176+
} while (optionalRegexes.length > 0);
177+
}
178+
179+
return [str, null];
180+
}
181+

src/input-helper.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export interface PullRequestOptions {
2727
ignoreTitle: boolean
2828
ignoreDescription: boolean
2929
checkAllCommitMessages: boolean // requires github token
30-
accessToken: string
30+
accessToken: string,
3131
}
3232

3333
/**
@@ -60,6 +60,12 @@ export async function getInputs(): Promise<ICheckerArguments> {
6060
const excludeDescriptionStr = core.getInput('excludeDescription')
6161
core.debug(`excludeDescription: ${excludeDescriptionStr}`)
6262

63+
// Debug regex
64+
const debugRegex = core.getInput('debugRegex')
65+
core.debug(`debugRegex: ${debugRegex}`)
66+
67+
if (debugRegex.length > 0) result.debugRegex = JSON.parse(debugRegex);
68+
6369
// Get checkAllCommitMessages
6470
const checkAllCommitMessagesStr = core.getInput('checkAllCommitMessages')
6571
core.debug(`checkAllCommitMessages: ${checkAllCommitMessagesStr}`)

0 commit comments

Comments
 (0)