mirror of
https://github.com/peter-evans/create-or-update-comment.git
synced 2025-01-18 19:22:44 +08:00
Improve code, add support for fileEncoding
This commit is contained in:
parent
9d950b6220
commit
63c92e5e90
41
dist/index.js
vendored
41
dist/index.js
vendored
@ -8573,7 +8573,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 } = __nccwpck_require__(7147);
|
||||
const { readFileSync, existsSync } = __nccwpck_require__(7147);
|
||||
const core = __nccwpck_require__(2186);
|
||||
const github = __nccwpck_require__(5438);
|
||||
|
||||
@ -8647,6 +8647,7 @@ async function run() {
|
||||
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")
|
||||
@ -8667,13 +8668,20 @@ async function run() {
|
||||
return;
|
||||
}
|
||||
|
||||
const octokit = github.getOctokit(inputs.token);
|
||||
|
||||
let bodyToUse = inputs.body;
|
||||
if (inputs.file) {
|
||||
bodyToUse = readFileSync(inputs.file, "utf8");
|
||||
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 && !inputs.file) {
|
||||
@ -8681,7 +8689,9 @@ async function run() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (bodyToUse) {
|
||||
const body = getBodyOrFile(inputs);
|
||||
|
||||
if (body) {
|
||||
var commentBody = "";
|
||||
if (editMode == "append") {
|
||||
// Get the comment body
|
||||
@ -8693,7 +8703,7 @@ async function run() {
|
||||
commentBody = comment.body + "\n";
|
||||
}
|
||||
|
||||
commentBody = commentBody + bodyToUse;
|
||||
commentBody = commentBody + body;
|
||||
core.debug(`Comment body: ${commentBody}`);
|
||||
await octokit.rest.issues.updateComment({
|
||||
owner: repo[0],
|
||||
@ -8711,15 +8721,18 @@ async function run() {
|
||||
}
|
||||
} else if (inputs.issueNumber) {
|
||||
// Create a comment
|
||||
if (!inputs.body && !inputs.file) {
|
||||
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: bodyToUse,
|
||||
body,
|
||||
});
|
||||
core.info(
|
||||
`Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.`
|
||||
@ -8743,6 +8756,14 @@ async function run() {
|
||||
}
|
||||
}
|
||||
|
||||
function getBodyOrFile (inputs) {
|
||||
if (inputs.body) {
|
||||
return inputs.body;
|
||||
} else if (inputs.file) {
|
||||
return readFileSync(inputs.file, inputs.fileEncoding);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
|
||||
})();
|
||||
|
41
index.js
41
index.js
@ -1,5 +1,5 @@
|
||||
const { inspect } = require("util");
|
||||
const { readFileSync } = require("fs");
|
||||
const { readFileSync, existsSync } = require("fs");
|
||||
const core = require("@actions/core");
|
||||
const github = require("@actions/github");
|
||||
|
||||
@ -73,6 +73,7 @@ async function run() {
|
||||
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")
|
||||
@ -93,13 +94,20 @@ async function run() {
|
||||
return;
|
||||
}
|
||||
|
||||
const octokit = github.getOctokit(inputs.token);
|
||||
|
||||
let bodyToUse = inputs.body;
|
||||
if (inputs.file) {
|
||||
bodyToUse = readFileSync(inputs.file, "utf8");
|
||||
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 && !inputs.file) {
|
||||
@ -107,7 +115,9 @@ async function run() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (bodyToUse) {
|
||||
const body = getBodyOrFile(inputs);
|
||||
|
||||
if (body) {
|
||||
var commentBody = "";
|
||||
if (editMode == "append") {
|
||||
// Get the comment body
|
||||
@ -119,7 +129,7 @@ async function run() {
|
||||
commentBody = comment.body + "\n";
|
||||
}
|
||||
|
||||
commentBody = commentBody + bodyToUse;
|
||||
commentBody = commentBody + body;
|
||||
core.debug(`Comment body: ${commentBody}`);
|
||||
await octokit.rest.issues.updateComment({
|
||||
owner: repo[0],
|
||||
@ -137,15 +147,18 @@ async function run() {
|
||||
}
|
||||
} else if (inputs.issueNumber) {
|
||||
// Create a comment
|
||||
if (!inputs.body && !inputs.file) {
|
||||
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: bodyToUse,
|
||||
body,
|
||||
});
|
||||
core.info(
|
||||
`Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.`
|
||||
@ -169,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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user