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