From 20cbb6b54d209d09050079ed1ff84e7ff081a3db Mon Sep 17 00:00:00 2001 From: Colin Kennedy Date: Fri, 21 Apr 2023 10:25:32 -0300 Subject: [PATCH 1/3] refactor: remove dup implementation for first/last --- src/find.ts | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/find.ts b/src/find.ts index 5aa54a9..d7b8057 100644 --- a/src/find.ts +++ b/src/find.ts @@ -54,28 +54,17 @@ export async function findComment( issue_number: inputs.issueNumber } - if (inputs.direction == 'first') { - for await (const {data: comments} of octokit.paginate.iterator( - octokit.rest.issues.listComments, - parameters - )) { - // Search each page for the comment - const comment = comments.find(comment => - findCommentPredicate(inputs, comment) - ) - if (comment) return comment - } - } else { - // direction == 'last' - const comments = await octokit.paginate( - octokit.rest.issues.listComments, - parameters - ) + const comments = await octokit.paginate( + octokit.rest.issues.listComments, + parameters + ) + if (inputs.direction == 'last') { comments.reverse() - const comment = comments.find(comment => - findCommentPredicate(inputs, comment) - ) - if (comment) return comment } + const comment = comments.find(comment => + findCommentPredicate(inputs, comment) + ) + if (comment) return comment; + return undefined } From 82b6df52108c7e4203bfcdb00e6e07dcf6b8f113 Mon Sep 17 00:00:00 2001 From: Colin Kennedy Date: Fri, 21 Apr 2023 10:29:50 -0300 Subject: [PATCH 2/3] remove `;` for linter --- src/find.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/find.ts b/src/find.ts index d7b8057..6430b93 100644 --- a/src/find.ts +++ b/src/find.ts @@ -64,7 +64,7 @@ export async function findComment( const comment = comments.find(comment => findCommentPredicate(inputs, comment) ) - if (comment) return comment; - + if (comment) return comment + return undefined } From be965e04b7a8e589a312d844b826d9bbb76b6b54 Mon Sep 17 00:00:00 2001 From: Colin Kennedy Date: Fri, 21 Apr 2023 10:45:51 -0300 Subject: [PATCH 3/3] feat: add `nth` input config --- README.md | 1 + __test__/find.unit.test.ts | 42 +++++++++++++++++++++++++------------- src/find.ts | 13 ++++++++---- src/main.ts | 3 ++- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 38e84aa..443944c 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,7 @@ The action will output the comment ID of the comment matching the search criteri | `body-includes` | A string to search for in the body of comments. | | | `body-regex` | A regular expression to search for in the body of comments. | | | `direction` | Search direction, specified as `first` or `last` | `first` | +| `nth` | 0-indexed number, specifying which comment to return if multiple are found | 0 | #### Outputs diff --git a/__test__/find.unit.test.ts b/__test__/find.unit.test.ts index c2375c2..f7cf95a 100644 --- a/__test__/find.unit.test.ts +++ b/__test__/find.unit.test.ts @@ -11,7 +11,8 @@ describe('find comment tests', () => { commentAuthor: '', bodyIncludes: 'Kansas', bodyRegex: '', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -31,7 +32,8 @@ describe('find comment tests', () => { commentAuthor: '', bodyIncludes: 'not-exist', bodyRegex: '', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -53,7 +55,8 @@ describe('find comment tests', () => { commentAuthor: '', bodyIncludes: '', bodyRegex: '^.*Kansas.*$', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -73,7 +76,8 @@ describe('find comment tests', () => { commentAuthor: '', bodyIncludes: '', bodyRegex: '^.*not-exist.*$', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -95,7 +99,8 @@ describe('find comment tests', () => { commentAuthor: 'dorothy', bodyIncludes: '', bodyRegex: '', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -115,7 +120,8 @@ describe('find comment tests', () => { commentAuthor: 'toto', bodyIncludes: '', bodyRegex: '', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -137,7 +143,8 @@ describe('find comment tests', () => { commentAuthor: 'dorothy', bodyIncludes: 'Kansas', bodyRegex: '', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -157,7 +164,8 @@ describe('find comment tests', () => { commentAuthor: 'dorothy', bodyIncludes: 'not-exist', bodyRegex: '', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -177,7 +185,8 @@ describe('find comment tests', () => { commentAuthor: 'toto', bodyIncludes: 'Kansas', bodyRegex: '', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -199,7 +208,8 @@ describe('find comment tests', () => { commentAuthor: 'dorothy', bodyIncludes: '', bodyRegex: '^.*Kansas.*$', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -219,7 +229,8 @@ describe('find comment tests', () => { commentAuthor: 'dorothy', bodyIncludes: '', bodyRegex: '/^.*KaNsAs.*$/i', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -239,7 +250,8 @@ describe('find comment tests', () => { commentAuthor: 'dorothy', bodyIncludes: '', bodyRegex: '^.*not-exist.*$', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -259,7 +271,8 @@ describe('find comment tests', () => { commentAuthor: 'toto', bodyIncludes: '', bodyRegex: '^.*Kansas.*$', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, @@ -281,7 +294,8 @@ describe('find comment tests', () => { commentAuthor: 'dorothy', bodyIncludes: 'feeling', bodyRegex: '^.*Kansas.*$', - direction: 'direction' + direction: 'direction', + nth: 0 }, { id: 1, diff --git a/src/find.ts b/src/find.ts index 6430b93..d48ac56 100644 --- a/src/find.ts +++ b/src/find.ts @@ -8,6 +8,7 @@ export interface Inputs { bodyIncludes: string bodyRegex: string direction: string + nth: number } export interface Comment { @@ -54,17 +55,21 @@ export async function findComment( issue_number: inputs.issueNumber } - const comments = await octokit.paginate( + const allComments = await octokit.paginate( octokit.rest.issues.listComments, parameters ) if (inputs.direction == 'last') { - comments.reverse() + allComments.reverse() } - const comment = comments.find(comment => + const matchingComments = allComments.filter(comment => findCommentPredicate(inputs, comment) ) - if (comment) return comment + + const comment = matchingComments[inputs.nth] + if (comment) { + return comment + } return undefined } diff --git a/src/main.ts b/src/main.ts index 71091e5..22bdea5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -16,7 +16,8 @@ async function run(): Promise { commentAuthor: core.getInput('comment-author'), bodyIncludes: core.getInput('body-includes'), bodyRegex: core.getInput('body-regex'), - direction: core.getInput('direction') + direction: core.getInput('direction'), + nth: Number(core.getInput('nth')) } core.debug(`Inputs: ${inspect(inputs)}`)