Skip to content

Commit

Permalink
Merge pull request #115 from umanghome/main
Browse files Browse the repository at this point in the history
Support reading body from a file
  • Loading branch information
peter-evans authored Oct 24, 2022
2 parents 666805d + 029195c commit b9257b6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
14 changes: 3 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`) | |

Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -25,5 +27,5 @@ runs:
using: 'node16'
main: 'dist/index.js'
branding:
icon: 'message-square'
icon: 'message-square'
color: 'gray-dark'
42 changes: 35 additions & 7 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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],
Expand All @@ -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}'.`
Expand All @@ -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();

})();
Expand Down
42 changes: 35 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { inspect } = require("util");
const { readFileSync, existsSync } = require("fs");
const core = require("@actions/core");
const github = require("@actions/github");

Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand All @@ -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],
Expand All @@ -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}'.`
Expand All @@ -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();

0 comments on commit b9257b6

Please sign in to comment.