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

pullRequestActions.ts

139 lines4.9 KB

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