fix: get diffs properly
This commit is contained in:
+29
-8
@@ -27,6 +27,28 @@ export type DiffFile = {
|
|||||||
patch?: string | undefined;
|
patch?: string | undefined;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse raw `git diff` output into per-file DiffFile objects.
|
||||||
|
* Used as a fallback when the Gitea API doesn't return patch data.
|
||||||
|
*/
|
||||||
|
export function parseDiffToFiles(rawDiff: string): DiffFile[] {
|
||||||
|
const files: DiffFile[] = [];
|
||||||
|
const parts = rawDiff.split(/^(?=diff --git )/m);
|
||||||
|
for (const part of parts) {
|
||||||
|
if (!part.trim()) continue;
|
||||||
|
const headerMatch = part.match(/^diff --git a\/.+ b\/(.+)\n/);
|
||||||
|
if (!headerMatch) continue;
|
||||||
|
const filename = headerMatch[1].trim();
|
||||||
|
const patchStart = part.indexOf("\n@@");
|
||||||
|
if (patchStart === -1) {
|
||||||
|
files.push({ filename });
|
||||||
|
} else {
|
||||||
|
files.push({ filename, patch: part.slice(patchStart + 1) });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return files;
|
||||||
|
}
|
||||||
|
|
||||||
export function formatFilesWithLineNumbers(files: DiffFile[]): FormatFilesResult {
|
export function formatFilesWithLineNumbers(files: DiffFile[]): FormatFilesResult {
|
||||||
const output: string[] = [];
|
const output: string[] = [];
|
||||||
const tocEntries: Array<{ filename: string; startLine: number; endLine: number }> = [];
|
const tocEntries: Array<{ filename: string; startLine: number; endLine: number }> = [];
|
||||||
@@ -136,14 +158,13 @@ export async function fetchAndFormatPrDiff(
|
|||||||
ctx: ToolContext,
|
ctx: ToolContext,
|
||||||
pullNumber: number
|
pullNumber: number
|
||||||
): Promise<FetchAndFormatPrDiffResult> {
|
): Promise<FetchAndFormatPrDiffResult> {
|
||||||
const r = await ctx.gitea.request(
|
const r = await ctx.gitea.rest.repository.repoDownloadPullDiffOrPatch({
|
||||||
"GET /repos/{owner}/{repo}/pulls/{index}/files",
|
owner: ctx.repo.owner,
|
||||||
{ owner: ctx.repo.owner, repo: ctx.repo.name, index: pullNumber, limit: 50 }
|
repo: ctx.repo.name,
|
||||||
);
|
index: pullNumber,
|
||||||
const files: DiffFile[] = (r.data as Array<{ filename?: string; patch?: string }>).map((f) => ({
|
diffType: "diff",
|
||||||
filename: f.filename,
|
});
|
||||||
patch: f.patch,
|
const files = parseDiffToFiles(r.data);
|
||||||
}));
|
|
||||||
return { ...formatFilesWithLineNumbers(files), files };
|
return { ...formatFilesWithLineNumbers(files), files };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-9
@@ -11,6 +11,7 @@ import {
|
|||||||
import { fixDoubleEscapedString } from "../utils/fixDoubleEscapedString.ts";
|
import { fixDoubleEscapedString } from "../utils/fixDoubleEscapedString.ts";
|
||||||
import type { ChangedFileWithPatch } from "../utils/gitea.ts";
|
import type { ChangedFileWithPatch } from "../utils/gitea.ts";
|
||||||
import { retry } from "../utils/retry.ts";
|
import { retry } from "../utils/retry.ts";
|
||||||
|
import { parseDiffToFiles } from "./checkout.ts";
|
||||||
import { deleteProgressComment } from "./comment.ts";
|
import { deleteProgressComment } from "./comment.ts";
|
||||||
import type { ToolContext } from "./server.ts";
|
import type { ToolContext } from "./server.ts";
|
||||||
import { execute, tool } from "./shared.ts";
|
import { execute, tool } from "./shared.ts";
|
||||||
@@ -62,17 +63,16 @@ export async function buildCommentableMap(
|
|||||||
const currentSha = ctx.toolState.checkoutSha;
|
const currentSha = ctx.toolState.checkoutSha;
|
||||||
if (cached && cachedFor === pullNumber && cachedSha && cachedSha === currentSha) return cached;
|
if (cached && cachedFor === pullNumber && cachedSha && cachedSha === currentSha) return cached;
|
||||||
|
|
||||||
const r = await ctx.gitea.request(
|
const r = await ctx.gitea.rest.repository.repoDownloadPullDiffOrPatch({
|
||||||
"GET /repos/{owner}/{repo}/pulls/{index}/files",
|
owner: ctx.repo.owner,
|
||||||
{ owner: ctx.repo.owner, repo: ctx.repo.name, index: pullNumber, limit: 50 }
|
repo: ctx.repo.name,
|
||||||
);
|
index: pullNumber,
|
||||||
const files = r.data as ChangedFileWithPatch[];
|
diffType: "diff",
|
||||||
|
});
|
||||||
|
const files = parseDiffToFiles(r.data);
|
||||||
const map = new Map<string, CommentableLines>();
|
const map = new Map<string, CommentableLines>();
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
if (file.filename) {
|
if (file.filename) map.set(file.filename, commentableLinesForFile(file.patch));
|
||||||
map.set(file.filename, commentableLinesForFile(file.patch));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user