From 5396071fce5691e1c84c4303a8e7333145993c19 Mon Sep 17 00:00:00 2001 From: Merott Movahedi Date: Tue, 17 Jan 2023 12:38:27 +0000 Subject: [PATCH] feat: support updating an existing comment instead of creating one --- README.md | 3 ++- action.yml | 6 +++++- dist/index.js | 36 +++++++++++++++++++++++++++--------- src/config.js | 5 +++++ src/github.js | 24 ++++++++++++++++++------ src/index.js | 7 ++++--- 6 files changed, 61 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 9e244c51..9827b438 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,8 @@ Here are all the inputs [deploy-to-vercel-action](https://github.com/BetaHuhn/de | `GITHUB_DEPLOYMENT` | Create a deployment on GitHub | **No** | true | | `GITHUB_DEPLOYMENT_ENV` | Custom environment for the GitHub deployment. | **No** | `Production` or `Preview` | | `PRODUCTION` | Create a production deployment on Vercel and GitHub | **No** | true (false for PR deployments) | -| `DELETE_EXISTING_COMMENT` | Delete existing PR comment when redeploying PR | **No** | true | +| `UPDATE_EXISTING_COMMENT` | Update existing PR comment when redeploying PR | **No** | false | +| `DELETE_EXISTING_COMMENT` | Delete existing PR comment when redeploying PR | **No** | true (ignored if `UPDATE_EXISTING_COMMENT`) | | `CREATE_COMMENT` | Create PR comment when deploying | **No** | true | | `ATTACH_COMMIT_METADATA` | Attach metadata about the commit to the Vercel deployment | **No** | true | | `TRIM_COMMIT_MESSAGE` | When passing meta data to Vercel deployment, trim the commit message to subject only | **No** | false | diff --git a/action.yml b/action.yml index 2efcf759..c34c6917 100644 --- a/action.yml +++ b/action.yml @@ -31,9 +31,13 @@ inputs: description: | Create PR comment when deploying (default: true). required: false + UPDATE_EXISTING_COMMENT: + description: | + Update existing PR comment when redeploying PR (default: false). + required: false DELETE_EXISTING_COMMENT: description: | - Delete existing PR comment when redeploying PR (default: true). + Delete existing PR comment when redeploying PR (default: true, ignored if UPDATE_EXISTING_COMMENT is true). required: false ATTACH_COMMIT_METADATA: description: | diff --git a/dist/index.js b/dist/index.js index f3940806..0062b141 100644 --- a/dist/index.js +++ b/dist/index.js @@ -15928,6 +15928,11 @@ const context = { type: 'boolean', default: true }), + UPDATE_EXISTING_COMMENT: parser.getInput({ + key: 'UPDATE_EXISTING_COMMENT', + type: 'boolean', + default: false + }), ATTACH_COMMIT_METADATA: parser.getInput({ key: 'ATTACH_COMMIT_METADATA', type: 'boolean', @@ -16091,16 +16096,20 @@ const init = () => { return deploymentStatus.data } - const deleteExistingComment = async () => { + const findExistingComment = async () => { const { data } = await client.issues.listComments({ owner: USER, repo: REPOSITORY, issue_number: PR_NUMBER }) - if (data.length < 1) return + return data.find((comment) => + comment.body.includes('This pull request has been deployed to Vercel.') + ) + } - const comment = data.find((comment) => comment.body.includes('This pull request has been deployed to Vercel.')) + const deleteExistingComment = async () => { + const comment = findExistingComment() if (comment) { await client.issues.deleteComment({ owner: USER, @@ -16112,16 +16121,24 @@ const init = () => { } } - const createComment = async (body) => { + const createComment = async (body, updateExisting = false) => { // Remove indentation const dedented = body.replace(/^[^\S\n]+/gm, '') - const comment = await client.issues.createComment({ + const commentParams = { owner: USER, repo: REPOSITORY, issue_number: PR_NUMBER, body: dedented - }) + } + + const existingComment = updateExisting ? findExistingComment() : null + const comment = existingComment ? + await client.issues.updateComment({ + comment_id: existingComment.id, ...commentParams + }) : + await client.issues.createComment(commentParams) + return comment.data } @@ -16552,6 +16569,7 @@ const { PR_LABELS, CREATE_COMMENT, DELETE_EXISTING_COMMENT, + UPDATE_EXISTING_COMMENT, PR_PREVIEW_DOMAIN, ALIAS_DOMAINS, ATTACH_COMMIT_METADATA, @@ -16648,7 +16666,7 @@ const run = async () => { } if (IS_PR) { - if (DELETE_EXISTING_COMMENT) { + if (DELETE_EXISTING_COMMENT && !UPDATE_EXISTING_COMMENT) { core.info('Checking for existing comment on PR') const deletedCommentId = await github.deleteExistingComment() @@ -16678,8 +16696,8 @@ const run = async () => { [View Workflow Logs](${ LOG_URL }) ` - const comment = await github.createComment(body) - core.info(`Comment created: ${ comment.html_url }`) + const comment = await github.createComment(body, UPDATE_EXISTING_COMMENT) + core.info(`Commented: ${ comment.html_url }`) } if (PR_LABELS) { diff --git a/src/config.js b/src/config.js index accfb047..a92728d4 100644 --- a/src/config.js +++ b/src/config.js @@ -42,6 +42,11 @@ const context = { type: 'boolean', default: true }), + UPDATE_EXISTING_COMMENT: parser.getInput({ + key: 'UPDATE_EXISTING_COMMENT', + type: 'boolean', + default: false + }), ATTACH_COMMIT_METADATA: parser.getInput({ key: 'ATTACH_COMMIT_METADATA', type: 'boolean', diff --git a/src/github.js b/src/github.js index 0ff69d93..97d7391f 100644 --- a/src/github.js +++ b/src/github.js @@ -49,16 +49,20 @@ const init = () => { return deploymentStatus.data } - const deleteExistingComment = async () => { + const findExistingComment = async () => { const { data } = await client.issues.listComments({ owner: USER, repo: REPOSITORY, issue_number: PR_NUMBER }) - if (data.length < 1) return + return data.find((comment) => + comment.body.includes('This pull request has been deployed to Vercel.') + ) + } - const comment = data.find((comment) => comment.body.includes('This pull request has been deployed to Vercel.')) + const deleteExistingComment = async () => { + const comment = await findExistingComment() if (comment) { await client.issues.deleteComment({ owner: USER, @@ -70,16 +74,24 @@ const init = () => { } } - const createComment = async (body) => { + const createComment = async (body, updateExisting = false) => { // Remove indentation const dedented = body.replace(/^[^\S\n]+/gm, '') - const comment = await client.issues.createComment({ + const commentParams = { owner: USER, repo: REPOSITORY, issue_number: PR_NUMBER, body: dedented - }) + } + + const existingComment = updateExisting ? await findExistingComment() : null + const comment = existingComment ? + await client.issues.updateComment({ + comment_id: existingComment.id, ...commentParams + }) : + await client.issues.createComment(commentParams) + return comment.data } diff --git a/src/index.js b/src/index.js index c290e5c0..deba1397 100644 --- a/src/index.js +++ b/src/index.js @@ -14,6 +14,7 @@ const { PR_LABELS, CREATE_COMMENT, DELETE_EXISTING_COMMENT, + UPDATE_EXISTING_COMMENT, PR_PREVIEW_DOMAIN, ALIAS_DOMAINS, ATTACH_COMMIT_METADATA, @@ -110,7 +111,7 @@ const run = async () => { } if (IS_PR) { - if (DELETE_EXISTING_COMMENT) { + if (DELETE_EXISTING_COMMENT && !UPDATE_EXISTING_COMMENT) { core.info('Checking for existing comment on PR') const deletedCommentId = await github.deleteExistingComment() @@ -140,8 +141,8 @@ const run = async () => { [View Workflow Logs](${ LOG_URL }) ` - const comment = await github.createComment(body) - core.info(`Comment created: ${ comment.html_url }`) + const comment = await github.createComment(body, UPDATE_EXISTING_COMMENT) + core.info(`Commented: ${ comment.html_url }`) } if (PR_LABELS) {