Skip to content

Commit a54c31d

Browse files
peter-evansNiloCK
andauthored
feat: allow nth as a config input (#170)
* 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>
1 parent 4809031 commit a54c31d

7 files changed

Lines changed: 239 additions & 53 deletions

File tree

.github/workflows/ci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,16 @@ jobs:
132132
- if: steps.fc8.outputs.comment-id != 620947762
133133
run: exit 1
134134

135+
- name: Find nth comment by body-includes
136+
uses: ./
137+
id: fc9
138+
with:
139+
issue-number: 1
140+
body-includes: comment 1
141+
nth: 2
142+
- if: steps.fc9.outputs.comment-id != 703343294
143+
run: exit 1
144+
135145
package:
136146
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
137147
needs: [test]

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ The action will output the comment ID of the comment matching the search criteri
6565
direction: last
6666
```
6767
68+
### Find the nth comment containing the specified string
69+
70+
```yml
71+
- name: Find Comment
72+
uses: peter-evans/find-comment@v2
73+
id: fc
74+
with:
75+
issue-number: 1
76+
body-includes: search string 1
77+
nth: 1 # second matching comment (0-indexed)
78+
```
79+
6880
### Action inputs
6981
7082
| Name | Description | Default |
@@ -76,6 +88,7 @@ The action will output the comment ID of the comment matching the search criteri
7688
| `body-includes` | A string to search for in the body of comments. | |
7789
| `body-regex` | A regular expression to search for in the body of comments. | |
7890
| `direction` | Search direction, specified as `first` or `last` | `first` |
91+
| `nth` | 0-indexed number, specifying which comment to return if multiple are found | 0 |
7992

8093
#### Outputs
8194

__test__/find.unit.test.ts

Lines changed: 156 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import {findCommentPredicate} from '../src/find'
1+
import {findCommentPredicate, findMatchingComment} from '../src/find'
22

3-
describe('find comment tests', () => {
3+
describe('findCommentPredicate tests', () => {
44
test('find by bodyIncludes', async () => {
55
expect(
66
findCommentPredicate(
@@ -11,7 +11,8 @@ describe('find comment tests', () => {
1111
commentAuthor: '',
1212
bodyIncludes: 'Kansas',
1313
bodyRegex: '',
14-
direction: 'direction'
14+
direction: 'direction',
15+
nth: 0
1516
},
1617
{
1718
id: 1,
@@ -31,7 +32,8 @@ describe('find comment tests', () => {
3132
commentAuthor: '',
3233
bodyIncludes: 'not-exist',
3334
bodyRegex: '',
34-
direction: 'direction'
35+
direction: 'direction',
36+
nth: 0
3537
},
3638
{
3739
id: 1,
@@ -53,7 +55,8 @@ describe('find comment tests', () => {
5355
commentAuthor: '',
5456
bodyIncludes: '',
5557
bodyRegex: '^.*Kansas.*$',
56-
direction: 'direction'
58+
direction: 'direction',
59+
nth: 0
5760
},
5861
{
5962
id: 1,
@@ -73,7 +76,8 @@ describe('find comment tests', () => {
7376
commentAuthor: '',
7477
bodyIncludes: '',
7578
bodyRegex: '^.*not-exist.*$',
76-
direction: 'direction'
79+
direction: 'direction',
80+
nth: 0
7781
},
7882
{
7983
id: 1,
@@ -95,7 +99,8 @@ describe('find comment tests', () => {
9599
commentAuthor: 'dorothy',
96100
bodyIncludes: '',
97101
bodyRegex: '',
98-
direction: 'direction'
102+
direction: 'direction',
103+
nth: 0
99104
},
100105
{
101106
id: 1,
@@ -115,7 +120,8 @@ describe('find comment tests', () => {
115120
commentAuthor: 'toto',
116121
bodyIncludes: '',
117122
bodyRegex: '',
118-
direction: 'direction'
123+
direction: 'direction',
124+
nth: 0
119125
},
120126
{
121127
id: 1,
@@ -137,7 +143,8 @@ describe('find comment tests', () => {
137143
commentAuthor: 'dorothy',
138144
bodyIncludes: 'Kansas',
139145
bodyRegex: '',
140-
direction: 'direction'
146+
direction: 'direction',
147+
nth: 0
141148
},
142149
{
143150
id: 1,
@@ -157,7 +164,8 @@ describe('find comment tests', () => {
157164
commentAuthor: 'dorothy',
158165
bodyIncludes: 'not-exist',
159166
bodyRegex: '',
160-
direction: 'direction'
167+
direction: 'direction',
168+
nth: 0
161169
},
162170
{
163171
id: 1,
@@ -177,7 +185,8 @@ describe('find comment tests', () => {
177185
commentAuthor: 'toto',
178186
bodyIncludes: 'Kansas',
179187
bodyRegex: '',
180-
direction: 'direction'
188+
direction: 'direction',
189+
nth: 0
181190
},
182191
{
183192
id: 1,
@@ -199,7 +208,8 @@ describe('find comment tests', () => {
199208
commentAuthor: 'dorothy',
200209
bodyIncludes: '',
201210
bodyRegex: '^.*Kansas.*$',
202-
direction: 'direction'
211+
direction: 'direction',
212+
nth: 0
203213
},
204214
{
205215
id: 1,
@@ -219,7 +229,8 @@ describe('find comment tests', () => {
219229
commentAuthor: 'dorothy',
220230
bodyIncludes: '',
221231
bodyRegex: '/^.*KaNsAs.*$/i',
222-
direction: 'direction'
232+
direction: 'direction',
233+
nth: 0
223234
},
224235
{
225236
id: 1,
@@ -239,7 +250,8 @@ describe('find comment tests', () => {
239250
commentAuthor: 'dorothy',
240251
bodyIncludes: '',
241252
bodyRegex: '^.*not-exist.*$',
242-
direction: 'direction'
253+
direction: 'direction',
254+
nth: 0
243255
},
244256
{
245257
id: 1,
@@ -259,7 +271,8 @@ describe('find comment tests', () => {
259271
commentAuthor: 'toto',
260272
bodyIncludes: '',
261273
bodyRegex: '^.*Kansas.*$',
262-
direction: 'direction'
274+
direction: 'direction',
275+
nth: 0
263276
},
264277
{
265278
id: 1,
@@ -281,7 +294,8 @@ describe('find comment tests', () => {
281294
commentAuthor: 'dorothy',
282295
bodyIncludes: 'feeling',
283296
bodyRegex: '^.*Kansas.*$',
284-
direction: 'direction'
297+
direction: 'direction',
298+
nth: 0
285299
},
286300
{
287301
id: 1,
@@ -293,3 +307,129 @@ describe('find comment tests', () => {
293307
).toEqual(true)
294308
})
295309
})
310+
311+
describe('findMatchingComment tests', () => {
312+
// Note: Use `testComments.slice()` to avoid mutating the original array.
313+
const testComments = [
314+
{
315+
id: 1,
316+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
317+
user: {login: 'dorothy'},
318+
created_at: '2020-01-01T00:00:00Z'
319+
},
320+
{
321+
id: 2,
322+
body: `You've always had the power, my dear. You just had to learn it for yourself.`,
323+
user: {login: 'glinda'},
324+
created_at: '2020-01-01T00:00:00Z'
325+
},
326+
{
327+
id: 3,
328+
body: `I'll get you, my pretty, and your little dog too!`,
329+
user: {login: 'wicked-witch'},
330+
created_at: '2020-01-01T00:00:00Z'
331+
},
332+
{
333+
id: 4,
334+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
335+
user: {login: 'dorothy'},
336+
created_at: '2020-01-01T00:00:00Z'
337+
},
338+
{
339+
id: 5,
340+
body: `I'll get you, my pretty, and your little dog too!`,
341+
user: {login: 'wicked-witch'},
342+
created_at: '2020-01-01T00:00:00Z'
343+
}
344+
]
345+
346+
test('no comments', async () => {
347+
expect(
348+
findMatchingComment(
349+
{
350+
token: 'token',
351+
repository: 'repository',
352+
issueNumber: 1,
353+
commentAuthor: '',
354+
bodyIncludes: 'Kansas',
355+
bodyRegex: '',
356+
direction: 'first',
357+
nth: 0
358+
},
359+
[]
360+
)
361+
).toEqual(undefined)
362+
})
363+
364+
test('find with search direction first', async () => {
365+
expect(
366+
findMatchingComment(
367+
{
368+
token: 'token',
369+
repository: 'repository',
370+
issueNumber: 1,
371+
commentAuthor: '',
372+
bodyIncludes: 'Kansas',
373+
bodyRegex: '',
374+
direction: 'first',
375+
nth: 0
376+
},
377+
testComments.slice()
378+
)?.id
379+
).toEqual(1)
380+
})
381+
382+
test('find with search direction last', async () => {
383+
expect(
384+
findMatchingComment(
385+
{
386+
token: 'token',
387+
repository: 'repository',
388+
issueNumber: 1,
389+
commentAuthor: '',
390+
bodyIncludes: 'Kansas',
391+
bodyRegex: '',
392+
direction: 'last',
393+
nth: 0
394+
},
395+
testComments.slice()
396+
)?.id
397+
).toEqual(4)
398+
})
399+
400+
test('find nth with search direction first', async () => {
401+
expect(
402+
findMatchingComment(
403+
{
404+
token: 'token',
405+
repository: 'repository',
406+
issueNumber: 1,
407+
commentAuthor: '',
408+
bodyIncludes: 'Kansas',
409+
bodyRegex: '',
410+
direction: 'first',
411+
nth: 1
412+
},
413+
testComments.slice()
414+
)?.id
415+
).toEqual(4)
416+
})
417+
418+
test('find nth with search direction last', async () => {
419+
expect(
420+
findMatchingComment(
421+
{
422+
token: 'token',
423+
repository: 'repository',
424+
issueNumber: 1,
425+
commentAuthor: '',
426+
bodyIncludes: 'Kansas',
427+
bodyRegex: '',
428+
direction: 'last',
429+
nth: 1
430+
},
431+
testComments.slice()
432+
)?.id
433+
).toEqual(1)
434+
})
435+
})

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ inputs:
1818
direction:
1919
description: 'Search direction, specified as `first` or `last`'
2020
default: first
21+
nth:
22+
description: '0-indexed number, specifying which comment to return if multiple are found'
23+
default: 0
2124
outputs:
2225
comment-id:
2326
description: 'The id of the matching comment found.'

0 commit comments

Comments
 (0)