mirror of
https://github.com/community-scripts/ProxmoxVE-Local.git
synced 2026-08-01 18:08:40 -04:00
The previous regex required exactly three path segments (host/owner/repo), rejecting nested GitLab group structures such as: https://git.example.com/group/sub-group/repo Replace with a looser pattern that only requires a host and at least one path segment after it, which covers GitHub, GitLab, Bitbucket and self-hosted instances with arbitrarily nested namespaces.
42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
/**
|
|
* Repository URL validation (JS mirror for server.js).
|
|
*/
|
|
const VALID_REPO_URL =
|
|
/^https?:\/\/[^/]+\/.+$/;
|
|
|
|
export const REPO_URL_ERROR_MESSAGE =
|
|
'Invalid repository URL. Supported: GitHub, GitLab, Bitbucket, and custom Git servers (e.g. https://host/owner/repo or https://host/group/sub-group/repo).';
|
|
|
|
export function isValidRepositoryUrl(url) {
|
|
if (typeof url !== 'string' || !url.trim()) return false;
|
|
return VALID_REPO_URL.test(url.trim());
|
|
}
|
|
|
|
export function getRepoProvider(url) {
|
|
if (!isValidRepositoryUrl(url)) throw new Error(REPO_URL_ERROR_MESSAGE);
|
|
try {
|
|
const hostname = new URL(url.trim()).hostname.toLowerCase();
|
|
if (hostname === 'github.com') return 'github';
|
|
if (hostname === 'gitlab.com') return 'gitlab';
|
|
if (hostname === 'bitbucket.org') return 'bitbucket';
|
|
return 'custom';
|
|
} catch {
|
|
return 'custom';
|
|
}
|
|
}
|
|
|
|
export function parseRepoUrl(url) {
|
|
if (!isValidRepositoryUrl(url)) throw new Error(REPO_URL_ERROR_MESSAGE);
|
|
try {
|
|
const u = new URL(url.trim());
|
|
const pathParts = u.pathname.replace(/^\/+/, '').replace(/\.git\/?$/, '').split('/');
|
|
return {
|
|
origin: u.origin,
|
|
owner: pathParts[0] ?? '',
|
|
repo: pathParts[1] ?? '',
|
|
};
|
|
} catch {
|
|
throw new Error(REPO_URL_ERROR_MESSAGE);
|
|
}
|
|
}
|