Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions packages/shared/pr-gitlab.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, expect, test } from "bun:test";
import { parsePaginatedArray } from "./pr-gitlab";

describe("parsePaginatedArray", () => {
test("parses a single-page array", () => {
const stdout = JSON.stringify([{ a: 1 }, { a: 2 }]);
expect(parsePaginatedArray<{ a: number }>(stdout)).toEqual([{ a: 1 }, { a: 2 }]);
});

test("merges adjacent JSON arrays from --paginate output", () => {
const stdout = JSON.stringify([{ a: 1 }]) + JSON.stringify([{ a: 2 }, { a: 3 }]);
expect(parsePaginatedArray<{ a: number }>(stdout)).toEqual([
{ a: 1 },
{ a: 2 },
{ a: 3 },
]);
});

test("merges three or more pages with whitespace between them", () => {
const stdout = [
JSON.stringify([1, 2]),
JSON.stringify([3, 4]),
JSON.stringify([5]),
].join("\n");
expect(parsePaginatedArray<number>(stdout)).toEqual([1, 2, 3, 4, 5]);
});

test("handles strings containing brackets without splitting prematurely", () => {
// Diff content frequently contains `][` inside JSON strings — must not be
// confused with a page boundary.
const page1 = [{ diff: "before][after", new_path: "a" }];
const page2 = [{ diff: "second", new_path: "b" }];
const stdout = JSON.stringify(page1) + JSON.stringify(page2);
expect(parsePaginatedArray(stdout)).toEqual([...page1, ...page2]);
});

test("handles escaped quotes inside strings", () => {
const page1 = [{ diff: 'has \\"quote\\" and ] bracket', new_path: "a" }];
const page2 = [{ diff: "second", new_path: "b" }];
const stdout = JSON.stringify(page1) + JSON.stringify(page2);
expect(parsePaginatedArray(stdout)).toEqual([...page1, ...page2]);
});

test("returns empty array for empty input", () => {
expect(parsePaginatedArray("")).toEqual([]);
expect(parsePaginatedArray(" \n")).toEqual([]);
});

test("handles empty pages mixed with non-empty ones", () => {
const stdout = "[]" + JSON.stringify([{ a: 1 }]) + "[]";
expect(parsePaginatedArray<{ a: number }>(stdout)).toEqual([{ a: 1 }]);
});
});
58 changes: 55 additions & 3 deletions packages/shared/pr-gitlab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,61 @@ interface GitLabDiffEntry {
renamed_file: boolean;
}

/** Parse JSON array from glab api --paginate output (already merged by glab) */
function parsePaginatedArray<T>(stdout: string): T[] {
return JSON.parse(stdout) as T[];
/**
* Parse output of `glab api --paginate`.
*
* glab concatenates pages as adjacent JSON arrays (`[...][...]`) which is not
* valid JSON. Walk the output, split it into top-level arrays, and merge them.
* Single-page output (the common case) round-trips through the same path.
*/
export function parsePaginatedArray<T>(stdout: string): T[] {
const trimmed = stdout.trim();
if (!trimmed) return [];

const slices: string[] = [];
let depth = 0;
let inString = false;
let escape = false;
let start = -1;

for (let i = 0; i < trimmed.length; i++) {
const c = trimmed[i];
if (inString) {
if (escape) {
escape = false;
} else if (c === "\\") {
escape = true;
} else if (c === '"') {
inString = false;
}
continue;
}
if (c === '"') {
inString = true;
continue;
}
if (c === "[" || c === "{") {
if (depth === 0 && c === "[") start = i;
depth++;
} else if (c === "]" || c === "}") {
depth--;
if (depth === 0 && c === "]" && start !== -1) {
slices.push(trimmed.slice(start, i + 1));
start = -1;
}
}
}

if (slices.length === 0) {
return JSON.parse(trimmed) as T[];
}

const merged: T[] = [];
for (const slice of slices) {
const page = JSON.parse(slice) as T[];
if (Array.isArray(page)) merged.push(...page);
}
return merged;
}

/**
Expand Down
Loading