section 1: Phase 1: Shell environment owner and repo working-directory

This commit is contained in:
Chris Scott committed 2026-10-03 00:56:39 -04:00
1 parent 52b6147ed3
commit b494ee9f4a
11 files changed
+280 -17

No files matched your search

+10
View File
@@ -23,6 +23,8 @@ interface RepoRow {
const REPO_GIT_CREDENTIAL_SETTING_KEY = 'gitCredentialId'
const REPO_GIT_IDENTITY_SETTING_KEY = 'gitIdentityId'
const REPO_SANDBOX_GIT_CREDENTIALS_SETTING_KEY = 'sandboxGitCredentials'
function rowToRepo(row: RepoRow): Repo {
@@ -82,6 +84,14 @@ export function setRepoGitCredentialId(db: Database, repoId: number, credentialI
setRepoSetting(db, repoId, REPO_GIT_CREDENTIAL_SETTING_KEY, credentialId)
}
export function getRepoGitIdentityId(db: Database, repoId: number): string | null {
return getRepoSetting(db, repoId, REPO_GIT_IDENTITY_SETTING_KEY)
}
export function setRepoGitIdentityId(db: Database, repoId: number, identityId: string | null): void {
setRepoSetting(db, repoId, REPO_GIT_IDENTITY_SETTING_KEY, identityId)
}
export function getRepoSandboxGitCredentials(db: Database, repoId: number): boolean | null {
const value = getRepoSetting(db, repoId, REPO_SANDBOX_GIT_CREDENTIALS_SETTING_KEY)
if (value === null) return null
@@ -7,7 +7,7 @@ export function createInternalGitCredentialsRoutes(db: Database) {
app.get('/gh-env', (c) => {
const provider = new CredentialProvider(db)
return c.json(provider.getGhCliEnv({ cwd: c.req.query('cwd') }))
return c.json(provider.getShellEnv({ cwd: c.req.query('cwd') }))
})
return app
+5 -13
View File
@@ -18,14 +18,9 @@ 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 { ensureAssistantMode, getAssistantModeStatus } from '../services/assistant-mode'
import path from 'path'
function resolveRepo(database: Database, id: number): Repo | null {
return getRepoById(database, id) ?? (id === ASSISTANT_REPO_ID ? buildAssistantRepo() : null)
}
const DeleteWorkspaceRequestSchema = z.object({
directory: z.string().trim().min(1),
})
@@ -150,7 +145,7 @@ app.get('/', async (c) => {
try {
const id = parseInt(c.req.param('id'))
const repo: Repo | null = resolveRepo(database, id)
const repo: Repo | null = repoService.resolveRepoOrAssistant(database, id)
if (!repo) {
return c.json({ error: 'Repo not found' }, 404)
@@ -270,10 +265,7 @@ app.get('/', async (c) => {
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,
)
const worktree = repoService.findSiblingByDirectory(siblings.filter(isWorktreeSibling), directory)
if (!worktree) return c.json({ error: 'Not a deletable worktree of this repo' }, 400)
try {
@@ -491,7 +483,7 @@ app.get('/', async (c) => {
try {
const id = parseInt(c.req.param('id'))
const repo: Repo | null = resolveRepo(database, id)
const repo: Repo | null = repoService.resolveRepoOrAssistant(database, id)
if (!repo) {
return c.json({ error: 'Repo not found' }, 404)
@@ -509,7 +501,7 @@ app.get('/', async (c) => {
try {
const id = parseInt(c.req.param('id'))
const repo: Repo | null = resolveRepo(database, id)
const repo: Repo | null = repoService.resolveRepoOrAssistant(database, id)
if (!repo) {
return c.json({ error: 'Repo not found' }, 404)
@@ -6,6 +6,7 @@ import {
getSSHCredentialsForHost,
createGitEnv,
createGhCliEnv,
createGitIdentityEnv,
findGitHubCredential,
type ResolvedGitCredential,
} from '../utils/git-auth'
@@ -14,6 +15,7 @@ import { logger } from '../utils/logger'
import {
getRepoByDirectory,
getRepoGitCredentialId,
getRepoGitIdentityId,
getRepoSandboxGitCredentials,
listRepos,
} from '../db/queries'
@@ -95,6 +97,21 @@ export class CredentialProvider {
return this.getGhCliEnvForContext(this.resolveContext(options))
}
getShellEnv(options: CredentialResolutionOptions = {}): Record<string, string> {
return { ...this.getGhCliEnv(options), ...this.getAssignedGitIdentityEnv(options) }
}
getAssignedGitIdentityEnv(options: CredentialResolutionOptions = {}): Record<string, string> {
const repo = this.resolveRepo(options)
if (!repo) return {}
const identityId = getRepoGitIdentityId(this.database, repo.id)
if (!identityId) return {}
const profile = this.getPreferences().gitIdentities?.find((identity) => identity.id === identityId)
return profile ? createGitIdentityEnv(profile) : {}
}
private resolveContext(options: CredentialResolutionOptions, repo = this.resolveRepo(options)): CredentialResolutionContext {
const preferences = this.getPreferences()
return {
+22 -1
View File
@@ -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, SCP_STYLE_URL_PATTERN, ASSISTANT_REPO_ID } from '@opencode-manager/shared/utils'
import type { GitAuthService } from './git-auth'
import { isGitHubHttpsUrl } from '../utils/git-auth'
import path from 'path'
@@ -18,6 +18,7 @@ import { resolveProjectId, isGitMainCheckout } from './project-id-resolver'
import { listRepos } from '../db/queries'
import { listActiveScheduleRunWorktreePaths } from '../db/schedules'
import { SettingsService } from './settings'
import { buildAssistantRepo } from './assistant-mode'
import type { OpenCodeClient } from './opencode/client'
import { openCodeLocation } from '@opencode-manager/shared/opencode'
import { canonicalPathSync, mkdirSafe } from '../utils/fs-safe'
@@ -1168,6 +1169,26 @@ export async function resolveRepoProjectId(openCodeClient: OpenCodeClient, direc
return project.id
}
export function resolveRepoOrAssistant(database: Database, id: number): Repo | null {
return getRepoById(database, id) ?? (id === ASSISTANT_REPO_ID ? buildAssistantRepo() : null)
}
export function findSiblingByDirectory<T extends { fullPath: string }>(siblings: T[], directory: string): T | undefined {
const requestedDirectory = canonicalPathSync(path.resolve(directory))
return siblings.find((sibling) => canonicalPathSync(path.resolve(sibling.fullPath)) === requestedDirectory)
}
export async function resolveRepoWorkingDirectory(
repo: Repo,
directory: string | undefined,
loadSiblings: () => Promise<Array<{ fullPath: string }>>,
): Promise<string | null> {
if (directory === undefined) return repo.fullPath
if (canonicalPathSync(path.resolve(directory)) === canonicalPathSync(path.resolve(repo.fullPath))) return repo.fullPath
return findSiblingByDirectory(await loadSiblings(), directory)?.fullPath ?? null
}
export async function getSiblingRepos(
database: Database,
repoId: number,