mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix(frontend): remove permission immediately after responding (#205)
* fix(backend): normalize git credential extraheaders * fix(frontend): remove permission immediately after responding Call removePermission synchronously after a successful API response, matching the existing pattern used by replyToQuestion and rejectQuestion. This ensures the UI updates promptly even if the SSE permission.replied event is delayed or missed. --------- Co-authored-by: Chris Scott <>
This commit is contained in:
1 parent
320b06b0ac
commit
389120559f
2 files changed
+98
-30
No files matched your search
@@ -1,28 +1,25 @@
|
||||
import type { GitCredential } from '@opencode-manager/shared'
|
||||
|
||||
export function isGitHubHttpsUrl(repoUrl: string): boolean {
|
||||
try {
|
||||
const parsed = new URL(repoUrl)
|
||||
return parsed.protocol === 'https:' && parsed.hostname === 'github.com'
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
const url = normalizeGitCredentialUrl(repoUrl)
|
||||
return url?.protocol === 'https:' && url.hostname === 'github.com'
|
||||
}
|
||||
|
||||
export function getDefaultUsername(host: string): string {
|
||||
try {
|
||||
const parsed = new URL(host)
|
||||
const hostname = parsed.hostname.toLowerCase()
|
||||
const hostname = normalizeGitCredentialUrl(host)?.hostname.toLowerCase() ?? ''
|
||||
if (hostname === 'github.com') {
|
||||
return 'x-access-token'
|
||||
}
|
||||
return 'oauth2'
|
||||
}
|
||||
|
||||
if (hostname === 'github.com') {
|
||||
return 'x-access-token'
|
||||
}
|
||||
if (hostname === 'gitlab.com' || hostname.includes('gitlab')) {
|
||||
return 'oauth2'
|
||||
}
|
||||
return 'oauth2'
|
||||
function normalizeGitCredentialUrl(host: string): URL | null {
|
||||
const rawHost = host.trim().replace(/\/+$/, '')
|
||||
if (!rawHost) return null
|
||||
try {
|
||||
return new URL(rawHost.includes('://') ? rawHost : `https://${rawHost}`)
|
||||
} catch {
|
||||
return 'oauth2'
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,11 +62,9 @@ export function extractHostFromSSHUrl(url: string): string | null {
|
||||
return null
|
||||
}
|
||||
|
||||
export function normalizeHost(host: string): string {
|
||||
if (!host.endsWith('/')) {
|
||||
return `${host}/`
|
||||
}
|
||||
return host
|
||||
export function normalizeHost(host: string): string | null {
|
||||
const url = normalizeGitCredentialUrl(host)
|
||||
return url ? `${url.protocol}//${url.host}/` : null
|
||||
}
|
||||
|
||||
export function createGitEnv(credentials: GitCredential[]): Record<string, string> {
|
||||
@@ -90,6 +85,9 @@ export function createGitEnv(credentials: GitCredential[]): Record<string, strin
|
||||
}
|
||||
|
||||
const host = normalizeHost(cred.host)
|
||||
if (!host) {
|
||||
continue
|
||||
}
|
||||
const username = cred.username || getDefaultUsername(host)
|
||||
const basicAuth = Buffer.from(`${username}:${cred.token}`, 'utf8').toString('base64')
|
||||
|
||||
@@ -107,12 +105,9 @@ export function findGitHubCredential(credentials: GitCredential[]): GitCredentia
|
||||
if (!credentials || credentials.length === 0) return null
|
||||
|
||||
return credentials.find(cred => {
|
||||
try {
|
||||
const parsed = new URL(cred.host)
|
||||
return parsed.hostname.toLowerCase() === 'github.com'
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
if (cred.type === 'ssh') return false
|
||||
const hostname = normalizeGitCredentialUrl(cred.host)?.hostname.toLowerCase()
|
||||
return hostname === 'github.com'
|
||||
}) || null
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { fetchGitHubUserInfo, findGitHubCredential, resolveGitIdentity, createGitIdentityEnv, getSSHCredentialsForHost } from '../../src/utils/git-auth'
|
||||
import { fetchGitHubUserInfo, findGitHubCredential, resolveGitIdentity, createGitIdentityEnv, getSSHCredentialsForHost, createGitEnv, getDefaultUsername, normalizeHost } from '../../src/utils/git-auth'
|
||||
import type { GitCredential } from '@opencode-manager/shared'
|
||||
|
||||
describe('fetchGitHubUserInfo', () => {
|
||||
@@ -206,6 +206,62 @@ describe('fetchGitHubUserInfo', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('createGitEnv', () => {
|
||||
it('creates URL-scoped extraheader keys that git can match against HTTPS remotes', () => {
|
||||
const env = createGitEnv([
|
||||
{ name: 'github', host: 'github.com', type: 'pat', token: 'github-token' },
|
||||
])
|
||||
|
||||
expect(env.GIT_CONFIG_COUNT).toBe('1')
|
||||
expect(env.GIT_CONFIG_KEY_0).toBe('http.https://github.com/.extraheader')
|
||||
expect(env.GIT_CONFIG_VALUE_0).toBe(`AUTHORIZATION: basic ${Buffer.from('x-access-token:github-token', 'utf8').toString('base64')}`)
|
||||
})
|
||||
|
||||
it('normalizes hosts with protocols, paths, and trailing slashes', () => {
|
||||
const env = createGitEnv([
|
||||
{ name: 'github', host: 'https://github.com/', type: 'pat', token: 'github-token' },
|
||||
{ name: 'gitlab', host: 'https://gitlab.com/group/project.git', type: 'pat', token: 'gitlab-token' },
|
||||
])
|
||||
|
||||
expect(env.GIT_CONFIG_COUNT).toBe('2')
|
||||
expect(env.GIT_CONFIG_KEY_0).toBe('http.https://github.com/.extraheader')
|
||||
expect(env.GIT_CONFIG_KEY_1).toBe('http.https://gitlab.com/.extraheader')
|
||||
})
|
||||
|
||||
it('skips credentials whose host cannot be parsed', () => {
|
||||
const env = createGitEnv([
|
||||
{ name: 'broken', host: ' ', type: 'pat', token: 'broken-token' },
|
||||
{ name: 'github', host: 'github.com', type: 'pat', token: 'github-token' },
|
||||
])
|
||||
|
||||
expect(env.GIT_CONFIG_COUNT).toBe('1')
|
||||
expect(env.GIT_CONFIG_KEY_0).toBe('http.https://github.com/.extraheader')
|
||||
})
|
||||
})
|
||||
|
||||
describe('normalizeHost', () => {
|
||||
it('returns a URL-form host prefix for git url matching', () => {
|
||||
expect(normalizeHost('github.com')).toBe('https://github.com/')
|
||||
expect(normalizeHost('https://github.com/owner/repo.git')).toBe('https://github.com/')
|
||||
})
|
||||
|
||||
it('returns null when the host cannot be parsed', () => {
|
||||
expect(normalizeHost(' ')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('getDefaultUsername', () => {
|
||||
it('uses the GitHub token username for bare and URL hosts', () => {
|
||||
expect(getDefaultUsername('github.com')).toBe('x-access-token')
|
||||
expect(getDefaultUsername('https://github.com/')).toBe('x-access-token')
|
||||
})
|
||||
|
||||
it('falls back to oauth2 for non-github hosts', () => {
|
||||
expect(getDefaultUsername('gitlab.com')).toBe('oauth2')
|
||||
expect(getDefaultUsername('https://git.example.com/')).toBe('oauth2')
|
||||
})
|
||||
})
|
||||
|
||||
describe('findGitHubCredential', () => {
|
||||
it('should find GitHub credential in credentials array', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
@@ -250,6 +306,23 @@ describe('findGitHubCredential', () => {
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it('should find GitHub credential when host is stored without a protocol', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'github', host: 'github.com', type: 'pat', token: 'github-token' },
|
||||
]
|
||||
|
||||
expect(findGitHubCredential(credentials)).toEqual(credentials[0])
|
||||
})
|
||||
|
||||
it('should ignore SSH credentials when looking up GitHub PAT', () => {
|
||||
const credentials: GitCredential[] = [
|
||||
{ name: 'github-ssh', host: 'git@github.com', type: 'ssh', token: 'ssh-key' },
|
||||
{ name: 'github-pat', host: 'https://github.com/', type: 'pat', token: 'pat-token' },
|
||||
]
|
||||
|
||||
expect(findGitHubCredential(credentials)).toEqual(credentials[1])
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolveGitIdentity', () => {
|
||||
@@ -507,4 +580,4 @@ describe('getSSHCredentialsForHost', () => {
|
||||
const creds = [makeSSHCred('git@github.com:2222')]
|
||||
expect(getSSHCredentialsForHost(creds, 'github.com:22')).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user