Open History to see the last commit
1import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
2import type {
3 PullRequestAction,
4 PullRequestDetail,
5 PullRequestMergeMethod,
6 SourceControlProviderKind,
7} from "@t3tools/contracts";
8import type { AppSymbolName } from "../../components/AppSymbol";
9
10type ActionDetail = Pick<
11 PullRequestDetail,
12 "state" | "isDraft" | "mergeability" | "autoMergeEnabled" | "workflowApprovalsRequired"
13> & {
14 readonly capabilities: Pick<
15 PullRequestDetail["capabilities"],
16 "actions" | "mergeMethods" | "updateMethods"
17 >;
18 readonly viewerPermissions: Pick<
19 PullRequestDetail["viewerPermissions"],
20 "actions" | "updateMethods"
21 >;
22 readonly mergeCapabilities: PullRequestDetail["mergeCapabilities"];
23};
24
25export const MERGE_METHODS = {
26 squash: { title: "Squash and merge", description: "Combine the commits into one commit." },
27 merge: {
28 title: "Create a merge commit",
29 description: "Keep the commits and add a merge commit.",
30 },
31 rebase: { title: "Rebase and merge", description: "Add the commits without a merge commit." },
32} as const satisfies Record<PullRequestMergeMethod, { title: string; description: string }>;
33
34export const PR_ACTION_LABELS = {
35 merge: "Merge pull request",
36 ready: "Mark ready for review",
37 draft: "Convert to draft",
38 close: "Close pull request",
39 reopen: "Reopen pull request",
40 "update-branch": "Update branch",
41 "enable-auto-merge": "Enable auto-merge",
42 "disable-auto-merge": "Disable auto-merge",
43 revert: "Create revert pull request",
44 "approve-workflows": "Approve workflows",
45} as const satisfies Record<PullRequestAction, string>;
46
47export const PR_ACTION_ICONS = {
48 merge: "arrow.triangle.merge",
49 ready: "checkmark.circle",
50 draft: "pencil",
51 close: "xmark",
52 reopen: "arrow.clockwise",
53 "update-branch": "arrow.up.circle",
54 "enable-auto-merge": "clock",
55 "disable-auto-merge": "xmark",
56 revert: "arrow.uturn.backward",
57 "approve-workflows": "checkmark",
58} as const satisfies Record<PullRequestAction, AppSymbolName>;
59
60export const ALL_PR_ACTIONS: readonly PullRequestAction[] = [
61 "merge",
62 "ready",
63 "draft",
64 "update-branch",
65 "enable-auto-merge",
66 "disable-auto-merge",
67 "approve-workflows",
68 "close",
69 "reopen",
70 "revert",
71];
72
73export function allowedMergeMethods(pr: ActionDetail) {
74 return pr.capabilities.mergeMethods.filter((method) => pr.mergeCapabilities[method]);
75}
76
77export function allowedUpdateMethods(pr: ActionDetail) {
78 return (pr.capabilities.updateMethods ?? []).filter((method) =>
79 pr.viewerPermissions.updateMethods?.includes(method),
80 );
81}
82
83/** Provider support, account permissions, and the PR's state all narrow the offered actions. */
84export function canPerformPullRequestAction(pr: ActionDetail, action: PullRequestAction) {
85 if (!pr.capabilities.actions.includes(action) || !pr.viewerPermissions.actions.includes(action))
86 return false;
87 if (action === "reopen") return pr.state === "closed";
88 if (action === "revert") return pr.state === "merged";
89 if (pr.state !== "open") return false;
90 switch (action) {
91 case "update-branch":
92 return allowedUpdateMethods(pr).length > 0;
93 case "approve-workflows":
94 return (pr.workflowApprovalsRequired ?? 0) > 0;
95 case "ready":
96 return pr.isDraft;
97 case "draft":
98 return !pr.isDraft;
99 case "merge":
100 return !pr.isDraft && pr.mergeability !== "conflicting" && allowedMergeMethods(pr).length > 0;
101 case "enable-auto-merge":
102 return !pr.isDraft && pr.autoMergeEnabled === false && allowedMergeMethods(pr).length > 0;
103 case "disable-auto-merge":
104 return pr.autoMergeEnabled === true;
105 default:
106 return true;
107 }
108}
109
110export const MERGE_CONFLICT_REASON = "Resolve the branch conflicts before merging.";
111
112export function mergeUnavailableReason(pr: ActionDetail, connectionAllowsWrites: boolean) {
113 if (pr.state === "merged") return "This pull request has been merged.";
114 if (pr.state === "closed") return "Reopen this pull request before merging.";
115 if (!connectionAllowsWrites) return "This environment connection is read-only.";
116 if (!pr.capabilities.actions.includes("merge"))
117 return "This provider does not support merging here.";
118 if (!pr.viewerPermissions.actions.includes("merge"))
119 return "Your account cannot merge this pull request.";
120 if (pr.isDraft) return "Mark this draft ready for review before merging.";
121 if (pr.mergeability === "conflicting") return MERGE_CONFLICT_REASON;
122 if (!allowedMergeMethods(pr).length) return "This repository has no available merge methods.";
123 return null;
124}
125
126export function mergeSummary(
127 pr: ActionDetail & Pick<PullRequestDetail, "autoMergeMethod">,
128 connectionAllowsWrites: boolean,
129) {
130 if (pr.state === "merged") return "Merged";
131 if (pr.state === "closed") return "Closed";
132 if (pr.isDraft) return "Draft";
133 if (pr.mergeability === "conflicting") return "Has conflicts";
134 if (!connectionAllowsWrites) return "Read-only connection";
135 const allowed = allowedMergeMethods(pr);
136 const method = pr.autoMergeMethod ?? (allowed.includes("squash") ? "squash" : allowed[0]);
137 const methodLabel = method ? `${method[0]?.toUpperCase()}${method.slice(1)}` : "Merge";
138 if (pr.autoMergeEnabled) return `${methodLabel} · auto-merge on`;
139 if (mergeUnavailableReason(pr, connectionAllowsWrites)) return "Unavailable";
140 return `${methodLabel}${pr.autoMergeEnabled === false ? " · auto-merge off" : ""}`;
141}
142
143export const openOnProviderLabel = (provider: SourceControlProviderKind) =>
144 `Open on ${sourceControlClients.get(provider).label}`;