Skip to content

Commit b9257b6

Browse files
authored
Merge pull request #115 from umanghome/main
Support reading body from a file
2 parents 666805d + 029195c commit b9257b6

File tree

4 files changed

+76
-26
lines changed

4 files changed

+76
-26
lines changed

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ This action was created to help facilitate a GitHub Actions "ChatOps" solution i
5555
| `issue-number` | The number of the issue or pull request in which to create a comment. | |
5656
| `comment-id` | The id of the comment to update. | |
5757
| `body` | The comment body. | |
58+
| `file` | The path to a file that can be read as `body`. Use either `file` or `body`, but not both. | |
59+
| `fileEncoding` | The encoding of the file provided as `file`. | `utf8` |
5860
| `edit-mode` | The mode when updating a comment, `replace` or `append`. | `append` |
5961
| `reactions` | A comma separated list of reactions to add to the comment. (`+1`, `-1`, `laugh`, `confused`, `heart`, `hooray`, `rocket`, `eyes`) | |
6062

@@ -158,22 +160,12 @@ If required, the create and update steps can be separated for greater control.
158160

159161
### Setting the comment body from a file
160162

161-
This example shows how file content can be read into a variable and passed to the action.
162-
163163
```yml
164-
- id: get-comment-body
165-
run: |
166-
body="$(cat comment-body.txt)"
167-
delimiter="$(openssl rand -hex 8)"
168-
echo "body<<$delimiter" >> $GITHUB_OUTPUT
169-
echo "$body" >> $GITHUB_OUTPUT
170-
echo "$delimiter" >> $GITHUB_OUTPUT
171-
172164
- name: Create comment
173165
uses: peter-evans/create-or-update-comment@v2
174166
with:
175167
issue-number: 1
176-
body: ${{ steps.get-comment-body.outputs.body }}
168+
file: 'comment-body.txt'
177169
```
178170

179171
### Using a markdown template

action.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ inputs:
1212
description: 'The id of the comment to update.'
1313
body:
1414
description: 'The comment body.'
15+
file:
16+
description: 'The path to a file that can be read as `body`. Use either `file` or `body`, but not both.'
1517
edit-mode:
1618
description: 'The mode when updating a comment, "replace" or "append".'
1719
reaction-type:
@@ -25,5 +27,5 @@ runs:
2527
using: 'node16'
2628
main: 'dist/index.js'
2729
branding:
28-
icon: 'message-square'
30+
icon: 'message-square'
2931
color: 'gray-dark'

