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
15 changes: 13 additions & 2 deletions apps/server/src/pullRequest/PullRequestService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3614,7 +3614,7 @@ it.effect("hands a host's own resume position back on the next read", () =>
}),
);

it.effect("judges the review filter only on a host that summarises its reviews", () =>
it.effect("judges review and checks filters only on a host that summarises them", () =>
Effect.gen(function* () {
const service = yield* makeService({
projects: [
Expand All @@ -3632,10 +3632,15 @@ it.effect("judges the review filter only on a host that summarises its reviews",
listChangeRequests: () =>
Effect.succeed({
items: [
{ ...changeRequest(1, "2026-07-02T00:00:00Z"), reviewDecision: null },
{
...changeRequest(1, "2026-07-02T00:00:00Z"),
reviewDecision: null,
checksState: "failing" as const,
},
{
...changeRequest(2, "2026-07-02T00:00:00Z"),
reviewDecision: "approved" as const,
checksState: null,
},
],
truncated: false,
Expand All @@ -3658,6 +3663,12 @@ it.effect("judges the review filter only on a host that summarises its reviews",

const approved = yield* service.list({ state: "open", filters: { review: "approved" } });
assert.deepStrictEqual(approved.entries.map((entry) => entry.number).toSorted(), [2, 3]);

const failing = yield* service.list({ state: "open", filters: { checks: "failing" } });
assert.deepStrictEqual(failing.entries.map((entry) => entry.number).toSorted(), [1, 3]);

const passing = yield* service.list({ state: "open", filters: { checks: "passing" } });
assert.deepStrictEqual(passing.entries.map((entry) => entry.number).toSorted(), [3]);
}),
);

Expand Down
4 changes: 3 additions & 1 deletion apps/server/src/pullRequest/PullRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,6 @@ export const make = Effect.gen(function* () {
{ concurrency: REPOSITORY_CONCURRENCY },
);

// Only the host can filter checks; listed rows have no check state.
const matchesRowFilters = (
item: ProviderChangeRequest,
filters: PullRequestListFilters | undefined,
Expand All @@ -1213,6 +1212,9 @@ export const make = Effect.gen(function* () {
(filters.review === "none"
? item.reviewDecision === null
: item.reviewDecision === filters.review)) &&
(filters.checks === undefined ||
item.checksState === undefined ||
item.checksState === filters.checks) &&
(filters.labels === undefined || filters.labels.every((group) => group.some(holds))) &&
(filters.excludedLabels === undefined || !filters.excludedLabels.some(holds)) &&
(filters.author === undefined ||
Expand Down
230 changes: 227 additions & 3 deletions packages/source-control-gitlab/src/server/GitLabPullRequestCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,40 @@ function callAt(index: number) {
return call[0];
}

function graphqlListPage(
numbers: ReadonlyArray<number>,
pageInfo: { readonly hasNextPage: boolean; readonly endCursor: string | null },
): string {
return JSON.stringify({
data: {
metadata: { enterprise: true },
project: {
mergeRequests: {
pageInfo,
nodes: numbers.map((number) => ({
iid: String(number),
title: `Merge request ${number}`,
webUrl: `https://gitlab.com/acme/web/-/merge_requests/${number}`,
createdAt: "2026-07-01T00:00:00Z",
updatedAt: "2026-07-02T00:00:00Z",
sourceBranch: "feat/page",
targetBranch: "main",
approved: true,
approvedBy: { nodes: [{ username: "julius" }] },
})),
},
},
},
});
}

function graphqlRequestOf(index: number) {
return JSON.parse(callAt(index).stdin ?? "{}") as {
readonly query: string;
readonly variables: Record<string, unknown>;
};
}

afterEach(() => {
mockedExecute.mockReset();
});
Expand Down Expand Up @@ -144,16 +178,16 @@ layer("GitLabPullRequestCli.layer", (it) => {
});
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
const reference = { cwd: "/w", repository: "acme/web", host: "gitlab.internal" };
expect(yield* cli.getReviewDecisions({ ...reference, numbers: [] })).toEqual(new Map());
expect(yield* cli.getListStates({ ...reference, numbers: [] })).toEqual(new Map());
expect(mockedExecute).not.toHaveBeenCalled();
const decisions = yield* cli.getReviewDecisions({
const decisions = yield* cli.getListStates({
...reference,
numbers: Array.from({ length: 101 }, (_, index) => index + 1),
});
expect(callAt(0).host).toBe(reference.host);
expect(callAt(1).host).toBe(reference.host);
expect(decisions.size).toBe(101);
expect(decisions.get(101)).toBe("approved");
expect(decisions.get(101)).toEqual({ reviewDecision: "approved", checksState: null });
expect(mockedExecute).toHaveBeenCalledTimes(2);
expect(
[0, 1].map((index) => JSON.parse(callAt(index).stdin ?? "{}").variables.iids.length),
Expand Down Expand Up @@ -238,6 +272,7 @@ layer("GitLabPullRequestCli.layer", (it) => {
excludedLabels: ["wontfix", "stale", "any"],
author: "@me",
draft: "only",
review: "approved",
},
});

Expand All @@ -246,6 +281,195 @@ layer("GitLabPullRequestCli.layer", (it) => {
expect(params.get("not[labels]")).toBe("wontfix,stale");
expect(params.get("author_username")).toBe("bilal");
expect(params.get("wip")).toBe("yes");
expect(params.get("approved_by_usernames[]")).toBe("Any");
}),
);

it.effect("narrows by approval only where GitLab returns every row the decision keeps", () =>
Effect.gen(function* () {
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
const reviews = ["none", "review-required", "changes-requested"] as const;
for (const review of reviews) {
mockedExecute.mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 1))));
yield* cli.listMergeRequests({
cwd: "/w",
repository: "acme/web",
state: "open",
involvement: "all",
viewer: "bilal",
limit: 10,
filters: { review },
});
}

const sent = reviews.map((_, call) => {
const params = new URLSearchParams((argsOfCall(call)[1] ?? "").split("?")[1]);
return [params.get("approved_by_ids[]"), params.get("approved_by_usernames[]")];
});
expect(sent).toEqual([
["None", null],
[null, null],
[null, null],
]);
}),
);

it.effect(
"lists through GraphQL with the filters GitLab narrows there, declaring only those",
() =>
Effect.gen(function* () {
mockedExecute.mockReturnValue(
Effect.succeed(output(graphqlListPage([1], { hasNextPage: false, endCursor: null }))),
);
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;

yield* cli.listMergeRequests({
cwd: "/w",
repository: "acme/web",
state: "open",
involvement: "all",
viewer: "bilal",
limit: 10,
query: " page ",
graphql: true,
cursor: {
updatedBefore: "2026-07-03T00:00:00Z",
delivered: 10,
resumeAfter: "eyJpZCI6IjEifQ",
},
filters: {
labels: [["bug"], ["size:S", "size:XS"]],
excludedLabels: ["wontfix"],
author: "octocat",
draft: "hide",
review: "changes-requested",
},
});

const { query, variables } = graphqlRequestOf(0);
expect(variables).toEqual({
fullPath: "acme/web",
first: 10,
after: "eyJpZCI6IjEifQ",
state: "opened",
authorUsername: "octocat",
search: "page",
labels: ["bug"],
draft: false,
reviewStates: ["REQUESTED_CHANGES"],
notLabels: ["wontfix"],
});
expect(query).toContain("reviewStates: $reviewStates");
expect(query).toContain("not: { labels: $notLabels }");
expect(query).not.toContain("$approvedBy");

// Beside a reviewer, GitLab would match only that reviewer's own state.
yield* cli.listMergeRequests({
cwd: "/w",
repository: "acme/web",
state: "open",
involvement: "reviewing",
viewer: "bilal",
limit: 10,
graphql: true,
filters: { review: "changes-requested" },
});
expect(graphqlRequestOf(1).variables).toMatchObject({ reviewerUsername: "bilal" });
expect(graphqlRequestOf(1).variables).not.toHaveProperty("reviewStates");
}),
);

it.effect("walks GraphQL pages by GitLab's cursor and hands the last one back", () =>
Effect.gen(function* () {
mockedExecute
.mockReturnValueOnce(
Effect.succeed(output(graphqlListPage([1, 2], { hasNextPage: true, endCursor: "c1" }))),
)
.mockReturnValueOnce(
Effect.succeed(output(graphqlListPage([3], { hasNextPage: true, endCursor: "c2" }))),
);
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;

const batch = yield* cli.listMergeRequests({
cwd: "/w",
repository: "acme/web",
state: "open",
involvement: "all",
viewer: "bilal",
limit: 3,
graphql: true,
filters: { review: "approved" },
});

expect(batch).toMatchObject({ truncated: true, cursorAdvance: 3, resumeAfter: "c2" });
expect(batch.items.map((item) => item.number)).toEqual([1, 2, 3]);
expect(batch.listStates?.get(3)).toEqual({ reviewDecision: "approved", checksState: null });
expect([0, 1].map((call) => graphqlRequestOf(call).variables)).toMatchObject([
{ first: 3, after: null, approvedBy: ["Any"] },
{ first: 1, after: "c1" },
]);
}),
);

it.effect("reads REST for a REST cursor, and when a first GraphQL read or command fails", () =>
Effect.gen(function* () {
mockedExecute
.mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 1))))
.mockReturnValueOnce(
Effect.succeed(
output(JSON.stringify({ errors: [{ message: "Field 'search' doesn't exist" }] })),
),
)
.mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 1))))
.mockReturnValueOnce(Effect.succeed(output(JSON.stringify({ errors: [] }))))
.mockReturnValueOnce(
Effect.fail(
new GitLabCli.GitLabCliCommandError({
operation: "execute",
command: "glab",
cwd: "/w",
cause: new Error("502 Bad Gateway"),
}),
),
)
.mockReturnValueOnce(Effect.succeed(output(mergeRequests(1, 2))));
const cli = yield* GitLabPullRequestCli.GitLabPullRequestCli;
const list = (cursor?: { updatedBefore: string; delivered: number; resumeAfter?: string }) =>
cli.listMergeRequests({
cwd: "/w",
repository: "acme/web",
state: "open",
involvement: "all",
viewer: "bilal",
limit: 10,
// A cursor, not the host's current answer, picks the API.
graphql: cursor?.resumeAfter === undefined,
...(cursor === undefined ? {} : { cursor }),
});

yield* list({ updatedBefore: "2026-07-03T00:00:00Z", delivered: 10 });
const fellBack = yield* list();
// A resumed GraphQL read stays on GraphQL and fails, since REST would read `delivered` as an offset.
const resumed = yield* Effect.result(
list({ updatedBefore: "2026-07-03T00:00:00Z", delivered: 10, resumeAfter: "c1" }),
);
const commandFailed = yield* list();

expect(fellBack.items.map((item) => item.number)).toEqual([1]);
expect(fellBack).not.toHaveProperty("resumeAfter");
expect(mockedExecute.mock.calls.map(([request]) => request.args[1]?.split("?")[0])).toEqual([
"projects/acme%2Fweb/merge_requests",
"graphql",
"projects/acme%2Fweb/merge_requests",
"graphql",
"graphql",
"projects/acme%2Fweb/merge_requests",
]);
expect(commandFailed.items.map((item) => item.number)).toEqual([2]);
expect(resumed).toMatchObject({
_tag: "Failure",
failure: { _tag: "GitLabMergeRequestReadError" },
});
}),
);

Expand Down
Loading
Loading