Use origin instead of protocol, host for git credentials

This commit is contained in:
Alex Gleason committed 2025-12-21 00:08:56 -06:00
1 parent 9c2f4f9eb6
commit fcc305788d
7 files changed
+34 -82

No files matched your search

+3 -6
View File
@@ -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',
+2 -4
View File
@@ -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,
};
+9 -4
View File
@@ -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>): GitCredential {
function createTestCredential(overrides: Partial<GitCredential> & { 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,
};
}
+9 -9
View File
@@ -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];
}