Files
openchamber/packages/web/server/lib/gitlab/repo.js
T
bot-hermes d798b3f479 feat(web): add GitLab issues/MRs server module
Add a server-side GitLab integration mirroring the GitHub module:
- auth.js: PAT auth storage with multi-account support and configurable
  base URL (gitlab.com default, self-hosted instances supported)
- client.js: raw-fetch GitLab REST v4 client with per-request timeout,
  ETag conditional-GET cache, own rate-limit cooldown, pagination, and
  single-follow redirect handling
- repo.js: GitLab remote URL parser + directory resolution
- routes.js: read-only /api/gitlab/* routes (auth, issues, MRs, branches)
- index.js + DOCUMENTATION.md + unit tests for auth, client, repo, routes
- Wire registerGitLabRoutes into feature-routes-runtime
2026-08-16 15:42:26 +00:00

123 lines
3.1 KiB
JavaScript

import { getRemoteUrl } from '../git/index.js';
import { getGitLabAuthAccounts, normalizeBaseUrl } from './auth.js';
// When no explicit host allowlist is provided, accept gitlab.com or any host
// that matches the base URL of a stored GitLab account. Never github.com.
function acceptedHosts(knownHosts) {
const hosts = new Set();
if (knownHosts instanceof Set) {
for (const host of knownHosts) {
if (typeof host === 'string' && host.trim()) {
hosts.add(host.trim().toLowerCase());
}
}
return hosts;
}
if (Array.isArray(knownHosts)) {
for (const host of knownHosts) {
if (typeof host === 'string' && host.trim()) {
hosts.add(host.trim().toLowerCase());
}
}
return hosts;
}
hosts.add('gitlab.com');
for (const account of getGitLabAuthAccounts()) {
try {
const host = new URL(normalizeBaseUrl(account.baseUrl) || account.baseUrl).hostname.toLowerCase();
if (host) {
hosts.add(host);
}
} catch {
// ignore malformed stored account base URLs
}
}
return hosts;
}
/**
* Parse a GitLab remote URL into `{ namespace, project, host, baseUrl, url }`.
*
* Supports:
* - `git@HOST:NS/PROJ.git` (NS may be multi-segment, e.g. `a/b/c`)
* - `ssh://git@HOST/NS/PROJ.git`
* - `https://HOST/NS/PROJ(.git)`
*
* `knownHosts` (optional Set of hostnames) restricts which hosts are accepted.
* When omitted, `gitlab.com` and hosts from stored auth accounts are accepted.
* github.com is never accepted.
*/
export const parseGitLabRemoteUrl = (raw, knownHosts) => {
if (typeof raw !== 'string') {
return null;
}
const value = raw.trim();
if (!value) {
return null;
}
let host = '';
let path = '';
// git@HOST:NS/PROJ.git
const scpLike = value.match(/^git@([^:]+):(.+)$/);
if (scpLike) {
host = scpLike[1].toLowerCase();
path = scpLike[2];
} else if (value.startsWith('ssh://') || /^https?:\/\//.test(value)) {
try {
const url = new URL(value);
host = url.hostname.toLowerCase();
path = url.pathname.replace(/^\/+/, '');
} catch {
return null;
}
} else {
return null;
}
if (!host) {
return null;
}
if (host === 'github.com') {
return null;
}
if (!acceptedHosts(knownHosts).has(host)) {
return null;
}
path = path.replace(/\/+$/, '');
if (path.endsWith('.git')) {
path = path.slice(0, -4);
}
const segments = path.split('/').filter(Boolean);
if (segments.length < 2) {
return null;
}
const project = segments[segments.length - 1];
const namespace = segments.slice(0, -1).join('/');
if (!project || !namespace) {
return null;
}
return {
namespace,
project,
host,
baseUrl: `https://${host}`,
url: `https://${host}/${namespace}/${project}`,
};
};
export async function resolveGitLabRepoFromDirectory(directory, remoteName = 'origin') {
const remoteUrl = await getRemoteUrl(directory, remoteName).catch(() => null);
if (!remoteUrl) {
return { repo: null, remoteUrl: null };
}
return {
repo: parseGitLabRemoteUrl(remoteUrl),
remoteUrl,
};
}