Minor refactor and tests for body-file input

This commit is contained in:
Peter Evans 2022-10-24 15:38:52 +09:00
parent b9257b685e
commit 40bf395e0a
8 changed files with 71 additions and 60 deletions

7
.github/comment-body-edited.md vendored Normal file
View File

@ -0,0 +1,7 @@
This is a multi-line test comment read from a file.
- With GitHub **Markdown** :sparkles:
- Created by [create-or-update-comment][1]
[1]: https://github.com/peter-evans/create-or-update-comment
*updated info*

View File

@ -92,6 +92,21 @@ jobs:
comment-id: ${{ steps.couc.outputs.comment-id }} comment-id: ${{ steps.couc.outputs.comment-id }}
reactions: heart, hooray, laugh 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-edited.md
reactions: eyes
package: package:
if: github.event_name == 'push' && github.ref == 'refs/heads/main' if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [test] needs: [test]

View File

@ -61,19 +61,11 @@ jobs:
reactions: hooray reactions: hooray
# Test create with body from file # 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 - name: Create comment
uses: ./ uses: ./
with: with:
issue-number: 1 issue-number: 1
body: ${{ steps.get-comment-body.outputs.body }} body-file: .github/comment-body.md
# Test create from template # Test create from template
- name: Render template - name: Render template

View File

@ -54,9 +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 | | `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. | | | `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. Cannot be used in conjunction with `body-file`. | |
| `file` | The path to a file that can be read as `body`. Use either `file` or `body`, but not both. | | | `body-file` | The path to a file containing the comment body. Cannot be used in conjunction with `body`. | |
| `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`) | |
@ -165,7 +164,7 @@ If required, the create and update steps can be separated for greater control.
uses: peter-evans/create-or-update-comment@v2 uses: peter-evans/create-or-update-comment@v2
with: with:
issue-number: 1 issue-number: 1
file: 'comment-body.txt' body-file: 'comment-body.md'
``` ```
### Using a markdown template ### Using a markdown template

View File

@ -11,9 +11,9 @@ inputs:
comment-id: comment-id:
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. Cannot be used in conjunction with `body-file`.'
file: body-file:
description: 'The path to a file that can be read as `body`. Use either `file` or `body`, but not both.' description: 'The path to a file containing the comment body. Cannot be used in conjunction with `body`.'
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:

43
dist/index.js vendored
View File

@ -9750,6 +9750,16 @@ async function addReactions(octokit, repo, comment_id, reactions) {
results = undefined; 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() { async function run() {
try { try {
const inputs = { const inputs = {
@ -9758,8 +9768,7 @@ 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"), bodyFile: core.getInput("body-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")
@ -9780,29 +9789,29 @@ async function run() {
return; return;
} }
if (inputs.file && inputs.body) { if (inputs.bodyFile && inputs.body) {
core.setFailed("Only one of 'file' or 'body' can be set."); core.setFailed("Only one of 'body' or 'body-file' can be set.");
return; return;
} }
if (inputs.file) { if (inputs.bodyFile) {
if (!existsSync(inputs.file)) { if (!existsSync(inputs.bodyFile)) {
core.setFailed(`File '${inputs.file}' does not exist.`); core.setFailed(`File '${inputs.bodyFile}' does not exist.`);
return; return;
} }
} }
const body = getBody(inputs);
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 && !inputs.file) { if (!body && !inputs.reactions) {
core.setFailed("Missing either comment 'body', 'file', or 'reactions'."); core.setFailed("Missing comment 'body', 'body-file', or 'reactions'.");
return; return;
} }
const body = getBodyOrFile(inputs);
if (body) { if (body) {
var commentBody = ""; var commentBody = "";
if (editMode == "append") { if (editMode == "append") {
@ -9833,10 +9842,8 @@ async function run() {
} }
} else if (inputs.issueNumber) { } else if (inputs.issueNumber) {
// Create a comment // Create a comment
const body = getBodyOrFile(inputs);
if (!body) { if (!body) {
core.setFailed("Missing comment 'body' or 'file'."); core.setFailed("Missing comment 'body' or 'body-file'.");
return; return;
} }
@ -9868,14 +9875,6 @@ 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

@ -64,6 +64,16 @@ async function addReactions(octokit, repo, comment_id, reactions) {
results = undefined; 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() { async function run() {
try { try {
const inputs = { const inputs = {
@ -72,8 +82,7 @@ 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"), bodyFile: core.getInput("body-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")
@ -94,29 +103,29 @@ async function run() {
return; return;
} }
if (inputs.file && inputs.body) { if (inputs.bodyFile && inputs.body) {
core.setFailed("Only one of 'file' or 'body' can be set."); core.setFailed("Only one of 'body' or 'body-file' can be set.");
return; return;
} }
if (inputs.file) { if (inputs.bodyFile) {
if (!existsSync(inputs.file)) { if (!existsSync(inputs.bodyFile)) {
core.setFailed(`File '${inputs.file}' does not exist.`); core.setFailed(`File '${inputs.bodyFile}' does not exist.`);
return; return;
} }
} }
const body = getBody(inputs);
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 && !inputs.file) { if (!body && !inputs.reactions) {
core.setFailed("Missing either comment 'body', 'file', or 'reactions'."); core.setFailed("Missing comment 'body', 'body-file', or 'reactions'.");
return; return;
} }
const body = getBodyOrFile(inputs);
if (body) { if (body) {
var commentBody = ""; var commentBody = "";
if (editMode == "append") { if (editMode == "append") {
@ -147,10 +156,8 @@ async function run() {
} }
} else if (inputs.issueNumber) { } else if (inputs.issueNumber) {
// Create a comment // Create a comment
const body = getBodyOrFile(inputs);
if (!body) { if (!body) {
core.setFailed("Missing comment 'body' or 'file'."); core.setFailed("Missing comment 'body' or 'body-file'.");
return; return;
} }
@ -182,12 +189,4 @@ async function run() {
} }
} }
function getBodyOrFile (inputs) {
if (inputs.body) {
return inputs.body;
} else if (inputs.file) {
return readFileSync(inputs.file, inputs.fileEncoding);
}
}
run(); run();