mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
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:
1 parent
ba08f5c24a
commit
7c8a4fe64c
31 files changed
+1136
-132
No files matched your search
@@ -0,0 +1,119 @@
|
||||
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
|
||||
import { promises as fs } from 'fs'
|
||||
import path from 'path'
|
||||
import os from 'os'
|
||||
import { pathToFileURL } from 'url'
|
||||
import { installGhEnvPlugin, getGhEnvPluginDir } from '../../src/services/opencode-gh-env-plugin'
|
||||
|
||||
type ShellEnvHook = (
|
||||
input: { cwd: string },
|
||||
output: { env: Record<string, string> },
|
||||
) => Promise<void>
|
||||
type PluginFactory = () => Promise<{ 'shell.env': ShellEnvHook }>
|
||||
|
||||
async function loadPlugin(configHome: string): Promise<PluginFactory> {
|
||||
const file = path.join(getGhEnvPluginDir(configHome), 'ocm-gh-env.js')
|
||||
const mod = await import(pathToFileURL(file).href)
|
||||
return mod.default as PluginFactory
|
||||
}
|
||||
|
||||
describe('ocm-gh-env plugin', () => {
|
||||
let configHome: string
|
||||
|
||||
beforeEach(async () => {
|
||||
configHome = await fs.mkdtemp(path.join(os.tmpdir(), 'ocm-ghenv-'))
|
||||
await installGhEnvPlugin(configHome)
|
||||
process.env.OCM_INTERNAL_API_URL = 'http://localhost:5003/api/internal'
|
||||
process.env.OCM_INTERNAL_TOKEN = 'secret-token'
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
vi.unstubAllGlobals()
|
||||
delete process.env.OCM_INTERNAL_API_URL
|
||||
delete process.env.OCM_INTERNAL_TOKEN
|
||||
await fs.rm(configHome, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
it('writes the plugin file into the auto-discovery dir', async () => {
|
||||
const file = path.join(getGhEnvPluginDir(configHome), 'ocm-gh-env.js')
|
||||
await expect(fs.access(file)).resolves.toBeUndefined()
|
||||
})
|
||||
|
||||
it('injects fetched GH env into output.env', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
||||
})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const factory = await loadPlugin(configHome)
|
||||
const hooks = await factory()
|
||||
const output = { env: {} as Record<string, string> }
|
||||
await hooks['shell.env']({ cwd: '/repo' }, output)
|
||||
|
||||
expect(output.env).toEqual({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' })
|
||||
const [url, options] = fetchMock.mock.calls[0]!
|
||||
expect(url.toString()).toBe('http://localhost:5003/api/internal/git-credentials/gh-env?cwd=%2Frepo')
|
||||
expect(options).toEqual({ headers: { Authorization: 'Bearer secret-token' } })
|
||||
})
|
||||
|
||||
it('does not fetch when the internal env vars are missing', async () => {
|
||||
delete process.env.OCM_INTERNAL_API_URL
|
||||
delete process.env.OCM_INTERNAL_TOKEN
|
||||
const fetchMock = vi.fn()
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const factory = await loadPlugin(configHome)
|
||||
const hooks = await factory()
|
||||
const output = { env: {} as Record<string, string> }
|
||||
await hooks['shell.env']({ cwd: '/repo' }, output)
|
||||
|
||||
expect(output.env).toEqual({})
|
||||
expect(fetchMock).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('never throws when the fetch fails', async () => {
|
||||
const fetchMock = vi.fn().mockRejectedValue(new Error('network down'))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const factory = await loadPlugin(configHome)
|
||||
const hooks = await factory()
|
||||
const output = { env: {} as Record<string, string> }
|
||||
|
||||
await expect(hooks['shell.env']({ cwd: '/repo' }, output)).resolves.toBeUndefined()
|
||||
expect(output.env).toEqual({})
|
||||
})
|
||||
|
||||
it('caches results within the TTL so rapid calls fetch once', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
||||
})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const factory = await loadPlugin(configHome)
|
||||
const hooks = await factory()
|
||||
const out1 = { env: {} as Record<string, string> }
|
||||
const out2 = { env: {} as Record<string, string> }
|
||||
await hooks['shell.env']({ cwd: '/repo' }, out1)
|
||||
await hooks['shell.env']({ cwd: '/repo' }, out2)
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1)
|
||||
expect(out2.env).toEqual({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' })
|
||||
})
|
||||
|
||||
it('caches results per cwd', async () => {
|
||||
const fetchMock = vi.fn().mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => ({ GH_TOKEN: 'ghp', GITHUB_TOKEN: 'ghp' }),
|
||||
})
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
const factory = await loadPlugin(configHome)
|
||||
const hooks = await factory()
|
||||
await hooks['shell.env']({ cwd: '/repo-a' }, { env: {} })
|
||||
await hooks['shell.env']({ cwd: '/repo-b' }, { env: {} })
|
||||
|
||||
expect(fetchMock).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user