refactor: extract CredentialProvider service and consolidate git-auth utilities (#283)

* refactor: extract CredentialProvider service and consolidate git-auth utilities

* refactor: add git-credentials route and gh env plugin, pass internal token to OpenCode

* refactor: update credential provider integration and git route

* refactor: integrate credential provider across git auth, routes, and IPC
This commit is contained in:
Chris Scott committed 2026-06-20 23:20:39 -04:00
1 parent ba08f5c24a
commit 7c8a4fe64c
31 files changed
+1136 -132

No files matched your search

@@ -0,0 +1,14 @@
import { Hono } from 'hono'
import type { Database } from 'bun:sqlite'
import { CredentialProvider } from '../../services/credential-provider'
export function createInternalGitCredentialsRoutes(db: Database) {
const app = new Hono()
app.get('/gh-env', (c) => {
const provider = new CredentialProvider(db)
return c.json(provider.getGhCliEnv({ cwd: c.req.query('cwd') }))
})
return app
}
+2
View File
@@ -13,6 +13,7 @@ import { createInternalRepoSyncRoutes } from './repo-sync'
import { createInternalRepoMirrorRoutes as mirrorRoutes } from './repo-mirror'
import { createInternalOpenCodeWorkspacesRoutes } from './opencode-workspaces'
import { createInternalAssistantRoutes } from './assistant'
import { createInternalGitCredentialsRoutes } from './git-credentials'
export function createInternalRoutes(
db: Database,
@@ -34,5 +35,6 @@ export function createInternalRoutes(
app.route('/repos', repos)
app.route('/opencode-workspaces', createInternalOpenCodeWorkspacesRoutes(db))
app.route('/assistant', createInternalAssistantRoutes(openCodeClient))
app.route('/git-credentials', createInternalGitCredentialsRoutes(db))
return app
}
+3 -1
View File
@@ -7,12 +7,14 @@ import { parseGitError } from '../utils/git-errors'
import { GitService } from '../services/git/GitService'
import type { GitAuthService } from '../services/git-auth'
import { SettingsService } from '../services/settings'
import { CredentialProvider } from '../services/credential-provider'
import type { GitStatusResponse } from '../types/git'
export function createRepoGitRoutes(database: Database, gitAuthService: GitAuthService) {
const app = new Hono()
const settingsService = new SettingsService(database)
const git = new GitService(gitAuthService, settingsService)
const credentialProvider = new CredentialProvider(database)
const git = new GitService(gitAuthService, settingsService, credentialProvider)
app.get('/:id/git/status', async (c) => {
try {
+40 -3
View File
@@ -3,7 +3,7 @@ import type { ContentfulStatusCode } from 'hono/utils/http-status'
import type { Database } from 'bun:sqlite'
import type { Repo } from '@opencode-manager/shared/types'
import { DiscoverReposRequestSchema, AssistantModeInitRequestSchema } from '@opencode-manager/shared/schemas'
import { listRepos, getRepoById, updateLastAccessed, updateRepoConfigName } from '../db/queries'
import { listRepos, getRepoById, updateLastAccessed, updateRepoConfigName, getRepoGitCredentialId, setRepoGitCredentialId } from '../db/queries'
import * as repoService from '../services/repo'
import * as archiveService from '../services/archive'
import { SettingsService } from '../services/settings'
@@ -36,6 +36,13 @@ function resolveRepo(database: Database, id: number): Repo | null {
return getRepoById(database, id) ?? (id === ASSISTANT_REPO_ID ? buildAssistantRepo() : null)
}
function withRepoSettings(database: Database, repo: Repo): Repo {
return {
...repo,
gitCredentialId: getRepoGitCredentialId(database, repo.id) ?? undefined,
}
}
export function createRepoRoutes(
database: Database,
gitAuthService: GitAuthService,
@@ -128,7 +135,7 @@ app.get('/', async (c) => {
repos.map(async (repo) => {
const env = gitAuthService.getGitEnvironment()
const currentBranch = repo.id === ASSISTANT_REPO_ID ? undefined : await repoService.getCurrentBranch(repo, env)
return { ...repo, currentBranch }
return { ...withRepoSettings(database, repo), currentBranch }
})
)
return c.json(reposWithCurrentBranch)
@@ -170,7 +177,7 @@ app.get('/', async (c) => {
const currentBranch = id === ASSISTANT_REPO_ID ? undefined : await repoService.getCurrentBranch(repo, gitAuthService.getGitEnvironment())
return c.json({ ...repo, currentBranch })
return c.json({ ...withRepoSettings(database, repo), currentBranch })
} catch (error: unknown) {
logger.error('Failed to get repo:', error)
return c.json({ error: getErrorMessage(error) }, 500)
@@ -212,6 +219,36 @@ app.get('/', async (c) => {
}
})
app.patch('/:id/git-credential', async (c) => {
try {
const id = parseInt(c.req.param('id'))
const repo = getRepoById(database, id)
if (!repo) {
return c.json({ error: 'Repo not found' }, 404)
}
const body = await c.req.json()
const credentialId = typeof body.credentialId === 'string' && body.credentialId.trim() !== ''
? body.credentialId.trim()
: null
if (credentialId) {
const settingsService = new SettingsService(database)
const settings = settingsService.getSettings()
if (!(settings.preferences.gitCredentials || []).some((credential) => credential.id === credentialId)) {
return c.json({ error: 'Credential not found' }, 400)
}
}
setRepoGitCredentialId(database, id, credentialId)
return c.json(withRepoSettings(database, repo))
} catch (error: unknown) {
logger.error('Failed to update repo git credential:', error)
return c.json({ error: getErrorMessage(error) }, 500)
}
})
app.delete('/:id/workspaces/:workspaceId', async (c) => {
try {
const id = parseInt(c.req.param('id'))
+6 -1
View File
@@ -1,6 +1,7 @@
import { Hono, type Context } from 'hono'
import { z } from 'zod'
import { execSync, spawnSync } from 'child_process'
import { randomUUID } from 'crypto'
import { existsSync } from 'fs'
import { resolve, dirname } from 'path'
import type { Database } from 'bun:sqlite'
@@ -394,6 +395,7 @@ export function createSettingsRoutes(db: Database, gitAuthService: GitAuthServic
const result: GitCredential = {
...cred,
id: cred.id || randomUUID(),
sshPrivateKeyEncrypted: encryptSecret(cred.sshPrivateKey),
hasPassphrase: validation.hasPassphrase,
passphrase: cred.passphrase ? encryptSecret(cred.passphrase) : undefined,
@@ -401,10 +403,13 @@ export function createSettingsRoutes(db: Database, gitAuthService: GitAuthServic
delete result.sshPrivateKey
return result
}
return cred
return { ...cred, id: cred.id || randomUUID() }
})
)
validated.preferences.gitCredentials = validations
if (validated.preferences.defaultGitCredentialId && !validations.some((cred) => cred.id === validated.preferences.defaultGitCredentialId)) {
validated.preferences.defaultGitCredentialId = undefined
}
}
const currentSettings = settingsService.getSettings(userId)