From fcc305788d8373405d7f8a8fb887ae1685dd9f74 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 21 Dec 2025 00:08:56 -0600 Subject: [PATCH] Use origin instead of protocol, host for git credentials --- src/components/GitDialog.test.tsx | 5 ++- src/contexts/GitSettingsContext.ts | 3 +- src/lib/configUtils.test.ts | 9 ++--- src/lib/configUtils.ts | 6 +-- src/lib/gitCredentials.test.ts | 13 +++++-- src/lib/gitCredentials.ts | 18 ++++----- src/pages/GitSettings.tsx | 62 ++++-------------------------- 7 files changed, 34 insertions(+), 82 deletions(-) diff --git a/src/components/GitDialog.test.tsx b/src/components/GitDialog.test.tsx index c7201a8c..8f6b10c1 100644 --- a/src/components/GitDialog.test.tsx +++ b/src/components/GitDialog.test.tsx @@ -61,8 +61,9 @@ describe('GitDialog', () => { settings: { credentials: [ { - protocol: 'https', - host: 'github.com', + id: crypto.randomUUID(), + name: 'GitHub', + origin: 'https://github.com', username: 'git', password: 'github-token', }, diff --git a/src/contexts/GitSettingsContext.ts b/src/contexts/GitSettingsContext.ts index 068b3c68..ab446f63 100644 --- a/src/contexts/GitSettingsContext.ts +++ b/src/contexts/GitSettingsContext.ts @@ -3,8 +3,7 @@ import { createContext } from 'react'; export interface GitCredential { id: string; // UUID generated with crypto.randomUUID() name: string; // Display name for the credential - protocol: string; - host: string; // Includes port if non-standard (e.g., "github.com:8080") + origin: string; // Full origin URL (e.g., "https://github.com") username: string; password: string; } diff --git a/src/lib/configUtils.test.ts b/src/lib/configUtils.test.ts index 4e07afd6..ce6c1ad0 100644 --- a/src/lib/configUtils.test.ts +++ b/src/lib/configUtils.test.ts @@ -159,8 +159,7 @@ describe('configUtils', () => { { id: crypto.randomUUID(), name: 'GitHub', - protocol: 'https', - host: 'github.com', + origin: 'https://github.com', username: 'user', password: 'token123', }, @@ -194,8 +193,7 @@ describe('configUtils', () => { expect(mockFS.readFile).toHaveBeenCalledWith('/config/git.json', 'utf8'); expect(result.credentials).toHaveLength(1); expect(result.credentials[0]).toMatchObject({ - protocol: 'https', - host: 'github.com', + origin: 'https://github.com', username: 'user', password: 'token123', name: 'GitHub', @@ -220,8 +218,7 @@ describe('configUtils', () => { expect(result.credentials).toHaveLength(1); expect(result.credentials[0]).toMatchObject({ - protocol: 'https', - host: 'git.example.com:8080', + origin: 'https://git.example.com:8080', username: 'user', password: 'token456', name: 'git.example.com', diff --git a/src/lib/configUtils.ts b/src/lib/configUtils.ts index 78c23cd7..54d419c7 100644 --- a/src/lib/configUtils.ts +++ b/src/lib/configUtils.ts @@ -61,8 +61,7 @@ const aiSettingsSchema = z.object({ const gitCredentialSchema = z.object({ id: z.string(), name: z.string(), - protocol: z.string(), - host: z.string(), + origin: z.string(), username: z.string(), password: z.string(), }); @@ -251,8 +250,7 @@ function convertCredentialsToNewFormat( return { id: crypto.randomUUID(), name: generateCredentialName(host), - protocol: url.protocol.replace(/:$/, ''), - host, + origin, username: cred.username, password: cred.password, }; diff --git a/src/lib/gitCredentials.test.ts b/src/lib/gitCredentials.test.ts index d4a39a00..d5998f6b 100644 --- a/src/lib/gitCredentials.test.ts +++ b/src/lib/gitCredentials.test.ts @@ -3,15 +3,20 @@ import { findCredentialsForRepo } from './gitCredentials'; import type { GitCredential } from '@/contexts/GitSettingsContext'; // Helper to create test credentials with required fields -function createTestCredential(overrides: Partial): GitCredential { +function createTestCredential(overrides: Partial & { protocol?: string; host?: string } = {}): GitCredential { + // Extract protocol and host if provided (for backward compatibility with tests) + const { protocol = 'https', host = 'github.com', ...rest } = overrides; + + // Construct origin from protocol and host if origin is not provided + const origin = rest.origin || `${protocol}://${host}`; + return { id: crypto.randomUUID(), name: 'Test Credential', - protocol: 'https', - host: 'github.com', + origin, username: 'user', password: 'pass', - ...overrides, + ...rest, }; } diff --git a/src/lib/gitCredentials.ts b/src/lib/gitCredentials.ts index 64a1b176..7f896e4b 100644 --- a/src/lib/gitCredentials.ts +++ b/src/lib/gitCredentials.ts @@ -24,30 +24,30 @@ function matchesCredential( urlString: string ): boolean { const targetURL = new URL(urlString); - const credURL = new URL(`${credential.protocol}://${credential.host}/`); - + const credURL = new URL(credential.origin); + // 1. Protocol must match exactly if (credURL.protocol !== targetURL.protocol) { return false; } - + // 2. Hostname must match exactly if (credURL.hostname !== targetURL.hostname) { return false; } - + // 3. Port must match exactly (after default conversion) if (getEffectivePort(credURL) !== getEffectivePort(targetURL)) { return false; } - + // 4. Username must match if both are specified if (credential.username && targetURL.username) { if (credential.username !== targetURL.username) { return false; } } - + return true; } @@ -61,15 +61,15 @@ export function findCredentialsForRepo( ): GitCredential | undefined { const targetURL = new URL(urlString); const matches = credentials.filter(cred => matchesCredential(cred, urlString)); - + if (matches.length === 0) { return undefined; } - + // Prefer credentials with matching username const withUsername = matches.find( cred => cred.username && cred.username === targetURL.username, ); - + return withUsername ?? matches[0]; } \ No newline at end of file diff --git a/src/pages/GitSettings.tsx b/src/pages/GitSettings.tsx index 71243234..2eb77629 100644 --- a/src/pages/GitSettings.tsx +++ b/src/pages/GitSettings.tsx @@ -77,8 +77,7 @@ interface SortableCredentialItemProps { function SortableCredentialItem({ credential, onUpdate, onRemove, showDragHandle }: SortableCredentialItemProps) { const { t } = useTranslation(); - const origin = `${credential.protocol}://${credential.host}`; - const preset = PRESET_PROVIDERS.find(p => p.origin === origin); + const preset = PRESET_PROVIDERS.find(p => p.origin === credential.origin); const isCustom = !preset; const { @@ -96,32 +95,10 @@ function SortableCredentialItem({ credential, onUpdate, onRemove, showDragHandle opacity: isDragging ? 0.5 : 1, }; - // Helper to parse origin URL into credential parts - const parseOrigin = (origin: string): { protocol: string; host: string } => { - try { - const url = new URL(origin); - return { - protocol: url.protocol.replace(':', ''), // Remove trailing colon - host: url.host, // Includes port if non-standard (e.g., "github.com:8080") - }; - } catch { - // Fallback: assume https and treat as hostname - return { - protocol: 'https', - host: origin, - }; - } - }; - const updateCredential = (updates: Partial) => { onUpdate({ ...credential, ...updates }); }; - const handleOriginChange = (newOrigin: string) => { - const parsed = parseOrigin(newOrigin); - updateCredential(parsed); - }; - return ( )} } /> @@ -171,8 +148,8 @@ function SortableCredentialItem({ credential, onUpdate, onRemove, showDragHandle handleOriginChange(e.target.value)} + value={credential.origin} + onChange={(e) => updateCredential({ origin: e.target.value })} />
@@ -231,33 +208,15 @@ export function GitSettings() { }) ); - // Helper to parse origin URL into credential parts - const parseOrigin = (origin: string): { protocol: string; host: string } => { - try { - const url = new URL(origin); - return { - protocol: url.protocol.replace(':', ''), // Remove trailing colon - host: url.host, // Includes port if non-standard (e.g., "github.com:8080") - }; - } catch { - // Fallback: assume https and treat as hostname - return { - protocol: 'https', - host: origin, - }; - } - }; - const handleAddPresetProvider = (preset: PresetProvider) => { const token = presetTokens[preset.id] as string | undefined; if (!token?.trim()) return; - const parsed = parseOrigin(preset.origin); const newCredential: GitCredential = { id: crypto.randomUUID(), name: preset.name, - ...parsed, + origin: preset.origin, username: preset.username, password: token.trim(), }; @@ -275,12 +234,10 @@ export function GitSettings() { const handleAddCustomProvider = () => { if (!customName.trim() || !customOrigin.trim() || !customUsername.trim() || !customPassword.trim()) return; - const parsed = parseOrigin(customOrigin.trim()); - const newCredential: GitCredential = { id: crypto.randomUUID(), name: customName.trim(), - ...parsed, + origin: customOrigin.trim(), username: customUsername.trim(), password: customPassword.trim(), }; @@ -321,13 +278,8 @@ export function GitSettings() { } }; - // Helper to get origin string from credential - const getOriginFromCredential = (cred: GitCredential): string => { - return `${cred.protocol}://${cred.host}`; // host already includes port if non-standard - }; - // Get list of configured origins - const configuredOrigins = settings.credentials.map(getOriginFromCredential); + const configuredOrigins = settings.credentials.map(cred => cred.origin); const availablePresets = PRESET_PROVIDERS.filter(preset => !configuredOrigins.includes(preset.origin)); return (