Issue number (#64)

* Major code reorg
* Add issue arg
* Paginate existing comment list
This commit is contained in:
Michael Shick 2022-11-08 10:12:22 -08:00 committed by GitHub
parent e8076c64f7
commit 8645f3f0ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 2187 additions and 1968 deletions

71
src/comments.ts Normal file
View file

@ -0,0 +1,71 @@
import { GitHub } from '@actions/github/lib/utils'
import { Endpoints } from '@octokit/types'
export type CreateIssueCommentResponseData =
Endpoints['POST /repos/{owner}/{repo}/issues/{issue_number}/comments']['response']['data']
export async function getExistingCommentId(
octokit: InstanceType<typeof GitHub>,
owner: string,
repo: string,
issueNumber: number,
messageId: string,
): Promise<number | undefined> {
const parameters = {
owner,
repo,
issue_number: issueNumber,
per_page: 100,
}
let found
for await (const comments of octokit.paginate.iterator(
octokit.rest.issues.listComments,
parameters,
)) {
found = comments.data.find(({ body }) => {
return (body?.search(messageId) ?? -1) > -1
})
if (found) {
break
}
}
return found?.id
}
export async function updateComment(
octokit: InstanceType<typeof GitHub>,
owner: string,
repo: string,
existingCommentId: number,
body: string,
): Promise<CreateIssueCommentResponseData> {
const updatedComment = await octokit.rest.issues.updateComment({
comment_id: existingCommentId,
owner,
repo,
body,
})
return updatedComment.data
}
export async function createComment(
octokit: InstanceType<typeof GitHub>,
owner: string,
repo: string,
issueNumber: number,
body: string,
): Promise<CreateIssueCommentResponseData> {
const createdComment = await octokit.rest.issues.createComment({
issue_number: issueNumber,
owner,
repo,
body,
})
return createdComment.data
}