mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
section 9: Phase 9: One owner for creating and removing OpenCode worksp
This commit is contained in:
1 parent
8cd00e1a8a
commit
f402e8479a
3 files changed
+195
-15
No files matched your search
@@ -12,14 +12,13 @@ import type { OpenCodeClient } from '../services/opencode/client'
|
||||
import { logger } from '../utils/logger'
|
||||
import { getErrorMessage, getStatusCode } from '../utils/error-utils'
|
||||
import { handleOpenCodeError } from '../utils/route-helpers'
|
||||
import { ASSISTANT_REPO_ID, isWorktreeSibling } from '@opencode-manager/shared/utils'
|
||||
import { ASSISTANT_REPO_ID } from '@opencode-manager/shared/utils'
|
||||
import { isWorktreeError, openCodeLocation } from '@opencode-manager/shared/opencode'
|
||||
import { createRepoGitRoutes } from './repo-git'
|
||||
import { createScheduleRoutes } from './schedules'
|
||||
import type { GitAuthService } from '../services/git-auth'
|
||||
import { ScheduleService } from '../services/schedules'
|
||||
import { ensureAssistantMode, getAssistantModeStatus, buildAssistantRepo } from '../services/assistant-mode'
|
||||
import { canonicalPathSync } from '../utils/fs-safe'
|
||||
import path from 'path'
|
||||
|
||||
function resolveRepo(database: Database, id: number): Repo | null {
|
||||
@@ -267,19 +266,13 @@ app.get('/', async (c) => {
|
||||
const body = await c.req.json().catch(() => null)
|
||||
const parsed = DeleteWorkspaceRequestSchema.safeParse(body)
|
||||
if (!parsed.success) return c.json({ error: 'directory is required' }, 400)
|
||||
const directory = parsed.data.directory
|
||||
|
||||
const siblings = await repoService.getSiblingRepos(database, id, gitAuthService.getGitEnvironment(), openCodeClient)
|
||||
const requestedDirectory = canonicalPathSync(path.resolve(directory))
|
||||
const worktree = siblings.find(
|
||||
(sibling) => isWorktreeSibling(sibling) && canonicalPathSync(path.resolve(sibling.fullPath)) === requestedDirectory,
|
||||
)
|
||||
if (!worktree) return c.json({ error: 'Not a deletable worktree of this repo' }, 400)
|
||||
|
||||
try {
|
||||
const projectID = await repoService.resolveRepoProjectId(openCodeClient, repo.fullPath)
|
||||
await openCodeClient.api.worktree.remove({ projectID, directory: worktree.fullPath, force: true })
|
||||
await repoService.removeRepoWorkspace(database, openCodeClient, gitAuthService.getGitEnvironment(), repo, parsed.data.directory)
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof repoService.RepoWorkspaceError) {
|
||||
return c.json({ error: error.message }, error.status)
|
||||
}
|
||||
if (isWorktreeError(error)) {
|
||||
return c.json({ error: error.data.message }, 409)
|
||||
}
|
||||
@@ -302,8 +295,7 @@ app.get('/', async (c) => {
|
||||
if (!repo || repo.cloneStatus !== 'ready') return c.json({ error: 'Repo not found' }, 404)
|
||||
|
||||
try {
|
||||
const projectID = await repoService.resolveRepoProjectId(openCodeClient, repo.fullPath)
|
||||
const worktree = await openCodeClient.api.worktree.create({ projectID })
|
||||
const worktree = await repoService.createRepoWorkspace(openCodeClient, repo)
|
||||
return c.json(worktree)
|
||||
} catch (error: unknown) {
|
||||
if (isWorktreeError(error)) {
|
||||
|
||||
@@ -7,7 +7,7 @@ import type { Database } from 'bun:sqlite'
|
||||
import type { Repo, CreateRepoInput } from '../types/repo'
|
||||
import { logger } from '../utils/logger'
|
||||
import { getReposPath, getScheduleWorktreesPath } from '@opencode-manager/shared/config/env'
|
||||
import { normalizeRepoDirectoryName, sanitizeRepoDirectoryName, sanitizeBranchForDirectory, getRepoBaseDirectoryName, normalizeRepoUrlForCompare, isSSHUrl, normalizeSSHUrl, SCP_STYLE_URL_PATTERN } from '@opencode-manager/shared/utils'
|
||||
import { normalizeRepoDirectoryName, sanitizeRepoDirectoryName, sanitizeBranchForDirectory, getRepoBaseDirectoryName, normalizeRepoUrlForCompare, isSSHUrl, normalizeSSHUrl, isWorktreeSibling, SCP_STYLE_URL_PATTERN } from '@opencode-manager/shared/utils'
|
||||
import type { GitAuthService } from './git-auth'
|
||||
import { isGitHubHttpsUrl } from '../utils/git-auth'
|
||||
import path from 'path'
|
||||
@@ -1263,3 +1263,44 @@ export async function getSiblingRepos(
|
||||
return repoSiblings
|
||||
}
|
||||
}
|
||||
|
||||
export class RepoWorkspaceError extends Error {
|
||||
readonly status: 400 | 404
|
||||
|
||||
constructor(message: string, status: 400 | 404) {
|
||||
super(message)
|
||||
this.name = 'RepoWorkspaceError'
|
||||
this.status = status
|
||||
}
|
||||
}
|
||||
|
||||
export async function createRepoWorkspace(
|
||||
openCodeClient: OpenCodeClient,
|
||||
repo: Repo,
|
||||
options: { name?: string; ref?: string } = {},
|
||||
): Promise<{ directory: string }> {
|
||||
const projectID = await resolveRepoProjectId(openCodeClient, repo.fullPath)
|
||||
return openCodeClient.api.worktree.create({
|
||||
projectID,
|
||||
...(options.name ? { name: options.name } : {}),
|
||||
...(options.ref ? { branch: options.ref } : {}),
|
||||
})
|
||||
}
|
||||
|
||||
export async function removeRepoWorkspace(
|
||||
database: Database,
|
||||
openCodeClient: OpenCodeClient,
|
||||
gitEnv: Record<string, string>,
|
||||
repo: Repo,
|
||||
directory: string,
|
||||
): Promise<void> {
|
||||
const siblings = await getSiblingRepos(database, repo.id, gitEnv, openCodeClient)
|
||||
const requestedDirectory = canonicalPathSync(path.resolve(directory))
|
||||
const worktree = siblings.find(
|
||||
(sibling) => isWorktreeSibling(sibling) && canonicalPathSync(path.resolve(sibling.fullPath)) === requestedDirectory,
|
||||
)
|
||||
if (!worktree) throw new RepoWorkspaceError('Not a deletable worktree of this repo', 400)
|
||||
|
||||
const projectID = await resolveRepoProjectId(openCodeClient, repo.fullPath)
|
||||
await openCodeClient.api.worktree.remove({ projectID, directory: worktree.fullPath, force: true })
|
||||
}
|
||||
Reference in new issue
Block a user