Skip to content

Commit

Permalink
feat: search comment body using a regular expression (#77)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
peter-evans and dluftspring authored Nov 29, 2022
1 parent 1c328ad commit f4499a7
Show file tree
Hide file tree
Showing 9 changed files with 903 additions and 132 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ jobs:
- if: steps.fc7.outputs.comment-id != 771260630
run: exit 1

- name: Find comment by body-regex
uses: ./
id: fc8
with:
issue-number: 1
body-regex: '^.*search string 1.*$'
- if: steps.fc8.outputs.comment-id != 620947762
run: exit 1

package:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [test]
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ The action will output the comment ID of the comment matching the search criteri
body-includes: search string 1
```
### Find the first comment matching the specified regular expression
```yml
- name: Find Comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: 1
body-regex: '^.*search string 1.*$'
```
### Find the last comment containing the specified string
```yml
Expand All @@ -63,6 +74,7 @@ The action will output the comment ID of the comment matching the search criteri
| `issue-number` | The number of the issue or pull request in which to search. | |
| `comment-author` | The GitHub user name of the comment author. | |
| `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` |

#### Outputs
Expand Down
262 changes: 262 additions & 0 deletions __test__/find.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
import {findCommentPredicate} from '../lib/find'

describe('find comment tests', () => {
test('find by bodyIncludes', async () => {
expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(true)

expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: 'not-exist',
bodyRegex: '',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(false)
})

test('find by bodyRegex', async () => {
expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: '',
bodyRegex: '^.*Kansas.*$',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(true)

expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: '',
bodyIncludes: '',
bodyRegex: '^.*not-exist.*$',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(false)
})

test('find by author', async () => {
expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'dorothy',
bodyIncludes: '',
bodyRegex: '',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(true)

expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'toto',
bodyIncludes: '',
bodyRegex: '',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(false)
})

test('find by bodyIncludes and author', async () => {
expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'dorothy',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(true)

expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'dorothy',
bodyIncludes: 'not-exist',
bodyRegex: '',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(false)

expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'toto',
bodyIncludes: 'Kansas',
bodyRegex: '',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(false)
})

test('find by bodyRegex and author', async () => {
expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'dorothy',
bodyIncludes: '',
bodyRegex: '^.*Kansas.*$',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(true)

expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'dorothy',
bodyIncludes: '',
bodyRegex: '^.*not-exist.*$',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(false)

expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'toto',
bodyIncludes: '',
bodyRegex: '^.*Kansas.*$',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(false)
})

test('find by bodyIncludes, bodyRegex and author', async () => {
expect(
findCommentPredicate(
{
token: 'token',
repository: 'repository',
issueNumber: 1,
commentAuthor: 'dorothy',
bodyIncludes: 'feeling',
bodyRegex: '^.*Kansas.*$',
direction: 'direction'
},
{
id: 1,
body: `Toto, I've a feeling we're not in Kansas anymore.`,
user: {login: 'dorothy'}
}
)
).toEqual(true)
})
})
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ inputs:
description: 'The GitHub user name of the comment author.'
body-includes:
description: 'A string to search for in the body of comments.'
body-regex:
description: 'A regular expression to search for in the body of comments.'
direction:
description: 'Search direction, specified as `first` or `last`'
default: first
Expand All @@ -27,5 +29,5 @@ runs:
using: 'node16'
main: 'dist/index.js'
branding:
icon: 'search'
icon: 'search'
color: 'gray-dark'
Loading

0 comments on commit f4499a7

Please sign in to comment.