mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Simplify credentials in GitDialog, use configured credentials
This commit is contained in:
5 files changed
+310
-59
No files matched your search
@@ -0,0 +1,92 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { extractGitOrigin, findCredentialsForRepo, getOriginDisplayName } from './gitCredentials';
|
||||
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||
|
||||
describe('gitCredentials', () => {
|
||||
describe('extractGitOrigin', () => {
|
||||
it('should extract origin from HTTPS URLs', () => {
|
||||
expect(extractGitOrigin('https://github.com/user/repo.git')).toBe('https://github.com');
|
||||
expect(extractGitOrigin('https://gitlab.com/user/repo.git')).toBe('https://gitlab.com');
|
||||
expect(extractGitOrigin('https://git.example.com:8080/user/repo.git')).toBe('https://git.example.com:8080');
|
||||
});
|
||||
|
||||
it('should extract origin from HTTP URLs', () => {
|
||||
expect(extractGitOrigin('http://git.example.com/user/repo.git')).toBe('http://git.example.com');
|
||||
});
|
||||
|
||||
it('should extract origin from SSH URLs', () => {
|
||||
expect(extractGitOrigin('[email protected]:user/repo.git')).toBe('https://github.com');
|
||||
expect(extractGitOrigin('[email protected]:user/repo.git')).toBe('https://gitlab.com');
|
||||
expect(extractGitOrigin('[email protected]:user/repo.git')).toBe('https://git.example.com');
|
||||
});
|
||||
|
||||
it('should return null for invalid URLs', () => {
|
||||
expect(extractGitOrigin('')).toBe(null);
|
||||
expect(extractGitOrigin('invalid-url')).toBe(null);
|
||||
expect(extractGitOrigin('ftp://example.com/repo')).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findCredentialsForRepo', () => {
|
||||
const mockCredentials: Record<string, GitCredential> = {
|
||||
'https://github.com': { username: 'git', password: 'github-token' },
|
||||
'https://gitlab.com': { username: 'git', password: 'gitlab-token' },
|
||||
'https://git.example.com': { username: 'user', password: 'custom-token' },
|
||||
};
|
||||
|
||||
it('should find exact matches', () => {
|
||||
expect(findCredentialsForRepo('https://github.com/user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
|
||||
expect(findCredentialsForRepo('https://gitlab.com/user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'gitlab-token' });
|
||||
});
|
||||
|
||||
it('should find matches for SSH URLs', () => {
|
||||
expect(findCredentialsForRepo('[email protected]:user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
});
|
||||
|
||||
it('should handle case-insensitive matching', () => {
|
||||
expect(findCredentialsForRepo('https://GitHub.com/user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
});
|
||||
|
||||
it('should handle www variations', () => {
|
||||
const credentialsWithWww = {
|
||||
'https://www.github.com': { username: 'git', password: 'github-token' },
|
||||
};
|
||||
|
||||
expect(findCredentialsForRepo('https://github.com/user/repo.git', credentialsWithWww))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
|
||||
expect(findCredentialsForRepo('https://www.github.com/user/repo.git', mockCredentials))
|
||||
.toEqual({ username: 'git', password: 'github-token' });
|
||||
});
|
||||
|
||||
it('should return null when no credentials found', () => {
|
||||
expect(findCredentialsForRepo('https://unknown.com/user/repo.git', mockCredentials))
|
||||
.toBe(null);
|
||||
|
||||
expect(findCredentialsForRepo('invalid-url', mockCredentials))
|
||||
.toBe(null);
|
||||
});
|
||||
|
||||
it('should return null for empty credentials', () => {
|
||||
expect(findCredentialsForRepo('https://github.com/user/repo.git', {}))
|
||||
.toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getOriginDisplayName', () => {
|
||||
it('should extract hostname from valid URLs', () => {
|
||||
expect(getOriginDisplayName('https://github.com')).toBe('github.com');
|
||||
expect(getOriginDisplayName('https://git.example.com:8080')).toBe('git.example.com:8080');
|
||||
});
|
||||
|
||||
it('should return original string for invalid URLs', () => {
|
||||
expect(getOriginDisplayName('invalid-url')).toBe('invalid-url');
|
||||
expect(getOriginDisplayName('')).toBe('');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
import type { GitCredential } from '@/contexts/GitSettingsContext';
|
||||
|
||||
/**
|
||||
* Extract the origin (protocol + hostname + port) from a Git URL
|
||||
*/
|
||||
export function extractGitOrigin(gitUrl: string): string | null {
|
||||
try {
|
||||
// Handle SSH URLs like [email protected]:user/repo.git
|
||||
if (gitUrl.startsWith('git@')) {
|
||||
const match = gitUrl.match(/^git@([^:]+):/);
|
||||
if (match) {
|
||||
return `https://${match[1]}`;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle HTTP/HTTPS URLs
|
||||
if (gitUrl.startsWith('http://') || gitUrl.startsWith('https://')) {
|
||||
const url = new URL(gitUrl);
|
||||
return `${url.protocol}//${url.host}`;
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find matching credentials for a repository URL
|
||||
*/
|
||||
export function findCredentialsForRepo(
|
||||
repoUrl: string,
|
||||
credentials: Record<string, GitCredential>
|
||||
): GitCredential | null {
|
||||
const repoOrigin = extractGitOrigin(repoUrl);
|
||||
if (!repoOrigin) return null;
|
||||
|
||||
// Direct match
|
||||
if (credentials[repoOrigin]) {
|
||||
return credentials[repoOrigin];
|
||||
}
|
||||
|
||||
// Try to find a match by normalizing common variations
|
||||
const normalizedRepoOrigin = repoOrigin.toLowerCase();
|
||||
|
||||
for (const [origin, credential] of Object.entries(credentials)) {
|
||||
const normalizedOrigin = origin.toLowerCase();
|
||||
|
||||
// Exact match (case-insensitive)
|
||||
if (normalizedOrigin === normalizedRepoOrigin) {
|
||||
return credential;
|
||||
}
|
||||
|
||||
// Handle www variations
|
||||
if (normalizedOrigin.replace(/^https?:\/\/www\./, 'https://') ===
|
||||
normalizedRepoOrigin.replace(/^https?:\/\/www\./, 'https://')) {
|
||||
return credential;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the display name for a Git origin URL
|
||||
*/
|
||||
export function getOriginDisplayName(origin: string): string {
|
||||
try {
|
||||
const url = new URL(origin);
|
||||
return url.host; // Use host instead of hostname to include port
|
||||
} catch {
|
||||
return origin;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user