Skip to content

Commit 5adcb0b

Browse files
authored
Merge pull request peter-evans#133 from peter-evans/body-file
Add input body-file
2 parents 666805d + deb2c81 commit 5adcb0b

File tree

8 files changed

+92
-37
lines changed

8 files changed

+92
-37
lines changed

.github/comment-body-addition.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**Edit:** Some additional info
File renamed without changes.

.github/workflows/ci.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ jobs:
9292
comment-id: ${{ steps.couc.outputs.comment-id }}
9393
reactions: heart, hooray, laugh
9494

95+
- name: Test create comment from file
96+
uses: ./
97+
id: couc2
98+
with:
99+
issue-number: ${{ needs.build.outputs.issue-number }}
100+
body-file: .github/comment-body.md
101+
reactions: '+1'
102+
103+
- name: Test update comment from file
104+
uses: ./
105+
with:
106+
comment-id: ${{ steps.couc2.outputs.comment-id }}
107+
body-file: .github/comment-body-addition.md
108+
reactions: eyes
109+
95110
package:
96111
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
97112
needs: [test]

.github/workflows/test-command.yml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,11 @@ jobs:
6161
reactions: hooray
6262

6363
# Test create with body from file
64-
- id: get-comment-body
65-
run: |
66-
body="$(cat .github/multiline-content.md)"
67-
delimiter="$(openssl rand -hex 8)"
68-
echo "body<<$delimiter" >> $GITHUB_OUTPUT
69-
echo "$body" >> $GITHUB_OUTPUT
70-
echo "$delimiter" >> $GITHUB_OUTPUT
71-
7264
- name: Create comment
7365
uses: ./
7466
with:
7567
issue-number: 1
76-
body: ${{ steps.get-comment-body.outputs.body }}
68+
body-file: .github/comment-body.md
7769

7870
# Test create from template
7971
- name: Render template

README.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ This action was created to help facilitate a GitHub Actions "ChatOps" solution i
5454
| `repository` | The full name of the repository in which to create or update a comment. | Current repository |
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. | |
57-
| `body` | The comment body. | |
57+
| `body` | The comment body. Cannot be used in conjunction with `body-file`. | |
58+
| `body-file` | The path to a file containing the comment body. Cannot be used in conjunction with `body`. | |
5859
| `edit-mode` | The mode when updating a comment, `replace` or `append`. | `append` |
5960
| `reactions` | A comma separated list of reactions to add to the comment. (`+1`, `-1`, `laugh`, `confused`, `heart`, `hooray`, `rocket`, `eyes`) | |
6061

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

159160
### Setting the comment body from a file
160161

161-
This example shows how file content can be read into a variable and passed to the action.
162-
163162
```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-
172163
- name: Create comment
173164
uses: peter-evans/create-or-update-comment@v2
174165
with:
175166
issue-number: 1
176-
body: ${{ steps.get-comment-body.outputs.body }}
167+
body-file: 'comment-body.md'
177168
```
178169

179170
### Using a markdown template

action.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ inputs:
1111
comment-id:
1212
description: 'The id of the comment to update.'
1313
body:
14-
description: 'The comment body.'
14+
description: 'The comment body. Cannot be used in conjunction with `body-file`.'
15+
body-file:
16+
description: 'The path to a file containing the comment body. Cannot be used in conjunction with `body`.'
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: 34 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

@@ -9749,6 +9750,16 @@ async function addReactions(octokit, repo, comment_id, reactions) {
97499750
results = undefined;
97509751
}
97519752

