@cepharum/mcp-gitlab 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/LICENSE +21 -0
- package/README.md +170 -2
- package/package.json +50 -5
- package/src/audit.js +185 -0
- package/src/cli.js +411 -0
- package/src/connect.js +335 -0
- package/src/consent.js +115 -0
- package/src/downloads.js +94 -0
- package/src/errors.js +13 -0
- package/src/git.js +94 -0
- package/src/gitlab/client.js +406 -0
- package/src/gitlab/diff.js +79 -0
- package/src/hosts.js +228 -0
- package/src/paths.js +71 -0
- package/src/permissions/catalog.js +75 -0
- package/src/permissions/policy.js +230 -0
- package/src/permissions/profiles.js +66 -0
- package/src/pins.js +36 -0
- package/src/platform/index.js +76 -0
- package/src/platform/macos.js +129 -0
- package/src/platform/service.js +2 -0
- package/src/process.js +56 -0
- package/src/refs.js +146 -0
- package/src/secrets.js +51 -0
- package/src/server.js +146 -0
- package/src/session.js +140 -0
- package/src/text.js +68 -0
- package/src/tools/artifacts.js +288 -0
- package/src/tools/audit.js +53 -0
- package/src/tools/environments.js +147 -0
- package/src/tools/files.js +101 -0
- package/src/tools/index.js +33 -0
- package/src/tools/issues.js +465 -0
- package/src/tools/merge-requests.js +698 -0
- package/src/tools/packages.js +322 -0
- package/src/tools/pipelines.js +415 -0
- package/src/tools/registry.js +107 -0
- package/src/tools/repositories.js +174 -0
- package/src/tools/schedules.js +521 -0
- package/src/tools/shared.js +93 -0
- package/src/tools/variables.js +294 -0
- package/src/tools/wikis.js +168 -0
- package/src/zip.js +178 -0
|
@@ -0,0 +1,322 @@
|
|
|
1
|
+
import { realpath, stat } from "node:fs/promises";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { basename, isAbsolute, parse, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { cloneRoot } from "../git.js";
|
|
6
|
+
import { projectId } from "../gitlab/client.js";
|
|
7
|
+
import { formatRef } from "../refs.js";
|
|
8
|
+
import { findPackage } from "./artifacts.js";
|
|
9
|
+
|
|
10
|
+
/** Largest file package_upload sends. */
|
|
11
|
+
const MAX_UPLOAD_BYTES = 1024 * 1024 * 1024;
|
|
12
|
+
|
|
13
|
+
/** Names GitLab accepts for generic packages, versions and their files, without anything that would change the URL. */
|
|
14
|
+
const NAME = /^(?:[\w.+~]|-)+$/;
|
|
15
|
+
|
|
16
|
+
/** Files never uploaded, as they usually hold credentials. */
|
|
17
|
+
const SECRET_FILE = /^(?:id_(?:rsa|dsa|ecdsa|ed25519)(?:_sk)?|.*\.(?:pem|key|p12|pfx|jks|keystore|kdbx|ppk|asc|gpg))$/i;
|
|
18
|
+
|
|
19
|
+
const PACKAGE_INPUT = z.string().min( 1 ).max( 255 ).describe( "Name of the package, as package_list returns it" );
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {import("../refs.js").RepoRef} repo
|
|
23
|
+
* @param {string} [suffix]
|
|
24
|
+
* @returns {string}
|
|
25
|
+
*/
|
|
26
|
+
const projectEndpoint = ( repo, suffix = "" ) => `projects/${projectId( repo )}${suffix}`;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Formats a size for dialogs.
|
|
30
|
+
*
|
|
31
|
+
* @param {number} bytes
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
export function formatSize( bytes ) {
|
|
35
|
+
if ( bytes < 1024 ) {
|
|
36
|
+
return `${bytes} bytes`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const units = [ "KB", "MB", "GB" ];
|
|
40
|
+
let value = bytes;
|
|
41
|
+
let unit = -1;
|
|
42
|
+
|
|
43
|
+
while ( value >= 1024 && unit < units.length - 1 ) {
|
|
44
|
+
value /= 1024;
|
|
45
|
+
unit++;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return `${value.toFixed( value < 10 ? 1 : 0 )} ${units[unit]}`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Checks whether a path is inside a folder.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} folder
|
|
55
|
+
* @param {string} path
|
|
56
|
+
* @returns {string|null} the path relative to the folder, null if it is outside
|
|
57
|
+
*/
|
|
58
|
+
function inside( folder, path ) {
|
|
59
|
+
const rest = relative( folder, path );
|
|
60
|
+
|
|
61
|
+
return rest && rest !== ".." && !rest.startsWith( `..${sep}` ) && !isAbsolute( rest ) ? rest : null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Finds the local clones files may be uploaded from: the one in "directory",
|
|
66
|
+
* and the one the server runs in. A clone in the home folder or the root of
|
|
67
|
+
* the file system doesn't count, as it would allow almost every file.
|
|
68
|
+
*
|
|
69
|
+
* @param {string|undefined} directory
|
|
70
|
+
* @param {string} cwd
|
|
71
|
+
* @returns {Promise<string[]>} real paths of their top folders
|
|
72
|
+
*/
|
|
73
|
+
async function uploadRoots( directory, cwd ) {
|
|
74
|
+
const roots = new Set();
|
|
75
|
+
const tooWide = new Set( [ await realpath( homedir() ).catch( () => homedir() ), parse( cwd ).root ] );
|
|
76
|
+
|
|
77
|
+
for ( const folder of [ directory, cwd ] ) {
|
|
78
|
+
if ( folder && isAbsolute( folder ) ) {
|
|
79
|
+
const top = await cloneRoot( folder );
|
|
80
|
+
const real = top && await realpath( top ).catch( () => null );
|
|
81
|
+
|
|
82
|
+
if ( real && !tooWide.has( real ) ) {
|
|
83
|
+
roots.add( real );
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return [...roots];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Finds the local file to upload, refusing files outside the local clone,
|
|
93
|
+
* and files that usually hold credentials.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} file absolute, or relative to "directory" or the working directory
|
|
96
|
+
* @param {string|undefined} directory
|
|
97
|
+
* @param {string} cwd
|
|
98
|
+
* @returns {Promise<{ path: string, shown: string, size: number }>}
|
|
99
|
+
*/
|
|
100
|
+
export async function localFile( file, directory, cwd ) {
|
|
101
|
+
const roots = await uploadRoots( directory, cwd );
|
|
102
|
+
|
|
103
|
+
if ( roots.length === 0 ) {
|
|
104
|
+
throw new Error( "files are uploaded from a local clone only: give its folder as \"directory\"" );
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const requested = resolve( directory ?? cwd, file );
|
|
108
|
+
let path;
|
|
109
|
+
|
|
110
|
+
try {
|
|
111
|
+
path = await realpath( requested );
|
|
112
|
+
} catch {
|
|
113
|
+
throw new Error( `there is no file ${requested}` );
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const within = roots.map( root => inside( root, path ) ).find( rest => rest !== null ) ?? null;
|
|
117
|
+
|
|
118
|
+
if ( within === null ) {
|
|
119
|
+
throw new Error( `${requested} isn't inside the local clone, which is the only place files are uploaded from` );
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const segments = within.split( sep );
|
|
123
|
+
|
|
124
|
+
if ( segments.some( segment => segment.startsWith( "." ) ) ) {
|
|
125
|
+
throw new Error( `${within} is hidden or in a hidden folder such as .git, which aren't uploaded as they may hold credentials` );
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if ( SECRET_FILE.test( /** @type {string} */ ( segments.at( -1 ) ) ) ) {
|
|
129
|
+
throw new Error( `${within} looks like a key or keystore, which isn't uploaded` );
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const info = await stat( path );
|
|
133
|
+
|
|
134
|
+
if ( !info.isFile() ) {
|
|
135
|
+
throw new Error( `${within} isn't a file` );
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if ( info.size > MAX_UPLOAD_BYTES ) {
|
|
139
|
+
throw new Error( `${within} has ${formatSize( info.size )}, more than the ${formatSize( MAX_UPLOAD_BYTES )} uploaded at most` );
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return { path, shown: within, size: info.size };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Finds a version of a package, null if there is none.
|
|
147
|
+
*
|
|
148
|
+
* @param {import("./shared.js").ToolContext} context
|
|
149
|
+
* @param {string} name
|
|
150
|
+
* @param {string} version
|
|
151
|
+
* @returns {Promise<any>}
|
|
152
|
+
*/
|
|
153
|
+
async function existingVersion( { gitlab, repo }, name, version ) {
|
|
154
|
+
try {
|
|
155
|
+
return await findPackage( gitlab, repo, name, version );
|
|
156
|
+
} catch {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* What package_upload is about to do, found out once before the user is asked.
|
|
163
|
+
*
|
|
164
|
+
* @type {WeakMap<object, { local: { path: string, shown: string, size: number }, fileName: string, replaces: number }>}
|
|
165
|
+
*/
|
|
166
|
+
const uploads = new WeakMap();
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* @param {any} args
|
|
170
|
+
* @param {import("./shared.js").ToolContext} context
|
|
171
|
+
* @returns {Promise<{ local: { path: string, shown: string, size: number }, fileName: string, replaces: number }>}
|
|
172
|
+
*/
|
|
173
|
+
async function planUpload( args, context ) {
|
|
174
|
+
if ( !uploads.has( args ) ) {
|
|
175
|
+
const local = await localFile( args.file, args.directory, context.cwd );
|
|
176
|
+
const fileName = args.fileName ?? basename( local.path );
|
|
177
|
+
|
|
178
|
+
if ( !NAME.test( fileName ) ) {
|
|
179
|
+
throw new Error( `"${fileName}" can't be the name of a file of a package: use letters, digits and . _ - + ~ only, e.g. by giving "fileName"` );
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const found = await existingVersion( context, args.package, args.version );
|
|
183
|
+
let replaces = 0;
|
|
184
|
+
|
|
185
|
+
if ( found ) {
|
|
186
|
+
if ( found.package_type !== "generic" ) {
|
|
187
|
+
throw new Error( `"${args.package}" ${args.version} is a ${found.package_type} package, only generic packages take uploads` );
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const { items } = await context.gitlab.list( context.repo.host, projectEndpoint( context.repo, `/packages/${found.id}/package_files` ), { perPage: 100 } );
|
|
191
|
+
|
|
192
|
+
replaces = items.filter( item => item.file_name === fileName ).length;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
uploads.set( args, { local, fileName, replaces } );
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return /** @type {any} */ ( uploads.get( args ) );
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** @type {import("./shared.js").ToolDefinition[]} */
|
|
202
|
+
export const packageTools = [
|
|
203
|
+
{
|
|
204
|
+
name: "package_upload",
|
|
205
|
+
title: "Upload a file to a generic package",
|
|
206
|
+
description: "Uploads a local file to a version of a generic package in the package registry of a repository, creating the package or version if needed. Only files inside a local clone (\"directory\", or the one the server runs in) are uploaded, no hidden files or keys. The user confirms every upload.",
|
|
207
|
+
permission: "package.upload",
|
|
208
|
+
usesRepo: true,
|
|
209
|
+
mutates: true,
|
|
210
|
+
input: {
|
|
211
|
+
package: z.string().min( 1 ).max( 255 ).regex( NAME ).describe( "Name of the package, e.g. \"my-tool\"" ),
|
|
212
|
+
version: z.string().min( 1 ).max( 255 ).regex( NAME ).describe( "Version, e.g. \"1.2.0\"" ),
|
|
213
|
+
file: z.string().min( 1 ).max( 1000 ).regex( /^[^\0-\x1f]+$/ ).describe( "Local file, absolute or relative to \"directory\" or the working directory of the server" ),
|
|
214
|
+
fileName: z.string().min( 1 ).max( 255 ).optional().describe( "Name of the file in the package, default: the name of the local file" ),
|
|
215
|
+
hidden: z.boolean().optional().describe( "Hide the version from package lists in GitLab, e.g. while uploading several files" ),
|
|
216
|
+
},
|
|
217
|
+
precheck: async( args, context ) => {
|
|
218
|
+
await planUpload( args, context );
|
|
219
|
+
},
|
|
220
|
+
// uploading sends local files out, so the user confirms every one, whatever the profile says
|
|
221
|
+
asksUser: () => true,
|
|
222
|
+
async handler( args, context ) {
|
|
223
|
+
const { local, fileName, replaces } = await planUpload( args, context );
|
|
224
|
+
const { repo, gitlab, consent } = context;
|
|
225
|
+
const again = replaces > 0 ? ` The version has ${replaces === 1 ? "a file" : `${replaces} files`} of that name already; downloads will get the new one.` : "";
|
|
226
|
+
|
|
227
|
+
await consent.confirm(
|
|
228
|
+
"package.upload",
|
|
229
|
+
`An agent wants to upload ${local.path} (${formatSize( local.size )}) as "${fileName}" to the generic package "${args.package}" ${args.version} of ${formatRef( repo )}.${again}`,
|
|
230
|
+
"always",
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
const endpoint = projectEndpoint( repo, `/packages/generic/${encodeURIComponent( args.package )}/${encodeURIComponent( args.version )}/${encodeURIComponent( fileName )}` );
|
|
234
|
+
const result = await gitlab.upload( repo.host, endpoint, {
|
|
235
|
+
query: { select: "package_file", ...args.hidden ? { status: "hidden" } : {} },
|
|
236
|
+
file: local.path,
|
|
237
|
+
size: local.size,
|
|
238
|
+
} );
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
repo: formatRef( repo ),
|
|
242
|
+
package: args.package,
|
|
243
|
+
version: args.version,
|
|
244
|
+
file: fileName,
|
|
245
|
+
size: result?.size ?? local.size,
|
|
246
|
+
...result?.file_sha256 ? { sha256: result.file_sha256 } : {},
|
|
247
|
+
uploadedFrom: local.shown,
|
|
248
|
+
};
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: "package_delete",
|
|
253
|
+
title: "Delete a package version or file",
|
|
254
|
+
description: "Deletes a version of a package with all its files, or a single file of it. A file name occurring several times in a version is deleted in all its uploads.",
|
|
255
|
+
permission: "package.delete",
|
|
256
|
+
usesRepo: true,
|
|
257
|
+
mutates: true,
|
|
258
|
+
destructive: true,
|
|
259
|
+
input: {
|
|
260
|
+
package: PACKAGE_INPUT,
|
|
261
|
+
version: z.string().min( 1 ).max( 255 ).describe( "Version of the package, as package_list returns it" ),
|
|
262
|
+
file: z.string().min( 1 ).max( 1000 ).optional().describe( "Name of a file of the version to delete, as package_files returns it; default: the whole version" ),
|
|
263
|
+
},
|
|
264
|
+
async precheck( { package: name, version, file }, context ) {
|
|
265
|
+
const found = await findPackage( context.gitlab, context.repo, name, version );
|
|
266
|
+
|
|
267
|
+
if ( file && ( await filesNamed( context, found, file ) ).length === 0 ) {
|
|
268
|
+
throw new Error( `"${name}" ${version} has no file "${file}", see package_files` );
|
|
269
|
+
}
|
|
270
|
+
},
|
|
271
|
+
async confirmMessage( { package: name, version, file }, context ) {
|
|
272
|
+
const found = await findPackage( context.gitlab, context.repo, name, version );
|
|
273
|
+
|
|
274
|
+
if ( file ) {
|
|
275
|
+
const files = await filesNamed( context, found, file );
|
|
276
|
+
|
|
277
|
+
return `An agent wants to delete the file "${file}"${files.length > 1 ? ` (${files.length} uploads)` : ""} of the ${found.package_type} package "${name}" ${version} of ${formatRef( context.repo )}.`;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return `An agent wants to delete the ${found.package_type} package "${name}" ${version} of ${formatRef( context.repo )} with all its files.`;
|
|
281
|
+
},
|
|
282
|
+
async handler( { package: name, version, file }, context ) {
|
|
283
|
+
const { gitlab, repo } = context;
|
|
284
|
+
const found = await findPackage( gitlab, repo, name, version );
|
|
285
|
+
|
|
286
|
+
if ( file ) {
|
|
287
|
+
const files = await filesNamed( context, found, file );
|
|
288
|
+
|
|
289
|
+
for ( const item of files ) {
|
|
290
|
+
await gitlab.request( repo.host, "DELETE", projectEndpoint( repo, `/packages/${found.id}/package_files/${item.id}` ) );
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return { repo: formatRef( repo ), package: name, version, file, deleted: files.length };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
await gitlab.request( repo.host, "DELETE", projectEndpoint( repo, `/packages/${found.id}` ) );
|
|
297
|
+
|
|
298
|
+
return { repo: formatRef( repo ), package: name, version, deleted: true };
|
|
299
|
+
},
|
|
300
|
+
},
|
|
301
|
+
];
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Lists the uploads of a file of a package version.
|
|
305
|
+
*
|
|
306
|
+
* @param {import("./shared.js").ToolContext} context
|
|
307
|
+
* @param {any} found the package version
|
|
308
|
+
* @param {string} file
|
|
309
|
+
* @returns {Promise<any[]>}
|
|
310
|
+
*/
|
|
311
|
+
async function filesNamed( { gitlab, repo }, found, file ) {
|
|
312
|
+
const matches = [];
|
|
313
|
+
|
|
314
|
+
for ( let page = 1; page; ) {
|
|
315
|
+
const { items, nextPage } = await gitlab.list( repo.host, projectEndpoint( repo, `/packages/${found.id}/package_files` ), { page, perPage: 100 } );
|
|
316
|
+
|
|
317
|
+
matches.push( ...items.filter( item => item.file_name === file ) );
|
|
318
|
+
page = nextPage;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
return matches;
|
|
322
|
+
}
|