Refactor git credentials to be an array instead of an object

This commit is contained in:
Alex Gleason committed 2025-12-20 22:49:07 -06:00
1 parent 07807ea1bf
commit fbdcd01b38
12 files changed
+598 -220

No files matched your search

+6 -4
View File
@@ -59,12 +59,14 @@ describe('GitDialog', () => {
mockUseGitSettings.mockReturnValue({
settings: {
credentials: {
'https://github.com': {
credentials: [
{
protocol: 'https',
host: 'github.com',
username: 'git',
password: 'github-token',
},
},
],
},
});
});
@@ -217,7 +219,7 @@ describe('GitDialog', () => {
// Mock git settings with no credentials and mock git status with an https remote
mockUseGitSettings.mockReturnValue({
settings: {
credentials: {},
credentials: [],
},
});
+22 -14
View File
@@ -18,31 +18,35 @@ function TestComponent() {
const handleAdd = () => {
const credential: GitCredential = {
protocol: 'https',
host: 'github.com',
username: 'git',
password: 'test-token',
};
addCredential('https://github.com', credential);
addCredential(credential);
};
const handleUpdate = () => {
updateCredential('https://github.com', { password: 'updated-token' });
updateCredential(0, { password: 'updated-token' });
};
const handleRemove = () => {
removeCredential('https://github.com');
removeCredential(0);
};
const githubCred = settings.credentials.find(c => c.host === 'github.com');
return (
<div>
<div data-testid="configured">{isConfigured ? 'configured' : 'not-configured'}</div>
<div data-testid="count">{Object.keys(settings.credentials).length}</div>
<div data-testid="count">{settings.credentials.length}</div>
<button onClick={handleAdd}>Add GitHub</button>
<button onClick={handleUpdate}>Update GitHub</button>
<button onClick={handleRemove}>Remove GitHub</button>
{settings.credentials['https://github.com'] && (
{githubCred && (
<div data-testid="github-creds">
{settings.credentials['https://github.com'].username}:
{settings.credentials['https://github.com'].password}
{githubCred.username}:
{githubCred.password}
</div>
)}
</div>
@@ -67,7 +71,7 @@ describe('GitSettingsProvider', () => {
// Mock the read function to return default settings
const { readGitSettings } = await import('@/lib/configUtils');
vi.mocked(readGitSettings).mockResolvedValue({
credentials: {},
credentials: [],
});
});
@@ -164,12 +168,14 @@ describe('GitSettingsProvider', () => {
expect(writeGitSettings).toHaveBeenCalledWith(
expect.anything(), // fs instance
expect.objectContaining({
credentials: {
'https://github.com': {
credentials: [
{
protocol: 'https',
host: 'github.com',
username: 'git',
password: 'test-token',
}
}
]
}),
'/config' // configPath
);
@@ -178,12 +184,14 @@ describe('GitSettingsProvider', () => {
it('loads settings from VFS', async () => {
const settings = {
credentials: {
'https://gitlab.com': {
credentials: [
{
protocol: 'https',
host: 'gitlab.com',
username: 'git',
password: 'gitlab-token',
},
},
],
};
const { readGitSettings } = await import('@/lib/configUtils');
+18 -23
View File
@@ -9,7 +9,7 @@ interface GitSettingsProviderProps {
}
const DEFAULT_SETTINGS: GitSettings = {
credentials: {},
credentials: [],
};
export function GitSettingsProvider({ children }: GitSettingsProviderProps) {
@@ -54,40 +54,35 @@ export function GitSettingsProvider({ children }: GitSettingsProviderProps) {
setSettings(prev => ({ ...prev, ...newSettings }));
};
const addCredential = (origin: string, credential: GitCredential) => {
const addCredential = (credential: GitCredential) => {
setSettings(prev => ({
...prev,
credentials: {
...prev.credentials,
[origin]: credential,
},
credentials: [...prev.credentials, credential],
}));
};
const removeCredential = (origin: string) => {
const removeCredential = (index: number) => {
setSettings(prev => ({
...prev,
credentials: prev.credentials.filter((_, i) => i !== index),
}));
};
const updateCredential = (index: number, credential: Partial<GitCredential>) => {
setSettings(prev => {
const { [origin]: removed, ...rest } = prev.credentials;
const newCredentials = [...prev.credentials];
newCredentials[index] = {
...newCredentials[index],
...credential,
};
return {
...prev,
credentials: rest,
credentials: newCredentials,
};
});
};
const updateCredential = (origin: string, credential: Partial<GitCredential>) => {
setSettings(prev => ({
...prev,
credentials: {
...prev.credentials,
[origin]: {
...prev.credentials[origin],
...credential,
},
},
}));
};
const isConfigured = Object.entries(settings.credentials).length > 0;
const isConfigured = settings.credentials.length > 0;
const contextValue: GitSettingsContextType = {
settings,
+1 -1
View File
@@ -442,7 +442,7 @@ export function PullRequestDialog({
if (remoteInfo.platform !== 'nostr') {
credentials = findCredentialsForRepo(remoteUrl || '', settings.credentials);
console.log('Found credentials:', credentials ? 'Yes' : 'No');
console.log('Available credential origins:', Object.keys(settings.credentials));
console.log('Available credentials:', settings.credentials.map(c => `${c.protocol}://${c.host}`));
if (credentials) {
console.log('Credential username:', credentials.username);