diff --git a/.github/workflows/test-command.yml b/.github/workflows/test-command.yml index cf4b9b4a..13486238 100644 --- a/.github/workflows/test-command.yml +++ b/.github/workflows/test-command.yml @@ -59,7 +59,7 @@ jobs: uses: ./ with: issue-number: 1 - body-file: .github/comment-body.md + body-path: .github/comment-body.md # Test create from template - name: Render template diff --git a/README.md b/README.md index abf63b9e..56149bb6 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ A GitHub action to create or update an issue or pull request comment. | `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. | | | `comment-id` | The id of the comment to update. | | -| `body` | The comment body. Cannot be used in conjunction with `body-file`. | | -| `body-file` | The path to a file containing the comment body. Cannot be used in conjunction with `body`. | | +| `body` | The comment body. Cannot be used in conjunction with `body-path`. | | +| `body-path` | The path to a file containing the comment body. Cannot be used in conjunction with `body`. | | | `edit-mode` | The mode when updating a comment, `replace` or `append`. | `append` | | `append-separator` | The separator to use when appending to an existing comment. (`newline`, `space`, `none`) | `newline` | | `reactions` | A comma or newline separated list of reactions to add to the comment. (`+1`, `-1`, `laugh`, `confused`, `heart`, `hooray`, `rocket`, `eyes`) | | @@ -167,7 +167,7 @@ If required, the create and update steps can be separated for greater control. uses: peter-evans/create-or-update-comment@v3 with: issue-number: 1 - body-file: 'comment-body.md' + body-path: 'comment-body.md' ``` ### Using a markdown template diff --git a/action.yml b/action.yml index 52ae9907..30effcde 100644 --- a/action.yml +++ b/action.yml @@ -12,9 +12,11 @@ inputs: comment-id: description: 'The id of the comment to update.' body: - description: 'The comment body. Cannot be used in conjunction with `body-file`.' - body-file: + description: 'The comment body. Cannot be used in conjunction with `body-path`.' + body-path: description: 'The path to a file containing the comment body. Cannot be used in conjunction with `body`.' + body-file: + description: 'Deprecated in favour of `body-path`.' edit-mode: description: 'The mode when updating a comment, "replace" or "append".' default: 'append' diff --git a/dist/index.js b/dist/index.js index 001e9449..08f0e20b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -304,6 +304,15 @@ function getBody(inputs) { return ''; } } +function getBodyPathInput() { + const bodyPath = core.getInput('body-path'); + if (bodyPath) { + return bodyPath; + } + else { + return core.getInput('body-file'); + } +} function run() { return __awaiter(this, void 0, void 0, function* () { try { @@ -313,7 +322,7 @@ function run() { issueNumber: Number(core.getInput('issue-number')), commentId: Number(core.getInput('comment-id')), body: core.getInput('body'), - bodyFile: core.getInput('body-file'), + bodyPath: getBodyPathInput(), editMode: core.getInput('edit-mode'), appendSeparator: core.getInput('append-separator'), reactions: utils.getInputAsArray('reactions'), @@ -329,23 +338,23 @@ function run() { if (!['newline', 'space', 'none'].includes(inputs.appendSeparator)) { throw new Error(`Invalid append-separator '${inputs.appendSeparator}'.`); } - if (inputs.bodyFile && inputs.body) { - throw new Error("Only one of 'body' or 'body-file' can be set."); + if (inputs.bodyPath && inputs.body) { + throw new Error("Only one of 'body' or 'body-path' can be set."); } - if (inputs.bodyFile) { - if (!(0, fs_1.existsSync)(inputs.bodyFile)) { - throw new Error(`File '${inputs.bodyFile}' does not exist.`); + if (inputs.bodyPath) { + if (!(0, fs_1.existsSync)(inputs.bodyPath)) { + throw new Error(`File '${inputs.bodyPath}' does not exist.`); } } const body = getBody(inputs); if (inputs.commentId) { if (!body && !inputs.reactions) { - throw new Error("Missing comment 'body', 'body-file', or 'reactions'."); + throw new Error("Missing comment 'body', 'body-path', or 'reactions'."); } } else if (inputs.issueNumber) { if (!body) { - throw new Error("Missing comment 'body' or 'body-file'."); + throw new Error("Missing comment 'body' or 'body-path'."); } } else { diff --git a/index.js b/index.js deleted file mode 100644 index 98ebd7a6..00000000 --- a/index.js +++ /dev/null @@ -1,192 +0,0 @@ -const { inspect } = require("util"); -const { readFileSync, existsSync } = require("fs"); -const core = require("@actions/core"); -const github = require("@actions/github"); - -const REACTION_TYPES = [ - "+1", - "-1", - "laugh", - "confused", - "heart", - "hooray", - "rocket", - "eyes", -]; - -async function addReactions(octokit, repo, comment_id, reactions) { - let ReactionsSet = [ - ...new Set( - reactions - .replace(/\s/g, "") - .split(",") - .filter((item) => { - if (!REACTION_TYPES.includes(item)) { - core.info(`Skipping invalid reaction '${item}'.`); - return false; - } - return true; - }) - ), - ]; - - if (!ReactionsSet) { - core.setFailed( - `No valid reactions are contained in '${reactions}'.` - ); - return false; - } - - let results = await Promise.allSettled( - ReactionsSet.map(async (item) => { - await octokit.rest.reactions.createForIssueComment({ - owner: repo[0], - repo: repo[1], - comment_id: comment_id, - content: item, - }); - core.info(`Setting '${item}' reaction on comment.`); - }) - ); - - for (let i = 0, l = results.length; i < l; i++) { - if (results[i].status === "fulfilled") { - core.info( - `Added reaction '${ReactionsSet[i]}' to comment id '${comment_id}'.` - ); - } else if (results[i].status === "rejected") { - core.info( - `Adding reaction '${ReactionsSet[i]}' to comment id '${comment_id}' failed with ${results[i].reason}.` - ); - } - } - ReactionsSet = 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() { - try { - const inputs = { - token: core.getInput("token"), - repository: core.getInput("repository"), - issueNumber: core.getInput("issue-number"), - commentId: core.getInput("comment-id"), - body: core.getInput("body"), - bodyFile: core.getInput("body-file"), - editMode: core.getInput("edit-mode"), - reactions: core.getInput("reactions") - ? core.getInput("reactions") - : core.getInput("reaction-type"), - }; - core.debug(`Inputs: ${inspect(inputs)}`); - - const repository = inputs.repository - ? inputs.repository - : process.env.GITHUB_REPOSITORY; - const repo = repository.split("/"); - core.debug(`repository: ${repository}`); - - const editMode = inputs.editMode ? inputs.editMode : "append"; - core.debug(`editMode: ${editMode}`); - if (!["append", "replace"].includes(editMode)) { - core.setFailed(`Invalid edit-mode '${editMode}'.`); - return; - } - - if (inputs.bodyFile && inputs.body) { - core.setFailed("Only one of 'body' or 'body-file' can be set."); - return; - } - - if (inputs.bodyFile) { - if (!existsSync(inputs.bodyFile)) { - core.setFailed(`File '${inputs.bodyFile}' does not exist.`); - return; - } - } - - const body = getBody(inputs); - - const octokit = github.getOctokit(inputs.token); - - if (inputs.commentId) { - // Edit a comment - if (!body && !inputs.reactions) { - core.setFailed("Missing comment 'body', 'body-file', or 'reactions'."); - return; - } - - if (body) { - var commentBody = ""; - if (editMode == "append") { - // Get the comment body - const { data: comment } = await octokit.rest.issues.getComment({ - owner: repo[0], - repo: repo[1], - comment_id: inputs.commentId, - }); - commentBody = comment.body + "\n"; - } - - commentBody = commentBody + body; - core.debug(`Comment body: ${commentBody}`); - await octokit.rest.issues.updateComment({ - owner: repo[0], - repo: repo[1], - comment_id: inputs.commentId, - body: commentBody, - }); - core.info(`Updated comment id '${inputs.commentId}'.`); - core.setOutput("comment-id", inputs.commentId); - } - - // Set comment reactions - if (inputs.reactions) { - await addReactions(octokit, repo, inputs.commentId, inputs.reactions); - } - } else if (inputs.issueNumber) { - // Create a comment - if (!body) { - core.setFailed("Missing comment 'body' or 'body-file'."); - return; - } - - const { data: comment } = await octokit.rest.issues.createComment({ - owner: repo[0], - repo: repo[1], - issue_number: inputs.issueNumber, - body, - }); - core.info( - `Created comment id '${comment.id}' on issue '${inputs.issueNumber}'.` - ); - core.setOutput("comment-id", comment.id); - - // Set comment reactions - if (inputs.reactions) { - await addReactions(octokit, repo, comment.id, inputs.reactions); - } - } else { - core.setFailed("Missing either 'issue-number' or 'comment-id'."); - return; - } - } catch (error) { - core.debug(inspect(error)); - core.setFailed(error.message); - if (error.message == 'Resource not accessible by integration') { - core.error(`See this action's readme for details about this error`); - } - } -} - -run(); diff --git a/src/create-or-update-comment.ts b/src/create-or-update-comment.ts index dcf6d8e3..a6c232b9 100644 --- a/src/create-or-update-comment.ts +++ b/src/create-or-update-comment.ts @@ -9,7 +9,7 @@ export interface Inputs { issueNumber: number commentId: number body: string - bodyFile: string + bodyPath: string editMode: string appendSeparator: string reactions: string[] diff --git a/src/main.ts b/src/main.ts index 89e5899f..af771eb2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,6 +14,15 @@ function getBody(inputs) { } } +function getBodyPathInput(): string { + const bodyPath = core.getInput('body-path') + if (bodyPath) { + return bodyPath + } else { + return core.getInput('body-file') + } +} + async function run(): Promise { try { const inputs: Inputs = { @@ -22,7 +31,7 @@ async function run(): Promise { issueNumber: Number(core.getInput('issue-number')), commentId: Number(core.getInput('comment-id')), body: core.getInput('body'), - bodyFile: core.getInput('body-file'), + bodyPath: getBodyPathInput(), editMode: core.getInput('edit-mode'), appendSeparator: core.getInput('append-separator'), reactions: utils.getInputAsArray('reactions'), @@ -44,13 +53,13 @@ async function run(): Promise { throw new Error(`Invalid append-separator '${inputs.appendSeparator}'.`) } - if (inputs.bodyFile && inputs.body) { - throw new Error("Only one of 'body' or 'body-file' can be set.") + if (inputs.bodyPath && inputs.body) { + throw new Error("Only one of 'body' or 'body-path' can be set.") } - if (inputs.bodyFile) { - if (!existsSync(inputs.bodyFile)) { - throw new Error(`File '${inputs.bodyFile}' does not exist.`) + if (inputs.bodyPath) { + if (!existsSync(inputs.bodyPath)) { + throw new Error(`File '${inputs.bodyPath}' does not exist.`) } } @@ -58,11 +67,11 @@ async function run(): Promise { if (inputs.commentId) { if (!body && !inputs.reactions) { - throw new Error("Missing comment 'body', 'body-file', or 'reactions'.") + throw new Error("Missing comment 'body', 'body-path', or 'reactions'.") } } else if (inputs.issueNumber) { if (!body) { - throw new Error("Missing comment 'body' or 'body-file'.") + throw new Error("Missing comment 'body' or 'body-path'.") } } else { throw new Error("Missing either 'issue-number' or 'comment-id'.")