diff --git a/README.md b/README.md index 277c0ca..405d090 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ This action was created to help facilitate a GitHub Actions "ChatOps" solution i | `issue-number` | The number of the issue or pull request in which to create a comment. | | | `comment-id` | The id of the comment to update. | | | `body` | The comment body. | | +| `file` | The path to a file that can be read as `body`. Use either `file` or `body`, but not both. | | +| `fileEncoding` | The encoding of the file provided as `file`. | `utf8` | | `edit-mode` | The mode when updating a comment, `replace` or `append`. | `append` | | `reactions` | A comma separated list of reactions to add to the comment. (`+1`, `-1`, `laugh`, `confused`, `heart`, `hooray`, `rocket`, `eyes`) | | @@ -158,22 +160,12 @@ If required, the create and update steps can be separated for greater control. ### Setting the comment body from a file -This example shows how file content can be read into a variable and passed to the action. - ```yml - - id: get-comment-body - run: | - body="$(cat comment-body.txt)" - delimiter="$(openssl rand -hex 8)" - echo "body<<$delimiter" >> $GITHUB_OUTPUT - echo "$body" >> $GITHUB_OUTPUT - echo "$delimiter" >> $GITHUB_OUTPUT - - name: Create comment uses: peter-evans/create-or-update-comment@v2 with: issue-number: 1 - body: ${{ steps.get-comment-body.outputs.body }} + file: 'comment-body.txt' ``` ### Using a markdown template diff --git a/action.yml b/action.yml index a3b8799..4e98fdb 100644 --- a/action.yml +++ b/action.yml @@ -12,6 +12,8 @@ inputs: description: 'The id of the comment to update.' body: description: 'The comment body.' + file: + description: 'The path to a file that can be read as `body`. Use either `file` or `body`, but not both.' edit-mode: description: 'The mode when updating a comment, "replace" or "append".' reaction-type: @@ -25,5 +27,5 @@ runs: using: 'node16' main: 'dist/index.js' branding: - icon: 'message-square' + icon: 'message-square' color: 'gray-dark' diff --git a/dist/index.js b/dist/index.js index 175e242..aa5e2b7 100644 --- a/dist/index.js +++ b/dist/index.js @@ -9685,6 +9685,7 @@ var __webpack_exports__ = {}; // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. (() => { const { inspect } = __nccwpck_require__(3837); +const { readFileSync, existsSync } = __nccwpck_require__(7147); const core = __nccwpck_require__(2186); const github = __nccwpck_require__(5438); @@ -9757,6 +9758,8 @@ async function run() { issueNumber: core.getInput("issue-number"), commentId: core.getInput("comment-id"), body: core.getInput("body"), + file: core.getInput("file"), + fileEncoding: core.getInput("file-encoding") || 'utf8', editMode: core.getInput("edit-mode"), reactions: core.getInput("reactions") ? core.getInput("reactions") @@ -9777,16 +9780,30 @@ async function run() { return; } + if (inputs.file && inputs.body) { + core.setFailed("Only one of 'file' or 'body' can be set."); + return; + } + + if (inputs.file) { + if (!existsSync(inputs.file)) { + core.setFailed(`File '${inputs.file}' does not exist.`); + return; + } + } + const octokit = github.getOctokit(inputs.token); if (inputs.commentId) { // Edit a comment - if (!inputs.body && !inputs.reactions) { - core.setFailed("Missing either comment 'body' or 'reactions'."); + if (!inputs.body && !inputs.reactions && !inputs.file) { + core.setFailed("Missing either comment 'body', 'file', or 'reactions'."); return; } - if (inputs.body) { + const body = getBodyOrFile(inputs); + + if (body) { var commentBody = ""; if (editMode == "append") { // Get the comment body @@ -9798,7 +9815,7 @@ async function run() { commentBody = comment.body + "\n"; } - commentBody = commentBody + inputs.body; + commentBody = commentBody + body; core.debug(`Comment body: ${commentBody}`); await octokit.rest.issues.updateComment({ owner: repo[0], @@ -9816,15 +9833,18 @@ async function run() { } } else if (inputs.issueNumber) { // Create a comment - if (!inputs.body) { - core.setFailed("Missing comment 'body'."); + const body = getBodyOrFile(inputs); + + if (!body) { + core.setFailed("Missing comment 'body' or 'file'."); return; } + const { data: comment } = await octokit.rest.issues.createComment({ owner: repo[0], repo: repo[1], issue_number: inputs.issueNumber, - body: inputs.body, + body, }); core.info( `Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.` @@ -9848,6 +9868,14 @@ async function run() { } } +function getBodyOrFile (inputs) { + if (inputs.body) { + return inputs.body; + } else if (inputs.file) { + return readFileSync(inputs.file, inputs.fileEncoding); + } +} + run(); })(); diff --git a/index.js b/index.js index ce6c3a8..1017e11 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ const { inspect } = require("util"); +const { readFileSync, existsSync } = require("fs"); const core = require("@actions/core"); const github = require("@actions/github"); @@ -71,6 +72,8 @@ async function run() { issueNumber: core.getInput("issue-number"), commentId: core.getInput("comment-id"), body: core.getInput("body"), + file: core.getInput("file"), + fileEncoding: core.getInput("file-encoding") || 'utf8', editMode: core.getInput("edit-mode"), reactions: core.getInput("reactions") ? core.getInput("reactions") @@ -91,16 +94,30 @@ async function run() { return; } + if (inputs.file && inputs.body) { + core.setFailed("Only one of 'file' or 'body' can be set."); + return; + } + + if (inputs.file) { + if (!existsSync(inputs.file)) { + core.setFailed(`File '${inputs.file}' does not exist.`); + return; + } + } + const octokit = github.getOctokit(inputs.token); if (inputs.commentId) { // Edit a comment - if (!inputs.body && !inputs.reactions) { - core.setFailed("Missing either comment 'body' or 'reactions'."); + if (!inputs.body && !inputs.reactions && !inputs.file) { + core.setFailed("Missing either comment 'body', 'file', or 'reactions'."); return; } - if (inputs.body) { + const body = getBodyOrFile(inputs); + + if (body) { var commentBody = ""; if (editMode == "append") { // Get the comment body @@ -112,7 +129,7 @@ async function run() { commentBody = comment.body + "\n"; } - commentBody = commentBody + inputs.body; + commentBody = commentBody + body; core.debug(`Comment body: ${commentBody}`); await octokit.rest.issues.updateComment({ owner: repo[0], @@ -130,15 +147,18 @@ async function run() { } } else if (inputs.issueNumber) { // Create a comment - if (!inputs.body) { - core.setFailed("Missing comment 'body'."); + const body = getBodyOrFile(inputs); + + if (!body) { + core.setFailed("Missing comment 'body' or 'file'."); return; } + const { data: comment } = await octokit.rest.issues.createComment({ owner: repo[0], repo: repo[1], issue_number: inputs.issueNumber, - body: inputs.body, + body, }); core.info( `Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.` @@ -162,4 +182,12 @@ async function run() { } } +function getBodyOrFile (inputs) { + if (inputs.body) { + return inputs.body; + } else if (inputs.file) { + return readFileSync(inputs.file, inputs.fileEncoding); + } +} + run();