-
Notifications
You must be signed in to change notification settings - Fork 960
refactor(host-service): split workspace-creation router into focused files #3693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Kitenite
wants to merge
1
commit into
main
Choose a base branch
from
fluorescent-machine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
195 changes: 195 additions & 0 deletions
195
packages/host-service/src/trpc/router/workspace-creation/branch-helpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| import { resolve, sep } from "node:path"; | ||
| import { eq } from "drizzle-orm"; | ||
| import { workspaces } from "../../../db/schema"; | ||
| import { | ||
| asLocalRef, | ||
| asRemoteRef, | ||
| type ResolvedRef, | ||
| } from "../../../runtime/git/refs"; | ||
| import type { HostServiceContext } from "../../../types"; | ||
| import { type GitClient, projectWorktreesRoot } from "./helpers"; | ||
|
|
||
| export async function listWorktreeBranches( | ||
| ctx: HostServiceContext, | ||
| git: GitClient, | ||
| projectId: string, | ||
| ): Promise<{ | ||
| // A worktree counts as "ours" if its path either matches a row in | ||
| // the local `workspaces` table or lives under our managed root. The | ||
| // second case catches orphans (worktree on disk, no workspaces row, | ||
| // e.g. partial create rollback) so they surface for adoption. | ||
| worktreeMap: Map<string, string>; | ||
| // Every branch checked out in any git worktree, including the primary | ||
| // working tree. Used to disable the Checkout action when a branch is | ||
| // already in use elsewhere — `git worktree add <path> <branch>` would fail. | ||
| checkedOutBranches: Set<string>; | ||
| }> { | ||
| const managedRoot = projectWorktreesRoot(projectId); | ||
| const knownPaths = new Set<string>( | ||
| ctx.db | ||
| .select({ path: workspaces.worktreePath }) | ||
| .from(workspaces) | ||
| .where(eq(workspaces.projectId, projectId)) | ||
| .all() | ||
| .map((w) => w.path), | ||
| ); | ||
| const worktreeMap = new Map<string, string>(); | ||
| const checkedOutBranches = new Set<string>(); | ||
| try { | ||
| const raw = await git.raw(["worktree", "list", "--porcelain"]); | ||
| let currentPath: string | null = null; | ||
| for (const line of raw.split("\n")) { | ||
| if (line.startsWith("worktree ")) { | ||
| currentPath = line.slice("worktree ".length).trim(); | ||
| } else if (line.startsWith("branch refs/heads/") && currentPath) { | ||
| const branch = line.slice("branch refs/heads/".length).trim(); | ||
| if (!branch) continue; | ||
| checkedOutBranches.add(branch); | ||
| if ( | ||
| knownPaths.has(currentPath) || | ||
| currentPath.startsWith(managedRoot + sep) | ||
| ) { | ||
| worktreeMap.set(branch, currentPath); | ||
| } | ||
| } else if (line === "") { | ||
| currentPath = null; | ||
| } | ||
| } | ||
| } catch (err) { | ||
| console.warn( | ||
| "[workspace-creation] git worktree list failed; treating no branches as checked out:", | ||
| err, | ||
| ); | ||
| } | ||
| return { worktreeMap, checkedOutBranches }; | ||
| } | ||
|
|
||
| /** | ||
| * Check whether a git worktree is registered at `worktreePath` with the given | ||
| * branch checked out. Used by adopt when the caller provides an explicit path | ||
| * (e.g. v1→v2 migration) rather than a Superset-managed `.worktrees/<branch>` | ||
| * path discovered via `listWorktreeBranches`. | ||
| */ | ||
| export async function findWorktreeAtPath( | ||
| git: GitClient, | ||
| worktreePath: string, | ||
| expectedBranch: string, | ||
| ): Promise<boolean> { | ||
| const targetPath = resolve(worktreePath); | ||
| try { | ||
| const raw = await git.raw(["worktree", "list", "--porcelain"]); | ||
| let currentPath: string | null = null; | ||
| for (const line of raw.split("\n")) { | ||
| if (line.startsWith("worktree ")) { | ||
| currentPath = line.slice("worktree ".length).trim(); | ||
| } else if (line.startsWith("branch refs/heads/") && currentPath) { | ||
| if (resolve(currentPath) !== targetPath) continue; | ||
| const branch = line.slice("branch refs/heads/".length).trim(); | ||
| return branch === expectedBranch; | ||
| } else if (line === "") { | ||
| currentPath = null; | ||
| } | ||
| } | ||
| } catch (err) { | ||
| console.warn( | ||
| "[workspace-creation] git worktree list failed in findWorktreeAtPath:", | ||
| err, | ||
| ); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // Parses `git log -g` to return {branchName: ordinal} where 0 = most recent. | ||
| export async function getRecentBranchOrder( | ||
| git: GitClient, | ||
| limit: number, | ||
| ): Promise<Map<string, number>> { | ||
| const order = new Map<string, number>(); | ||
| try { | ||
| const raw = await git.raw([ | ||
| "log", | ||
| "-g", | ||
| "--pretty=%gs", | ||
| "--grep-reflog=checkout:", | ||
| "-n", | ||
| "500", | ||
| "HEAD", | ||
| "--", | ||
| ]); | ||
| const re = /^checkout: moving from .+ to (.+)$/; | ||
| for (const line of raw.split("\n")) { | ||
| const m = re.exec(line); | ||
| if (!m?.[1]) continue; | ||
| const name = m[1].trim(); | ||
| if (!name || /^[0-9a-f]{7,40}$/.test(name)) continue; // skip detached SHAs | ||
| if (!order.has(name)) { | ||
| order.set(name, order.size); | ||
| if (order.size >= limit) break; | ||
| } | ||
| } | ||
| } catch { | ||
| // ignore (e.g. unborn branch) | ||
| } | ||
| return order; | ||
| } | ||
|
|
||
| export async function listBranchNames( | ||
| ctx: HostServiceContext, | ||
| repoPath: string, | ||
| ): Promise<string[]> { | ||
| const git = await ctx.git(repoPath); | ||
| try { | ||
| const raw = await git.raw([ | ||
| "for-each-ref", | ||
| "--sort=-committerdate", | ||
| "--format=%(refname)", | ||
| "refs/heads/", | ||
| "refs/remotes/origin/", | ||
| ]); | ||
| const names = new Set<string>(); | ||
| for (const refname of raw.trim().split("\n").filter(Boolean)) { | ||
| // Use the full refname's structural prefix to classify (safe — a | ||
| // branch name can't contain `refs/heads/`). Stripping `origin/` | ||
| // from the SHORT name would misclassify a local branch named | ||
| // `origin/foo`. See GIT_REFS.md. | ||
| let name: string; | ||
| if (refname.startsWith("refs/heads/")) { | ||
| name = refname.slice("refs/heads/".length); | ||
| } else if (refname.startsWith("refs/remotes/origin/")) { | ||
| name = refname.slice("refs/remotes/origin/".length); | ||
| } else { | ||
| continue; | ||
| } | ||
| if (name && name !== "HEAD") names.add(name); | ||
| } | ||
| return Array.from(names); | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Build a `ResolvedRef` directly from the picker-supplied hint without | ||
| * probing git. Used when the caller already knows whether the row was | ||
| * local or remote-only — the picker has this info per row. | ||
| */ | ||
| export function buildStartPointFromHint( | ||
| branch: string, | ||
| source: "local" | "remote-tracking", | ||
| ): ResolvedRef { | ||
| if (source === "local") { | ||
| return { | ||
| kind: "local", | ||
| fullRef: asLocalRef(branch), | ||
| shortName: branch, | ||
| }; | ||
| } | ||
| const remote = "origin"; | ||
| return { | ||
| kind: "remote-tracking", | ||
| fullRef: asRemoteRef(remote, branch), | ||
| shortName: branch, | ||
| remote, | ||
| remoteShortName: `${remote}/${branch}`, | ||
| }; | ||
| } | ||
156 changes: 156 additions & 0 deletions
156
packages/host-service/src/trpc/router/workspace-creation/finish-checkout.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| import { existsSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
| import { getDeviceName, getHashedDeviceId } from "@superset/shared/device-info"; | ||
| import { TRPCError } from "@trpc/server"; | ||
| import { workspaces } from "../../../db/schema"; | ||
| import { createTerminalSessionInternal } from "../../../terminal/terminal"; | ||
| import type { HostServiceContext } from "../../../types"; | ||
| import type { GitClient } from "./helpers"; | ||
| import { clearProgress, setProgress } from "./progress"; | ||
|
|
||
| /** | ||
| * Shared postlude for `checkout` (both branch and PR paths). | ||
| * | ||
| * - Writes `branch.<name>.base` from `composer.baseBranch` for the Changes tab. | ||
| * - `ensureV2Host` + `v2Workspace.create` with rollback on failure. | ||
| * - Inserts the local `workspaces` row. | ||
| * - Optionally spawns the setup terminal. | ||
| * - Clears progress. | ||
| */ | ||
| export async function finishCheckout( | ||
| ctx: HostServiceContext, | ||
| args: { | ||
| pendingId: string; | ||
| projectId: string; | ||
| workspaceName: string; | ||
| branch: string; | ||
| worktreePath: string; | ||
| baseBranch: string | undefined; | ||
| runSetupScript: boolean; | ||
| git: GitClient; | ||
| extraWarnings: string[]; | ||
| }, | ||
| ): Promise<{ | ||
| workspace: { id: string }; | ||
| terminals: Array<{ id: string; role: string; label: string }>; | ||
| warnings: string[]; | ||
| alreadyExists?: false; | ||
| }> { | ||
| setProgress(args.pendingId, "registering"); | ||
|
|
||
| // Record the base branch for the Changes tab (skipped if unset — matches | ||
| // `create`'s head-start-point behavior). | ||
| if (args.baseBranch) { | ||
| await args.git | ||
| .raw([ | ||
| "-C", | ||
| args.worktreePath, | ||
| "config", | ||
| `branch.${args.branch}.base`, | ||
| args.baseBranch, | ||
| ]) | ||
| .catch((err) => { | ||
| console.warn( | ||
| `[workspaceCreation.checkout] failed to record base branch ${args.baseBranch}:`, | ||
| err, | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
| const rollbackWorktree = async () => { | ||
| try { | ||
| await args.git.raw(["worktree", "remove", args.worktreePath]); | ||
| } catch (err) { | ||
| console.warn("[workspaceCreation.checkout] failed to rollback worktree", { | ||
| worktreePath: args.worktreePath, | ||
| err, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const deviceClientId = getHashedDeviceId(); | ||
| const deviceName = getDeviceName(); | ||
|
|
||
| let host: { id: string }; | ||
| try { | ||
| host = await ctx.api.device.ensureV2Host.mutate({ | ||
| organizationId: ctx.organizationId, | ||
| machineId: deviceClientId, | ||
| name: deviceName, | ||
| }); | ||
| } catch (err) { | ||
| console.error("[workspaceCreation.checkout] ensureV2Host failed", err); | ||
| clearProgress(args.pendingId); | ||
| await rollbackWorktree(); | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: `Failed to register host: ${err instanceof Error ? err.message : String(err)}`, | ||
| }); | ||
| } | ||
|
|
||
| const cloudRow = await ctx.api.v2Workspace.create | ||
| .mutate({ | ||
| organizationId: ctx.organizationId, | ||
| projectId: args.projectId, | ||
| name: args.workspaceName, | ||
| branch: args.branch, | ||
| hostId: host.id, | ||
| }) | ||
| .catch(async (err) => { | ||
| console.error( | ||
| "[workspaceCreation.checkout] v2Workspace.create failed", | ||
| err, | ||
| ); | ||
| clearProgress(args.pendingId); | ||
| await rollbackWorktree(); | ||
| throw err; | ||
| }); | ||
|
|
||
| if (!cloudRow) { | ||
| clearProgress(args.pendingId); | ||
| await rollbackWorktree(); | ||
| throw new TRPCError({ | ||
| code: "INTERNAL_SERVER_ERROR", | ||
| message: "Cloud workspace create returned no row", | ||
| }); | ||
| } | ||
|
|
||
| ctx.db | ||
| .insert(workspaces) | ||
| .values({ | ||
| id: cloudRow.id, | ||
| projectId: args.projectId, | ||
| worktreePath: args.worktreePath, | ||
| branch: args.branch, | ||
| }) | ||
| .run(); | ||
|
|
||
| const terminals: Array<{ id: string; role: string; label: string }> = []; | ||
| const warnings: string[] = [...args.extraWarnings]; | ||
|
|
||
| if (args.runSetupScript) { | ||
| const setupScriptPath = join(args.worktreePath, ".superset", "setup.sh"); | ||
| if (existsSync(setupScriptPath)) { | ||
| const terminalId = crypto.randomUUID(); | ||
| const result = createTerminalSessionInternal({ | ||
| terminalId, | ||
| workspaceId: cloudRow.id, | ||
| db: ctx.db, | ||
| initialCommand: `bash "${setupScriptPath}"`, | ||
| }); | ||
| if ("error" in result) { | ||
| warnings.push(`Failed to start setup terminal: ${result.error}`); | ||
| } else { | ||
| terminals.push({ | ||
| id: terminalId, | ||
| role: "setup", | ||
| label: "Workspace Setup", | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| clearProgress(args.pendingId); | ||
|
|
||
| return { workspace: cloudRow, terminals, warnings }; | ||
| } |
32 changes: 32 additions & 0 deletions
32
packages/host-service/src/trpc/router/workspace-creation/github-schemas.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| export const IssueSchema = z.object({ | ||
| number: z.number(), | ||
| title: z.string(), | ||
| body: z.string().nullable().optional(), | ||
| url: z.string(), | ||
| state: z.string(), | ||
| author: z.object({ login: z.string() }).optional(), | ||
| createdAt: z.string().optional(), | ||
| updatedAt: z.string().optional(), | ||
| }); | ||
|
|
||
| export const PrSchema = z.object({ | ||
| number: z.number(), | ||
| title: z.string(), | ||
| body: z.string().nullable().optional(), | ||
| url: z.string(), | ||
| state: z.string(), | ||
| headRefName: z.string(), | ||
| baseRefName: z.string(), | ||
| // `gh pr view` returns null when the PR's head fork repository has been | ||
| // deleted. Nullable so the schema parse doesn't fail; consumers decide | ||
| // how to handle a missing owner (client surfaces a clear error for | ||
| // cross-repo PRs — same-repo PRs shouldn't see null in practice). | ||
| headRepositoryOwner: z.object({ login: z.string() }).nullable(), | ||
| isCrossRepository: z.boolean(), | ||
| isDraft: z.boolean(), | ||
| author: z.object({ login: z.string() }).optional(), | ||
| createdAt: z.string().optional(), | ||
| updatedAt: z.string().optional(), | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 2856
🏁 Script executed:
rg -n -A 10 "const knownPaths" packages/host-service/src/trpc/router/workspace-creation/branch-helpers.tsRepository: superset-sh/superset
Length of output: 396
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 5285
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 1066
🏁 Script executed:
Repository: superset-sh/superset
Length of output: 1847
Path-separator mismatch can make managed-root check miss orphans on Windows.
git worktree list --porcelainemits paths with forward slashes on Windows, whileprojectWorktreesRoot()returns a path normalized bypath.resolve()(using backslashes on Windows). BothknownPaths.has(currentPath)(line 49) andcurrentPath.startsWith(managedRoot + sep)(line 50) will fail on Windows because the paths have incompatible separators. This prevents orphan worktrees (on disk under the managed root but not in theworkspacestable) from being detected for adoption, silently breaking the behavior described in the header comment ("partial create rollback").Normalize
currentPathviapath.resolve()before the comparisons, mirroring the pattern already used correctly infindWorktreeAtPath()(line 86).🔧 Proposed fix
for (const line of raw.split("\n")) { if (line.startsWith("worktree ")) { - currentPath = line.slice("worktree ".length).trim(); + currentPath = resolve(line.slice("worktree ".length).trim()); } else if (line.startsWith("branch refs/heads/") && currentPath) { const branch = line.slice("branch refs/heads/".length).trim(); if (!branch) continue;📝 Committable suggestion
🤖 Prompt for AI Agents