import { GITHUB_EMPTY_RESPONSE_SCHEMA, GITHUB_ISSUE_COMMENT_SCHEMA, } from "@automate.ax/integration-contracts/github" import * as z from "zod" import { defineAction } from "../../../automation/actions" import { getGitHubApi } from "../lib/api" import { GITHUB_ISSUE_NUMBER_INPUT_SCHEMA, GITHUB_PAGE_INPUT_SCHEMA, GITHUB_REPOSITORY_INPUT_SCHEMA, } from "../lib/input-schemas" import { GITHUB_ISSUE_COMMENT_READ_REQUIREMENT, GITHUB_ISSUE_COMMENT_REQUIREMENT, } from "../lib/scopes" /** Lists comments on one GitHub issue or pull request. */ export const listGitHubIssueComments = defineAction( "List GitHub issue comments", ) .describe("Lists comments on one GitHub issue or pull request.") .account("github", GITHUB_ISSUE_COMMENT_READ_REQUIREMENT) .input( GITHUB_ISSUE_NUMBER_INPUT_SCHEMA.extend({ ...GITHUB_PAGE_INPUT_SCHEMA, /** Sort direction. */ direction: z.enum(["asc", "desc"]).optional(), /** Only return comments updated after this timestamp. */ since: z.iso.datetime({ offset: true }).optional(), /** Comment sort field. */ sort: z.enum(["created", "updated"]).optional(), }), ) .output(GITHUB_ISSUE_COMMENT_SCHEMA.array()) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { data } = await getGitHubApi( account.secret, ).rest.issues.listComments({ issue_number: input.issueNumber, owner: input.owner, page: input.page, per_page: input.perPage, repo: input.repository, ...(input.direction && { direction: input.direction }), ...(input.since && { since: input.since }), ...(input.sort && { sort: input.sort }), }) return data }) /** Gets one GitHub issue or pull request comment by ID. */ export const getGitHubIssueComment = defineAction("Get GitHub issue comment") .describe("Gets one issue or pull request comment by repository comment ID.") .account("github", GITHUB_ISSUE_COMMENT_READ_REQUIREMENT) .input( GITHUB_REPOSITORY_INPUT_SCHEMA.extend({ /** Repository-scoped issue comment ID. */ commentId: z.number().int().positive(), }), ) .output(GITHUB_ISSUE_COMMENT_SCHEMA) .retry({ replaySafety: "safe" }) .handler(async ({ account, input }) => { const { data } = await getGitHubApi(account.secret).rest.issues.getComment({ comment_id: input.commentId, owner: input.owner, repo: input.repository, }) return data }) /** Updates one GitHub issue or pull request comment. */ export const updateGitHubIssueComment = defineAction( "Update GitHub issue comment", ) .describe("Replaces the Markdown body of an issue or pull request comment.") .account("github", GITHUB_ISSUE_COMMENT_REQUIREMENT) .input( GITHUB_REPOSITORY_INPUT_SCHEMA.extend({ /** Replacement Markdown body. */ body: z.string().min(1), /** Repository-scoped issue comment ID. */ commentId: z.number().int().positive(), }), ) .output(GITHUB_ISSUE_COMMENT_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { const { data } = await getGitHubApi( account.secret, ).rest.issues.updateComment({ body: input.body, comment_id: input.commentId, owner: input.owner, repo: input.repository, }) return data }) /** Deletes one GitHub issue or pull request comment. */ export const deleteGitHubIssueComment = defineAction( "Delete GitHub issue comment", ) .describe("Deletes one issue or pull request comment by ID.") .account("github", GITHUB_ISSUE_COMMENT_REQUIREMENT) .input( GITHUB_REPOSITORY_INPUT_SCHEMA.extend({ /** Repository-scoped issue comment ID. */ commentId: z.number().int().positive(), }), ) .output(GITHUB_EMPTY_RESPONSE_SCHEMA) .retry({ replaySafety: "unsafe" }) .handler(async ({ account, input }) => { await getGitHubApi(account.secret).rest.issues.deleteComment({ comment_id: input.commentId, owner: input.owner, repo: input.repository, }) return {} })