Add option to skip updating or recreating comments when message is unchanged

This commit is contained in:
Alex Burgel 2023-08-14 12:51:55 -04:00
parent 077277a006
commit e5439e773a
No known key found for this signature in database
6 changed files with 96 additions and 19 deletions

View file

@ -15,6 +15,10 @@ function headerComment(header: String): string {
return `<!-- Sticky Pull Request Comment${header} -->`
}
function bodyWithHeader(body: string, header: string): string {
return `${body}\n${headerComment(header)}`
}
export async function findPreviousComment(
octokit: InstanceType<typeof GitHub>,
repo: {
@ -100,7 +104,7 @@ export async function updateComment(
id,
body: previousBody
? `${previousBody}\n${body}`
: `${body}\n${headerComment(header)}`
: bodyWithHeader(body, header)
}
}
)
@ -126,7 +130,7 @@ export async function createComment(
issue_number,
body: previousBody
? `${previousBody}\n${body}`
: `${body}\n${headerComment(header)}`
: bodyWithHeader(body, header)
})
}
export async function deleteComment(
@ -162,7 +166,7 @@ export async function minimizeComment(
}
export function getBodyOf(
previous: {body?: string},
previous: {body: string},
append: boolean,
hideDetails: boolean
): string | undefined {
@ -176,3 +180,12 @@ export function getBodyOf(
return previous.body?.replace(/(<details.*?)\s*\bopen\b(.*>)/g, "$1$2")
}
export function commentsEqual(
body: string,
previous: string,
header: string
): boolean {
const newBody = bodyWithHeader(body, header)
return newBody === previous
}

View file

@ -28,6 +28,9 @@ export const onlyCreateComment = core.getBooleanInput("only_create", {
export const onlyUpdateComment = core.getBooleanInput("only_update", {
required: true
})
export const skipUnchanged = core.getBooleanInput("skip_unchanged", {
required: true
})
export const hideOldComment = core.getBooleanInput("hide", {required: true})
export const githubToken = core.getInput("GITHUB_TOKEN", {required: true})
export const ignoreEmpty = core.getBooleanInput("ignore_empty", {

View file

@ -14,6 +14,7 @@ import {
recreate,
repo,
ignoreEmpty,
skipUnchanged,
onlyCreateComment,
onlyUpdateComment
} from "./config"
@ -23,7 +24,8 @@ import {
findPreviousComment,
getBodyOf,
minimizeComment,
updateComment
updateComment,
commentsEqual
} from "./comment"
async function run(): Promise<undefined> {
@ -99,6 +101,11 @@ async function run(): Promise<undefined> {
return
}
if (skipUnchanged && commentsEqual(body, previous.body, header)) {
// don't recreate or update if the message is unchanged
return
}
const previousBody = getBodyOf(previous, append, hideDetails)
if (recreate) {
await deleteComment(octokit, previous.id)