Skip to content

Commit

Permalink
feat: allow nth as a config input (#164)
Browse files Browse the repository at this point in the history
* refactor: remove dup implementation for first/last

* remove `;` for linter

* feat: add `nth` input config

---------

Co-authored-by: Peter Evans <[email protected]>
  • Loading branch information
NiloCK and peter-evans authored Apr 25, 2023
1 parent 4809031 commit e3ce6a8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 28 additions & 14 deletions __test__/find.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('find comment tests', () => {
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -31,7 +32,8 @@ describe('find comment tests', () => {
commentAuthor: '',
bodyIncludes: 'not-exist',
bodyRegex: '',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -53,7 +55,8 @@ describe('find comment tests', () => {
commentAuthor: '',
bodyIncludes: '',
bodyRegex: '^.*Kansas.*$',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -73,7 +76,8 @@ describe('find comment tests', () => {
commentAuthor: '',
bodyIncludes: '',
bodyRegex: '^.*not-exist.*$',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -95,7 +99,8 @@ describe('find comment tests', () => {
commentAuthor: 'dorothy',
bodyIncludes: '',
bodyRegex: '',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -115,7 +120,8 @@ describe('find comment tests', () => {
commentAuthor: 'toto',
bodyIncludes: '',
bodyRegex: '',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -137,7 +143,8 @@ describe('find comment tests', () => {
commentAuthor: 'dorothy',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -157,7 +164,8 @@ describe('find comment tests', () => {
commentAuthor: 'dorothy',
bodyIncludes: 'not-exist',
bodyRegex: '',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -177,7 +185,8 @@ describe('find comment tests', () => {
commentAuthor: 'toto',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -199,7 +208,8 @@ describe('find comment tests', () => {
commentAuthor: 'dorothy',
bodyIncludes: '',
bodyRegex: '^.*Kansas.*$',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -219,7 +229,8 @@ describe('find comment tests', () => {
commentAuthor: 'dorothy',
bodyIncludes: '',
bodyRegex: '/^.*KaNsAs.*$/i',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -239,7 +250,8 @@ describe('find comment tests', () => {
commentAuthor: 'dorothy',
bodyIncludes: '',
bodyRegex: '^.*not-exist.*$',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -259,7 +271,8 @@ describe('find comment tests', () => {
commentAuthor: 'toto',
bodyIncludes: '',
bodyRegex: '^.*Kansas.*$',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand All @@ -281,7 +294,8 @@ describe('find comment tests', () => {
commentAuthor: 'dorothy',
bodyIncludes: 'feeling',
bodyRegex: '^.*Kansas.*$',
direction: 'direction'
direction: 'direction',
nth: 0
},
{
id: 1,
Expand Down
13 changes: 9 additions & 4 deletions src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Inputs {
bodyIncludes: string
bodyRegex: string
direction: string
nth: number
}

export interface Comment {
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ async function run(): Promise<void> {
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)}`)

Expand Down

0 comments on commit e3ce6a8

Please sign in to comment.