Skip to content
Open
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
82 changes: 82 additions & 0 deletions apps/server/src/pullRequest/GiteaPullRequestApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,51 @@ layer("GiteaPullRequestApi", (it) => {
}),
);

it.effect("decodes nullable tracking summaries when explicitly requested", () =>
Effect.gen(function* () {
mockedRequest.mockReturnValueOnce(
Effect.succeed(
response(
rawPullRequest(7, {
review_decision: "approved",
checks_state: "passing",
}),
),
),
);
mockedRequest.mockReturnValueOnce(
Effect.succeed(
response(
rawPullRequest(8, {
review_decision: null,
checks_state: null,
}),
),
),
);
const api = yield* GiteaPullRequestApi.make;
const pullRequest = yield* api.getPullRequest({
host: "forge.example.test",
repository: "acme/web",
number: 7,
includeTracking: true,
});

expect(pullRequest.reviewDecision).toBe("approved");
expect(pullRequest.checksState).toBe("passing");
expect(callAt(0).path).toBe("/repos/acme/web/pulls/7?include_tracking=true");

const nullablePullRequest = yield* api.getPullRequest({
host: "forge.example.test",
repository: "acme/web",
number: 8,
includeTracking: true,
});
expect(nullablePullRequest.reviewDecision).toBeNull();
expect(nullablePullRequest.checksState).toBeNull();
}),
);

