From 4aef663965e80d049d5b130b2dacf01227e75373 Mon Sep 17 00:00:00 2001 From: Michael Lahargou Date: Tue, 2 Apr 2024 13:35:15 -0700 Subject: [PATCH 1/3] Add only-delete mode You can now pass `only-delete` which just deletes the comment if it exists. If it doesn't exist, it will log a message and return. --- src/main.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main.ts b/src/main.ts index 62e1ee81..25e9211e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -18,8 +18,8 @@ async function run() { const mode: string = core.getInput('mode'); const create_if_not_exists: boolean = core.getInput('create_if_not_exists') === 'true'; - if (!message && !filePath) { - core.setFailed('Either "filePath" or "message" should be provided as input'); + if (!message && !filePath && mode !== 'only-delete') { + core.setFailed('Either "filePath" or "message" should be provided as input unless running as "only-delete".'); return; } @@ -157,12 +157,21 @@ async function run() { body, }); return; + } else if (mode === 'only-delete') { + await deleteComment({ + ...context.repo, + comment_id: comment.id, + }); + return; } else if (mode === 'delete') { core.debug('Registering this comment to be deleted.'); } else { - core.setFailed(`Mode ${mode} is unknown. Please use 'upsert', 'recreate' or 'delete'.`); + core.setFailed(`Mode ${mode} is unknown. Please use 'upsert', 'recreate', 'delete', or 'only-delete'.`); return; } + } else if (mode === 'only-delete') { + core.info('No comment has been found with asked pattern. Nothing to delete.'); + return; } else if (create_if_not_exists) { core.info('No comment has been found with asked pattern. Creating a new comment.'); } else { From ad056f5c720b87b2892c9328e1fb51fa031276c0 Mon Sep 17 00:00:00 2001 From: Michael Lahargou Date: Tue, 2 Apr 2024 13:37:01 -0700 Subject: [PATCH 2/3] Update README --- README.md | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 83b7dd62..12d54675 100644 --- a/README.md +++ b/README.md @@ -92,11 +92,10 @@ _That is particularly interesting while committing multiple times in a PR and th Note: the input `mode` can be used to either `upsert` (by default) or `recreate` the comment (= delete and create) -### Delete a comment - -Deleting an existing comment is also possible thanks to the `comment_tag` input combined with `mode: delete`. +### Delete a comment on cleanup -This will delete the comment at the end of the job. +Deleting a comment upon job completion is possible with the `mode: delete` flag. +The comment will be created initially, and on cleanup, it will be deleted. ```yml ... @@ -110,7 +109,29 @@ This will delete the comment at the end of the job. ``` -## Inputs +### Delete a comment + +Deleting a comment with a specific `comment_tag` is possible with the +`mode: only-delete` flag. If a comment with the `comment_tag` exists, it will +be deleted when ran. + +```yml +... +- name: Create a comment + uses: thollander/actions-comment-pull-request@v2 + with: + message: Example comment + comment_tag: to_delete + +- name: Delete the comment + uses: thollander/actions-comment-pull-request@v2 + with: + comment_tag: to_delete + mode: only-delete + +``` + +## Inputs ### Action inputs From e26105f21a4fbd1f1925e8be626c2d43158bc61e Mon Sep 17 00:00:00 2001 From: Michael Lahargou Date: Tue, 2 Apr 2024 13:50:25 -0700 Subject: [PATCH 3/3] npm run build --- lib/index.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/index.js b/lib/index.js index c7410c4d..81575767 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9554,8 +9554,8 @@ async function run() { const reactions = core.getInput('reactions'); const mode = core.getInput('mode'); const create_if_not_exists = core.getInput('create_if_not_exists') === 'true'; - if (!message && !filePath) { - core.setFailed('Either "filePath" or "message" should be provided as input'); + if (!message && !filePath && mode !== 'only-delete') { + core.setFailed('Either "filePath" or "message" should be provided as input unless running as "only-delete".'); return; } let content = message; @@ -9651,14 +9651,25 @@ async function run() { }); return; } + else if (mode === 'only-delete') { + await deleteComment({ + ...context.repo, + comment_id: comment.id, + }); + return; + } else if (mode === 'delete') { core.debug('Registering this comment to be deleted.'); } else { - core.setFailed(`Mode ${mode} is unknown. Please use 'upsert', 'recreate' or 'delete'.`); + core.setFailed(`Mode ${mode} is unknown. Please use 'upsert', 'recreate', 'delete', or 'only-delete'.`); return; } } + else if (mode === 'only-delete') { + core.info('No comment has been found with asked pattern. Nothing to delete.'); + return; + } else if (create_if_not_exists) { core.info('No comment has been found with asked pattern. Creating a new comment.'); }