Merge pull request #115 from umanghome/main

Support reading body from a file
This commit is contained in:
Peter Evans 2022-10-24 14:51:14 +09:00 committed by GitHub
commit b9257b685e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 26 deletions

View File

@ -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. | | | `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. | | | `comment-id` | The id of the comment to update. | |
| `body` | The comment body. | | | `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` | | `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`) | | | `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 ### 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 ```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 - name: Create comment
uses: peter-evans/create-or-update-comment@v2 uses: peter-evans/create-or-update-comment@v2
with: with:
issue-number: 1 issue-number: 1
body: ${{ steps.get-comment-body.outputs.body }} file: 'comment-body.txt'
``` ```
### Using a markdown template ### Using a markdown template

View File

@ -12,6 +12,8 @@ inputs:
description: 'The id of the comment to update.' description: 'The id of the comment to update.'
body: body:
description: 'The comment 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: edit-mode:
description: 'The mode when updating a comment, "replace" or "append".' description: 'The mode when updating a comment, "replace" or "append".'
reaction-type: reaction-type:

42
dist/index.js vendored
View File

@ -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. // 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 { inspect } = __nccwpck_require__(3837);
const { readFileSync, existsSync } = __nccwpck_require__(7147);
const core = __nccwpck_require__(2186); const core = __nccwpck_require__(2186);
const github = __nccwpck_require__(5438); const github = __nccwpck_require__(5438);
@ -9757,6 +9758,8 @@ async function run() {
issueNumber: core.getInput("issue-number"), issueNumber: core.getInput("issue-number"),
commentId: core.getInput("comment-id"), commentId: core.getInput("comment-id"),
body: core.getInput("body"), body: core.getInput("body"),
file: core.getInput("file"),
fileEncoding: core.getInput("file-encoding") || 'utf8',
editMode: core.getInput("edit-mode"), editMode: core.getInput("edit-mode"),
reactions: core.getInput("reactions") reactions: core.getInput("reactions")
? core.getInput("reactions") ? core.getInput("reactions")
@ -9777,16 +9780,30 @@ async function run() {
return; 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); const octokit = github.getOctokit(inputs.token);
if (inputs.commentId) { if (inputs.commentId) {
// Edit a comment // Edit a comment
if (!inputs.body && !inputs.reactions) { if (!inputs.body && !inputs.reactions && !inputs.file) {
core.setFailed("Missing either comment 'body' or 'reactions'."); core.setFailed("Missing either comment 'body', 'file', or 'reactions'.");
return; return;
} }
if (inputs.body) { const body = getBodyOrFile(inputs);
if (body) {
var commentBody = ""; var commentBody = "";
if (editMode == "append") { if (editMode == "append") {
// Get the comment body // Get the comment body
@ -9798,7 +9815,7 @@ async function run() {
commentBody = comment.body + "\n"; commentBody = comment.body + "\n";
} }
commentBody = commentBody + inputs.body; commentBody = commentBody + body;
core.debug(`Comment body: ${commentBody}`); core.debug(`Comment body: ${commentBody}`);
await octokit.rest.issues.updateComment({ await octokit.rest.issues.updateComment({
owner: repo[0], owner: repo[0],
@ -9816,15 +9833,18 @@ async function run() {
} }
} else if (inputs.issueNumber) { } else if (inputs.issueNumber) {
// Create a comment // Create a comment
if (!inputs.body) { const body = getBodyOrFile(inputs);
core.setFailed("Missing comment 'body'.");
if (!body) {
core.setFailed("Missing comment 'body' or 'file'.");
return; return;
} }
const { data: comment } = await octokit.rest.issues.createComment({ const { data: comment } = await octokit.rest.issues.createComment({
owner: repo[0], owner: repo[0],
repo: repo[1], repo: repo[1],
issue_number: inputs.issueNumber, issue_number: inputs.issueNumber,
body: inputs.body, body,
}); });
core.info( core.info(
`Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.` `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(); run();
})(); })();

View File

@ -1,4 +1,5 @@
const { inspect } = require("util"); const { inspect } = require("util");
const { readFileSync, existsSync } = require("fs");
const core = require("@actions/core"); const core = require("@actions/core");
const github = require("@actions/github"); const github = require("@actions/github");
@ -71,6 +72,8 @@ async function run() {
issueNumber: core.getInput("issue-number"), issueNumber: core.getInput("issue-number"),
commentId: core.getInput("comment-id"), commentId: core.getInput("comment-id"),
body: core.getInput("body"), body: core.getInput("body"),
file: core.getInput("file"),
fileEncoding: core.getInput("file-encoding") || 'utf8',
editMode: core.getInput("edit-mode"), editMode: core.getInput("edit-mode"),
reactions: core.getInput("reactions") reactions: core.getInput("reactions")
? core.getInput("reactions") ? core.getInput("reactions")
@ -91,16 +94,30 @@ async function run() {
return; 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); const octokit = github.getOctokit(inputs.token);
if (inputs.commentId) { if (inputs.commentId) {
// Edit a comment // Edit a comment
if (!inputs.body && !inputs.reactions) { if (!inputs.body && !inputs.reactions && !inputs.file) {
core.setFailed("Missing either comment 'body' or 'reactions'."); core.setFailed("Missing either comment 'body', 'file', or 'reactions'.");
return; return;
} }
if (inputs.body) { const body = getBodyOrFile(inputs);
if (body) {
var commentBody = ""; var commentBody = "";
if (editMode == "append") { if (editMode == "append") {
// Get the comment body // Get the comment body
@ -112,7 +129,7 @@ async function run() {
commentBody = comment.body + "\n"; commentBody = comment.body + "\n";
} }
commentBody = commentBody + inputs.body; commentBody = commentBody + body;
core.debug(`Comment body: ${commentBody}`); core.debug(`Comment body: ${commentBody}`);
await octokit.rest.issues.updateComment({ await octokit.rest.issues.updateComment({
owner: repo[0], owner: repo[0],
@ -130,15 +147,18 @@ async function run() {
} }
} else if (inputs.issueNumber) { } else if (inputs.issueNumber) {
// Create a comment // Create a comment
if (!inputs.body) { const body = getBodyOrFile(inputs);
core.setFailed("Missing comment 'body'.");
if (!body) {
core.setFailed("Missing comment 'body' or 'file'.");
return; return;
} }
const { data: comment } = await octokit.rest.issues.createComment({ const { data: comment } = await octokit.rest.issues.createComment({
owner: repo[0], owner: repo[0],
repo: repo[1], repo: repo[1],
issue_number: inputs.issueNumber, issue_number: inputs.issueNumber,
body: inputs.body, body,
}); });
core.info( core.info(
`Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.` `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(); run();