From 9c2f4f9eb6f2a55b5f9f2bba02148996b47b8c80 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 20 Dec 2025 23:54:00 -0600 Subject: [PATCH] Add id and name to GitCredentials --- src/components/GitSettingsProvider.test.tsx | 27 ++- src/components/GitSettingsProvider.tsx | 19 +-- src/contexts/GitSettingsContext.ts | 5 +- src/hooks/useGitHubOAuth.ts | 2 + src/lib/configUtils.test.ts | 38 +++-- src/lib/configUtils.ts | 27 ++- src/lib/gitCredentials.test.ts | 180 +++++++------------- src/pages/GitSettings.tsx | 99 +++++++---- 8 files changed, 204 insertions(+), 193 deletions(-) diff --git a/src/components/GitSettingsProvider.test.tsx b/src/components/GitSettingsProvider.test.tsx index daa08fbd..e8898854 100644 --- a/src/components/GitSettingsProvider.test.tsx +++ b/src/components/GitSettingsProvider.test.tsx @@ -14,10 +14,12 @@ vi.mock('@/lib/configUtils', () => ({ // Test component that uses the hook function TestComponent() { - const { settings, addCredential, removeCredential, updateCredential, isConfigured } = useGitSettings(); + const { settings, addCredential, removeCredential, setCredentials, isConfigured } = useGitSettings(); const handleAdd = () => { const credential: GitCredential = { + id: crypto.randomUUID(), + name: 'GitHub', protocol: 'https', host: 'github.com', username: 'git', @@ -27,11 +29,20 @@ function TestComponent() { }; const handleUpdate = () => { - updateCredential(0, { password: 'updated-token' }); + const githubCred = settings.credentials.find(c => c.host === 'github.com'); + if (githubCred) { + const updatedCredentials = settings.credentials.map((cred) => + cred.id === githubCred.id ? { ...cred, password: 'updated-token' } : cred + ); + setCredentials(updatedCredentials); + } }; const handleRemove = () => { - removeCredential(0); + const githubCred = settings.credentials.find(c => c.host === 'github.com'); + if (githubCred) { + removeCredential(githubCred.id); + } }; const githubCred = settings.credentials.find(c => c.host === 'github.com'); @@ -168,14 +179,14 @@ describe('GitSettingsProvider', () => { expect(writeGitSettings).toHaveBeenCalledWith( expect.anything(), // fs instance expect.objectContaining({ - credentials: [ - { + credentials: expect.arrayContaining([ + expect.objectContaining({ protocol: 'https', host: 'github.com', username: 'git', password: 'test-token', - } - ] + }) + ]) }), '/config' // configPath ); @@ -186,6 +197,8 @@ describe('GitSettingsProvider', () => { const settings = { credentials: [ { + id: crypto.randomUUID(), + name: 'GitLab', protocol: 'https', host: 'gitlab.com', username: 'git', diff --git a/src/components/GitSettingsProvider.tsx b/src/components/GitSettingsProvider.tsx index 5d95d138..ba2e17a0 100644 --- a/src/components/GitSettingsProvider.tsx +++ b/src/components/GitSettingsProvider.tsx @@ -61,27 +61,13 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) { })); }; - const removeCredential = (index: number) => { + const removeCredential = (id: string) => { setSettings(prev => ({ ...prev, - credentials: prev.credentials.filter((_, i) => i !== index), + credentials: prev.credentials.filter((cred) => cred.id !== id), })); }; - const updateCredential = (index: number, credential: Partial) => { - setSettings(prev => { - const newCredentials = [...prev.credentials]; - newCredentials[index] = { - ...newCredentials[index], - ...credential, - }; - return { - ...prev, - credentials: newCredentials, - }; - }); - }; - const setCredentials = (credentials: GitCredential[]) => { setSettings(prev => ({ ...prev, @@ -96,7 +82,6 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) { updateSettings, addCredential, removeCredential, - updateCredential, setCredentials, isConfigured, isInitialized, diff --git a/src/contexts/GitSettingsContext.ts b/src/contexts/GitSettingsContext.ts index 77e7efe2..068b3c68 100644 --- a/src/contexts/GitSettingsContext.ts +++ b/src/contexts/GitSettingsContext.ts @@ -1,6 +1,8 @@ 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") username: string; @@ -18,8 +20,7 @@ export interface GitSettingsContextType { settings: GitSettings; updateSettings: (settings: Partial) => void; addCredential: (credential: GitCredential) => void; - removeCredential: (index: number) => void; - updateCredential: (index: number, credential: Partial) => void; + removeCredential: (id: string) => void; setCredentials: (credentials: GitCredential[]) => void; isConfigured: boolean; isInitialized: boolean; diff --git a/src/hooks/useGitHubOAuth.ts b/src/hooks/useGitHubOAuth.ts index 19dd5bec..589ae9c1 100644 --- a/src/hooks/useGitHubOAuth.ts +++ b/src/hooks/useGitHubOAuth.ts @@ -72,6 +72,8 @@ export function useGitHubOAuth() { // Store the credentials addCredential({ + id: crypto.randomUUID(), + name: 'GitHub', protocol: 'https', host: 'github.com', username: result.username || 'git', diff --git a/src/lib/configUtils.test.ts b/src/lib/configUtils.test.ts index 66681d19..4e07afd6 100644 --- a/src/lib/configUtils.test.ts +++ b/src/lib/configUtils.test.ts @@ -157,6 +157,8 @@ describe('configUtils', () => { const sampleGitSettingsNew: GitSettings = { credentials: [ { + id: crypto.randomUUID(), + name: 'GitHub', protocol: 'https', host: 'github.com', username: 'user', @@ -190,16 +192,16 @@ describe('configUtils', () => { const result = await readGitSettings(mockFS); expect(mockFS.readFile).toHaveBeenCalledWith('/config/git.json', 'utf8'); - expect(result).toEqual({ - credentials: [ - { - protocol: 'https', - host: 'github.com', - username: 'user', - password: 'token123', - }, - ], + expect(result.credentials).toHaveLength(1); + expect(result.credentials[0]).toMatchObject({ + protocol: 'https', + host: 'github.com', + username: 'user', + password: 'token123', + name: 'GitHub', }); + expect(result.credentials[0].id).toBeDefined(); + expect(typeof result.credentials[0].id).toBe('string'); }); it('should handle old format with custom ports', async () => { @@ -216,16 +218,16 @@ describe('configUtils', () => { const result = await readGitSettings(mockFS); - expect(result).toEqual({ - credentials: [ - { - protocol: 'https', - host: 'git.example.com:8080', - username: 'user', - password: 'token456', - }, - ], + expect(result.credentials).toHaveLength(1); + expect(result.credentials[0]).toMatchObject({ + protocol: 'https', + host: 'git.example.com:8080', + username: 'user', + password: 'token456', + name: 'git.example.com', }); + expect(result.credentials[0].id).toBeDefined(); + expect(typeof result.credentials[0].id).toBe('string'); }); it('should return default settings if file does not exist and no localStorage data', async () => { diff --git a/src/lib/configUtils.ts b/src/lib/configUtils.ts index 2b36c978..78c23cd7 100644 --- a/src/lib/configUtils.ts +++ b/src/lib/configUtils.ts @@ -59,6 +59,8 @@ const aiSettingsSchema = z.object({ // New format for git credentials const gitCredentialSchema = z.object({ + id: z.string(), + name: z.string(), protocol: z.string(), host: z.string(), username: z.string(), @@ -217,17 +219,40 @@ export async function writeAISettings(fs: JSRuntimeFS, settings: AISettings, con await fs.writeFile(aiConfigPath, JSON.stringify(settings, null, 2), 'utf8'); } +/** + * Generate a display name for a credential based on its hostname + */ +function generateCredentialName(host: string): string { + const hostname = host.split(':')[0]; // Remove port if present + + if (hostname === 'github.com') { + return 'GitHub'; + } + + if (hostname === 'gitlab.com') { + return 'GitLab'; + } + + // For other hosts, use the hostname as the name + return hostname; +} + /** * Convert old format credentials (map) to new format (array) + * This is only used for migrating from the old map-based format */ function convertCredentialsToNewFormat( credentials: Record ): GitCredential[] { return Object.entries(credentials).map(([origin, cred]) => { const url = new URL(origin); + const host = url.host; // Includes port if non-standard + return { + id: crypto.randomUUID(), + name: generateCredentialName(host), protocol: url.protocol.replace(/:$/, ''), - host: url.host, // Includes port if non-standard + host, username: cred.username, password: cred.password, }; diff --git a/src/lib/gitCredentials.test.ts b/src/lib/gitCredentials.test.ts index 498cce98..d4a39a00 100644 --- a/src/lib/gitCredentials.test.ts +++ b/src/lib/gitCredentials.test.ts @@ -2,6 +2,19 @@ import { describe, it, expect } from 'vitest'; import { findCredentialsForRepo } from './gitCredentials'; import type { GitCredential } from '@/contexts/GitSettingsContext'; +// Helper to create test credentials with required fields +function createTestCredential(overrides: Partial): GitCredential { + return { + id: crypto.randomUUID(), + name: 'Test Credential', + protocol: 'https', + host: 'github.com', + username: 'user', + password: 'pass', + ...overrides, + }; +} + describe('gitCredentials', () => { describe('findCredentialsForRepo', () => { it('returns undefined when no credentials are provided', () => { @@ -11,12 +24,9 @@ describe('gitCredentials', () => { it('returns undefined when no credentials match', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', + createTestCredential({ host: 'gitlab.com', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials); expect(result).toBeUndefined(); @@ -24,12 +34,9 @@ describe('gitCredentials', () => { it('matches credential with exact protocol, hostname, and default port', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', + createTestCredential({ host: 'github.com', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -37,12 +44,9 @@ describe('gitCredentials', () => { it('matches credential with explicit port 443 for https', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', + createTestCredential({ host: 'github.com:443', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -50,12 +54,10 @@ describe('gitCredentials', () => { it('matches credential with explicit port 80 for http', () => { const credentials: GitCredential[] = [ - { + createTestCredential({ protocol: 'http', host: 'example.com:80', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('http://example.com/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -63,12 +65,9 @@ describe('gitCredentials', () => { it('matches credential with custom port when URL has same custom port', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', + createTestCredential({ host: 'gitlab.local:8443', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://gitlab.local:8443/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -76,12 +75,9 @@ describe('gitCredentials', () => { it('does not match when ports differ', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', + createTestCredential({ host: 'gitlab.local:8443', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://gitlab.local:9443/repo.git', credentials); expect(result).toBeUndefined(); @@ -89,12 +85,9 @@ describe('gitCredentials', () => { it('does not match when protocol differs', () => { const credentials: GitCredential[] = [ - { + createTestCredential({ protocol: 'http', - host: 'github.com', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://github.com/user/repo.git', credentials); expect(result).toBeUndefined(); @@ -102,12 +95,9 @@ describe('gitCredentials', () => { it('does not match when hostname differs', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', + createTestCredential({ host: 'github.com', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://gitlab.com/user/repo.git', credentials); expect(result).toBeUndefined(); @@ -115,18 +105,14 @@ describe('gitCredentials', () => { it('prefers credential with matching username over credential without username', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', + createTestCredential({ username: '', password: 'pass1', - }, - { - protocol: 'https', - host: 'github.com', + }), + createTestCredential({ username: 'alice', password: 'pass2', - }, + }), ]; const result = findCredentialsForRepo('https://alice@github.com/repo.git', credentials); expect(result).toBe(credentials[1]); @@ -134,18 +120,14 @@ describe('gitCredentials', () => { it('returns first match when no username preference exists', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', + createTestCredential({ username: '', password: 'pass1', - }, - { - protocol: 'https', - host: 'github.com', + }), + createTestCredential({ username: '', password: 'pass2', - }, + }), ]; const result = findCredentialsForRepo('https://github.com/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -153,12 +135,9 @@ describe('gitCredentials', () => { it('does not match when usernames differ', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', + createTestCredential({ username: 'alice', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://bob@github.com/repo.git', credentials); expect(result).toBeUndefined(); @@ -166,12 +145,9 @@ describe('gitCredentials', () => { it('matches when credential has username but URL does not', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', + createTestCredential({ username: 'alice', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://github.com/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -179,12 +155,9 @@ describe('gitCredentials', () => { it('matches when URL has username but credential does not', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', + createTestCredential({ username: '', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('https://alice@github.com/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -192,24 +165,18 @@ describe('gitCredentials', () => { it('handles multiple matching credentials and returns the one with matching username', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', + createTestCredential({ username: 'alice', password: 'pass1', - }, - { - protocol: 'https', - host: 'github.com', + }), + createTestCredential({ username: 'bob', password: 'pass2', - }, - { - protocol: 'https', - host: 'github.com', + }), + createTestCredential({ username: '', password: 'pass3', - }, + }), ]; const result = findCredentialsForRepo('https://bob@github.com/repo.git', credentials); expect(result).toBe(credentials[1]); @@ -217,12 +184,7 @@ describe('gitCredentials', () => { it('handles complex URLs with paths and query parameters', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', - username: 'user', - password: 'pass', - }, + createTestCredential({}), ]; const result = findCredentialsForRepo( 'https://github.com/org/repo.git?foo=bar#fragment', @@ -233,12 +195,9 @@ describe('gitCredentials', () => { it('handles URLs with encoded characters', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', + createTestCredential({ username: 'user%40email.com', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo( 'https://user%40email.com@github.com/repo.git', @@ -249,12 +208,9 @@ describe('gitCredentials', () => { it('handles subdomains correctly', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', + createTestCredential({ host: 'api.github.com', - username: 'user', - password: 'pass', - }, + }), ]; // Should not match github.com const result1 = findCredentialsForRepo('https://github.com/repo.git', credentials); @@ -267,12 +223,11 @@ describe('gitCredentials', () => { it('handles credentials with empty username', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', + createTestCredential({ host: 'github.com', username: '', password: 'token', - }, + }), ]; const result = findCredentialsForRepo('https://github.com/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -280,12 +235,10 @@ describe('gitCredentials', () => { it('returns credential when both URL and credential have no username', () => { const credentials: GitCredential[] = [ - { - protocol: 'https', - host: 'github.com', + createTestCredential({ username: '', password: 'token', - }, + }), ]; const result = findCredentialsForRepo('https://github.com/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -293,12 +246,10 @@ describe('gitCredentials', () => { it('handles IPv4 addresses as hostnames', () => { const credentials: GitCredential[] = [ - { + createTestCredential({ protocol: 'http', host: '192.168.1.100:8080', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('http://192.168.1.100:8080/repo.git', credentials); expect(result).toBe(credentials[0]); @@ -306,12 +257,10 @@ describe('gitCredentials', () => { it('does not match IPv4 addresses with different ports', () => { const credentials: GitCredential[] = [ - { + createTestCredential({ protocol: 'http', host: '192.168.1.100:8080', - username: 'user', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('http://192.168.1.100:9090/repo.git', credentials); expect(result).toBeUndefined(); @@ -319,12 +268,11 @@ describe('gitCredentials', () => { it('handles localhost URLs', () => { const credentials: GitCredential[] = [ - { + createTestCredential({ protocol: 'http', host: 'localhost:3000', username: 'dev', - password: 'pass', - }, + }), ]; const result = findCredentialsForRepo('http://localhost:3000/repo.git', credentials); expect(result).toBe(credentials[0]); diff --git a/src/pages/GitSettings.tsx b/src/pages/GitSettings.tsx index e67310a7..71243234 100644 --- a/src/pages/GitSettings.tsx +++ b/src/pages/GitSettings.tsx @@ -70,13 +70,12 @@ const PRESET_PROVIDERS: PresetProvider[] = [ interface SortableCredentialItemProps { credential: GitCredential; - index: number; - onUpdate: (index: number, credential: Partial) => void; - onRemove: (index: number) => void; + onUpdate: (updatedCredential: GitCredential) => void; + onRemove: (id: string) => void; showDragHandle: boolean; } -function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDragHandle }: 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); @@ -89,7 +88,7 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra transform, transition, isDragging, - } = useSortable({ id: `credential-${index}` }); + } = useSortable({ id: credential.id }); const style = { transform: CSS.Transform.toString(transform), @@ -114,16 +113,20 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra } }; + const updateCredential = (updates: Partial) => { + onUpdate({ ...credential, ...updates }); + }; + const handleOriginChange = (newOrigin: string) => { const parsed = parseOrigin(newOrigin); - onUpdate(index, parsed); + updateCredential(parsed); }; return ( @@ -143,7 +146,7 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra fallback={} /> - {preset?.name || credential.host} + {credential.name} {isCustom && {t('custom')}} @@ -151,42 +154,53 @@ function SortableCredentialItem({ credential, index, onUpdate, onRemove, showDra
-
+
+ handleOriginChange(e.target.value)} />
-
-