diff --git a/README.md b/README.md index 84e59033..432136e9 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ A GitHub action to create or update an issue or pull request comment. | `comment-id` | The id of the comment to update. | | | `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` | +| `edit-mode` | The mode when updating a comment, `append`, or `replace`, or `recreate` . | `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`) | | | `reactions-edit-mode` | The mode when updating comment reactions, `replace` or `append`. | `append` | diff --git a/action.yml b/action.yml index 29d32e1f..95863791 100644 --- a/action.yml +++ b/action.yml @@ -18,7 +18,7 @@ inputs: body-file: description: 'Deprecated in favour of `body-path`.' edit-mode: - description: 'The mode when updating a comment, "replace" or "append".' + description: 'The mode when updating a comment, "append", or "replace", or "recreate".' default: 'append' append-separator: description: 'The separator to use when appending to an existing comment. (`newline`, `space`, `none`)' diff --git a/src/create-or-update-comment.ts b/src/create-or-update-comment.ts index f6be7b93..68f89d04 100644 --- a/src/create-or-update-comment.ts +++ b/src/create-or-update-comment.ts @@ -151,35 +151,52 @@ async function updateComment( octokit, owner: string, repo: string, + issueNumber: number, commentId: number, body: string, editMode: string, appendSeparator: string ): Promise { - if (body) { - let commentBody = '' - if (editMode == 'append') { - // Get the comment body - const {data: comment} = await octokit.rest.issues.getComment({ - owner: owner, - repo: repo, - comment_id: commentId - }) - commentBody = appendSeparatorTo( - comment.body ? comment.body : '', - appendSeparator - ) - } - commentBody = truncateBody(commentBody + body) - core.debug(`Comment body: ${commentBody}`) - await octokit.rest.issues.updateComment({ + if (!body) { + return commentId + } + + if (editMode == 'recreate') { + await octokit.rest.issues.deleteComment({ owner: owner, repo: repo, - comment_id: commentId, - body: commentBody + comment_id: commentId + }) + core.info(`Removed comment id '${commentId}'.`) + + return await createComment(octokit, owner, repo, issueNumber, body) + } + + let commentBody = '' + if (editMode == 'append') { + // Get the comment body + const {data: comment} = await octokit.rest.issues.getComment({ + owner: owner, + repo: repo, + comment_id: commentId }) - core.info(`Updated comment id '${commentId}'.`) + commentBody = appendSeparatorTo( + comment.body ? comment.body : '', + appendSeparator + ) } + + commentBody = truncateBody(commentBody + body) + core.debug(`Comment body: ${commentBody}`) + + await octokit.rest.issues.updateComment({ + owner: owner, + repo: repo, + comment_id: commentId, + body: commentBody + }) + core.info(`Updated comment id '${commentId}'.`) + return commentId } @@ -247,6 +264,7 @@ export async function createOrUpdateComment( octokit, owner, repo, + inputs.issueNumber, inputs.commentId, body, inputs.editMode, diff --git a/src/main.ts b/src/main.ts index ba8e259f..9c6168c7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,7 +30,7 @@ async function run(): Promise { } core.debug(`Inputs: ${inspect(inputs)}`) - if (!['append', 'replace'].includes(inputs.editMode)) { + if (!['append', 'replace', 'recreate'].includes(inputs.editMode)) { throw new Error(`Invalid edit-mode '${inputs.editMode}'.`) }