import * as core from "@actions/core" import {GitHub} from "@actions/github/lib/utils" function headerComment(header: String): string { return `` } export async function findPreviousComment( octokit: InstanceType, 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, repo: { owner: string repo: string }, comment_id: number, body: string, header: string, previousBody?: string ): Promise { 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, repo: { owner: string repo: string }, issue_number: number, body: string, header: string, previousBody?: string ): Promise { 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, repo: { owner: string repo: string }, comment_id: number ): Promise { await octokit.rest.issues.deleteComment({ ...repo, comment_id }) }