mirror of
https://github.com/marocchino/sticky-pull-request-comment.git
synced 2025-12-16 13:08:28 +00:00
Make findPreviousComment only return comments made by the current user (usually github-actions[bot])
83 lines
1.9 KiB
TypeScript
83 lines
1.9 KiB
TypeScript
import * as core from "@actions/core"
|
|
import {GitHub} from "@actions/github/lib/utils"
|
|
|
|
function headerComment(header: String): string {
|
|
return `<!-- Sticky Pull Request Comment${header} -->`
|
|
}
|
|
|
|
export async function findPreviousComment(
|
|
octokit: InstanceType<typeof GitHub>,
|
|
repo: {
|
|
owner: string
|
|
repo: string
|
|
},
|
|
issue_number: number,
|
|
header: string
|
|
): Promise<{body?: string; id: number} | undefined> {
|
|
const {viewer} = await octokit.graphql("query { viewer { login } }")
|
|
const {data: comments} = await octokit.rest.issues.listComments({
|
|
...repo,
|
|
issue_number
|
|
})
|
|
const h = headerComment(header)
|
|
return comments.find(
|
|
comment => comment.user?.login === viewer.login && comment.body?.includes(h)
|
|
)
|
|
}
|
|
export async function updateComment(
|
|
octokit: InstanceType<typeof GitHub>,
|
|
repo: {
|
|
owner: string
|
|
repo: string
|
|
},
|
|
comment_id: number,
|
|
body: string,
|
|
header: string,
|
|
previousBody?: string
|
|
): Promise<void> {
|
|
if (!body && !previousBody)
|
|
return core.warning("Comment body cannot be blank")
|
|
|
|
await octokit.rest.issues.updateComment({
|
|
...repo,
|
|
comment_id,
|
|
body: previousBody
|
|
? `${previousBody}\n${body}`
|
|
: `${body}\n${headerComment(header)}`
|
|
})
|
|
}
|
|
export async function createComment(
|
|
octokit: InstanceType<typeof GitHub>,
|
|
repo: {
|
|
owner: string
|
|
repo: string
|
|
},
|
|
issue_number: number,
|
|
body: string,
|
|
header: string,
|
|
previousBody?: string
|
|
): Promise<void> {
|
|
if (!body && !previousBody)
|
|
return core.warning("Comment body cannot be blank")
|
|
|
|
await octokit.rest.issues.createComment({
|
|
...repo,
|
|
issue_number,
|
|
body: previousBody
|
|
? `${previousBody}\n${body}`
|
|
: `${body}\n${headerComment(header)}`
|
|
})
|
|
}
|
|
export async function deleteComment(
|
|
octokit: InstanceType<typeof GitHub>,
|
|
repo: {
|
|
owner: string
|
|
repo: string
|
|
},
|
|
comment_id: number
|
|
): Promise<void> {
|
|
await octokit.rest.issues.deleteComment({
|
|
...repo,
|
|
comment_id
|
|
})
|
|
}
|