Skip to content

Commit ff82b38

Browse files
committed
update replace behavior
1 parent 25e7c93 commit ff82b38

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ jobs:
8888
| update-only | with | Only update the comment if it already exists. | no | false |
8989
| GITHUB_TOKEN | env | Valid GitHub token, can alternatively be defined in the env. | no | |
9090
| preformatted | with | Treat message text as pre-formatted and place it in a codeblock | no | |
91+
| 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 | |
92+
| 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 | |
9193

9294
## Advanced Uses
9395

‎__tests__/add-pr-comment.test.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,7 @@ const handlers = [
9797
const server = setupServer(...handlers)
9898

9999
beforeAll(() => {
100-
// vi.spyOn(console, 'log').mockImplementation(() => {})
101-
// vi.spyOn(core, 'debug').mockImplementation(() => {})
102-
// vi.spyOn(core, 'info').mockImplementation(() => {})
103-
// vi.spyOn(core, 'warning').mockImplementation(() => {})
100+
vi.spyOn(console, 'log').mockImplementation(() => {})
104101
server.listen({ onUnhandledRequest: 'error' })
105102
})
106103
afterAll(() => server.close())
@@ -517,6 +514,35 @@ describe('find and replace', () => {
517514
expect(core.setOutput).toHaveBeenCalledWith('comment-id', commentId)
518515
})
519516

517+
it('can multiple find and replace a single pattern with a multiline replacement', async () => {
518+
inputs['find'] = 'hello'
519+
inputs['message'] = 'h\ne\nl\nl\no'
520+
521+
const body = `<!-- add-pr-comment:${inputs['message-id']} -->\n\nhello\nworld`
522+
523+
const commentId = 123
524+
525+
const replyBody = [
526+
{
527+
id: commentId,
528+
body,
529+
},
530+
]
531+
532+
getIssueCommentsResponse = replyBody
533+
postIssueCommentsResponse = {
534+
id: commentId,
535+
}
536+
537+
await run()
538+
539+
expect(`<!-- add-pr-comment:add-pr-comment -->\n\nh\ne\nl\nl\no\nworld`).toEqual(
540+
messagePayload?.body,
541+
)
542+
expect(core.setOutput).toHaveBeenCalledWith('comment-updated', 'true')
543+
expect(core.setOutput).toHaveBeenCalledWith('comment-id', commentId)
544+
})
545+
520546
it('can multiple find and replace text using a message-path', async () => {
521547
inputs['find'] = '<< FILE_CONTENTS >>'
522548
inputs['message-path'] = messagePath1Fixture

‎src/message.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ export function findAndReplaceInMessage(
107107

108108
for (const [i, f] of find.entries()) {
109109
const { regExp, modifiers } = splitFind(f)
110-
message = message.replace(new RegExp(regExp, modifiers), replacement[i] ?? replacement[0])
110+
message = message.replace(
111+
new RegExp(regExp, modifiers),
112+
replacement[i] ?? replacement.join('\n'),
113+
)
111114
}
112115

113116
return message

0 commit comments

Comments
 (0)