dist/index.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9685,6 +9685,7 @@ var __webpack_exports__ = {};
96859685
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
96869686
(() => {
96879687
const { inspect } = __nccwpck_require__(3837);
9688+
const { readFileSync, existsSync } = __nccwpck_require__(7147);
96889689
const core = __nccwpck_require__(2186);
96899690
const github = __nccwpck_require__(5438);
96909691

@@ -9757,6 +9758,8 @@ async function run() {
97579758
issueNumber: core.getInput("issue-number"),
97589759
commentId: core.getInput("comment-id"),
97599760
body: core.getInput("body"),
9761+
file: core.getInput("file"),
9762+
fileEncoding: core.getInput("file-encoding") || 'utf8',
97609763
editMode: core.getInput("edit-mode"),
97619764
reactions: core.getInput("reactions")
97629765
? core.getInput("reactions")
@@ -9777,16 +9780,30 @@ async function run() {
97779780
return;
97789781
}
97799782

9783+
if (inputs.file && inputs.body) {
9784+
core.setFailed("Only one of 'file' or 'body' can be set.");
9785+
return;
9786+
}
9787+
9788+
if (inputs.file) {
9789+
if (!existsSync(inputs.file)) {
9790+
core.setFailed(`File '${inputs.file}' does not exist.`);
9791+
return;
9792+
}
9793+
}
9794+
97809795
const octokit = github.getOctokit(inputs.token);
97819796

97829797
if (inputs.commentId) {
97839798
// Edit a comment
9784-
if (!inputs.body && !inputs.reactions) {
9785-
core.setFailed("Missing either comment 'body' or 'reactions'.");
9799+
if (!inputs.body && !inputs.reactions && !inputs.file) {
9800+
core.setFailed("Missing either comment 'body', 'file', or 'reactions'.");
97869801
return;
97879802
}
97889803

9789-
if (inputs.body) {
9804+
const body = getBodyOrFile(inputs);
9805+
9806+
if (body) {
97909807
var commentBody = "";
97919808
if (editMode == "append") {
97929809
// Get the comment body
@@ -9798,7 +9815,7 @@ async function run() {
97989815
commentBody = comment.body + "\n";
97999816
}
98009817

9801-
commentBody = commentBody + inputs.body;
9818+
commentBody = commentBody + body;
98029819
core.debug(`Comment body: ${commentBody}`);
98039820
await octokit.rest.issues.updateComment({
98049821
owner: repo[0],
@@ -9816,15 +9833,18 @@ async function run() {
98169833
}
98179834
} else if (inputs.issueNumber) {
98189835
// Create a comment
9819-
if (!inputs.body) {
9820-
core.setFailed("Missing comment 'body'.");
9836+
const body = getBodyOrFile(inputs);
9837+
9838+
if (!body) {
9839+
core.setFailed("Missing comment 'body' or 'file'.");
98219840
return;
98229841
}
9842+
98239843
const { data: comment } = await octokit.rest.issues.createComment({
98249844
owner: repo[0],
98259845
repo: repo[1],
98269846
issue_number: inputs.issueNumber,
9827-
body: inputs.body,
9847+
body,
98289848
});
98299849
core.info(
98309850
`Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.`
@@ -9848,6 +9868,14 @@ async function run() {
98489868
}
98499869
}
98509870

9871+
function getBodyOrFile (inputs) {
9872+
if (inputs.body) {
9873+
return inputs.body;
9874+
} else if (inputs.file) {
9875+
return readFileSync(inputs.file, inputs.fileEncoding);
9876+
}
9877+
}
9878+
98519879
run();
98529880

98539881
})();

index.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { inspect } = require("util");
2+
const { readFileSync, existsSync } = require("fs");
23
const core = require("@actions/core");
34
const github = require("@actions/github");
45

@@ -71,6 +72,8 @@ async function run() {
7172
issueNumber: core.getInput("issue-number"),
7273
commentId: core.getInput("comment-id"),
7374
body: core.getInput("body"),
75+
file: core.getInput("file"),
76+
fileEncoding: core.getInput("file-encoding") || 'utf8',
7477
editMode: core.getInput("edit-mode"),
7578
reactions: core.getInput("reactions")
7679
? core.getInput("reactions")
@@ -91,16 +94,30 @@ async function run() {
9194
return;
9295
}
9396

97+
if (inputs.file && inputs.body) {
98+
core.setFailed("Only one of 'file' or 'body' can be set.");
99+
return;
100+
}
101+
102+
if (inputs.file) {
103+
if (!existsSync(inputs.file)) {
104+
core.setFailed(`File '${inputs.file}' does not exist.`);
105+
return;
106+
}
107+
}
108+
94109
const octokit = github.getOctokit(inputs.token);
95110

96111
if (inputs.commentId) {
97112
// Edit a comment
98-
if (!inputs.body && !inputs.reactions) {
99-
core.setFailed("Missing either comment 'body' or 'reactions'.");
113+
if (!inputs.body && !inputs.reactions && !inputs.file) {
114+
core.setFailed("Missing either comment 'body', 'file', or 'reactions'.");
100115
return;
101116
}
102117

103-
if (inputs.body) {
118+
const body = getBodyOrFile(inputs);
119+
120+
if (body) {
104121
var commentBody = "";
105122
if (editMode == "append") {
106123
// Get the comment body
@@ -112,7 +129,7 @@ async function run() {
112129
commentBody = comment.body + "\n";
113130
}
114131

115-
commentBody = commentBody + inputs.body;
132+
commentBody = commentBody + body;
116133
core.debug(`Comment body: ${commentBody}`);
117134
await octokit.rest.issues.updateComment({
118135
owner: repo[0],
@@ -130,15 +147,18 @@ async function run() {
130147
}
131148
} else if (inputs.issueNumber) {
132149
// Create a comment
133-
if (!inputs.body) {
134-
core.setFailed("Missing comment 'body'.");
150+
const body = getBodyOrFile(inputs);
151+
152+
if (!body) {
153+
core.setFailed("Missing comment 'body' or 'file'.");
135154
return;
136155
}
156+
137157
const { data: comment } = await octokit.rest.issues.createComment({
138158
owner: repo[0],
139159
repo: repo[1],
140160
issue_number: inputs.issueNumber,
141-
body: inputs.body,
161+
body,
142162
});
143163
core.info(
144164
`Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.`
@@ -162,4 +182,12 @@ async function run() {
162182
}
163183
}
164184

185+
function getBodyOrFile (inputs) {
186+
if (inputs.body) {
187+
return inputs.body;
188+
} else if (inputs.file) {
189+
return readFileSync(inputs.file, inputs.fileEncoding);
190+
}
191+
}
192+
165193
run();

0 commit comments

Comments
 (0)