Open History to see the last commit
1import type {
2 ExecutionEnvironmentCapabilities,
3 ProjectId,
4 ThreadId,
5 ThreadPullRequestKey,
6} from "@t3tools/contracts";
7import { normalizeThreadPullRequestKey } from "./threadPullRequests.ts";
8
9type LinkingCapabilities = Pick<
10 ExecutionEnvironmentCapabilities,
11 "threadPullRequests" | "threadPullRequestLinking"
12>;
13
14export function threadPullRequestLinkMode(capabilities: LinkingCapabilities | null | undefined) {
15 return capabilities?.threadPullRequests === true
16 ? "multiple"
17 : capabilities?.threadPullRequestLinking === true
18 ? "single"
19 : "unsupported";
20}
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 const key = normalizeThreadPullRequestKey(reference);
40 switch (threadPullRequestLinkMode(capabilities)) {
41 case "multiple":
42 return linked
43 ? {
44 type: "thread.pull-request.link" as const,
45 input: { threadId, ...reference, ...key, source: "manual" as const },
46 }
47 : {
48 type: "thread.pull-request.unlink" as const,
49 input: {
50 threadId,
51 ...key,
52 },
53 };
54 case "single":
55 if (linked && legacyProjectId === null) return null;
56 return {
57 type: "thread.meta.update" as const,
58 input: {
59 threadId,
60 linkedPullRequest:
61 linked && legacyProjectId !== null
62 ? {
63 projectId: legacyProjectId,
64 repository: legacyRepository ?? reference.repository,
65 number: reference.number,
66 url: reference.url,
67 }
68 : null,
69 },
70 };
71 case "unsupported":
72 return null;
73 }
74}