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

@@ -3,6 +3,7 @@ import { Database } from 'bun:sqlite'
import { migrate } from '../../../src/db/migration-runner'
import { allMigrations } from '../../../src/db/migrations'
import { SettingsService } from '../../../src/services/settings'
import { createRepo, setRepoGitIdentityId } from '../../../src/db/queries'
import { createInternalGitCredentialsRoutes } from '../../../src/routes/internal/git-credentials'
import type { GitCredential } from '@opencode-manager/shared'
@@ -37,4 +38,54 @@ describe('internal git-credentials routes', () => {
expect(res.status).toBe(200)
expect(await res.json()).toEqual({})
})
it('GET /gh-env includes the assigned identity env for a repo cwd', async () => {
settingsService.updateSettings({
gitCredentials: [
{ name: 'github', host: 'github.com', type: 'pat', token: 'ghp_test_token' } as GitCredential,
],
gitIdentities: [{ id: 'identity-1', name: 'Alice', email: 'alice@example.com' }],
})
const repo = createRepo(db, {
repoUrl: 'https://github.com/acme/repo.git',
localPath: 'repo',
defaultBranch: 'main',
cloneStatus: 'ready',
clonedAt: Date.now(),
})
setRepoGitIdentityId(db, repo.id, 'identity-1')
const res = await app.request(`/gh-env?cwd=${encodeURIComponent(repo.fullPath)}`)
expect(res.status).toBe(200)
expect(await res.json()).toEqual({
GH_TOKEN: 'ghp_test_token',
GITHUB_TOKEN: 'ghp_test_token',
GIT_AUTHOR_NAME: 'Alice',
GIT_AUTHOR_EMAIL: 'alice@example.com',
GIT_COMMITTER_NAME: 'Alice',
GIT_COMMITTER_EMAIL: 'alice@example.com',
})
})
it('GET /gh-env omits the identity env without an assignment', async () => {
settingsService.updateSettings({
gitCredentials: [
{ name: 'github', host: 'github.com', type: 'pat', token: 'ghp_test_token' } as GitCredential,
],
gitIdentities: [{ id: 'identity-1', name: 'Alice', email: 'alice@example.com' }],
})
const repo = createRepo(db, {
repoUrl: 'https://github.com/acme/repo.git',
localPath: 'repo',
defaultBranch: 'main',
cloneStatus: 'ready',
clonedAt: Date.now(),
})
const res = await app.request(`/gh-env?cwd=${encodeURIComponent(repo.fullPath)}`)
expect(res.status).toBe(200)
expect(await res.json()).toEqual({ GH_TOKEN: 'ghp_test_token', GITHUB_TOKEN: 'ghp_test_token' })
})
})
+6
View File
@@ -32,6 +32,8 @@ vi.mock('../../src/services/repo', () => ({
createBranch: vi.fn(),
deleteRepoFiles: vi.fn(),
getSiblingRepos: vi.fn(),
resolveRepoOrAssistant: vi.fn(),
findSiblingByDirectory: vi.fn(),
}))
vi.mock('../../src/services/assistant-mode', () => ({
@@ -79,6 +81,7 @@ import type { AssistantModeStatus, Repo } from '@opencode-manager/shared/types'
import type { OpenCodeApi } from '@opencode-manager/shared/opencode'
import { ClientError } from '@opencode-manager/shared/opencode'
import { getAssistantModeStatus, ensureAssistantMode, buildAssistantRepo } from '../../src/services/assistant-mode'
import { ASSISTANT_REPO_ID } from '@opencode-manager/shared/utils'
const mockGitAuthService = {
getGitEnvironment: vi.fn().mockReturnValue({})
@@ -118,6 +121,9 @@ describe('Repo Routes', () => {
updatedAt: Date.now(),
})
vi.mocked(db.getRepoGitCredentialId).mockReturnValue(null)
vi.mocked(repoService.resolveRepoOrAssistant).mockImplementation(
(_database, id) => vi.mocked(db.getRepoById)(_database, id) ?? (id === ASSISTANT_REPO_ID ? buildAssistantRepo() : null),
)
})
describe('POST /:id/access', () => {
@@ -4,7 +4,7 @@ import { migrate } from '../../src/db/migration-runner'
import { allMigrations } from '../../src/db/migrations'
import { SettingsService } from '../../src/services/settings'
import { CredentialProvider } from '../../src/services/credential-provider'
import { createRepo, setRepoGitCredentialId, setRepoSandboxGitCredentials } from '../../src/db/queries'
import { createRepo, setRepoGitCredentialId, setRepoGitIdentityId, setRepoSandboxGitCredentials } from '../../src/db/queries'
import { SANDBOX_MAX_FORWARDED_GIT_CONFIGS } from '../../src/services/sandbox/shell-shim'
import type { GitCredential } from '@opencode-manager/shared'
@@ -316,4 +316,65 @@ describe('CredentialProvider', () => {
}
})
})
describe('shell env', () => {
function createGithubRepo() {
return createRepo(db, {
repoUrl: 'https://github.com/acme/repo.git',
localPath: 'repo',
defaultBranch: 'main',
cloneStatus: 'ready',
clonedAt: Date.now(),
})
}
beforeEach(() => {
settingsService.updateSettings({
gitCredentials: [createPatCredential('github', 'github.com', 'ghp_test_token', undefined, 'github-id')],
gitIdentities: [{ id: 'identity-1', name: 'Alice', email: 'alice@example.com' }],
})
})
it('getShellEnv returns only the GH CLI env without an assigned identity', () => {
expect(provider.getShellEnv()).toEqual({ GH_TOKEN: 'ghp_test_token', GITHUB_TOKEN: 'ghp_test_token' })
})
it('getAssignedGitIdentityEnv returns an empty object without an assignment', () => {
const repo = createGithubRepo()
expect(provider.getAssignedGitIdentityEnv({ cwd: repo.fullPath })).toEqual({})
})
it('getAssignedGitIdentityEnv returns the identity env for an assigned profile', () => {
const repo = createGithubRepo()
setRepoGitIdentityId(db, repo.id, 'identity-1')
expect(provider.getAssignedGitIdentityEnv({ cwd: repo.fullPath })).toEqual({
GIT_AUTHOR_NAME: 'Alice',
GIT_AUTHOR_EMAIL: 'alice@example.com',
GIT_COMMITTER_NAME: 'Alice',
GIT_COMMITTER_EMAIL: 'alice@example.com',
})
})
it('getShellEnv merges the GH CLI env with the assigned identity env', () => {
const repo = createGithubRepo()
setRepoGitIdentityId(db, repo.id, 'identity-1')
expect(provider.getShellEnv({ cwd: repo.fullPath })).toEqual({
GH_TOKEN: 'ghp_test_token',
GITHUB_TOKEN: 'ghp_test_token',
GIT_AUTHOR_NAME: 'Alice',
GIT_AUTHOR_EMAIL: 'alice@example.com',
GIT_COMMITTER_NAME: 'Alice',
GIT_COMMITTER_EMAIL: 'alice@example.com',
})
})
it('getAssignedGitIdentityEnv ignores a dangling assigned identity', () => {
const repo = createGithubRepo()
setRepoGitIdentityId(db, repo.id, 'missing-identity')
expect(provider.getAssignedGitIdentityEnv({ cwd: repo.fullPath })).toEqual({})
})
})
})
@@ -0,0 +1,98 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import fs from 'node:fs'
import os from 'node:os'
import path from 'node:path'
import { findSiblingByDirectory, resolveRepoWorkingDirectory } from '../../src/services/repo'
import type { Repo } from '@opencode-manager/shared/types'
describe('resolveRepoWorkingDirectory', () => {
let root: string
let repoPath: string
let siblingPath: string
let unrelatedPath: string
let repoLinkPath: string
let siblingLinkPath: string
beforeAll(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'ocm-repo-working-directory-'))
repoPath = path.join(root, 'repo')
siblingPath = path.join(root, 'sibling')
unrelatedPath = path.join(root, 'unrelated')
repoLinkPath = path.join(root, 'repo-link')
siblingLinkPath = path.join(root, 'sibling-link')
fs.mkdirSync(repoPath)
fs.mkdirSync(siblingPath)
fs.mkdirSync(unrelatedPath)
fs.symlinkSync(repoPath, repoLinkPath, 'dir')
fs.symlinkSync(siblingPath, siblingLinkPath, 'dir')
})
afterAll(() => {
fs.rmSync(root, { recursive: true, force: true })
})
function createRepo(): Repo {
return { fullPath: repoPath } as Repo
}
it('returns the repo path when no directory is given', async () => {
expect(await resolveRepoWorkingDirectory(createRepo(), undefined, async () => [])).toBe(repoPath)
})
it('returns the repo path for the repo directory itself', async () => {
expect(await resolveRepoWorkingDirectory(createRepo(), repoPath, async () => [])).toBe(repoPath)
})
it('returns the repo path for a symlinked equivalent of the repo directory', async () => {
expect(await resolveRepoWorkingDirectory(createRepo(), repoLinkPath, async () => [])).toBe(repoPath)
})
it('returns a matching sibling path', async () => {
const siblings = [{ fullPath: siblingPath }]
expect(await resolveRepoWorkingDirectory(createRepo(), siblingPath, async () => siblings)).toBe(siblingPath)
})
it('matches a sibling through a symlinked directory', async () => {
const siblings = [{ fullPath: siblingPath }]
expect(await resolveRepoWorkingDirectory(createRepo(), siblingLinkPath, async () => siblings)).toBe(siblingPath)
})
it('returns null for an unrelated directory', async () => {
const siblings = [{ fullPath: siblingPath }]
expect(await resolveRepoWorkingDirectory(createRepo(), unrelatedPath, async () => siblings)).toBeNull()
})
})
describe('findSiblingByDirectory', () => {
let root: string
let siblingPath: string
let siblingLinkPath: string
beforeAll(() => {
root = fs.mkdtempSync(path.join(os.tmpdir(), 'ocm-find-sibling-'))
siblingPath = path.join(root, 'sibling')
siblingLinkPath = path.join(root, 'sibling-link')
fs.mkdirSync(siblingPath)
fs.symlinkSync(siblingPath, siblingLinkPath, 'dir')
})
afterAll(() => {
fs.rmSync(root, { recursive: true, force: true })
})
it('returns the sibling whose canonical path matches', () => {
const sibling = { fullPath: siblingPath, id: 1 }
expect(findSiblingByDirectory([sibling], siblingPath)).toBe(sibling)
})
it('matches through a symlinked directory', () => {
const sibling = { fullPath: siblingPath, id: 1 }
expect(findSiblingByDirectory([sibling], siblingLinkPath)).toBe(sibling)
})
it('returns undefined when no sibling matches', () => {
const sibling = { fullPath: siblingPath, id: 1 }
expect(findSiblingByDirectory([sibling], path.join(root, 'missing'))).toBeUndefined()
})
})