Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ type Comment = {

export type UpdateIfExistsType = 'create' | 'replace' | 'append' | 'recreate'

export const createCommentKey = (key: string): string => `<!-- diff-action/${key} -->`

export const addComment = async (github: GitHubContext, comment: Comment): Promise<void> => {
if (!github.issueNumber) {
core.info(`Ignored non pull request event: ${github.eventName}`)
Expand All @@ -31,7 +33,7 @@ export const addComment = async (github: GitHubContext, comment: Comment): Promi
return
}

const commentKey = `<!-- diff-action/${comment.updateIfExistsKey} -->`
const commentKey = createCommentKey(comment.updateIfExistsKey)
core.info(`Finding key ${commentKey} from comments in #${github.issueNumber}`)
const existingComment = await findComment(github, commentKey)
if (!existingComment) {
Expand Down Expand Up @@ -107,3 +109,15 @@ const findComment = async (github: GitHubContext, key: string): Promise<Existing
}
}
}

export const deleteCommentIfExists = async (github: GitHubContext, updateIfExistsKey: string): Promise<void> => {
const comment = await findComment(github, createCommentKey(updateIfExistsKey))

if (comment) {
await github.octokit.rest.issues.deleteComment({
owner: github.owner,
repo: github.repo,
comment_id: comment.id,
})
}
}
18 changes: 11 additions & 7 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as core from '@actions/core'
import { GitHubContext } from './github.js'
import { computeDiff, showColorDiff } from './diff.js'
import { addLabels, removeLabels } from './label.js'
import { UpdateIfExistsType, addComment } from './comment.js'
import { UpdateIfExistsType, addComment, deleteCommentIfExists } from './comment.js'
import { formatComment } from './format.js'

type Inputs = {
Expand Down Expand Up @@ -33,12 +33,16 @@ export const run = async (github: GitHubContext, inputs: Inputs): Promise<Output
workflowRunURL: github.workflowRunURL,
})

if (inputs.comment && commentBody.length > 0) {
await addComment(github, {
body: `${inputs.commentHeader}\n\n${commentBody}\n\n${inputs.commentFooter}`,
updateIfExists: inputs.updateIfExists,
updateIfExistsKey: inputs.updateIfExistsKey,
})
if (inputs.comment) {
if (commentBody.length === 0) {
await deleteCommentIfExists(github, inputs.updateIfExistsKey)
} else {
await addComment(github, {
body: `${inputs.commentHeader}\n\n${commentBody}\n\n${inputs.commentFooter}`,
updateIfExists: inputs.updateIfExists,
updateIfExistsKey: inputs.updateIfExistsKey,
})
}
}

if (diffs.length > 0) {
Expand Down