rob-revisited/ide/ui/js/vcs-model.js

89 lines
5.1 KiB
JavaScript

// Version control choices, without any DOM: the hosts of a repository, the address of owner/repo on each, what a "Source control" step
// of New Project sends to the backend (ide/src/Vcs/SourceControl.php) and what is wrong with it. Tested by vcs-model.test.mjs.
export const FORGE = 'git.jumphp.net';
/** The hosts a repository can be on (the label is an English key of t()). `creates`: the IDE can make the empty repository there. */
export const HOSTS = [
{ id: 'forgejo', label: 'JumPHP forge (git.jumphp.net)', host: FORGE, creates: true, tokenUrl: 'https://git.jumphp.net/user/settings/applications' },
{ id: 'github', label: 'GitHub', host: 'github.com', creates: true, tokenUrl: 'https://github.com/settings/tokens' },
{ id: 'gitlab', label: 'GitLab (gitlab.com or your own server)', host: 'gitlab.com', creates: true, tokenUrl: 'https://gitlab.com/-/user_settings/personal_access_tokens' },
{ id: 'bitbucket', label: 'Bitbucket', host: 'bitbucket.org', creates: false, tokenUrl: null },
{ id: 'url', label: 'Another Git URL', host: '', creates: false, tokenUrl: null },
];
export function hostOf(url) {
const u = String(url || '').trim();
let m = u.match(/^[a-z][a-z0-9+.-]*:\/\/(?:[^@/]*@)?([^/:?#]+)/i);
if (m) return m[1].toLowerCase();
m = u.match(/^(?:[^@/\s]+@)?([A-Za-z0-9.-]+):(?!\/\/)/);
return m ? m[1].toLowerCase() : '';
}
/** forgejo | github | gitlab | bitbucket | url, from a remote's address. */
export function detectHost(url) {
const h = hostOf(url);
if (h === FORGE) return 'forgejo';
if (h === 'github.com' || h === 'www.github.com') return 'github';
if (h === 'gitlab.com' || h.startsWith('gitlab.') || h.includes('.gitlab.')) return 'gitlab';
if (h === 'bitbucket.org') return 'bitbucket';
return 'url';
}
export function hostLabel(id) {
return HOSTS.find((h) => h.id === id)?.label || 'Another Git URL';
}
/** A server typed as a name or an address (git.example.org, https://git.example.org/): its host name; '' when it is not one. */
export function serverName(server, fallback = 'gitlab.com') {
const s = String(server || '').trim();
if (!s) return fallback;
const name = /[/@]/.test(s) ? hostOf(/:\/\//.test(s) ? s : 'https://' + s) : s.toLowerCase();
return /^[a-z0-9][a-z0-9.-]*(:\d+)?$/.test(name) ? name : '';
}
/** https://host/owner/repo.git, or null when the owner / name are not usable. */
export function remoteUrl(hostId, owner, repo, base) {
const host = { forgejo: FORGE, github: 'github.com', bitbucket: 'bitbucket.org' }[hostId] || (hostId === 'gitlab' ? serverName(base) : '');
const o = String(owner || '').replace(/^\/+|\/+$/g, '');
const r = String(repo || '').replace(/\/+$/, '').replace(/\.git$/, '');
if (!host || !/^[A-Za-z0-9][A-Za-z0-9._-]*(\/[A-Za-z0-9][A-Za-z0-9._-]*)*$/.test(o) || !/^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(r)) return null;
return `https://${host}/${o}/${r}.git`;
}
/** A hint before the backend's own check (which stays the judge): null when the address looks right. @returns {string|null} an English key of t() */
export function urlProblem(url, vcs = 'git') {
const u = String(url || '').trim();
if (!u || /[\s\x00-\x1f]/.test(u) || u.startsWith('-')) return 'This is not a repository address.';
if (vcs === 'svn') return /^(https?|svn|svn\+ssh|file):\/\//i.test(u) ? null : 'not a Subversion URL (http://, https://, svn://, svn+ssh://, file://)';
if (!/^(https?|ssh|git|file):\/\//i.test(u) && !/^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+:[^/\s]/.test(u) && !/^(\/|\.\/|\.\.\/|[A-Za-z]:[\\/])/.test(u)) return 'not a Git URL (https://…, ssh://…, git@host:owner/repo.git or a folder)';
if (/^[a-z][a-z0-9+.-]*:\/\/[^/@]*:[^/@]+@/i.test(u)) return 'Do not put a password in the URL: add a token in the credentials of the IDE.';
return null;
}
/** The modes of the step: what the user picks. */
export const MODES = ['git', 'remote', 'none', 'svn'];
/** What the form holds -> what projects.create takes as `vcs`. */
export function stepSpec(form) {
const mode = MODES.includes(form.mode) ? form.mode : 'git';
if (mode === 'none' || mode === 'git') return { mode };
if (mode === 'svn') return { mode, url: String(form.url || '').trim() };
const host = form.host || 'forgejo';
return host === 'existing' ? { mode, host, url: String(form.url || '').trim() }
: { mode, host, private: form.private !== false, ...(host === 'gitlab' ? { gitlabHost: serverName(form.gitlabHost) } : {}) };
}
/** What is missing in a step before Create: an English key of t(), or null. */
export function stepProblem(spec) {
if (spec.mode === 'svn') return urlProblem(spec.url, 'svn');
if (spec.mode === 'remote' && spec.host === 'existing') return urlProblem(spec.url);
if (spec.mode === 'remote' && spec.host === 'gitlab' && !spec.gitlabHost) return 'This is not a repository address.';
return null;
}
/** The token a step needs: {host, hostId} when it creates a repository on a host, else null. */
export function stepNeedsHost(spec) {
if (spec.mode !== 'remote' || spec.host === 'existing') return null;
return { hostId: spec.host, host: spec.host === 'github' ? 'github.com' : spec.host === 'gitlab' ? spec.gitlabHost || 'gitlab.com' : FORGE };
}