update replace behavior

This commit is contained in:
Michael Shick 2023-05-07 09:03:15 -04:00
parent 25e7c93662
commit ff82b38f95
No known key found for this signature in database
GPG key ID: ADF5BC9704BB4A61
3 changed files with 36 additions and 5 deletions

View file

@ -88,6 +88,8 @@ jobs:
| update-only | with | Only update the comment if it already exists. | no | false |
| GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | no | |
| preformatted | with | Treat message text as pre-formatted and place it in a codeblock | no | |
| find | with | Patterns to find in an existing message and replace with either `replace` text or a resolved `message`. See [Find-and-Replace](#find-and-replace) for more detail. | no | |
| replace | with | Strings to replace a found pattern with. Each new line is a new replacement, or if you only have one pattern, you can replace with a multiline string. | no | |
## Advanced Uses

View file

@ -97,10 +97,7 @@ const handlers = [
const server = setupServer(...handlers)
beforeAll(() => {
// vi.spyOn(console, 'log').mockImplementation(() => {})
// vi.spyOn(core, 'debug').mockImplementation(() => {})
// vi.spyOn(core, 'info').mockImplementation(() => {})
// vi.spyOn(core, 'warning').mockImplementation(() => {})
vi.spyOn(console, 'log').mockImplementation(() => {})
server.listen({ onUnhandledRequest: 'error' })
})
afterAll(() => server.close())
@ -517,6 +514,35 @@ describe('find and replace', () => {
expect(core.setOutput).toHaveBeenCalledWith('comment-id', commentId)
})
it('can multiple find and replace a single pattern with a multiline replacement', async () => {
inputs['find'] = 'hello'
inputs['message'] = 'h\ne\nl\nl\no'
const body = `<!-- add-pr-comment:${inputs['message-id']} -->\n\nhello\nworld`
const commentId = 123
const replyBody = [
{
id: commentId,
body,
},
]
getIssueCommentsResponse = replyBody
postIssueCommentsResponse = {
id: commentId,
}
await run()
expect(`<!-- add-pr-comment:add-pr-comment -->\n\nh\ne\nl\nl\no\nworld`).toEqual(
messagePayload?.body,
)
expect(core.setOutput).toHaveBeenCalledWith('comment-updated', 'true')
expect(core.setOutput).toHaveBeenCalledWith('comment-id', commentId)
})
it('can multiple find and replace text using a message-path', async () => {
inputs['find'] = '<< FILE_CONTENTS >>'
inputs['message-path'] = messagePath1Fixture

View file

@ -107,7 +107,10 @@ export function findAndReplaceInMessage(
for (const [i, f] of find.entries()) {
const { regExp, modifiers } = splitFind(f)
message = message.replace(new RegExp(regExp, modifiers), replacement[i] ?? replacement[0])
message = message.replace(
new RegExp(regExp, modifiers),
replacement[i] ?? replacement.join('\n'),
)
}
return message