Files

47 lines
1.9 KiB
TypeScript

import { type } from "arktype";
import type { ToolContext } from "./server.ts";
import { execute, tool } from "./shared.ts";
interface GiteaPull {
number: number; html_url: string; title: string; body?: string | null;
state: string; draft?: boolean; merged?: boolean; allow_maintainer_edit?: boolean;
head?: { sha: string; ref: string; repo?: { full_name: string } | null };
base?: { ref: string; repo?: { full_name: string } };
user?: { login: string }; assignees?: Array<{ login: string }>;
labels?: Array<string | { name?: string }>;
}
export const PullRequestInfo = type({
pull_number: type.number.describe("The pull request number to fetch"),
});
export function PullRequestInfoTool(ctx: ToolContext) {
return tool({
name: "get_pull_request",
description:
"Retrieve PR metadata (title, body, state, branches, author, labels). " +
"Example: `get_pull_request({ pull_number: 1234 })`. To checkout a PR branch locally, use checkout_pr instead.",
parameters: PullRequestInfo,
execute: execute(async ({ pull_number }) => {
const r = await ctx.gitea.request(
"GET /repos/{owner}/{repo}/pulls/{index}",
{ owner: ctx.repo.owner, repo: ctx.repo.name, index: pull_number }
);
const data = r.data as GiteaPull;
const isFork = data.head?.repo?.full_name !== data.base?.repo?.full_name;
return {
number: data.number, url: data.html_url, title: data.title, body: data.body,
state: data.state, draft: data.draft, merged: data.merged,
maintainerCanModify: data.allow_maintainer_edit,
base: data.base?.ref, head: data.head?.ref, isFork,
author: data.user?.login,
assignees: data.assignees?.map((a) => a.login).filter((n): n is string => n !== undefined),
labels: data.labels
?.map((l) => (typeof l === "string" ? l : l.name))
.filter((n): n is string => n !== undefined),
closingIssues: [],
};
}),
});
}