Files

43 lines
1.6 KiB
TypeScript

import { type } from "arktype";
import { log } from "../utils/cli.ts";
import { fixDoubleEscapedString } from "../utils/fixDoubleEscapedString.ts";
import type { ToolContext } from "./server.ts";
import { execute, tool } from "./shared.ts";
interface GiteaIssue {
number: number; html_url: string; title: string; state: string;
labels?: Array<{ name?: string }>; assignees?: Array<{ login: string }>;
}
export const Issue = type({
title: type.string.describe("the title of the issue"),
body: type.string.describe("the body content of the issue"),
labels: type.string.array().optional(),
assignees: type.string.array().optional(),
});
export function IssueTool(ctx: ToolContext) {
return tool({
name: "create_issue",
description: "Create a new Gitea issue",
parameters: Issue,
execute: execute(async (params) => {
const r = await ctx.gitea.request(
"POST /repos/{owner}/{repo}/issues",
{
owner: ctx.repo.owner, repo: ctx.repo.name,
title: params.title, body: fixDoubleEscapedString(params.body),
...(params.assignees ? { assignees: params.assignees } : {}),
}
);
const data = r.data as GiteaIssue;
log.info(`» created issue #${data.number}`);
return {
success: true, number: data.number, url: data.html_url, title: data.title, state: data.state,
labels: data.labels?.map((l) => l.name).filter((n): n is string => n !== undefined),
assignees: data.assignees?.map((a) => a.login).filter((n): n is string => n !== undefined),
};
}),
});
}