mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2026-04-07 23:10:07 +00:00
Add prefix and suffix inputs to wrap comment body
Co-authored-by: marocchino <128431+marocchino@users.noreply.github.com>
This commit is contained in:
parent
58d8e91c23
commit
5b837e833d
4 changed files with 87 additions and 2 deletions
20
README.md
20
README.md
|
|
@ -112,6 +112,18 @@ with:
|
|||
path: path-to-comment-contents.txt
|
||||
```
|
||||
|
||||
### Wrap file output in a code block
|
||||
|
||||
Use `prefix` and `suffix` to wrap the comment body with arbitrary text, such as triple backticks for a code block.
|
||||
|
||||
````yaml
|
||||
uses: marocchino/sticky-pull-request-comment@v3
|
||||
with:
|
||||
path: path-to-comment-contents.txt
|
||||
prefix: "```"
|
||||
suffix: "```"
|
||||
````
|
||||
|
||||
### Delete the previous comment and add a comment at the end
|
||||
|
||||
```yaml
|
||||
|
|
@ -222,6 +234,14 @@ For more detailed information about permissions, you can read from the link belo
|
|||
|
||||
**Optional** Comment message
|
||||
|
||||
### `prefix`
|
||||
|
||||
**Optional** Text to prepend to the comment body (before `message` or `path` content). Useful for opening a code block with ` ``` `.
|
||||
|
||||
### `suffix`
|
||||
|
||||
**Optional** Text to append to the comment body (after `message` or `path` content). Useful for closing a code block with ` ``` `.
|
||||
|
||||
### `path`
|
||||
|
||||
**Optional** Path to file containing comment message
|
||||
|
|
|
|||
|
|
@ -210,4 +210,49 @@ describe("getBody", () => {
|
|||
expect(await config.getBody()).toBe("")
|
||||
expect(core.setFailed).toHaveBeenCalledWith("glob error")
|
||||
})
|
||||
|
||||
test("wraps message with prefix and suffix", async () => {
|
||||
const {config, core} = await loadConfig()
|
||||
vi.mocked(core.getInput).mockImplementation(name => {
|
||||
if (name === "message") return "hello there"
|
||||
if (name === "prefix") return "```"
|
||||
if (name === "suffix") return "```"
|
||||
return ""
|
||||
})
|
||||
expect(await config.getBody()).toBe("```\nhello there\n```")
|
||||
})
|
||||
|
||||
test("wraps file content with prefix and suffix", async () => {
|
||||
const {config, core} = await loadConfig()
|
||||
vi.mocked(core.getMultilineInput).mockReturnValue(["__tests__/assets/result"])
|
||||
vi.mocked(core.getInput).mockImplementation(name => {
|
||||
if (name === "prefix") return "```"
|
||||
if (name === "suffix") return "```"
|
||||
return ""
|
||||
})
|
||||
mockGlobCreate.mockResolvedValue({
|
||||
glob: vi.fn().mockResolvedValue([resolve("__tests__/assets/result")]),
|
||||
})
|
||||
expect(await config.getBody()).toBe("```\nhi there\n\n```")
|
||||
})
|
||||
|
||||
test("applies only prefix when suffix is not provided", async () => {
|
||||
const {config, core} = await loadConfig()
|
||||
vi.mocked(core.getInput).mockImplementation(name => {
|
||||
if (name === "message") return "hello there"
|
||||
if (name === "prefix") return "```"
|
||||
return ""
|
||||
})
|
||||
expect(await config.getBody()).toBe("```\nhello there")
|
||||
})
|
||||
|
||||
test("applies only suffix when prefix is not provided", async () => {
|
||||
const {config, core} = await loadConfig()
|
||||
vi.mocked(core.getInput).mockImplementation(name => {
|
||||
if (name === "message") return "hello there"
|
||||
if (name === "suffix") return "```"
|
||||
return ""
|
||||
})
|
||||
expect(await config.getBody()).toBe("hello there\n```")
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -54,6 +54,12 @@ inputs:
|
|||
message:
|
||||
description: "comment message"
|
||||
required: false
|
||||
prefix:
|
||||
description: "text to prepend to the message (e.g. ` ``` ` to open a code block)"
|
||||
required: false
|
||||
suffix:
|
||||
description: "text to append to the message (e.g. ` ``` ` to close a code block)"
|
||||
required: false
|
||||
path:
|
||||
description: "glob path to file(s) containing comment message"
|
||||
required: false
|
||||
|
|
|
|||
|
|
@ -50,13 +50,17 @@ export async function getBody(): Promise<string> {
|
|||
const followSymbolicLinks = core.getBooleanInput("follow_symbolic_links", {
|
||||
required: true,
|
||||
})
|
||||
const prefixInput = core.getInput("prefix", {required: false})
|
||||
const suffixInput = core.getInput("suffix", {required: false})
|
||||
|
||||
let body: string
|
||||
if (pathInput && pathInput.length > 0) {
|
||||
try {
|
||||
const globber = await create(pathInput.join("\n"), {
|
||||
followSymbolicLinks,
|
||||
matchDirectories: false,
|
||||
})
|
||||
return (await globber.glob()).map(path => readFileSync(path, "utf-8")).join("\n")
|
||||
body = (await globber.glob()).map(path => readFileSync(path, "utf-8")).join("\n")
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
core.setFailed(error.message)
|
||||
|
|
@ -64,6 +68,16 @@ export async function getBody(): Promise<string> {
|
|||
return ""
|
||||
}
|
||||
} else {
|
||||
return core.getInput("message", {required: false})
|
||||
body = core.getInput("message", {required: false})
|
||||
}
|
||||
|
||||
if (prefixInput || suffixInput) {
|
||||
const parts: string[] = []
|
||||
if (prefixInput) parts.push(prefixInput)
|
||||
parts.push(body)
|
||||
if (suffixInput) parts.push(suffixInput)
|
||||
return parts.join("\n")
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue