Skip to content

Commit

Permalink
feat: allow nth as a config input (#170)
Browse files Browse the repository at this point in the history
* feat: allow `nth` as a config input (#164)

* refactor: remove dup implementation for first/last

* remove `;` for linter

* feat: add `nth` input config

---------

Co-authored-by: Peter Evans <18365890+peter-evans@users.noreply.github.com>

* minor refactor and tests

* update readme

* ci test

---------

Co-authored-by: Colin Kennedy <NiloCK@users.noreply.github.com>
  • Loading branch information
peter-evans and NiloCK authored Apr 25, 2023
1 parent 4809031 commit a54c31d
Show file tree
Hide file tree
Showing 7 changed files with 239 additions and 53 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ jobs:
- if: steps.fc8.outputs.comment-id != 620947762
run: exit 1

- name: Find nth comment by body-includes
uses: ./
id: fc9
with:
issue-number: 1
body-includes: comment 1
nth: 2
- if: steps.fc9.outputs.comment-id != 703343294
run: exit 1

package:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [test]
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ The action will output the comment ID of the comment matching the search criteri
direction: last
```
### Find the nth comment containing the specified string
```yml
- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: 1
body-includes: search string 1
nth: 1 # second matching comment (0-indexed)
```
### Action inputs
| Name | Description | Default |
Expand All @@ -76,6 +88,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
172 changes: 156 additions & 16 deletions __test__/find.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {findCommentPredicate} from '../src/find'
import {findCommentPredicate, findMatchingComment} from '../src/find'

describe('find comment tests', () => {
describe('findCommentPredicate tests', () => {
test('find by bodyIncludes', async () => {
expect(
findCommentPredicate(
Expand All @@ -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 All @@ -293,3 +307,129 @@ describe('find comment tests', () => {
).toEqual(true)
})
})

describe('findMatchingComment tests', () => {
// Note: Use `testComments.slice()` to avoid mutating the original array.
const testComments = [
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'},
created_at: '2020-01-01T00:00:00Z'
},
{
id: 2,
body: `You've always had the power, my dear. You just had to learn it for yourself.`,
user: {login: 'glinda'},
created_at: '2020-01-01T00:00:00Z'
},
{
id: 3,
body: `I'll get you, my pretty, and your little dog too!`,
user: {login: 'wicked-witch'},
created_at: '2020-01-01T00:00:00Z'
},
{
id: 4,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'},
created_at: '2020-01-01T00:00:00Z'
},
{
id: 5,
body: `I'll get you, my pretty, and your little dog too!`,
user: {login: 'wicked-witch'},
created_at: '2020-01-01T00:00:00Z'
}
]

test('no comments', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'first',
nth: 0
},
[]
)
).toEqual(undefined)
})

test('find with search direction first', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'first',
nth: 0
},
testComments.slice()
)?.id
).toEqual(1)
})

test('find with search direction last', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'last',
nth: 0
},
testComments.slice()
)?.id
).toEqual(4)
})

test('find nth with search direction first', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'first',
nth: 1
},
testComments.slice()
)?.id
).toEqual(4)
})

test('find nth with search direction last', async () => {
expect(
findMatchingComment(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'last',
nth: 1
},
testComments.slice()
)?.id
).toEqual(1)
})
})
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ inputs:
direction:
description: 'Search direction, specified as `first` or `last`'
default: first
nth:
description: '0-indexed number, specifying which comment to return if multiple are found'
default: 0
outputs:
comment-id:
description: 'The id of the matching comment found.'
Expand Down
Loading

0 comments on commit a54c31d

Please sign in to comment.