Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: search comment body using a regular expression #77

Merged
merged 6 commits into from Nov 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
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
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
@@ -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
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'