Skip to content

Commit

Permalink
feat: support updating an existing comment instead of creating one
Browse files Browse the repository at this point in the history
  • Loading branch information
Merott committed Jan 17, 2023
1 parent 8aec30f commit 5396071
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
36 changes: 27 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand All @@ -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
}
Expand Down Expand Up @@ -16552,6 +16569,7 @@ const {
PR_LABELS,
CREATE_COMMENT,
DELETE_EXISTING_COMMENT,
UPDATE_EXISTING_COMMENT,
PR_PREVIEW_DOMAIN,
ALIAS_DOMAINS,
ATTACH_COMMIT_METADATA,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
24 changes: 18 additions & 6 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
PR_LABELS,
CREATE_COMMENT,
DELETE_EXISTING_COMMENT,
UPDATE_EXISTING_COMMENT,
PR_PREVIEW_DOMAIN,
ALIAS_DOMAINS,
ATTACH_COMMIT_METADATA,
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5396071

Please sign in to comment.