TypeScript
feat(web): add pull request merge defaults (#8088)
Bilal Bakr7d9aaf6a7eb2
diff --git a/apps/desktop/src/settings/DesktopClientSettings.test.ts b/apps/desktop/src/settings/DesktopClientSettings.test.tsindex 83ad62ccffd..ea2a8001012 100644--- a/apps/desktop/src/settings/DesktopClientSettings.test.ts+++ b/apps/desktop/src/settings/DesktopClientSettings.test.ts@@ -64,6 +64,7 @@ const clientSettings: ClientSettings = {legacySidebarEnabled: false,loadBalancingEnabled: false,loadBalancingWeights: { "environment-1": 75, "environment-2": 0 },+ pullRequestMergeMethodOverrides: {},timestampFormat: "24-hour",wordWrap: true,};diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsxindex 733a25a3520..de6f32aaa12 100644--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx@@ -53,14 +53,21 @@ import {import { type DraftId, useComposerDraftStore } from "~/composerDraftStore";import { useNewThreadHandler } from "~/hooks/useHandleNewThread";+import { useClientSettings } from "~/hooks/useSettings";import { useCopyToClipboard, writeTextToClipboard } from "~/hooks/useCopyToClipboard";+import {+ deriveLogicalProjectKeyFromSettings,+ derivePhysicalProjectKey,+ selectProjectGroupingSettings,+} from "~/logicalProject";import { changeRequestRepositoryUrl, gitHubPullRequestBrowserUrl } from "~/lib/openPullRequestLink";import { usePreparePullRequestThreadAction } from "~/lib/sourceControlActions";import { cn } from "~/lib/utils";import { readLocalApi } from "~/localApi";import type { ReviewCommentContext } from "~/reviewCommentContext";+import { buildPhysicalToLogicalProjectKeyMap } from "~/sidebarProjectGrouping";import { useProjects } from "~/state/entities";-import { useEnvironments } from "~/state/environments";+import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments";import { useEnvironmentQuery } from "~/state/query";import { useLiveRefresh } from "~/hooks/useLiveRefresh";import {@@ -71,6 +78,7 @@ import {import { useAtomCommand } from "~/state/use-atom-command";import { vcsEnvironment } from "~/state/vcs";import { formatRelativeTimeLabel } from "~/timestampFormat";+import { useUiStateStore } from "~/uiStateStore";import {AlertDialog,@@ -124,11 +132,13 @@ import {pullRequestCheckoutCommand,pullRequestFindingKey,pullRequestHandoffLabels,+ PULL_REQUEST_MERGE_METHOD_LABELS,readableFailure,readPullRequestDetailSnapshot,resolveDisplayedPullRequestDetail,resolvePullRequestPrimaryControl,resolveBaseFreshness,+ resolvePullRequestMergeMethod,type PullRequestFinding,shouldRefreshPullRequestActivity,writePullRequestDetailSnapshot,@@ -168,12 +178,6 @@ const ACTION_SUCCESS_LABELS: Record<PullRequestAction, string> = {"approve-workflows": "Workflows approved",};-const MERGE_METHOD_LABELS: Record<PullRequestMergeMethod, string> = {- merge: "Merge",- squash: "Squash",- rebase: "Rebase",-};-/** Said as the thing that did not happen, rather than as the operation that returned an error. */const ACTION_FAILURE_LABELS: Record<PullRequestAction, string> = {merge: "Could not merge this pull request",@@ -553,12 +557,16 @@ export function PullRequestDetailPanel({compensationRef.current = null;if (scroller) scroller.scrollTop = Math.max(0, scroller.scrollTop + delta);}, [condensed]);+ const lastSelectedMergeMethod = useUiStateStore((state) => state.pullRequestMergeMethod);+ const setLastSelectedMergeMethod = useUiStateStore((state) => state.setPullRequestMergeMethod);+ const mergeMethodOverrides = useClientSettings(+ (settings) => settings.pullRequestMergeMethodOverrides,+ );+ const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);const [mergeMethodSelection, setMergeMethodSelection] = useState<{readonly pullRequestKey: string;readonly method: PullRequestMergeMethod;- }>(() => ({ pullRequestKey, method: "merge" }));- const mergeMethod =- mergeMethodSelection.pullRequestKey === pullRequestKey ? mergeMethodSelection.method : "merge";+ } | null>(null);const setMergeMethod = (method: PullRequestMergeMethod) => {setMergeMethodSelection({ pullRequestKey, method });};@@ -763,6 +771,7 @@ export function PullRequestDetailPanel({const [titleSaving, setTitleSaving] = useState(false);const newThread = useNewThreadHandler();const { environments } = useEnvironments();+ const primaryEnvironmentId = usePrimaryEnvironmentId();const projects = useProjects();const unavailableGitHubUrl = useMemo(() => {const identity = projects.find(@@ -770,6 +779,30 @@ export function PullRequestDetailPanel({)?.repositoryIdentity;return gitHubPullRequestBrowserUrl(identity, reference.repository, reference.number);}, [environmentId, projects, reference.number, reference.projectId, reference.repository]);+ // Project settings store the override under the sidebar group's key, which a duplicate row+ // borrows from its siblings, so the project alone does not always name the same key.+ const projectDefaultMergeMethod = useMemo(() => {+ const project = projects.find(+ (candidate) =>+ candidate.environmentId === environmentId && candidate.id === reference.projectId,+ );+ if (!project) return undefined;+ const projectKey =+ buildPhysicalToLogicalProjectKeyMap({+ projects,+ settings: projectGroupingSettings,+ primaryEnvironmentId,+ }).get(derivePhysicalProjectKey(project)) ??+ deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings);+ return mergeMethodOverrides[projectKey];+ }, [+ environmentId,+ mergeMethodOverrides,+ primaryEnvironmentId,+ projectGroupingSettings,+ projects,+ reference.projectId,+ ]);// Beside a thread there is nothing to pick: the hand-offs land in that thread's composer, and// the thread is already on one server's copy of the branch.const pickableEnvironments = useMemo(@@ -1234,10 +1267,15 @@ export function PullRequestDetailPanel({const allowedMergeMethods = detail? detail.capabilities.mergeMethods.filter((method) => detail.mergeCapabilities[method]): [];- const selectedMergeMethod = allowedMergeMethods.includes(mergeMethod)- ? mergeMethod- : (allowedMergeMethods[0] ?? "merge");- const selectedMergeMethodLabel = MERGE_METHOD_LABELS[selectedMergeMethod];+ const currentMergeMethod =+ mergeMethodSelection?.pullRequestKey === pullRequestKey ? mergeMethodSelection.method : null;+ const selectedMergeMethod = resolvePullRequestMergeMethod(+ allowedMergeMethods,+ currentMergeMethod,+ projectDefaultMergeMethod,+ lastSelectedMergeMethod,+ );+ const selectedMergeMethodLabel = PULL_REQUEST_MERGE_METHOD_LABELS[selectedMergeMethod];const pendingAutoMergeLabel = `Auto-merge (${selectedMergeMethodLabel.toLowerCase()})`;const conflicting = detail?.state === "open" && detail.mergeability === "conflicting";// Only an outright yes arms it. A host that reports nothing has not said the merge is already@@ -1245,7 +1283,7 @@ export function PullRequestDetailPanel({const autoMergeArmed = detail?.state === "open" && detail.autoMergeEnabled === true;const armedMergeMethod = detail?.autoMergeMethod;const armedAutoMergeLabel = armedMergeMethod- ? `Auto-merge (${MERGE_METHOD_LABELS[armedMergeMethod].toLowerCase()})`+ ? `Auto-merge (${PULL_REQUEST_MERGE_METHOD_LABELS[armedMergeMethod].toLowerCase()})`: "Auto-merge";const workflowApprovalsRequired =detail?.state === "open" ? (detail.workflowApprovalsRequired ?? 0) : 0;@@ -1783,17 +1821,24 @@ export function PullRequestDetailPanel({) : null}<MenuRadioGroupvalue={selectedMergeMethod}- onValueChange={(method) =>- setMergeMethod(method as PullRequestMergeMethod)- }+ onValueChange={(method) => {+ const selectedMethod = method as PullRequestMergeMethod;+ setMergeMethod(selectedMethod);+ setLastSelectedMergeMethod(selectedMethod);+ }}>{allowedMergeMethods.map((method) => (- <MenuRadioItem key={method} value={method} disabled={actionPending}>+ <MenuRadioItem+ key={method}+ value={method}+ disabled={actionPending}+ closeOnClick+ >{/* The radio item lays its children out as one block, so theicon and the label need their own row to share a line. */}<span className="flex min-w-0 items-center gap-2"><GitMergeIcon className="size-3.5" />- <span>{MERGE_METHOD_LABELS[method]}</span>+ <span>{PULL_REQUEST_MERGE_METHOD_LABELS[method]}</span></span></MenuRadioItem>))}diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.tsindex 61c630c815e..000c594ec6e 100644--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts@@ -37,6 +37,7 @@ import {resolvePullRequestPrimaryControl,shouldRefreshPullRequestActivity,resolveBaseFreshness,+ resolvePullRequestMergeMethod,buildPullRequestTimeline,editPullRequestThreadComment,writePullRequestDetailSnapshot,@@ -85,6 +86,21 @@ const TIMELINE_SOURCE: Pick<closedAt: null,};+describe("pull request merge method", () => {+ it("uses the current choice, then the project default, then the last choice", () => {+ expect(+ resolvePullRequestMergeMethod(["merge", "squash", "rebase"], null, "squash", "rebase"),+ ).toBe("squash");+ expect(+ resolvePullRequestMergeMethod(["merge", "squash", "rebase"], "rebase", "squash", "merge"),+ ).toBe("rebase");+ expect(resolvePullRequestMergeMethod(["merge", "rebase"], null, "squash", "rebase")).toBe(+ "rebase",+ );+ expect(resolvePullRequestMergeMethod(["squash"], null, "merge", "rebase")).toBe("squash");+ });+});+describe("pull request activity refresh", () => {const first = {key: "project:acme/web#7",diff --git a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts b/apps/web/src/components/pullRequest/pullRequestDetail.logic.tsindex d00215f02d4..24b37aceafc 100644--- a/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts+++ b/apps/web/src/components/pullRequest/pullRequestDetail.logic.ts@@ -11,6 +11,7 @@ import {type PullRequestCommit,type PullRequestDetailView,type PullRequestMergeability,+ type PullRequestMergeMethod,type PullRequestReaction,type PullRequestReviewThread,type PullRequestState,@@ -21,6 +22,24 @@ import {import { inferReviewCommentFenceLanguage, type ReviewCommentContext } from "~/reviewCommentContext";+export const PULL_REQUEST_MERGE_METHOD_LABELS: Record<PullRequestMergeMethod, string> = {+ merge: "Merge",+ squash: "Squash and merge",+ rebase: "Rebase and merge",+};++export function resolvePullRequestMergeMethod(+ allowed: ReadonlyArray<PullRequestMergeMethod>,+ current: PullRequestMergeMethod | null,+ projectDefault: PullRequestMergeMethod | undefined,+ lastSelected: PullRequestMergeMethod,+): PullRequestMergeMethod {+ for (const method of [current, projectDefault, lastSelected]) {+ if (method && allowed.includes(method)) return method;+ }+ return allowed[0] ?? "merge";+}+const safeShellArgument = /^[A-Za-z0-9._/@+=,-]+$/;const bitbucketRepositoryName = /^[A-Za-z0-9._-]+\/[A-Za-z0-9._-]+$/;diff --git a/apps/web/src/components/settings/ProjectSettingsPanel.tsx b/apps/web/src/components/settings/ProjectSettingsPanel.tsxindex 0401fd733d0..e9cd1bbed43 100644--- a/apps/web/src/components/settings/ProjectSettingsPanel.tsx+++ b/apps/web/src/components/settings/ProjectSettingsPanel.tsx@@ -21,6 +21,7 @@ import {type ResolvedKeybindingsConfig,type ServerSettings,type ProviderDriverKind,+ type PullRequestMergeMethod,type SidebarProjectGroupingMode,type T3ProjectFileScript,type ThreadEnvMode,@@ -79,6 +80,7 @@ import { useAtomCommand } from "../../state/use-atom-command";import { ProviderModelPicker } from "../chat/ProviderModelPicker";import { TraitsPicker } from "../chat/TraitsPicker";import { ProjectFavicon } from "../ProjectFavicon";+import { PULL_REQUEST_MERGE_METHOD_LABELS } from "../pullRequest/pullRequestDetail.logic";import {EMPTY_PROJECT_SCRIPT_INPUT,editorRequestForScript,@@ -480,6 +482,19 @@ function ProjectDetail({setBooleanOverride("projectAgentBrowserAccessOverrides", enabled);const deleteProject = useAtomCommand(projectEnvironment.delete, { reportFailure: false });const projectNameEditedRef = useRef(false);+ const mergeMethodOverrides = useClientSettings(+ (settings) => settings.pullRequestMergeMethodOverrides,+ );+ const projectMergeMethod = mergeMethodOverrides[group.projectKey];+ const setProjectMergeMethod = (method: PullRequestMergeMethod | null) => {+ const nextOverrides = { ...mergeMethodOverrides };+ if (method === null) {+ delete nextOverrides[group.projectKey];+ } else {+ nextOverrides[group.projectKey] = method;+ }+ updateClientSettings({ pullRequestMergeMethodOverrides: nextOverrides });+ };const faviconPath = representative.faviconPath ?? null;const projectIcon = representative.projectIcon ?? null;@@ -983,6 +998,42 @@ function ProjectDetail({</div>}/>+ <SettingsRow+ title="Default merge method"+ description="Pull requests in this project start with this method. It overrides the last method selected."+ resetAction={+ projectMergeMethod !== undefined ? (+ <SettingResetButton+ label="project merge method"+ onClick={() => setProjectMergeMethod(null)}+ />+ ) : null+ }+ control={+ <Select+ value={projectMergeMethod ?? "inherit"}+ onValueChange={(value) =>+ setProjectMergeMethod(+ value === "inherit" ? null : (value as PullRequestMergeMethod),+ )+ }+ >+ <SelectTrigger aria-label="Default pull request merge method">+ <SelectValue>+ {projectMergeMethod === undefined+ ? "Last selected"+ : PULL_REQUEST_MERGE_METHOD_LABELS[projectMergeMethod]}+ </SelectValue>+ </SelectTrigger>+ <SelectPopup align="end" alignItemWithTrigger={false}>+ <SelectItem value="inherit">Last selected</SelectItem>+ <SelectItem value="merge">{PULL_REQUEST_MERGE_METHOD_LABELS.merge}</SelectItem>+ <SelectItem value="squash">{PULL_REQUEST_MERGE_METHOD_LABELS.squash}</SelectItem>+ <SelectItem value="rebase">{PULL_REQUEST_MERGE_METHOD_LABELS.rebase}</SelectItem>+ </SelectPopup>+ </Select>+ }+ /><SettingsRowtitle="Model"status={diff --git a/apps/web/src/uiStateStore.test.ts b/apps/web/src/uiStateStore.test.tsindex 1a95acbcfbf..4d68a3231b3 100644--- a/apps/web/src/uiStateStore.test.ts+++ b/apps/web/src/uiStateStore.test.ts@@ -26,6 +26,7 @@ function makeUiState(overrides: Partial<UiState> = {}): UiState {threadLastVisitedAtById: {},threadChangedFilesExpandedById: {},defaultAdvertisedEndpointKey: null,+ pullRequestMergeMethod: "merge",...overrides,};}@@ -158,6 +159,18 @@ describe("uiStateStore pure functions", () => {});describe("parsePersistedState", () => {+ it("hydrates the last selected pull request merge method", () => {+ const parsed = parsePersistedState({+ pullRequestMergeMethod: "squash",+ });+ const invalid = parsePersistedState({+ pullRequestMergeMethod: "fast-forward",+ });++ expect(parsed.pullRequestMergeMethod).toBe("squash");+ expect(invalid.pullRequestMergeMethod).toBe("merge");+ });+it("hydrates raw UI-owned state without server entities", () => {const parsed = parsePersistedState({projectExpandedById: {@@ -189,6 +202,7 @@ describe("parsePersistedState", () => {},defaultAdvertisedEndpointKey: "desktop-core:lan:http",sidebarProjectScopeKey: null,+ pullRequestMergeMethod: "merge",threadChangedFilesExpandedById: {"environment:thread-1": {"turn-1": false,@@ -317,6 +331,7 @@ describe("uiStateStore persistence", () => {"turn-2": true,},},+ pullRequestMergeMethod: "merge",});expect(parsePersistedState(persisted)).toEqual({...state,diff --git a/apps/web/src/uiStateStore.ts b/apps/web/src/uiStateStore.tsindex b14ce917c86..e82c86f2640 100644--- a/apps/web/src/uiStateStore.ts+++ b/apps/web/src/uiStateStore.ts@@ -1,4 +1,5 @@import { Debouncer } from "@tanstack/react-pacer";+import type { PullRequestMergeMethod } from "@t3tools/contracts";import { create } from "zustand";import { normalizeProjectPathForComparison } from "./lib/projectPaths";@@ -29,6 +30,7 @@ export interface PersistedUiState {sidebarProjectScopeKey?: string | null;threadChangedFilesExpansionVersion?: number;threadChangedFilesExpandedById?: Record<string, Record<string, boolean>>;+ pullRequestMergeMethod?: string;}export interface UiProjectState {@@ -49,7 +51,12 @@ export interface UiEndpointState {defaultAdvertisedEndpointKey: string | null;}-export interface UiState extends UiProjectState, UiThreadState, UiEndpointState {}+export interface UiPullRequestState {+ pullRequestMergeMethod: PullRequestMergeMethod;+}++export interface UiState+ extends UiProjectState, UiThreadState, UiEndpointState, UiPullRequestState {}const initialState: UiState = {projectExpandedById: {},@@ -58,6 +65,7 @@ const initialState: UiState = {threadLastVisitedAtById: {},threadChangedFilesExpandedById: {},defaultAdvertisedEndpointKey: null,+ pullRequestMergeMethod: "merge",};const LEGACY_PROJECT_CWD_PREFERENCE_PREFIX = "legacy-project-cwd:";@@ -109,6 +117,10 @@ function sanitizeTimestampRecord(value: unknown): Record<string, string> {);}+function isPullRequestMergeMethod(value: unknown): value is PullRequestMergeMethod {+ return value === "merge" || value === "squash" || value === "rebase";+}+export function parsePersistedState(parsed: PersistedUiState): UiState {const projectExpandedById =parsed.projectExpandedById === undefined@@ -143,6 +155,9 @@ export function parsePersistedState(parsed: PersistedUiState): UiState {: {},defaultAdvertisedEndpointKey: sanitizeOptionalKey(parsed.defaultAdvertisedEndpointKey),sidebarProjectScopeKey: sanitizeOptionalKey(parsed.sidebarProjectScopeKey),+ pullRequestMergeMethod: isPullRequestMergeMethod(parsed.pullRequestMergeMethod)+ ? parsed.pullRequestMergeMethod+ : initialState.pullRequestMergeMethod,};}@@ -216,6 +231,7 @@ export function persistState(state: UiState): void {sidebarProjectScopeKey: state.sidebarProjectScopeKey,threadChangedFilesExpansionVersion: THREAD_CHANGED_FILES_EXPANSION_VERSION,threadChangedFilesExpandedById: state.threadChangedFilesExpandedById,+ pullRequestMergeMethod: state.pullRequestMergeMethod,} satisfies PersistedUiState),);if (!legacyKeysCleanedUp) {@@ -324,6 +340,12 @@ export function setSidebarProjectScopeKey(state: UiState, projectKey: string | n};}+function setPullRequestMergeMethod(state: UiState, method: PullRequestMergeMethod): UiState {+ return state.pullRequestMergeMethod === method+ ? state+ : { ...state, pullRequestMergeMethod: method };+}+export function resolveProjectExpanded(projectExpandedById: Readonly<Record<string, boolean>>,preferenceKeys: readonly string[],@@ -407,6 +429,7 @@ interface UiStateStore extends UiState {setThreadChangedFilesExpanded: (threadId: string, turnId: string, expanded: boolean) => void;setDefaultAdvertisedEndpointKey: (key: string | null) => void;setSidebarProjectScopeKey: (projectKey: string | null) => void;+ setPullRequestMergeMethod: (method: PullRequestMergeMethod) => void;setProjectExpanded: (projectIds: string | readonly string[], expanded: boolean) => void;reorderProjects: (currentProjectOrder: readonly string[],@@ -427,6 +450,7 @@ export const useUiStateStore = create<UiStateStore>((set) => ({set((state) => setDefaultAdvertisedEndpointKey(state, key)),setSidebarProjectScopeKey: (projectKey) =>set((state) => setSidebarProjectScopeKey(state, projectKey)),+ setPullRequestMergeMethod: (method) => set((state) => setPullRequestMergeMethod(state, method)),setProjectExpanded: (projectIds, expanded) =>set((state) => setProjectExpanded(state, projectIds, expanded)),reorderProjects: (currentProjectOrder, draggedProjectIds, targetProjectIds) =>diff --git a/packages/contracts/src/settings.test.ts b/packages/contracts/src/settings.test.tsindex b0fc4031d6d..7d3cd2ceafe 100644--- a/packages/contracts/src/settings.test.ts+++ b/packages/contracts/src/settings.test.ts@@ -455,6 +455,25 @@ describe("ServerSettings thread settlement", () => {});});+describe("ClientSettings pull request merge methods", () => {+ it("defaults to no project overrides and accepts supported methods", () => {+ expect(decodeClientSettings({}).pullRequestMergeMethodOverrides).toEqual({});+ expect(+ decodeClientSettingsPatch({+ pullRequestMergeMethodOverrides: { project: "squash" },+ }).pullRequestMergeMethodOverrides,+ ).toEqual({ project: "squash" });+ });++ it("rejects unsupported project merge methods", () => {+ expect(() =>+ decodeClientSettingsPatch({+ pullRequestMergeMethodOverrides: { project: "fast-forward" },+ }),+ ).toThrow();+ });+});+describe("ServerSettings.providerInstances (slice-2 invariant)", () => {it("defaults text generation to Luna at low reasoning effort", () => {expect(DEFAULT_SERVER_SETTINGS.textGenerationModelSelection).toEqual({diff --git a/packages/contracts/src/settings.ts b/packages/contracts/src/settings.tsindex 3d24e488ce7..3491103da94 100644--- a/packages/contracts/src/settings.ts+++ b/packages/contracts/src/settings.ts@@ -32,6 +32,7 @@ import {ProviderInstanceId,type ProviderDriverKind,} from "./providerInstance.ts";+import { PullRequestMergeMethod } from "./pullRequest.ts";// ── Client Settings (local-only) ───────────────────────────────@@ -391,6 +392,10 @@ export const ClientSettingsSchema = Schema.Struct({modelOrder: Schema.Array(Schema.String).pipe(Schema.withDecodingDefault(Effect.succeed([]))),}),).pipe(Schema.withDecodingDefault(Effect.succeed({}))),+ pullRequestMergeMethodOverrides: Schema.Record(+ TrimmedNonEmptyString,+ PullRequestMergeMethod,+ ).pipe(Schema.withDecodingDefault(Effect.succeed({}))),// Legacy plan mode. The composer's Build/Plan toggle was removed from the// default UI; this beta flag restores it (plus the /plan and /default slash// commands) for users who still rely on the old workflow.@@ -1341,6 +1346,9 @@ export const ClientSettingsPatch = Schema.Struct({}),),),+ pullRequestMergeMethodOverrides: Schema.optionalKey(+ Schema.Record(TrimmedNonEmptyString, PullRequestMergeMethod),+ ),planModeEnabled: Schema.optionalKey(Schema.Boolean),contextWindowMeterEnabled: Schema.optionalKey(Schema.Boolean),composerCollapseOnScroll: Schema.optionalKey(Schema.Boolean),