Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadPullRequestCompatibility.ts

75 lines2.2 KB

1import type {
2 ExecutionEnvironmentCapabilities,
3 ProjectId,
4 ThreadId,
5 ThreadPullRequestKey,
6} from "@t3tools/contracts";
7​
8type LinkingCapabilities = Pick<
9 ExecutionEnvironmentCapabilities,
10 "threadPullRequests" | "threadPullRequestLinking"
11>;
12​
13/** Capability negotiation keeps both single-link and multi-link environments usable. */
14export function threadPullRequestLinkMode(capabilities: LinkingCapabilities | null | undefined) {
15 return capabilities?.threadPullRequests === true
16 ? "multiple"
17 : capabilities?.threadPullRequestLinking === true
18 ? "single"
19 : "unsupported";
21​
22export function planThreadPullRequestMutation({
23 capabilities,
24 threadId,
25 reference,
26 legacyProjectId,
27 legacyRepository,
28 linked,
29}: {
30 capabilities: LinkingCapabilities | null | undefined;
31 threadId: ThreadId;
32 reference: ThreadPullRequestKey & { readonly url: string };
33 /** Older servers need the exact repository checkout; same-host routing is not available. */
34 legacyProjectId: ProjectId | null;
35 /** Provider selector for legacy APIs, which omit Azure organization and project paths. */
36 legacyRepository?: string | undefined;
37 linked: boolean;
38}) {
39 switch (threadPullRequestLinkMode(capabilities)) {
40 case "multiple":
41 return linked
42 ? {
43 type: "thread.pull-request.link" as const,
44 input: { threadId, ...reference, source: "manual" as const },
45 }
46 : {
47 type: "thread.pull-request.unlink" as const,
48 input: {
49 threadId,
50 host: reference.host,
51 repository: reference.repository,
52 number: reference.number,
53 },
54 };
55 case "single":
56 if (linked && legacyProjectId === null) return null;
57 return {
58 type: "thread.meta.update" as const,
59 input: {
60 threadId,
61 linkedPullRequest:
62 linked && legacyProjectId !== null
63 ? {
64 projectId: legacyProjectId,
65 repository: legacyRepository ?? reference.repository,
66 number: reference.number,
67 url: reference.url,
68 }
69 : null,
70 },
71 };
72 case "unsupported":
73 return null;
74 }