Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 | 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 26x 47x 504x 504x 93x 98x 95x 94x 77x 76x 53x 31x 13x 13x 22x 13x 11x 11x 10x 10x 10x 1x 1x 10x 11x 11x 11x 8x 4x 4x 4x 4x 26x 21x 21x 21x 84x 21x 5x 5x 16x 2x 14x 5x 9x 16x 26x 42x 38x 37x 11x 21x 5x 5x 5x 5x 26x 19x 16x 15x 6x 6x 6x 9x 9x 5x 5x 5x 7x 2x 2x 1x 5x 5x 5x 5x 5x 5x 5x 5x 5x 7x 6x 6x 6x 6x 1x 1x 5x 5x 5x 4x 6x 6x 6x 6x 6x 6x 6x 6x 5x 4x 4x 2x 26x 13x 13x 3x 11x 9x 1x 8x 26x 39x 37x 36x 7x 6x 6x 2x 6x 3x 3x 3x 6x 4x 4x 4x 4x 4x 7x 7x 6x 26x 21x 21x 20x 4x 7x 2x 7x | import * as z from 'zod';
import {
BrowseProjectsSchema,
BrowseNamespacesSchema,
type BrowseProjectsOptions,
type BrowseNamespacesOptions,
} from './schema-readonly';
import {
ManageProjectSchema,
ManageNamespaceSchema,
type ManageProjectOptions,
type ManageNamespaceOptions,
} from './schema';
import { enhancedFetch } from '../../utils/fetch';
import { getGitLabBaseUrl } from '../../utils/gitlab-base-url';
import { normalizeProjectId } from '../../utils/projectIdentifier';
import { cleanGidsFromObject } from '../../utils/idConversion';
import { assertActionAllowed } from '../utils';
import { assertInstanceAtLeast, currentInstance, instanceAtLeast } from '../instance-version';
import { GROUP_DUO_SETTINGS, PROJECT_DUO_SETTINGS, withUnappliedSettings } from './duo-settings';
type ProjectSearch = Extract<BrowseProjectsOptions, { action: 'search' }>;
type ProjectList = Extract<BrowseProjectsOptions, { action: 'list' }>;
type NamespaceList = Extract<BrowseNamespacesOptions, { action: 'list' }>;
type ProjectCreate = Extract<ManageProjectOptions, { action: 'create' }>;
type GroupCreate = Extract<ManageNamespaceOptions, { action: 'create' }>;
/** Fields stay in wire order; true preserves false, zero and empty-string values. */
type ParameterField<T> = readonly [key: keyof T, keepFalsy: boolean];
const PROJECT_SEARCH_FIELDS = [
['with_programming_language', false],
['visibility', false],
['order_by', false],
['sort', false],
['per_page', false],
['page', false],
] as const satisfies readonly ParameterField<ProjectSearch>[];
const PROJECT_LIST_FIELDS = [
['visibility', false],
['archived', true],
['owned', true],
['starred', true],
['membership', true],
['search', false],
['simple', true],
['order_by', false],
['sort', false],
['per_page', false],
['page', false],
['include_subgroups', true],
['with_shared', true],
['with_programming_language', false],
['marked_for_deletion_on', false],
] as const satisfies readonly ParameterField<ProjectList>[];
const NAMESPACE_LIST_FIELDS = [
['search', false],
['owned_only', true],
['top_level_only', true],
['with_statistics', true],
['min_access_level', true],
['per_page', false],
['page', false],
] as const satisfies readonly ParameterField<NamespaceList>[];
const PROJECT_CREATE_FIELDS = [
['issues_enabled', true],
['merge_requests_enabled', true],
['jobs_enabled', true],
['wiki_enabled', true],
['snippets_enabled', true],
['lfs_enabled', true],
['request_access_enabled', true],
['only_allow_merge_if_pipeline_succeeds', true],
['only_allow_merge_if_all_discussions_are_resolved', true],
] as const satisfies readonly ParameterField<ProjectCreate>[];
const GROUP_CREATE_FIELDS = [
['description', false],
['visibility', false],
['parent_id', true],
['lfs_enabled', true],
['request_access_enabled', true],
['default_branch_protection', true],
['avatar', false],
['membership_lock', true],
['wiki_access_level', true],
] as const satisfies readonly ParameterField<GroupCreate>[];
function appendParameters<T>(
params: URLSearchParams,
input: T,
fields: readonly ParameterField<T>[],
): void {
for (const [key, keepFalsy] of fields) {
const value = input[key];
if (value === undefined || (!keepFalsy && !value)) continue;
params.set(String(key), String(value));
}
}
function assertSuccessful(response: Response): void {
if (!response.ok) throw new Error(`GitLab API error: ${response.status} ${response.statusText}`);
}
async function entityJson(url: string, options?: RequestInit): Promise<unknown> {
const response = options ? await enhancedFetch(url, options) : await enhancedFetch(url);
assertSuccessful(response);
const data: unknown = await response.json();
return data;
}
function entityUrl(kind: 'projects' | 'groups', id: string): string {
return `${getGitLabBaseUrl()}/api/v4/${kind}/${normalizeProjectId(id)}`;
}
function formRequest(method: 'POST' | 'PUT', body: URLSearchParams): RequestInit {
return {
method,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: body.toString(),
};
}
function updateBody(
params: Record<string, string | number | boolean | undefined>,
): URLSearchParams {
const body = new URLSearchParams();
for (const [key, value] of Object.entries(params)) {
Eif (value !== undefined) body.set(key, String(value));
}
return body;
}
function projectSearchParams(input: ProjectSearch): URLSearchParams {
const params = new URLSearchParams();
if (input.q) {
let search = input.q;
const topics = search.match(/topic:(\w+)/g);
if (topics) {
params.set('topic', topics.map((topic) => topic.replace('topic:', '')).join(','));
search = search.replace(/topic:\w+/g, '').trim();
}
Eif (search) params.set('search', search);
}
appendParameters(params, input, PROJECT_SEARCH_FIELDS);
params.set('active', 'true');
return params;
}
/** Preserve native active filtering or its server-side archived emulation on older GitLab. */
function applyActiveFilter(params: URLSearchParams, active: boolean, since: string): void {
if (instanceAtLeast(since)) {
params.delete('archived');
params.set('active', String(active));
} else {
params.delete('active');
params.set('archived', String(!active));
}
}
const PROJECT_LIST_DEFAULTS = [
['order_by', 'created_at'],
['sort', 'desc'],
['simple', 'true'],
['per_page', '20'],
] as const;
function projectListUrl(input: ProjectList): string {
const params = new URLSearchParams();
appendParameters(params, input, PROJECT_LIST_FIELDS);
for (const [key, value] of PROJECT_LIST_DEFAULTS) {
if (!params.has(key)) params.set(key, value);
}
if (input.group_id) {
// Native active: 18.5 for /projects, 18.8 for /groups/:id/projects.
if (input.active !== undefined) applyActiveFilter(params, input.active, '18.8');
return `${entityUrl('groups', input.group_id)}/projects?${params}`;
}
if (input.include_deleted) {
params.set('include_pending_delete', 'true');
} else if (input.active !== undefined) {
applyActiveFilter(params, input.active, '18.5');
} else {
params.set('active', 'true');
}
return `${getGitLabBaseUrl()}/api/v4/projects?${params}`;
}
export async function browseProjects(args: unknown): Promise<unknown> {
const input = BrowseProjectsSchema.parse(args);
assertActionAllowed('browse_projects', input.action);
switch (input.action) {
case 'search':
return cleanGidsFromObject(
await entityJson(`${getGitLabBaseUrl()}/api/v4/projects?${projectSearchParams(input)}`),
);
case 'list':
return cleanGidsFromObject(await entityJson(projectListUrl(input)));
case 'get': {
const params = new URLSearchParams();
Iif (input.statistics) params.set('statistics', 'true');
Iif (input.license) params.set('license', 'true');
return cleanGidsFromObject(
await entityJson(`${entityUrl('projects', input.project_id)}?${params}`),
);
}
}
}
export async function browseNamespaces(args: unknown): Promise<unknown> {
const input = BrowseNamespacesSchema.parse(args);
assertActionAllowed('browse_namespaces', input.action);
if (input.action === 'list') {
const params = new URLSearchParams();
appendParameters(params, input, NAMESPACE_LIST_FIELDS);
return cleanGidsFromObject(
await entityJson(`${getGitLabBaseUrl()}/api/v4/namespaces?${params}`),
);
}
const url = `${getGitLabBaseUrl()}/api/v4/namespaces/${encodeURIComponent(input.namespace_id)}`;
if (input.action === 'get') return cleanGidsFromObject(await entityJson(url));
const response = await enhancedFetch(url);
const data: unknown = response.ok ? await response.json() : null;
return {
exists: response.ok,
status: response.status,
namespace: input.namespace_id,
data,
};
}
async function resolveNamespace(
namespace: string | undefined,
): Promise<{ id: string; full_path: string } | null> {
if (!namespace) return null;
const response = await enhancedFetch(
`${getGitLabBaseUrl()}/api/v4/namespaces/${encodeURIComponent(namespace)}`,
);
if (!response.ok) throw new Error(`Namespace '${namespace}' not found or not accessible`);
return (await response.json()) as { id: string; full_path: string };
}
function projectCreateBody(
input: ProjectCreate,
namespaceId: string | undefined,
generatedPath: string,
): URLSearchParams {
const body = new URLSearchParams();
body.set('name', input.name);
body.set('path', generatedPath);
Iif (namespaceId) body.set('namespace_id', namespaceId);
// initialize_with_readme is truthy-only and uses the literal "true", not String(value).
if (input.description) body.set('description', input.description);
if (input.visibility) body.set('visibility', input.visibility);
if (input.initialize_with_readme) body.set('initialize_with_readme', 'true');
appendParameters(body, input, PROJECT_CREATE_FIELDS);
return body;
}
async function createProject(input: ProjectCreate): Promise<unknown> {
const resolved = await resolveNamespace(input.namespace);
const namespaceId = resolved ? String(resolved.id) : undefined;
const projectPath = `${resolved ? resolved.full_path : 'current-user'}/${input.name}`;
const existing = await enhancedFetch(
`${getGitLabBaseUrl()}/api/v4/projects/${encodeURIComponent(projectPath)}`,
);
if (existing.ok) {
const project = (await existing.json()) as { id: string };
throw new Error(`Project '${projectPath}' already exists (ID: ${project.id}).`);
}
const generatedPath = input.name
.toLowerCase()
.replace(/[^a-z0-9-]/g, '-')
.replace(/-+/g, '-')
.replace(/^-|-$/g, '');
const body = projectCreateBody(input, namespaceId, generatedPath);
const project = (await entityJson(
`${getGitLabBaseUrl()}/api/v4/projects`,
formRequest('POST', body),
)) as Record<string, unknown>;
return {
...project,
validation: {
namespace_resolved: input.namespace ? `${input.namespace} -> ${namespaceId}` : 'current-user',
generated_path: generatedPath,
},
};
}
async function forkProject(
input: Extract<ManageProjectOptions, { action: 'fork' }>,
): Promise<unknown> {
const body = new URLSearchParams();
if (input.namespace) body.set('namespace', input.namespace);
if (input.namespace_path) body.set('namespace_path', input.namespace_path);
if (input.fork_name) body.set('name', input.fork_name);
if (input.fork_path) body.set('path', input.fork_path);
return entityJson(`${entityUrl('projects', input.project_id)}/fork`, formRequest('POST', body));
}
async function updateProject(
input: Extract<ManageProjectOptions, { action: 'update' }>,
): Promise<unknown> {
const { project_id, action: _action, ...params } = input;
const result = await entityJson(
entityUrl('projects', project_id),
formRequest('PUT', updateBody(params)),
);
return withUnappliedSettings(params, result, PROJECT_DUO_SETTINGS);
}
async function deleteEntity(kind: 'projects' | 'groups', id: string): Promise<unknown> {
const response = await enhancedFetch(entityUrl(kind, id), { method: 'DELETE' });
assertSuccessful(response);
return { success: true, message: `${kind === 'projects' ? 'Project' : 'Group'} ${id} deleted` };
}
const RestoredEntitySchema = z
.object({
id: z.number().int().positive(),
marked_for_deletion_on: z.string().nullable().optional(),
})
.passthrough();
async function restoreEntity(kind: 'projects' | 'groups', id: string): Promise<unknown> {
const label = kind === 'projects' ? 'Project' : 'Group';
// Delayed deletion became unconditional on Free in 18.0; Premium supports the whole floor.
if (currentInstance()?.tier === 'free')
assertInstanceAtLeast('18.0', `${label} restore on GitLab Free`);
const restored = RestoredEntitySchema.safeParse(
await entityJson(`${entityUrl(kind, id)}/restore`, { method: 'POST' }),
);
if (!restored.success)
throw new Error(
`GitLab API error: unexpected restore response shape (${restored.error.issues[0]?.message ?? 'invalid'})`,
);
return restored.data;
}
export async function manageProject(args: unknown): Promise<unknown> {
const input = ManageProjectSchema.parse(args);
assertActionAllowed('manage_project', input.action);
switch (input.action) {
case 'create':
return createProject(input);
case 'fork':
return forkProject(input);
case 'update':
return updateProject(input);
case 'delete':
return deleteEntity('projects', input.project_id);
case 'archive':
case 'unarchive':
return entityJson(`${entityUrl('projects', input.project_id)}/${input.action}`, {
method: 'POST',
});
case 'transfer': {
const body = new URLSearchParams();
body.set('namespace', input.namespace);
return entityJson(
`${entityUrl('projects', input.project_id)}/transfer`,
formRequest('PUT', body),
);
}
case 'restore':
return restoreEntity('projects', input.project_id);
}
}
function groupCreateBody(input: GroupCreate): URLSearchParams {
const body = new URLSearchParams();
body.set('name', input.name);
body.set('path', input.path);
appendParameters(body, input, GROUP_CREATE_FIELDS);
return body;
}
async function updateNamespace(
input: Extract<ManageNamespaceOptions, { action: 'update' }>,
): Promise<unknown> {
const { group_id, action: _action, ...params } = input;
const result = await entityJson(
entityUrl('groups', group_id),
formRequest('PUT', updateBody(params)),
);
return withUnappliedSettings(params, result, GROUP_DUO_SETTINGS);
}
export async function manageNamespace(args: unknown): Promise<unknown> {
const input = ManageNamespaceSchema.parse(args);
assertActionAllowed('manage_namespace', input.action);
switch (input.action) {
case 'create':
return entityJson(
`${getGitLabBaseUrl()}/api/v4/groups`,
formRequest('POST', groupCreateBody(input)),
);
case 'update':
return updateNamespace(input);
case 'delete':
return deleteEntity('groups', input.group_id);
case 'restore':
return restoreEntity('groups', input.group_id);
}
}
|