Files
paseo/packages/cli/src/commands/clone.ts
Matt Cowger 218097b7cc feat: clone GitHub repo into a workspace (#1331)
* feat: clone GitHub repo into a workspace

Add an end-to-end "clone a GitHub repo and register it as a Paseo
workspace" flow: a new workspace.github.clone RPC, daemon handler,
client method, CLI `paseo clone` command, and a GitHub-repo mode in
the project picker modal. Gated behind the workspaceGithubClone
server capability flag.

- protocol: workspace.github.clone request/response schemas + feature flag
- server: handleWorkspaceGithubCloneRequest, normalizeCloneRepository
- client: DaemonClient.cloneGithubWorkspace
- cli: `paseo clone <repo> --dir <path> [--protocol https|ssh]`
- app: GitHub-repo mode, clone-protocol picker, error surfacing

Review fixes:
- CLI clone now checks the workspaceGithubClone capability and fails
  fast with a clear "update the host" error instead of hanging for the
  full request timeout against an older daemon.
- Replace the duplicated client-side URL-detection regex (app + CLI)
  with a shared isCompleteGitRemote() in @getpaseo/protocol/git-remote,
  backed by parseGitRemoteLocation so clients classify remotes
  identically to the daemon (fixes confusing errors for git://, ftp://,
  file:// inputs).
- Add git-remote.test.ts covering the shared classifier.

* Fix GitHub clone failure handling

---------

Co-authored-by: Mohamed Boudra <boudra.moha@gmail.com>
2026-07-13 11:15:40 +00:00

97 lines
3.0 KiB
TypeScript

import type { Command } from "commander";
import { isCompleteGitRemote } from "@getpaseo/protocol/git-remote";
import type { DaemonClient } from "@getpaseo/client/internal/daemon-client";
import { buildDaemonConnectionCommandError, connectToDaemon } from "../utils/client.js";
import type { CommandError, OutputSchema, SingleResult } from "../output/index.js";
import type { CommandOptions } from "../output/with-output.js";
type CloneProtocol = "https" | "ssh";
interface CloneCommandOptions extends CommandOptions {
protocol?: CloneProtocol;
}
export interface CloneResult {
repo: string;
checkoutPath: string;
workspaceId: string;
workspaceName: string;
}
export const cloneSchema: OutputSchema<CloneResult> = {
idField: "workspaceId",
columns: [
{ header: "REPO", field: "repo", width: 28 },
{ header: "WORKSPACE", field: "workspaceName", width: 28 },
{ header: "PATH", field: "checkoutPath", width: 56 },
],
};
function cmdError(code: string, message: string, details?: string): CommandError {
return details ? { code, message, details } : { code, message };
}
export async function runCloneCommand(
repo: string,
options: CloneCommandOptions,
_command: Command,
): Promise<SingleResult<CloneResult>> {
const targetDirectory = typeof options.dir === "string" ? options.dir.trim() : "";
if (!targetDirectory) {
throw cmdError("INVALID_ARGUMENT", "--dir is required");
}
const repoIsCompleteRemote = isCompleteGitRemote(repo);
if (!repoIsCompleteRemote && !options.protocol) {
throw cmdError("INVALID_ARGUMENT", "--protocol is required for owner/repo repository names");
}
let client: DaemonClient;
try {
client = await connectToDaemon({ host: options.host });
} catch (err) {
throw buildDaemonConnectionCommandError({ host: options.host, error: err });
}
if (client.getLastServerInfoMessage()?.features?.workspaceGithubClone !== true) {
await client.close().catch(() => {});
throw cmdError(
"UNSUPPORTED_BY_HOST",
"This daemon does not support cloning GitHub repos.",
"Update the host to a newer Paseo version.",
);
}
try {
const response = await client.cloneGithubWorkspace({
repo,
targetDirectory,
...(repoIsCompleteRemote ? {} : { cloneProtocol: options.protocol }),
});
if (response.error || !response.workspace || !response.checkoutPath) {
throw cmdError(
"CLONE_FAILED",
`Failed to clone GitHub repo: ${response.error ?? "no workspace returned"}`,
);
}
return {
type: "single",
data: {
repo: response.repo,
checkoutPath: response.checkoutPath,
workspaceId: response.workspace.id,
workspaceName: response.workspace.name,
},
schema: cloneSchema,
};
} catch (err) {
if (err && typeof err === "object" && "code" in err) {
throw err;
}
const message = err instanceof Error ? err.message : String(err);
throw cmdError("CLONE_FAILED", `Failed to clone GitHub repo: ${message}`);
} finally {
await client.close().catch(() => {});
}
}