9753+
function getBody(inputs) {
9754+
if (inputs.body) {
9755+
return inputs.body;
9756+
} else if (inputs.bodyFile) {
9757+
return readFileSync(inputs.bodyFile, 'utf-8');
9758+
} else {
9759+
return '';
9760+
}
9761+
}
9762+
97529763
async function run() {
97539764
try {
97549765
const inputs = {
@@ -9757,6 +9768,7 @@ async function run() {
97579768
issueNumber: core.getInput("issue-number"),
97589769
commentId: core.getInput("comment-id"),
97599770
body: core.getInput("body"),
9771+
bodyFile: core.getInput("body-file"),
97609772
editMode: core.getInput("edit-mode"),
97619773
reactions: core.getInput("reactions")
97629774
? core.getInput("reactions")
@@ -9777,16 +9789,30 @@ async function run() {
97779789
return;
97789790
}
97799791

9792+
if (inputs.bodyFile && inputs.body) {
9793+
core.setFailed("Only one of 'body' or 'body-file' can be set.");
9794+
return;
9795+
}
9796+
9797+
if (inputs.bodyFile) {
9798+
if (!existsSync(inputs.bodyFile)) {
9799+
core.setFailed(`File '${inputs.bodyFile}' does not exist.`);
9800+
return;
9801+
}
9802+
}
9803+
9804+
const body = getBody(inputs);
9805+
97809806
const octokit = github.getOctokit(inputs.token);
97819807

97829808
if (inputs.commentId) {
97839809
// Edit a comment
9784-
if (!inputs.body && !inputs.reactions) {
9785-
core.setFailed("Missing either comment 'body' or 'reactions'.");
9810+
if (!body && !inputs.reactions) {
9811+
core.setFailed("Missing comment 'body', 'body-file', or 'reactions'.");
97869812
return;
97879813
}
97889814

9789-
if (inputs.body) {
9815+
if (body) {
97909816
var commentBody = "";
97919817
if (editMode == "append") {
97929818
// Get the comment body
@@ -9798,7 +9824,7 @@ async function run() {
97989824
commentBody = comment.body + "\n";
97999825
}
98009826

9801-
commentBody = commentBody + inputs.body;
9827+
commentBody = commentBody + body;
98029828
core.debug(`Comment body: ${commentBody}`);
98039829
await octokit.rest.issues.updateComment({
98049830
owner: repo[0],
@@ -9816,15 +9842,16 @@ async function run() {
98169842
}
98179843
} else if (inputs.issueNumber) {
98189844
// Create a comment
9819-
if (!inputs.body) {
9820-
core.setFailed("Missing comment 'body'.");
9845+
if (!body) {
9846+
core.setFailed("Missing comment 'body' or 'body-file'.");
98219847
return;
98229848
}
9849+
98239850
const { data: comment } = await octokit.rest.issues.createComment({
98249851
owner: repo[0],
98259852
repo: repo[1],
98269853
issue_number: inputs.issueNumber,
9827-
body: inputs.body,
9854+
body,
98289855
});
98299856
core.info(
98309857
`Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.`

index.js

Lines changed: 34 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

@@ -63,6 +64,16 @@ async function addReactions(octokit, repo, comment_id, reactions) {
6364
results = undefined;
6465
}
6566

67+
function getBody(inputs) {
68+
if (inputs.body) {
69+
return inputs.body;
70+
} else if (inputs.bodyFile) {
71+
return readFileSync(inputs.bodyFile, 'utf-8');
72+
} else {
73+
return '';
74+
}
75+
}
76+
6677
async function run() {
6778
try {
6879
const inputs = {
@@ -71,6 +82,7 @@ async function run() {
7182
issueNumber: core.getInput("issue-number"),
7283
commentId: core.getInput("comment-id"),
7384
body: core.getInput("body"),
85+
bodyFile: core.getInput("body-file"),
7486
editMode: core.getInput("edit-mode"),
7587
reactions: core.getInput("reactions")
7688
? core.getInput("reactions")
@@ -91,16 +103,30 @@ async function run() {
91103
return;
92104
}
93105

106+
if (inputs.bodyFile && inputs.body) {
107+
core.setFailed("Only one of 'body' or 'body-file' can be set.");
108+
return;
109+
}
110+
111+
if (inputs.bodyFile) {
112+
if (!existsSync(inputs.bodyFile)) {
113+
core.setFailed(`File '${inputs.bodyFile}' does not exist.`);
114+
return;
115+
}
116+
}
117+
118+
const body = getBody(inputs);
119+
94120
const octokit = github.getOctokit(inputs.token);
95121

96122
if (inputs.commentId) {
97123
// Edit a comment
98-
if (!inputs.body && !inputs.reactions) {
99-
core.setFailed("Missing either comment 'body' or 'reactions'.");
124+
if (!body && !inputs.reactions) {
125+
core.setFailed("Missing comment 'body', 'body-file', or 'reactions'.");
100126
return;
101127
}
102128

103-
if (inputs.body) {
129+
if (body) {
104130
var commentBody = "";
105131
if (editMode == "append") {
106132
// Get the comment body
@@ -112,7 +138,7 @@ async function run() {
112138
commentBody = comment.body + "\n";
113139
}
114140

115-
commentBody = commentBody + inputs.body;
141+
commentBody = commentBody + body;
116142
core.debug(`Comment body: ${commentBody}`);
117143
await octokit.rest.issues.updateComment({
118144
owner: repo[0],
@@ -130,15 +156,16 @@ async function run() {
130156
}
131157
} else if (inputs.issueNumber) {
132158
// Create a comment
133-
if (!inputs.body) {
134-
core.setFailed("Missing comment 'body'.");
159+
if (!body) {
160+
core.setFailed("Missing comment 'body' or 'body-file'.");
135161
return;
136162
}
163+
137164
const { data: comment } = await octokit.rest.issues.createComment({
138165
owner: repo[0],
139166
repo: repo[1],
140167
issue_number: inputs.issueNumber,
141-
body: inputs.body,
168+
body,
142169
});
143170
core.info(
144171
`Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.`

0 commit comments

Comments
 (0)