it.effect("keeps merged and closed pull requests distinct and counts malformed rows", () =>
Effect.gen(function* () {
mockedRequest.mockReturnValueOnce(
Expand Down Expand Up @@ -329,6 +374,7 @@ layer("GiteaPullRequestApi", (it) => {
involvement: "all",
viewer: "reviewer",
limit: 2,
includeTracking: true,
});

expect(page.items.map((item) => [item.number, item.state])).toEqual([
Expand All @@ -339,6 +385,7 @@ layer("GiteaPullRequestApi", (it) => {
assert.isFalse(page.truncated);
expect(callAt(0).path).toContain("state=closed");
expect(callAt(0).path).toContain("sort=recentupdate");
expect(callAt(0).path).toContain("include_tracking=true");
}),
);

Expand Down Expand Up @@ -435,6 +482,41 @@ layer("GiteaPullRequestApi", (it) => {
}),
);

it.effect("passes tracking opt-in through native search and pull hydration", () =>
Effect.gen(function* () {
mockedRequest
.mockReturnValueOnce(Effect.succeed(response([{ number: 7 }])))
.mockReturnValueOnce(
Effect.succeed(
response(
rawPullRequest(7, {
review_decision: "review-required",
checks_state: "failing",
}),
),
),
);
const api = yield* GiteaPullRequestApi.make;
const page = yield* api.listPullRequests({
host: "forge.example.test",
repository: "acme/web",
state: "open",
involvement: "all",
viewer: "reviewer",
limit: 1,
query: "needs review",
includeTracking: true,
});

expect(page.items[0]).toMatchObject({
reviewDecision: "review-required",
checksState: "failing",
});
expect(callAt(0).path).toContain("include_tracking=true");
expect(callAt(1).path).toBe("/repos/acme/web/pulls/7?include_tracking=true");
}),
);

it.effect("returns a page-boundary search match without requesting the page after the cap", () =>
Effect.gen(function* () {
mockedRequest.mockImplementation((request) => {
Expand Down
21 changes: 20 additions & 1 deletion apps/server/src/pullRequest/GiteaPullRequestApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import type {
PullRequestMergeability,
PullRequestReaction,
PullRequestReactionContent,
PullRequestChecksState,
PullRequestReviewCommentDraft,
PullRequestReviewDecision,
PullRequestReviewThread,
PullRequestReviewVerdict,
PullRequestReviewerCandidateList,
Expand Down Expand Up @@ -91,6 +93,10 @@ const RawPullRequest = Schema.Struct({
merged: Schema.optional(Schema.Boolean),
mergeable: Schema.optional(Schema.NullOr(Schema.Boolean)),
draft: Schema.optional(Schema.Boolean),
review_decision: Schema.optional(
Schema.NullOr(Schema.Literals(["approved", "changes-requested", "review-required"])),
),
checks_state: Schema.optional(Schema.NullOr(Schema.Literals(["passing", "failing", "pending"]))),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
auto_merge_enabled: Schema.optional(Schema.NullOr(Schema.Boolean)),
auto_merge_method: Schema.optional(Schema.NullOr(Schema.String)),
html_url: Schema.String,
Expand Down Expand Up @@ -236,6 +242,8 @@ export interface GiteaPullRequest {
readonly commentCount: number;
readonly autoMergeEnabled?: boolean;
readonly autoMergeMethod?: PullRequestMergeMethod;
readonly reviewDecision?: PullRequestReviewDecision | null;
readonly checksState?: PullRequestChecksState | null;
}

export interface GiteaRepositoryAccess {
Expand Down Expand Up @@ -329,6 +337,8 @@ function pullRequest(value: RawPullRequest): GiteaPullRequest | null {
...(["merge", "squash", "rebase"].includes(value.auto_merge_method ?? "")
? { autoMergeMethod: value.auto_merge_method as PullRequestMergeMethod }
: {}),
...(value.review_decision === undefined ? {} : { reviewDecision: value.review_decision }),
...(value.checks_state === undefined ? {} : { checksState: value.checks_state }),
};
}

Expand Down Expand Up @@ -465,6 +475,7 @@ export class GiteaPullRequestApi extends Context.Service<
readonly limit: number;
readonly query?: string;
readonly cursor?: ProviderListCursor;
readonly includeTracking?: boolean;
}) => Effect.Effect<
{
items: ReadonlyArray<GiteaPullRequest>;
Expand All @@ -477,6 +488,7 @@ export class GiteaPullRequestApi extends Context.Service<
host: string;
repository: string;
number: number;
includeTracking?: boolean;
}) => Effect.Effect<GiteaPullRequest, GiteaPullRequestApiError>;
readonly getRepositoryAccess: (input: {
host: string;
Expand Down Expand Up @@ -706,14 +718,17 @@ export const make = Effect.gen(function* () {
host: string;
repository: string;
number: number;
includeTracking?: boolean;
}) {
const operation = "getPullRequest";
const response = yield* request({
operation,
host: input.host,
repository: input.repository,
method: "GET",
path: `${basePath(input.repository)}/pulls/${input.number}`,
path: query(`${basePath(input.repository)}/pulls/${input.number}`, {
include_tracking: input.includeTracking === true ? "true" : undefined,
}),
});
const raw = yield* decode(operation, RawPullRequest, response);
const mapped = pullRequest(raw);
Expand Down Expand Up @@ -823,6 +838,7 @@ export const make = Effect.gen(function* () {
readonly limit: number;
readonly query: string;
readonly cursor?: ProviderListCursor;
readonly includeTracking?: boolean;
}) {
const wanted = Math.max(1, input.limit);
const delivered = input.cursor?.delivered ?? 0;
Expand All @@ -835,6 +851,7 @@ export const make = Effect.gen(function* () {
viewer: input.viewer,
page,
limit: PAGE_SIZE,
includeTracking: input.includeTracking,
});
let rowsSeen = 0;
let rowsSkipped = 0;
Expand Down Expand Up @@ -869,6 +886,7 @@ export const make = Effect.gen(function* () {
host: input.host,
repository: input.repository,
number,
includeTracking: input.includeTracking,
});
},
{ concurrency: SEARCH_HYDRATION_CONCURRENCY },
Expand Down Expand Up @@ -987,6 +1005,7 @@ export const make = Effect.gen(function* () {
sort: "recentupdate",
page,
limit: PAGE_SIZE,
include_tracking: input.includeTracking === true ? "true" : undefined,
...(input.involvement === "authored" ? { poster: input.viewer } : {}),
});
let rowsSeen = 0;
Expand Down
47 changes: 46 additions & 1 deletion apps/server/src/pullRequest/GiteaPullRequestProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,55 @@ import { describe, expect, it } from "@effect/vitest";

import {
giteaBaseComparison,
giteaToChangeRequest,
giteaProviderFailure,
giteaViewerPermissions,
} from "./GiteaPullRequestProvider.ts";
import { GiteaPullRequestApiError } from "./GiteaPullRequestApi.ts";
import { GiteaPullRequestApiError, type GiteaPullRequest } from "./GiteaPullRequestApi.ts";

const trackedPullRequest: GiteaPullRequest = {
number: 7,
title: "Tracking summary",
body: "",
url: "https://forge.example.test/acme/web/pulls/7",
author: null,
headBranch: "feature",
headSha: "head-sha",
headRepositoryNameWithOwner: "acme/web",
baseBranch: "main",
baseSha: "base-sha",
mergeBaseSha: "base-sha",
state: "open",
isDraft: false,
mergeability: "mergeable",
additions: 1,
deletions: 1,
changedFiles: 1,
createdAt: "2026-09-04T00:00:00.000Z",
updatedAt: "2026-09-04T00:00:00.000Z",
mergedAt: null,
closedAt: null,
reviewRequestLogins: [],
reviewers: [],
labels: [],
commentCount: 0,
reviewDecision: "approved",
checksState: "failing",
};

it("maps Gitea tracking summaries into the neutral change request", () => {
expect(giteaToChangeRequest(trackedPullRequest)).toMatchObject({
reviewDecision: "approved",
checksState: "failing",
});
expect(
giteaToChangeRequest({
...trackedPullRequest,
reviewDecision: null,
checksState: null,
}),
).toMatchObject({ reviewDecision: null, checksState: null });
});

describe("giteaViewerPermissions", () => {
it("offers workflow approval only when the server supports it and the viewer can write", () => {
Expand Down
15 changes: 11 additions & 4 deletions apps/server/src/pullRequest/GiteaPullRequestProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export function giteaBaseComparison(
return pullRequest.baseSha === pullRequest.mergeBaseSha ? "up-to-date" : "behind";
}

function toChangeRequest(pullRequest: GiteaPullRequestApi.GiteaPullRequest): ProviderChangeRequest {
export function giteaToChangeRequest(
pullRequest: GiteaPullRequestApi.GiteaPullRequest,
): ProviderChangeRequest {
return {
number: pullRequest.number,
title: pullRequest.title,
Expand All @@ -111,6 +113,10 @@ function toChangeRequest(pullRequest: GiteaPullRequestApi.GiteaPullRequest): Pro
updatedAt: pullRequest.updatedAt,
reviewRequestLogins: pullRequest.reviewRequestLogins,
labels: pullRequest.labels,
...(pullRequest.reviewDecision === undefined
? {}
: { reviewDecision: pullRequest.reviewDecision }),
...(pullRequest.checksState === undefined ? {} : { checksState: pullRequest.checksState }),
};
}

Expand Down Expand Up @@ -160,13 +166,14 @@ export const make = Effect.gen(function* () {
involvement: input.involvement,
viewer: input.viewer,
limit: input.limit,
includeTracking: true,
...(input.query === undefined ? {} : { query: input.query }),
...(input.cursor === undefined ? {} : { cursor: input.cursor }),
})
.pipe(
Effect.mapError(fail("listChangeRequests")),
Effect.map((page) => ({
items: page.items.map(toChangeRequest),
items: page.items.map(giteaToChangeRequest),
truncated: page.truncated,
cursorAdvance: page.consumed,
continues: true,
Expand All @@ -176,7 +183,7 @@ export const make = Effect.gen(function* () {
getChangeRequest: (input) =>
Effect.all(
[
api.getPullRequest(input),
api.getPullRequest({ ...input, includeTracking: true }),
api.getRepositoryAccess(input),
api.getViewer(),
api.getAutoMergeEnabled(input),
Expand All @@ -190,7 +197,7 @@ export const make = Effect.gen(function* () {
api.listChecks({ ...input, sha: pullRequest.headSha }).pipe(
Effect.orElseSucceed(() => []),
Effect.map((checks): ProviderChangeRequestDetail => ({
...toChangeRequest(pullRequest),
...giteaToChangeRequest(pullRequest),
body: pullRequest.body,
changedFiles: pullRequest.changedFiles,
mergedAt: pullRequest.mergedAt,
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/pullRequest/GiteaSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ export function giteaSearchPath(input: {
readonly viewer: string;
readonly page: number;
readonly limit: number;
readonly includeTracking?: boolean;
}): string {
const search = new URLSearchParams({
type: "pulls",
q: input.query,
state: endpointState(input.state),
page: String(input.page),
limit: String(input.limit),
...(input.includeTracking === true ? { include_tracking: "true" } : {}),
...(input.involvement === "authored" ? { created_by: input.viewer } : {}),
});
return `${input.repositoryPath}/issues?${search}`;
Expand Down