Skip to content

Commit

Permalink
feat: support regex flags (#132)
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-evans authored Feb 26, 2023
1 parent a420f9b commit 034abe9
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
20 changes: 20 additions & 0 deletions __test__/find.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down
9 changes: 8 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
)
}
Expand Down

0 comments on commit 034abe9

Please sign in to comment.