diff --git a/README.md b/README.md index a527450..7cb5301 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/__tests__/add-pr-comment.test.ts b/__tests__/add-pr-comment.test.ts index fb70e40..6e20bb1 100644 --- a/__tests__/add-pr-comment.test.ts +++ b/__tests__/add-pr-comment.test.ts @@ -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 = `\n\nhello\nworld` + + const commentId = 123 + + const replyBody = [ + { + id: commentId, + body, + }, + ] + + getIssueCommentsResponse = replyBody + postIssueCommentsResponse = { + id: commentId, + } + + await run() + + expect(`\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 diff --git a/src/message.ts b/src/message.ts index 27e14ed..1e1ddfe 100644 --- a/src/message.ts +++ b/src/message.ts @@ -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