Skip to content

Commit f4499a7

Browse files
feat: search comment body using a regular expression (#77)
* First stab at adding support for regex searching comments with body-regex input * false should have been true * Minor refactor and tests * Add ci test * Update readme Co-authored-by: dluftspring <[email protected]>
1 parent 1c328ad commit f4499a7

File tree

9 files changed

+903
-132
lines changed

9 files changed

+903
-132
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ jobs:
122122
- if: steps.fc7.outputs.comment-id != 771260630
123123
run: exit 1
124124

125+
- name: Find comment by body-regex
126+
uses: ./
127+
id: fc8
128+
with:
129+
issue-number: 1
130+
body-regex: '^.*search string 1.*$'
131+
- if: steps.fc8.outputs.comment-id != 620947762
132+
run: exit 1
133+
125134
package:
126135
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
127136
needs: [test]

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ The action will output the comment ID of the comment matching the search criteri
4242
body-includes: search string 1
4343
```
4444
45+
### Find the first comment matching the specified regular expression
46+
47+
```yml
48+
- name: Find Comment
49+
uses: peter-evans/find-comment@v2
50+
id: fc
51+
with:
52+
issue-number: 1
53+
body-regex: '^.*search string 1.*$'
54+
```
55+
4556
### Find the last comment containing the specified string
4657
4758
```yml
@@ -63,6 +74,7 @@ The action will output the comment ID of the comment matching the search criteri
6374
| `issue-number` | The number of the issue or pull request in which to search. | |
6475
| `comment-author` | The GitHub user name of the comment author. | |
6576
| `body-includes` | A string to search for in the body of comments. | |
77+
| `body-regex` | A regular expression to search for in the body of comments. | |
6678
| `direction` | Search direction, specified as `first` or `last` | `first` |
6779

6880
#### Outputs

__test__/find.unit.test.ts

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
import {findCommentPredicate} from '../lib/find'
2+
3+
describe('find comment tests', () => {
4+
test('find by bodyIncludes', async () => {
5+
expect(
6+
findCommentPredicate(
7+
{
8+
token: 'token',
9+
repository: 'repository',
10+
issueNumber: 1,
11+
commentAuthor: '',
12+
bodyIncludes: 'Kansas',
13+
bodyRegex: '',
14+
direction: 'direction'
15+
},
16+
{
17+
id: 1,
18+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
19+
user: {login: 'dorothy'}
20+
}
21+
)
22+
).toEqual(true)
23+
24+
expect(
25+
findCommentPredicate(
26+
{
27+
token: 'token',
28+
repository: 'repository',
29+
issueNumber: 1,
30+
commentAuthor: '',
31+
bodyIncludes: 'not-exist',
32+
bodyRegex: '',
33+
direction: 'direction'
34+
},
35+
{
36+
id: 1,
37+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
38+
user: {login: 'dorothy'}
39+
}
40+
)
41+
).toEqual(false)
42+
})
43+
44+
test('find by bodyRegex', async () => {
45+
expect(
46+
findCommentPredicate(
47+
{
48+
token: 'token',
49+
repository: 'repository',
50+
issueNumber: 1,
51+
commentAuthor: '',
52+
bodyIncludes: '',
53+
bodyRegex: '^.*Kansas.*$',
54+
direction: 'direction'
55+
},
56+
{
57+
id: 1,
58+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
59+
user: {login: 'dorothy'}
60+
}
61+
)
62+
).toEqual(true)
63+
64+
expect(
65+
findCommentPredicate(
66+
{
67+
token: 'token',
68+
repository: 'repository',
69+
issueNumber: 1,
70+
commentAuthor: '',
71+
bodyIncludes: '',
72+
bodyRegex: '^.*not-exist.*$',
73+
direction: 'direction'
74+
},
75+
{
76+
id: 1,
77+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
78+
user: {login: 'dorothy'}
79+
}
80+
)
81+
).toEqual(false)
82+
})
83+
84+
test('find by author', async () => {
85+
expect(
86+
findCommentPredicate(
87+
{
88+
token: 'token',
89+
repository: 'repository',
90+
issueNumber: 1,
91+
commentAuthor: 'dorothy',
92+
bodyIncludes: '',
93+
bodyRegex: '',
94+
direction: 'direction'
95+
},
96+
{
97+
id: 1,
98+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
99+
user: {login: 'dorothy'}
100+
}
101+
)
102+
).toEqual(true)
103+
104+
expect(
105+
findCommentPredicate(
106+
{
107+
token: 'token',
108+
repository: 'repository',
109+
issueNumber: 1,
110+
commentAuthor: 'toto',
111+
bodyIncludes: '',
112+
bodyRegex: '',
113+
direction: 'direction'
114+
},
115+
{
116+
id: 1,
117+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
118+
user: {login: 'dorothy'}
119+
}
120+
)
121+
).toEqual(false)
122+
})
123+
124+
test('find by bodyIncludes and author', async () => {
125+
expect(
126+
findCommentPredicate(
127+
{
128+
token: 'token',
129+
repository: 'repository',
130+
issueNumber: 1,
131+
commentAuthor: 'dorothy',
132+
bodyIncludes: 'Kansas',
133+
bodyRegex: '',
134+
direction: 'direction'
135+
},
136+
{
137+
id: 1,
138+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
139+
user: {login: 'dorothy'}
140+
}
141+
)
142+
).toEqual(true)
143+
144+
expect(
145+
findCommentPredicate(
146+
{
147+
token: 'token',
148+
repository: 'repository',
149+
issueNumber: 1,
150+
commentAuthor: 'dorothy',
151+
bodyIncludes: 'not-exist',
152+
bodyRegex: '',
153+
direction: 'direction'
154+
},
155+
{
156+
id: 1,
157+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
158+
user: {login: 'dorothy'}
159+
}
160+
)
161+
).toEqual(false)
162+
163+
expect(
164+
findCommentPredicate(
165+
{
166+
token: 'token',
167+
repository: 'repository',
168+
issueNumber: 1,
169+
commentAuthor: 'toto',
170+
bodyIncludes: 'Kansas',
171+
bodyRegex: '',
172+
direction: 'direction'
173+
},
174+
{
175+
id: 1,
176+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
177+
user: {login: 'dorothy'}
178+
}
179+
)
180+
).toEqual(false)
181+
})
182+
183+
test('find by bodyRegex and author', async () => {
184+
expect(
185+
findCommentPredicate(
186+
{
187+
token: 'token',
188+
repository: 'repository',
189+
issueNumber: 1,
190+
commentAuthor: 'dorothy',
191+
bodyIncludes: '',
192+
bodyRegex: '^.*Kansas.*$',
193+
direction: 'direction'
194+
},
195+
{
196+
id: 1,
197+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
198+
user: {login: 'dorothy'}
199+
}
200+
)
201+
).toEqual(true)
202+
203+
expect(
204+
findCommentPredicate(
205+
{
206+
token: 'token',
207+
repository: 'repository',
208+
issueNumber: 1,
209+
commentAuthor: 'dorothy',
210+
bodyIncludes: '',
211+
bodyRegex: '^.*not-exist.*$',
212+
direction: 'direction'
213+
},
214+
{
215+
id: 1,
216+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
217+
user: {login: 'dorothy'}
218+
}
219+
)
220+
).toEqual(false)
221+
222+
expect(
223+
findCommentPredicate(
224+
{
225+
token: 'token',
226+
repository: 'repository',
227+
issueNumber: 1,
228+
commentAuthor: 'toto',
229+
bodyIncludes: '',
230+
bodyRegex: '^.*Kansas.*$',
231+
direction: 'direction'
232+
},
233+
{
234+
id: 1,
235+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
236+
user: {login: 'dorothy'}
237+
}
238+
)
239+
).toEqual(false)
240+
})
241+
242+
test('find by bodyIncludes, bodyRegex and author', async () => {
243+
expect(
244+
findCommentPredicate(
245+
{
246+
token: 'token',
247+
repository: 'repository',
248+
issueNumber: 1,
249+
commentAuthor: 'dorothy',
250+
bodyIncludes: 'feeling',
251+
bodyRegex: '^.*Kansas.*$',
252+
direction: 'direction'
253+
},
254+
{
255+
id: 1,
256+
body: `Toto, I've a feeling we're not in Kansas anymore.`,
257+
user: {login: 'dorothy'}
258+
}
259+
)
260+
).toEqual(true)
261+
})
262+
})

action.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ inputs:
1313
description: 'The GitHub user name of the comment author.'
1414
body-includes:
1515
description: 'A string to search for in the body of comments.'
16+
body-regex:
17+
description: 'A regular expression to search for in the body of comments.'
1618
direction:
1719
description: 'Search direction, specified as `first` or `last`'
1820
default: first
@@ -27,5 +29,5 @@ runs:
2729
using: 'node16'
2830
main: 'dist/index.js'
2931
branding:
30-
icon: 'search'
32+
icon: 'search'
3133
color: 'gray-dark'

0 commit comments

Comments
 (0)