feat(pull-requests): link multiple pull requests to threads (#10839)
This commit is contained in:
1 parent
383cc40f4d
commit
afb84898be
172 files changed
+12063
-914
No files matched your search
@@ -0,0 +1,230 @@
|
||||
import {
|
||||
CommandId,
|
||||
ProjectId,
|
||||
ProviderInstanceId,
|
||||
ThreadId,
|
||||
type GitRunStackedActionResult,
|
||||
type OrchestrationCommand,
|
||||
type OrchestrationProjectShell,
|
||||
type OrchestrationThreadShell,
|
||||
} from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Ref from "effect/Ref";
|
||||
import * as Stream from "effect/Stream";
|
||||
|
||||
import { OrchestrationCommandInvariantError } from "../orchestration/Errors.ts";
|
||||
import {
|
||||
OrchestrationEngineService,
|
||||
type OrchestrationEngineShape,
|
||||
} from "../orchestration/Services/OrchestrationEngine.ts";
|
||||
import { ProjectionSnapshotQuery } from "../orchestration/Services/ProjectionSnapshotQuery.ts";
|
||||
import { createdPullRequestKey, linkCreatedPullRequest } from "./linkCreatedPullRequest.ts";
|
||||
|
||||
const PROJECT_ID = ProjectId.make("project-1");
|
||||
const THREAD_ID = ThreadId.make("thread-1");
|
||||
const commandId = Effect.succeed(CommandId.make("server:pr-created-link:test"));
|
||||
|
||||
const project: OrchestrationProjectShell = {
|
||||
id: PROJECT_ID,
|
||||
title: "Project",
|
||||
workspaceRoot: "/workspace/project",
|
||||
defaultModelSelection: null,
|
||||
scripts: [],
|
||||
repositoryIdentity: {
|
||||
canonicalKey: "github.acme.test/platform/api",
|
||||
locator: {
|
||||
source: "git-remote",
|
||||
remoteName: "origin",
|
||||
remoteUrl: "[email protected]:Platform/API.git",
|
||||
},
|
||||
provider: "github",
|
||||
displayName: "Platform/API",
|
||||
owner: "Platform",
|
||||
name: "API",
|
||||
},
|
||||
createdAt: "2026-08-01T00:00:00.000Z",
|
||||
updatedAt: "2026-08-01T00:00:00.000Z",
|
||||
};
|
||||
|
||||
const thread: OrchestrationThreadShell = {
|
||||
id: THREAD_ID,
|
||||
projectId: PROJECT_ID,
|
||||
title: "Thread",
|
||||
modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" },
|
||||
runtimeMode: "full-access",
|
||||
interactionMode: "default",
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
pullRequests: [],
|
||||
latestTurn: null,
|
||||
createdAt: "2026-08-01T00:00:00.000Z",
|
||||
updatedAt: "2026-08-20T00:00:00.000Z",
|
||||
archivedAt: null,
|
||||
settledOverride: null,
|
||||
settledAt: null,
|
||||
session: null,
|
||||
latestUserMessageAt: "2026-08-20T00:00:00.000Z",
|
||||
hasPendingApprovals: false,
|
||||
hasPendingUserInput: false,
|
||||
hasActionableProposedPlan: false,
|
||||
};
|
||||
|
||||
function prResult(pr: GitRunStackedActionResult["pr"]): Pick<GitRunStackedActionResult, "pr"> {
|
||||
return { pr };
|
||||
}
|
||||
|
||||
const makeDependencies = (
|
||||
dispatch: OrchestrationEngineShape["dispatch"],
|
||||
threadShell: OrchestrationThreadShell | null = thread,
|
||||
) =>
|
||||
Layer.mergeAll(
|
||||
Layer.mock(ProjectionSnapshotQuery)({
|
||||
getThreadShellById: () => Effect.succeed(Option.fromNullishOr(threadShell)),
|
||||
getProjectShellById: () => Effect.succeed(Option.some(project)),
|
||||
}),
|
||||
Layer.mock(OrchestrationEngineService)({
|
||||
readEvents: () => Stream.empty,
|
||||
dispatch,
|
||||
streamDomainEvents: Stream.empty,
|
||||
latestSequence: Effect.succeed(0),
|
||||
}),
|
||||
);
|
||||
|
||||
const recordingDispatch = Effect.fn("recordingDispatch")(function* () {
|
||||
const commands = yield* Ref.make<ReadonlyArray<OrchestrationCommand>>([]);
|
||||
const dispatch: OrchestrationEngineShape["dispatch"] = (command) =>
|
||||
Ref.update(commands, (recorded) => [...recorded, command]).pipe(Effect.as({ sequence: 1 }));
|
||||
return { commands, dispatch };
|
||||
});
|
||||
|
||||
describe("createdPullRequestKey", () => {
|
||||
it("reads host and repository from the URL when it is recognisable", () => {
|
||||
expect(
|
||||
createdPullRequestKey(
|
||||
prResult({
|
||||
status: "created",
|
||||
number: 12,
|
||||
url: "https://github.com/Other/Fork/pull/12",
|
||||
}),
|
||||
project,
|
||||
),
|
||||
).toEqual({
|
||||
host: "github.com",
|
||||
repository: "other/fork",
|
||||
number: 12,
|
||||
url: "https://github.com/Other/Fork/pull/12",
|
||||
});
|
||||
});
|
||||
|
||||
it("falls back to the project's host and repository for an unreadable URL", () => {
|
||||
expect(
|
||||
createdPullRequestKey(
|
||||
prResult({ status: "opened_existing", number: 3, url: "https://ghe.internal/x/3" }),
|
||||
project,
|
||||
),
|
||||
).toEqual({
|
||||
host: "github.acme.test",
|
||||
repository: "platform/api",
|
||||
number: 3,
|
||||
url: "https://ghe.internal/x/3",
|
||||
});
|
||||
expect(
|
||||
createdPullRequestKey(
|
||||
prResult({ status: "created", number: 3, url: "https://ghe.internal/x/3" }),
|
||||
undefined,
|
||||
),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("yields nothing when no pull request came out of the action", () => {
|
||||
expect(
|
||||
createdPullRequestKey(prResult({ status: "skipped_not_requested" }), project),
|
||||
).toBeNull();
|
||||
expect(
|
||||
createdPullRequestKey(
|
||||
prResult({ status: "created", url: "https://github.com/a/b/pull/1" }),
|
||||
project,
|
||||
),
|
||||
).toBeNull();
|
||||
expect(createdPullRequestKey(prResult({ status: "created", number: 1 }), project)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("linkCreatedPullRequest", () => {
|
||||
it.effect("links a created pull request to the thread with source created", () =>
|
||||
Effect.gen(function* () {
|
||||
const { commands, dispatch } = yield* recordingDispatch();
|
||||
yield* linkCreatedPullRequest({
|
||||
threadId: THREAD_ID,
|
||||
result: prResult({
|
||||
status: "created",
|
||||
number: 42,
|
||||
url: "https://github.com/t3tools/t3code/pull/42",
|
||||
}),
|
||||
commandId,
|
||||
}).pipe(Effect.provide(makeDependencies(dispatch)));
|
||||
|
||||
expect(yield* Ref.get(commands)).toEqual([
|
||||
{
|
||||
type: "thread.pull-request.link",
|
||||
commandId: "server:pr-created-link:test",
|
||||
threadId: THREAD_ID,
|
||||
host: "github.com",
|
||||
repository: "t3tools/t3code",
|
||||
number: 42,
|
||||
url: "https://github.com/t3tools/t3code/pull/42",
|
||||
source: "created",
|
||||
},
|
||||
]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("dispatches nothing when the action produced no pull request", () =>
|
||||
Effect.gen(function* () {
|
||||
const { commands, dispatch } = yield* recordingDispatch();
|
||||
const dependencies = makeDependencies(dispatch);
|
||||
yield* linkCreatedPullRequest({
|
||||
threadId: THREAD_ID,
|
||||
result: prResult({ status: "skipped_not_requested" }),
|
||||
commandId,
|
||||
}).pipe(Effect.provide(dependencies));
|
||||
yield* linkCreatedPullRequest({
|
||||
threadId: THREAD_ID,
|
||||
result: prResult({ status: "created", url: "https://github.com/t3tools/t3code/pull/42" }),
|
||||
commandId,
|
||||
}).pipe(Effect.provide(dependencies));
|
||||
|
||||
expect(yield* Ref.get(commands)).toEqual([]);
|
||||
}),
|
||||
);
|
||||
|
||||
it.effect("swallows an already-linked rejection and other dispatch failures", () =>
|
||||
Effect.gen(function* () {
|
||||
const rejecting: OrchestrationEngineShape["dispatch"] = (command) =>
|
||||
Effect.fail(
|
||||
new OrchestrationCommandInvariantError({
|
||||
commandType: command.type,
|
||||
detail: "already linked",
|
||||
}),
|
||||
);
|
||||
const result = prResult({
|
||||
status: "opened_existing",
|
||||
number: 7,
|
||||
url: "https://github.com/t3tools/t3code/pull/7",
|
||||
});
|
||||
yield* linkCreatedPullRequest({ threadId: THREAD_ID, result, commandId }).pipe(
|
||||
Effect.provide(makeDependencies(rejecting)),
|
||||
);
|
||||
yield* linkCreatedPullRequest({ threadId: THREAD_ID, result, commandId }).pipe(
|
||||
Effect.provide(makeDependencies(() => Effect.die(new Error("engine down")))),
|
||||
);
|
||||
// A thread that vanished between the action and the link is not an error either.
|
||||
yield* linkCreatedPullRequest({ threadId: THREAD_ID, result, commandId }).pipe(
|
||||
Effect.provide(makeDependencies(() => Effect.die(new Error("unreachable")), null)),
|
||||
);
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,98 @@
|
||||
import { sourceControlRepositorySelector } from "@t3tools/shared/sourceControl";
|
||||
import {
|
||||
type CommandId,
|
||||
pullRequestHostOf,
|
||||
type GitRunStackedActionResult,
|
||||
type OrchestrationProjectShell,
|
||||
type SourceControlProviderKind,
|
||||
type ThreadId,
|
||||
} from "@t3tools/contracts";
|
||||
import { parseChangeRequestUrl } from "@t3tools/shared/changeRequestUrl";
|
||||
import * as Cause from "effect/Cause";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Option from "effect/Option";
|
||||
|
||||
import * as OrchestrationEngine from "../orchestration/Services/OrchestrationEngine.ts";
|
||||
import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
|
||||
|
||||
export interface CreatedPullRequestKey {
|
||||
readonly host: string;
|
||||
readonly repository: string;
|
||||
readonly number: number;
|
||||
readonly url: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The identity a stacked action's pull request should be linked under, or
|
||||
* null when the action did not leave one behind. Reading the host and
|
||||
* repository from the URL keeps the link host-level even when the checkout's
|
||||
* remote differs from where the PR was opened (a fork, say); the project is
|
||||
* only consulted when the URL is one this cannot read.
|
||||
*/
|
||||
export function createdPullRequestKey(
|
||||
result: Pick<GitRunStackedActionResult, "pr">,
|
||||
project: OrchestrationProjectShell | undefined,
|
||||
): CreatedPullRequestKey | null {
|
||||
const { status, number, url } = result.pr;
|
||||
if ((status !== "created" && status !== "opened_existing") || number === undefined || !url) {
|
||||
return null;
|
||||
}
|
||||
const parsed = parseChangeRequestUrl(url);
|
||||
if (parsed !== null) return { ...parsed, url };
|
||||
const identity = project?.repositoryIdentity;
|
||||
const kind = identity?.provider as SourceControlProviderKind | undefined;
|
||||
const repository = sourceControlRepositorySelector(identity);
|
||||
if (!identity || kind === undefined || repository === null) return null;
|
||||
return {
|
||||
host: pullRequestHostOf(identity, kind),
|
||||
repository: repository.toLowerCase(),
|
||||
number,
|
||||
url,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Links the pull request a `create_pr`-shaped action produced to the thread it
|
||||
* ran beside. Never fails: the git action already succeeded and its result is
|
||||
* on its way to the client, so a link that cannot be made is logged and
|
||||
* dropped. A duplicate link is the decider saying the thread already knew.
|
||||
*/
|
||||
export const linkCreatedPullRequest = <E>(input: {
|
||||
readonly threadId: ThreadId;
|
||||
readonly result: Pick<GitRunStackedActionResult, "pr">;
|
||||
readonly commandId: Effect.Effect<CommandId, E>;
|
||||
}): Effect.Effect<
|
||||
void,
|
||||
never,
|
||||
OrchestrationEngine.OrchestrationEngineService | ProjectionSnapshotQuery.ProjectionSnapshotQuery
|
||||
> =>
|
||||
Effect.gen(function* () {
|
||||
const engine = yield* OrchestrationEngine.OrchestrationEngineService;
|
||||
const snapshots = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
|
||||
const thread = yield* snapshots.getThreadShellById(input.threadId);
|
||||
if (Option.isNone(thread)) return;
|
||||
const project = Option.getOrUndefined(
|
||||
yield* snapshots.getProjectShellById(thread.value.projectId),
|
||||
);
|
||||
const key = createdPullRequestKey(input.result, project);
|
||||
if (key === null) return;
|
||||
const commandId = yield* input.commandId;
|
||||
yield* engine
|
||||
.dispatch({
|
||||
type: "thread.pull-request.link",
|
||||
commandId,
|
||||
threadId: input.threadId,
|
||||
...key,
|
||||
source: "created",
|
||||
})
|
||||
.pipe(Effect.catchTags({ OrchestrationCommandInvariantError: () => Effect.void }));
|
||||
}).pipe(
|
||||
Effect.withSpan("linkCreatedPullRequest"),
|
||||
Effect.catchCause((cause) =>
|
||||
Cause.hasInterruptsOnly(cause)
|
||||
? Effect.failCause(cause as Cause.Cause<never>)
|
||||
: Effect.logWarning("failed to link created pull request to thread", {
|
||||
threadId: input.threadId,
|
||||
}),
|
||||
),
|
||||
);
|
||||
Reference in new issue
Block a user