mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
fix(sandbox): guard enable path and harden UI rendering performance (#341)
* fix(sandbox): guard enable path and harden UI rendering performance * chore(deps): allow esbuild postinstall in pnpm onlyBuiltDependencies * fix(sandbox): align availability reporting and address PR review feedback * fix(sandbox): block planning without process attestation
This commit is contained in:
1 parent
ba8e8f4b29
commit
d63198ec8a
19 files changed
+501
-82
No files matched your search
@@ -1,10 +1,11 @@
|
||||
import type { Database } from 'bun:sqlite'
|
||||
import type { GitCredential, Repo } from '@opencode-manager/shared'
|
||||
import type { GitCredential, Repo, UserPreferences } from '@opencode-manager/shared'
|
||||
import { SettingsService } from './settings'
|
||||
import {
|
||||
findPatCredentialForHost,
|
||||
getSSHCredentialsForHost,
|
||||
createGitEnv,
|
||||
createGhCliEnv,
|
||||
findGitHubCredential,
|
||||
type ResolvedGitCredential,
|
||||
} from '../utils/git-auth'
|
||||
@@ -22,6 +23,12 @@ interface CredentialResolutionOptions {
|
||||
repoId?: number
|
||||
}
|
||||
|
||||
interface CredentialResolutionContext {
|
||||
preferences: UserPreferences
|
||||
credentials: GitCredential[]
|
||||
repo: Repo | null
|
||||
}
|
||||
|
||||
export class CredentialProvider {
|
||||
private settingsService: SettingsService
|
||||
private database: Database
|
||||
@@ -32,8 +39,7 @@ export class CredentialProvider {
|
||||
}
|
||||
|
||||
getGitCredentials(): GitCredential[] {
|
||||
const settings = this.settingsService.getSettings('default')
|
||||
return (settings.preferences.gitCredentials || []) as GitCredential[]
|
||||
return this.getCredentials(this.getPreferences())
|
||||
}
|
||||
|
||||
getGitCredentialById(credentialId: string | undefined): GitCredential | null {
|
||||
@@ -42,10 +48,10 @@ export class CredentialProvider {
|
||||
}
|
||||
|
||||
getPatCredentialForHost(hostname: string, options: CredentialResolutionOptions = {}): ResolvedGitCredential | null {
|
||||
const credentials = this.getGitCredentials()
|
||||
const selectedCredential = this.getSelectedCredential(options, credentials)
|
||||
const context = this.resolveContext(options)
|
||||
const selectedCredential = this.getSelectedCredential(context)
|
||||
const selectedMatch = selectedCredential ? findPatCredentialForHost([selectedCredential], hostname) : null
|
||||
return selectedMatch ?? findPatCredentialForHost(credentials, hostname)
|
||||
return selectedMatch ?? findPatCredentialForHost(context.credentials, hostname)
|
||||
}
|
||||
|
||||
getSshCredentialsForHost(host: string): GitCredential[] {
|
||||
@@ -57,24 +63,22 @@ export class CredentialProvider {
|
||||
}
|
||||
|
||||
getGitEnv(options: CredentialResolutionOptions = {}): Record<string, string> {
|
||||
const credentials = this.getGitCredentials()
|
||||
return createGitEnv(credentials, this.getSelectedCredential(options, credentials))
|
||||
return this.getGitEnvForContext(this.resolveContext(options))
|
||||
}
|
||||
|
||||
isSandboxGitCredentialsAllowed(options: CredentialResolutionOptions = {}): boolean {
|
||||
const repo = this.resolveRepo(options)
|
||||
if (repo) {
|
||||
const repoOverride = getRepoSandboxGitCredentials(this.database, repo.id)
|
||||
if (repoOverride !== null) return repoOverride
|
||||
}
|
||||
|
||||
return this.settingsService.getSettings('default').preferences.sandbox?.gitCredentials === true
|
||||
return this.getSandboxGitCredentialsAllowed(options)
|
||||
}
|
||||
|
||||
getSandboxGitEnv(options: CredentialResolutionOptions = {}): Record<string, string> {
|
||||
if (!this.isSandboxGitCredentialsAllowed(options)) return {}
|
||||
const repo = this.resolveRepo(options)
|
||||
const repoOverride = repo ? getRepoSandboxGitCredentials(this.database, repo.id) : null
|
||||
if (repoOverride === false) return {}
|
||||
|
||||
const gitEnv = this.getGitEnv(options)
|
||||
const context = this.resolveContext(options, repo)
|
||||
if (repoOverride !== true && context.preferences.sandbox?.gitCredentials !== true) return {}
|
||||
|
||||
const gitEnv = this.getGitEnvForContext(context)
|
||||
if (gitEnv.GIT_CONFIG_COUNT === '0') return {}
|
||||
|
||||
const { env, dropped } = limitForwardedGitConfigs(gitEnv)
|
||||
@@ -84,7 +88,43 @@ export class CredentialProvider {
|
||||
)
|
||||
}
|
||||
|
||||
return { ...env, ...this.getGhCliEnv(options) }
|
||||
return { ...env, ...this.getGhCliEnvForContext(context) }
|
||||
}
|
||||
|
||||
getGhCliEnv(options: CredentialResolutionOptions = {}): Record<string, string> {
|
||||
return this.getGhCliEnvForContext(this.resolveContext(options))
|
||||
}
|
||||
|
||||
private resolveContext(options: CredentialResolutionOptions, repo = this.resolveRepo(options)): CredentialResolutionContext {
|
||||
const preferences = this.getPreferences()
|
||||
return {
|
||||
preferences,
|
||||
credentials: this.getCredentials(preferences),
|
||||
repo,
|
||||
}
|
||||
}
|
||||
|
||||
private getPreferences(): UserPreferences {
|
||||
return this.settingsService.getSettings('default').preferences
|
||||
}
|
||||
|
||||
private getCredentials(preferences: UserPreferences): GitCredential[] {
|
||||
return (preferences.gitCredentials || []) as GitCredential[]
|
||||
}
|
||||
|
||||
private getGitEnvForContext(context: CredentialResolutionContext): Record<string, string> {
|
||||
return createGitEnv(context.credentials, this.getSelectedCredential(context))
|
||||
}
|
||||
|
||||
private getGhCliEnvForContext(context: CredentialResolutionContext): Record<string, string> {
|
||||
const credential = this.getGhCliCredential(context)
|
||||
return createGhCliEnv(credential ? [credential] : [])
|
||||
}
|
||||
|
||||
private getSandboxGitCredentialsAllowed(options: CredentialResolutionOptions): boolean {
|
||||
const repo = this.resolveRepo(options)
|
||||
const repoOverride = repo ? getRepoSandboxGitCredentials(this.database, repo.id) : null
|
||||
return repoOverride ?? (this.getPreferences().sandbox?.gitCredentials === true)
|
||||
}
|
||||
|
||||
private resolveRepo(options: CredentialResolutionOptions): Repo | null {
|
||||
@@ -94,34 +134,25 @@ export class CredentialProvider {
|
||||
return options.cwd ? getRepoByDirectory(this.database, options.cwd) : null
|
||||
}
|
||||
|
||||
getGhCliEnv(options: CredentialResolutionOptions = {}): Record<string, string> {
|
||||
const credential = this.getGhCliCredential(options)
|
||||
if (!credential?.token) return {}
|
||||
return { GH_TOKEN: credential.token, GITHUB_TOKEN: credential.token }
|
||||
}
|
||||
|
||||
private getGhCliCredential(options: CredentialResolutionOptions): GitCredential | null {
|
||||
const credentials = this.getGitCredentials()
|
||||
const selectedCredential = this.getSelectedCredential(options, credentials)
|
||||
private getGhCliCredential(context: CredentialResolutionContext): GitCredential | null {
|
||||
const selectedCredential = this.getSelectedCredential(context)
|
||||
if (this.isGithubPatCredential(selectedCredential)) return selectedCredential
|
||||
|
||||
return findGitHubCredential(credentials)
|
||||
return findGitHubCredential(context.credentials)
|
||||
}
|
||||
|
||||
private getSelectedCredential(options: CredentialResolutionOptions, credentials: GitCredential[]): GitCredential | null {
|
||||
const repoCredential = this.getRepoCredential(options, credentials)
|
||||
private getSelectedCredential(context: CredentialResolutionContext): GitCredential | null {
|
||||
const repoCredential = this.getRepoCredential(context)
|
||||
if (repoCredential) return repoCredential
|
||||
|
||||
const settings = this.settingsService.getSettings('default')
|
||||
return credentials.find((credential) => credential.id === settings.preferences.defaultGitCredentialId) ?? null
|
||||
return context.credentials.find((credential) => credential.id === context.preferences.defaultGitCredentialId) ?? null
|
||||
}
|
||||
|
||||
private getRepoCredential(options: CredentialResolutionOptions, credentials: GitCredential[]): GitCredential | null {
|
||||
const repo = this.resolveRepo(options)
|
||||
if (!repo) return null
|
||||
private getRepoCredential(context: CredentialResolutionContext): GitCredential | null {
|
||||
if (!context.repo) return null
|
||||
|
||||
const credentialId = getRepoGitCredentialId(this.database, repo.id)
|
||||
return credentials.find((credential) => credential.id === credentialId) ?? null
|
||||
const credentialId = getRepoGitCredentialId(this.database, context.repo.id)
|
||||
return context.credentials.find((credential) => credential.id === credentialId) ?? null
|
||||
}
|
||||
|
||||
private isGithubPatCredential(credential: GitCredential | null): credential is GitCredential {
|
||||
|
||||
Reference in new issue
Block a user