From 73d56db72bcc1fbe21523a961966c1eff21cdff0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?T=C3=A9rence=20Hollander?= Date: Wed, 14 Dec 2022 18:34:50 +0100 Subject: [PATCH] feat: mode for updating comment (#170) --- .github/workflows/ci.yaml | 3 ++- README.md | 1 + action.yml | 3 +++ lib/index.js | 34 +++++++++++++++++++++++++++------- src/main.ts | 34 +++++++++++++++++++++++++++------- 5 files changed, 60 insertions(+), 15 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0ebab36a..cf4bf71a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -17,4 +17,5 @@ jobs: Current branch is `${{ github.head_ref }}`. _(execution **${{ github.run_id }}** / attempt **${{ github.run_attempt }}**)_ comment_tag: nrt - reactions: eyes, rocket \ No newline at end of file + reactions: eyes, rocket + mode: recreate \ No newline at end of file diff --git a/README.md b/README.md index 747f136a..7d6b060a 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ _That is particularly interesting while committing multiple times in a PR and th | `reactions` | List of reactions for the comment (comma separated). See https://docs.github.com/en/rest/reactions#reaction-types | | | | `pr_number` | The number of the pull request where to create the comment | | current pull-request/issue number (deduced from context) | | `comment_tag` | A tag on your comment that will be used to identify a comment in case of replacement | | | +| `mode` | Mode that will be used to update comment (upsert/recreate) | | upsert | ## Contributing diff --git a/action.yml b/action.yml index 06093bc6..76315a45 100644 --- a/action.yml +++ b/action.yml @@ -17,6 +17,9 @@ inputs: description: 'Manual pull request number' comment_tag: description: 'A tag on your comment that will be used to identify a comment in case of replacement.' + mode: + description: 'Mode that will be used to update comment (upsert/recreate)' + default: 'upsert' runs: using: 'node16' main: 'lib/index.js' diff --git a/lib/index.js b/lib/index.js index 7e41387e..724a8bea 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9547,6 +9547,7 @@ async function run() { const pr_number = core.getInput('pr_number'); const comment_tag = core.getInput('comment_tag'); const reactions = core.getInput('reactions'); + const mode = core.getInput('mode'); const context = github.context; const issue_number = parseInt(pr_number) || context.payload.pull_request?.number || context.payload.issue?.number; const octokit = github.getOctokit(github_token); @@ -9582,13 +9583,32 @@ async function run() { break; } if (comment) { - await octokit.rest.issues.updateComment({ - ...context.repo, - comment_id: comment.id, - body, - }); - await addReactions(comment.id, reactions); - return; + if (mode === 'upsert') { + await octokit.rest.issues.updateComment({ + ...context.repo, + comment_id: comment.id, + body, + }); + await addReactions(comment.id, reactions); + return; + } + else if (mode === 'recreate') { + await octokit.rest.issues.deleteComment({ + ...context.repo, + comment_id: comment.id, + }); + const { data: newComment } = await octokit.rest.issues.createComment({ + ...context.repo, + issue_number, + body, + }); + await addReactions(newComment.id, reactions); + return; + } + else { + core.setFailed(`Mode ${mode} is unknown. Please use 'upsert' or 'recreate'.`); + return; + } } else { core.info('No comment has been found with asked pattern. Creating a new comment.'); diff --git a/src/main.ts b/src/main.ts index 71128f92..328e86e9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,6 +13,7 @@ async function run() { const pr_number: string = core.getInput('pr_number'); const comment_tag: string = core.getInput('comment_tag'); const reactions: string = core.getInput('reactions'); + const mode: string = core.getInput('mode'); const context = github.context; const issue_number = parseInt(pr_number) || context.payload.pull_request?.number || context.payload.issue?.number; @@ -60,13 +61,32 @@ async function run() { } if (comment) { - await octokit.rest.issues.updateComment({ - ...context.repo, - comment_id: comment.id, - body, - }); - await addReactions(comment.id, reactions); - return; + if (mode === 'upsert') { + await octokit.rest.issues.updateComment({ + ...context.repo, + comment_id: comment.id, + body, + }); + await addReactions(comment.id, reactions); + return; + } else if (mode === 'recreate') { + await octokit.rest.issues.deleteComment({ + ...context.repo, + comment_id: comment.id, + }); + + const { data: newComment } = await octokit.rest.issues.createComment({ + ...context.repo, + issue_number, + body, + }); + + await addReactions(newComment.id, reactions); + return; + } else { + core.setFailed(`Mode ${mode} is unknown. Please use 'upsert' or 'recreate'.`); + return; + } } else { core.info('No comment has been found with asked pattern. Creating a new comment.'); }