diff --git a/.github/comment-body-addition.md b/.github/comment-body-addition.md new file mode 100644 index 00000000..30f398d9 --- /dev/null +++ b/.github/comment-body-addition.md @@ -0,0 +1 @@ +**Edit:** Some additional info diff --git a/.github/multiline-content.md b/.github/comment-body.md similarity index 100% rename from .github/multiline-content.md rename to .github/comment-body.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4a417a52..63102e58 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -92,6 +92,21 @@ jobs: comment-id: ${{ steps.couc.outputs.comment-id }} reactions: heart, hooray, laugh + - name: Test create comment from file + uses: ./ + id: couc2 + with: + issue-number: ${{ needs.build.outputs.issue-number }} + body-file: .github/comment-body.md + reactions: '+1' + + - name: Test update comment from file + uses: ./ + with: + comment-id: ${{ steps.couc2.outputs.comment-id }} + body-file: .github/comment-body-addition.md + reactions: eyes + package: if: github.event_name == 'push' && github.ref == 'refs/heads/main' needs: [test] diff --git a/.github/workflows/test-command.yml b/.github/workflows/test-command.yml index ded4a9e3..eb5ec6cc 100644 --- a/.github/workflows/test-command.yml +++ b/.github/workflows/test-command.yml @@ -61,19 +61,11 @@ jobs: reactions: hooray # Test create with body from file - - id: get-comment-body - run: | - body="$(cat .github/multiline-content.md)" - delimiter="$(openssl rand -hex 8)" - echo "body<<$delimiter" >> $GITHUB_OUTPUT - echo "$body" >> $GITHUB_OUTPUT - echo "$delimiter" >> $GITHUB_OUTPUT - - name: Create comment uses: ./ with: issue-number: 1 - body: ${{ steps.get-comment-body.outputs.body }} + body-file: .github/comment-body.md # Test create from template - name: Render template diff --git a/README.md b/README.md index 277c0ca9..1e0d7519 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,8 @@ This action was created to help facilitate a GitHub Actions "ChatOps" solution i | `repository` | The full name of the repository in which to create or update a comment. | Current repository | | `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. | | +| `body` | The comment body. Cannot be used in conjunction with `body-file`. | | +| `body-file` | The path to a file containing the comment body. Cannot be used in conjunction with `body`. | | | `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 +159,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 }} + body-file: 'comment-body.md' ``` ### Using a markdown template diff --git a/action.yml b/action.yml index a3b8799d..56c63298 100644 --- a/action.yml +++ b/action.yml @@ -11,7 +11,9 @@ inputs: comment-id: description: 'The id of the comment to update.' body: - description: 'The comment body.' + description: 'The comment body. Cannot be used in conjunction with `body-file`.' + body-file: + description: 'The path to a file containing the comment body. Cannot be used in conjunction with `body`.' 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 175e2427..cbff30b5 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); @@ -9749,6 +9750,16 @@ async function addReactions(octokit, repo, comment_id, reactions) { results = undefined; } +function getBody(inputs) { + if (inputs.body) { + return inputs.body; + } else if (inputs.bodyFile) { + return readFileSync(inputs.bodyFile, 'utf-8'); + } else { + return ''; + } +} + async function run() { try { const inputs = { @@ -9757,6 +9768,7 @@ async function run() { issueNumber: core.getInput("issue-number"), commentId: core.getInput("comment-id"), body: core.getInput("body"), + bodyFile: core.getInput("body-file"), editMode: core.getInput("edit-mode"), reactions: core.getInput("reactions") ? core.getInput("reactions") @@ -9777,16 +9789,30 @@ async function run() { return; } + if (inputs.bodyFile && inputs.body) { + core.setFailed("Only one of 'body' or 'body-file' can be set."); + return; + } + + if (inputs.bodyFile) { + if (!existsSync(inputs.bodyFile)) { + core.setFailed(`File '${inputs.bodyFile}' does not exist.`); + return; + } + } + + const body = getBody(inputs); + 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 (!body && !inputs.reactions) { + core.setFailed("Missing comment 'body', 'body-file', or 'reactions'."); return; } - if (inputs.body) { + if (body) { var commentBody = ""; if (editMode == "append") { // Get the comment body @@ -9798,7 +9824,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 +9842,16 @@ async function run() { } } else if (inputs.issueNumber) { // Create a comment - if (!inputs.body) { - core.setFailed("Missing comment 'body'."); + if (!body) { + core.setFailed("Missing comment 'body' or 'body-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}'.` diff --git a/index.js b/index.js index ce6c3a8c..98ebd7a6 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"); @@ -63,6 +64,16 @@ async function addReactions(octokit, repo, comment_id, reactions) { results = undefined; } +function getBody(inputs) { + if (inputs.body) { + return inputs.body; + } else if (inputs.bodyFile) { + return readFileSync(inputs.bodyFile, 'utf-8'); + } else { + return ''; + } +} + async function run() { try { const inputs = { @@ -71,6 +82,7 @@ async function run() { issueNumber: core.getInput("issue-number"), commentId: core.getInput("comment-id"), body: core.getInput("body"), + bodyFile: core.getInput("body-file"), editMode: core.getInput("edit-mode"), reactions: core.getInput("reactions") ? core.getInput("reactions") @@ -91,16 +103,30 @@ async function run() { return; } + if (inputs.bodyFile && inputs.body) { + core.setFailed("Only one of 'body' or 'body-file' can be set."); + return; + } + + if (inputs.bodyFile) { + if (!existsSync(inputs.bodyFile)) { + core.setFailed(`File '${inputs.bodyFile}' does not exist.`); + return; + } + } + + const body = getBody(inputs); + 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 (!body && !inputs.reactions) { + core.setFailed("Missing comment 'body', 'body-file', or 'reactions'."); return; } - if (inputs.body) { + if (body) { var commentBody = ""; if (editMode == "append") { // Get the comment body @@ -112,7 +138,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 +156,16 @@ async function run() { } } else if (inputs.issueNumber) { // Create a comment - if (!inputs.body) { - core.setFailed("Missing comment 'body'."); + if (!body) { + core.setFailed("Missing comment 'body' or 'body-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}'.`