diff --git a/__test__/find.unit.test.ts b/__test__/find.unit.test.ts index 381370a..c2375c2 100644 --- a/__test__/find.unit.test.ts +++ b/__test__/find.unit.test.ts @@ -210,6 +210,26 @@ describe('find comment tests', () => { ) ).toEqual(true) + expect( + findCommentPredicate( + { + token: 'token', + repository: 'repository', + issueNumber: 1, + commentAuthor: 'dorothy', + bodyIncludes: '', + bodyRegex: '/^.*KaNsAs.*$/i', + direction: 'direction' + }, + { + id: 1, + body: `Toto, I've a feeling we're not in Kansas anymore.`, + user: {login: 'dorothy'}, + created_at: '2020-01-01T00:00:00Z' + } + ) + ).toEqual(true) + expect( findCommentPredicate( { diff --git a/dist/index.js b/dist/index.js index 9c71fc4..90d910b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -48,6 +48,13 @@ var __asyncValues = (this && this.__asyncValues) || function (o) { Object.defineProperty(exports, "__esModule", ({ value: true })); exports.findComment = exports.findCommentPredicate = void 0; const github = __importStar(__nccwpck_require__(5438)); +function stringToRegex(s) { + const m = s.match(/^(.)(.*?)\1([gimsuy]*)$/); + if (m) + return new RegExp(m[2], m[3]); + else + return new RegExp(s); +} function findCommentPredicate(inputs, comment) { return ((inputs.commentAuthor && comment.user ? comment.user.login === inputs.commentAuthor @@ -56,7 +63,7 @@ function findCommentPredicate(inputs, comment) { ? comment.body.includes(inputs.bodyIncludes) : true) && (inputs.bodyRegex && comment.body - ? comment.body.match(inputs.bodyRegex) !== null + ? comment.body.match(stringToRegex(inputs.bodyRegex)) !== null : true)); } exports.findCommentPredicate = findCommentPredicate; diff --git a/src/find.ts b/src/find.ts index 5c28771..5aa54a9 100644 --- a/src/find.ts +++ b/src/find.ts @@ -19,6 +19,12 @@ export interface Comment { created_at: string } +function stringToRegex(s: string): RegExp { + const m = s.match(/^(.)(.*?)\1([gimsuy]*)$/) + if (m) return new RegExp(m[2], m[3]) + else return new RegExp(s) +} + export function findCommentPredicate( inputs: Inputs, comment: Comment @@ -31,7 +37,7 @@ export function findCommentPredicate( ? comment.body.includes(inputs.bodyIncludes) : true) && (inputs.bodyRegex && comment.body - ? comment.body.match(inputs.bodyRegex) !== null + ? comment.body.match(stringToRegex(inputs.bodyRegex)) !== null : true) ) }