marocchino-sticky-pull-requ.../src/comment.ts
Rob Cowsill 65b0a353cc
Fix: Ignore other users' comments (#332)
Make findPreviousComment only return comments made by the current user
(usually github-actions[bot])
2021-06-15 03:25:28 +09:00

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
})
}