fix: issues with pagination not resolving correct url templates

This commit is contained in:
2026-05-31 03:18:07 -05:00
parent 2aca1a3aa3
commit 0438688e32
14 changed files with 287 additions and 467 deletions
+27 -24
View File
@@ -136,15 +136,13 @@ export async function fetchAndFormatPrDiff(
ctx: ToolContext,
pullNumber: number
): Promise<FetchAndFormatPrDiffResult> {
const raw = await ctx.gitea.paginate(ctx.gitea.rest.repository.repoGetPullRequestFiles, {
owner: ctx.repo.owner,
repo: ctx.repo.name,
index: pullNumber,
});
// ChangedFile from the SDK omits `patch`; Gitea does return it, so we map here.
const files: DiffFile[] = raw.map((f) => ({
const r = await ctx.gitea.request(
"GET /repos/{owner}/{repo}/pulls/{index}/files",
{ owner: ctx.repo.owner, repo: ctx.repo.name, index: pullNumber, limit: 50 }
);
const files: DiffFile[] = (r.data as Array<{ filename?: string; patch?: string }>).map((f) => ({
filename: f.filename,
patch: (f as unknown as { patch?: string }).patch,
patch: f.patch,
}));
return { ...formatFilesWithLineNumbers(files), files };
}
@@ -190,7 +188,8 @@ type CheckoutPrBranchParams = {
async function abortIfPullRequestMoved(args: { gitea: Gitea; owner: string; repo: string; pr: PrData }): Promise<void> {
try {
const data = (await args.gitea.rest.repository.repoGetPullRequest({ owner: args.owner, repo: args.repo, index: args.pr.number })).data;
const r = await args.gitea.request("GET /repos/{owner}/{repo}/pulls/{index}", { owner: args.owner, repo: args.repo, index: args.pr.number });
const data = r.data as { state?: string; head?: { sha?: string } };
if (data.state !== "open" || data.head?.sha !== args.pr.headSha) {
throw new Error(`PR #${args.pr.number} is no longer in the state it was at dispatch. Aborting.`);
}
@@ -203,15 +202,14 @@ async function abortIfPullRequestMoved(args: { gitea: Gitea; owner: string; repo
type CreateTempBranchParams = { gitea: Gitea; owner: string; repo: string; branchName: string; sha: string };
async function createTempBranch(params: CreateTempBranchParams) {
await params.gitea.rest.repository.repoCreateBranch({
owner: params.owner,
repo: params.repo,
body: { new_branch_name: params.branchName, old_ref_name: params.sha },
await params.gitea.request("POST /repos/{owner}/{repo}/branches", {
owner: params.owner, repo: params.repo,
new_branch_name: params.branchName, old_ref_name: params.sha,
});
return {
async [Symbol.asyncDispose]() {
try {
await params.gitea.rest.repository.repoDeleteBranch({ owner: params.owner, repo: params.repo, branch: params.branchName });
await params.gitea.request("DELETE /repos/{owner}/{repo}/branches/{branch}", { owner: params.owner, repo: params.repo, branch: params.branchName });
log.debug(`» deleted temp branch ${params.branchName}`);
} catch (e) {
log.debug(`» failed to delete temp branch ${params.branchName}: ${e instanceof Error ? e.message : String(e)}`);
@@ -300,13 +298,13 @@ export async function checkoutPrBranch(
let deepenDepth = 0;
try {
const [prComp, beforeComp] = await Promise.all([
gitea.rest.repository.repoCompareDiff({ owner, repo: name, basehead: `${pr.baseRef}...${toolState.checkoutSha}` }),
gitea.request("GET /repos/{owner}/{repo}/compare/{basehead}", { owner, repo: name, basehead: `${pr.baseRef}...${toolState.checkoutSha}` }),
beforeSha && beforeShaReachable
? gitea.rest.repository.repoCompareDiff({ owner, repo: name, basehead: `${pr.baseRef}...${beforeSha}` })
? gitea.request("GET /repos/{owner}/{repo}/compare/{basehead}", { owner, repo: name, basehead: `${pr.baseRef}...${beforeSha}` })
: undefined,
]);
const prTotal = prComp.data.total_commits ?? 0;
const beforeTotal = beforeComp?.data.total_commits ?? 0;
const prTotal = (prComp.data as { total_commits?: number }).total_commits ?? 0;
const beforeTotal = (beforeComp?.data as { total_commits?: number } | undefined)?.total_commits ?? 0;
deepenDepth = Math.max(prTotal, beforeTotal) + 10;
log.debug(`» compare: PR=${prTotal}, before=${beforeTotal}, deepen=${deepenDepth}`);
} catch {
@@ -358,12 +356,17 @@ function describeHead(h: InitialHead): string {
export function CheckoutPrTool(ctx: ToolContext) {
const runCheckout = async (pull_number: number): Promise<CheckoutPrResult> => {
const prResult = await ctx.gitea.rest.repository.repoGetPullRequest({
owner: ctx.repo.owner,
repo: ctx.repo.name,
index: pull_number,
});
const prData = prResult.data;
const prResult = await ctx.gitea.request(
"GET /repos/{owner}/{repo}/pulls/{index}",
{ owner: ctx.repo.owner, repo: ctx.repo.name, index: pull_number }
);
const prData = prResult.data as {
number?: number; title?: string; body?: string | null; html_url?: string;
merged?: boolean; allow_maintainer_edit?: boolean;
head?: { sha?: string; ref?: string; repo?: { full_name?: string } | null };
base?: { ref?: string; repo?: { full_name?: string } };
state?: string;
};
const headRepo = prData.head?.repo;
if (!headRepo) throw new Error(`PR #${pull_number} source repository was deleted`);