@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
package/src/connect.js
ADDED
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import { access, copyFile, readFile, realpath, rename, stat, writeFile } from "node:fs/promises";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { basename, delimiter, isAbsolute, join } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
import { run } from "./process.js";
|
|
7
|
+
|
|
8
|
+
/** Agents `connect` can add the server to. */
|
|
9
|
+
export const CLIENTS = Object.freeze( [ "claude-code", "claude-desktop" ] );
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* How an agent starts the server.
|
|
13
|
+
*
|
|
14
|
+
* @typedef {object} ServerEntry
|
|
15
|
+
* @property {string} command
|
|
16
|
+
* @property {string[]} args
|
|
17
|
+
* @property {Record<string, string>} [env]
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {object} ConnectIO
|
|
22
|
+
* @property {(...parts: string[]) => void} print
|
|
23
|
+
* @property {(question: string) => Promise<boolean>} confirm asks the user, true if the user agrees
|
|
24
|
+
* @property {typeof run} [run] runs a program, replaced in tests
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The path of the running Node.js that survives upgrades where possible.
|
|
29
|
+
* `process.execPath` is the resolved binary, e.g. in a versioned folder of
|
|
30
|
+
* Homebrew that an upgrade removes, while the link to it on the PATH stays.
|
|
31
|
+
*
|
|
32
|
+
* @param {{ execPath?: string, path?: string }} [options] replaced in tests
|
|
33
|
+
* @returns {Promise<string>}
|
|
34
|
+
*/
|
|
35
|
+
export async function stableNodePath( { execPath = process.execPath, path = process.env.PATH ?? "" } = {} ) {
|
|
36
|
+
const binary = await realpath( execPath ).catch( () => execPath );
|
|
37
|
+
const name = basename( execPath );
|
|
38
|
+
|
|
39
|
+
for ( const folder of path.split( delimiter ) ) {
|
|
40
|
+
if ( !isAbsolute( folder ) ) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const candidate = join( folder, name );
|
|
45
|
+
|
|
46
|
+
if ( await realpath( candidate ).catch( () => null ) === binary ) {
|
|
47
|
+
return candidate;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return execPath;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* The entry starting this installation of the server. Absolute paths are
|
|
56
|
+
* used because Claude Desktop starts servers with a minimal PATH, which
|
|
57
|
+
* lacks Node.js installed via nvm or Homebrew.
|
|
58
|
+
*
|
|
59
|
+
* @param {{ profile?: string, node?: string }} options
|
|
60
|
+
* @returns {ServerEntry}
|
|
61
|
+
*/
|
|
62
|
+
export function serverEntry( { profile, node = process.execPath } ) {
|
|
63
|
+
return {
|
|
64
|
+
command: node,
|
|
65
|
+
args: [ fileURLToPath( new URL( "./cli.js", import.meta.url ) ), "serve" ],
|
|
66
|
+
...profile ? { env: { MCP_GITLAB_PROFILE: profile } } : {},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The configuration file of Claude Desktop.
|
|
72
|
+
*
|
|
73
|
+
* @param {NodeJS.Platform} [platform]
|
|
74
|
+
* @returns {string}
|
|
75
|
+
*/
|
|
76
|
+
export function desktopConfigFile( platform = process.platform ) {
|
|
77
|
+
switch ( platform ) {
|
|
78
|
+
case "darwin" :
|
|
79
|
+
return join( homedir(), "Library", "Application Support", "Claude", "claude_desktop_config.json" );
|
|
80
|
+
case "win32" :
|
|
81
|
+
return join( process.env.APPDATA ?? join( homedir(), "AppData", "Roaming" ), "Claude", "claude_desktop_config.json" );
|
|
82
|
+
default :
|
|
83
|
+
throw new Error( `Claude Desktop isn't available on ${platform}` );
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Describes an entry without the values of its environment, which may hold
|
|
89
|
+
* secrets of a server that isn't this one.
|
|
90
|
+
*
|
|
91
|
+
* @param {any} entry
|
|
92
|
+
* @returns {string}
|
|
93
|
+
*/
|
|
94
|
+
export function describeEntry( entry ) {
|
|
95
|
+
if ( !entry || typeof entry !== "object" ) {
|
|
96
|
+
return "(none)";
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const words = [ entry.command ?? entry.url ?? "(no command)", ...Array.isArray( entry.args ) ? entry.args : [] ];
|
|
100
|
+
const env = entry.env && typeof entry.env === "object" ? Object.keys( entry.env ) : [];
|
|
101
|
+
|
|
102
|
+
return `${words.map( word => ( /\s/.test( word ) ? JSON.stringify( word ) : word ) ).join( " " )}${env.length ? ` (environment: ${env.map( key => ( key === "MCP_GITLAB_PROFILE" ? `${key}=${entry.env[key]}` : key ) ).join( ", " )})` : ""}`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* @param {string} file
|
|
107
|
+
* @returns {Promise<boolean>}
|
|
108
|
+
*/
|
|
109
|
+
async function exists( file ) {
|
|
110
|
+
try {
|
|
111
|
+
await access( file );
|
|
112
|
+
|
|
113
|
+
return true;
|
|
114
|
+
} catch {
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Adds the server to the configuration of Claude Desktop, keeping everything
|
|
121
|
+
* else in that file and a copy of it as .bak.
|
|
122
|
+
*
|
|
123
|
+
* @param {{ name: string, entry: ServerEntry, file?: string }} options
|
|
124
|
+
* @param {ConnectIO} io
|
|
125
|
+
* @returns {Promise<boolean>} true if the file was changed
|
|
126
|
+
*/
|
|
127
|
+
export async function connectDesktop( { name, entry, file = desktopConfigFile() }, { print, confirm } ) {
|
|
128
|
+
let text = null;
|
|
129
|
+
|
|
130
|
+
try {
|
|
131
|
+
text = await readFile( file, "utf8" );
|
|
132
|
+
} catch ( error ) {
|
|
133
|
+
if ( error.code !== "ENOENT" ) {
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let config = {};
|
|
139
|
+
|
|
140
|
+
if ( text !== null && text.trim() ) {
|
|
141
|
+
try {
|
|
142
|
+
config = JSON.parse( text );
|
|
143
|
+
} catch ( error ) {
|
|
144
|
+
throw new Error( `${file} isn't valid JSON (${error.message}), fix it first: it is left untouched` );
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if ( !config || typeof config !== "object" || Array.isArray( config ) ) {
|
|
148
|
+
throw new Error( `${file} doesn't hold a JSON object, it is left untouched` );
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const servers = config.mcpServers && typeof config.mcpServers === "object" ? config.mcpServers : {};
|
|
153
|
+
const current = servers[name];
|
|
154
|
+
|
|
155
|
+
if ( isDeepStrictEqual( current, entry ) ) {
|
|
156
|
+
print( `Claude Desktop starts this installation as "${name}" already, nothing to change.` );
|
|
157
|
+
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
print( `Claude Desktop: ${file}` );
|
|
162
|
+
print( ` server "${name}"` );
|
|
163
|
+
print( ` now: ${describeEntry( current )}` );
|
|
164
|
+
print( ` new: ${describeEntry( entry )}` );
|
|
165
|
+
print( " other entries of the file are kept" );
|
|
166
|
+
|
|
167
|
+
if ( !await confirm( current ? `Replace the server "${name}"?` : `Add the server "${name}"?` ) ) {
|
|
168
|
+
print( "nothing changed" );
|
|
169
|
+
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const next = { ...config, mcpServers: { ...servers, [name]: entry } };
|
|
174
|
+
const mode = text === null ? 0o600 : ( await stat( file ) ).mode & 0o777;
|
|
175
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
176
|
+
|
|
177
|
+
if ( text !== null ) {
|
|
178
|
+
await copyFile( file, `${file}.bak` );
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
await writeFile( temporary, `${JSON.stringify( next, null, 2 )}\n`, { mode } );
|
|
182
|
+
await rename( temporary, file );
|
|
183
|
+
|
|
184
|
+
print( `saved${text === null ? "" : `, the previous version is ${file}.bak`}; restart Claude Desktop to use it` );
|
|
185
|
+
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Adds the server to the user configuration of Claude Code, using its CLI so
|
|
191
|
+
* the file it keeps for itself is never written here.
|
|
192
|
+
*
|
|
193
|
+
* @param {{ name: string, entry: ServerEntry }} options
|
|
194
|
+
* @param {ConnectIO} io
|
|
195
|
+
* @returns {Promise<boolean>} true if the configuration was changed
|
|
196
|
+
*/
|
|
197
|
+
export async function connectCode( { name, entry }, { print, confirm, run: runProgram = run } ) {
|
|
198
|
+
const json = JSON.stringify( { type: "stdio", ...entry } );
|
|
199
|
+
const add = [ "mcp", "add-json", name, json, "--scope", "user" ];
|
|
200
|
+
|
|
201
|
+
print( "Claude Code, for all projects of the user:" );
|
|
202
|
+
print( ` server "${name}": ${describeEntry( entry )}` );
|
|
203
|
+
print( ` runs: claude mcp add-json ${name} '${json}' --scope user` );
|
|
204
|
+
|
|
205
|
+
if ( !await confirm( `Add the server "${name}"?` ) ) {
|
|
206
|
+
print( "nothing changed" );
|
|
207
|
+
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
try {
|
|
212
|
+
await runProgram( "claude", add );
|
|
213
|
+
} catch ( error ) {
|
|
214
|
+
if ( error.code === "ENOENT" ) {
|
|
215
|
+
throw new Error( "the command \"claude\" of Claude Code isn't on the PATH" );
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if ( !/already exists/i.test( error.message ) ) {
|
|
219
|
+
throw error;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const existing = parseCodeEntry( await runProgram( "claude", [ "mcp", "get", name ] ).catch( () => "" ) );
|
|
223
|
+
|
|
224
|
+
print( `Claude Code has a server "${name}" already: ${describeEntry( existing )}` );
|
|
225
|
+
|
|
226
|
+
if ( !await confirm( "Replace it?" ) ) {
|
|
227
|
+
print( "nothing changed" );
|
|
228
|
+
|
|
229
|
+
return false;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
await runProgram( "claude", [ "mcp", "remove", name, "--scope", "user" ] );
|
|
233
|
+
await runProgram( "claude", add );
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
print( "added; start a new session of Claude Code, or reconnect via /mcp, to use it" );
|
|
237
|
+
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Tells whether the program and script of an entry still exist, e.g. after
|
|
243
|
+
* Node.js was upgraded via nvm.
|
|
244
|
+
*
|
|
245
|
+
* @param {{ command?: string, args?: string[] }} entry
|
|
246
|
+
* @param {{ inheritsPath?: boolean }} [options] whether the client starts servers with the PATH of the user's shell
|
|
247
|
+
* @returns {Promise<string[]>} problems found, empty if none
|
|
248
|
+
*/
|
|
249
|
+
export async function entryProblems( entry, { inheritsPath = false } = {} ) {
|
|
250
|
+
const problems = [];
|
|
251
|
+
|
|
252
|
+
if ( !entry?.command ) {
|
|
253
|
+
return ["it has no command"];
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if ( isAbsolute( entry.command ) ) {
|
|
257
|
+
if ( !await exists( entry.command ) ) {
|
|
258
|
+
problems.push( `${entry.command} doesn't exist any more` );
|
|
259
|
+
}
|
|
260
|
+
} else if ( !inheritsPath ) {
|
|
261
|
+
problems.push( `it starts "${entry.command}" from the PATH, which Claude Desktop lacks: run "mcp-gitlab connect claude-desktop" again to use absolute paths` );
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const script = entry.args?.find( arg => /\.m?js$/.test( arg ) );
|
|
265
|
+
|
|
266
|
+
if ( script && isAbsolute( script ) && !await exists( script ) ) {
|
|
267
|
+
problems.push( `${script} doesn't exist any more` );
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return problems;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Reads the entry of a server from what `claude mcp get` prints.
|
|
275
|
+
*
|
|
276
|
+
* @param {string} output
|
|
277
|
+
* @returns {{ command?: string, args: string[] }|null}
|
|
278
|
+
*/
|
|
279
|
+
export function parseCodeEntry( output ) {
|
|
280
|
+
const command = /^\s*Command:\s*(.+)$/m.exec( output )?.[1]?.trim();
|
|
281
|
+
|
|
282
|
+
if ( !command ) {
|
|
283
|
+
return null;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const args = /^\s*Args:\s*(.*)$/m.exec( output )?.[1]?.trim();
|
|
287
|
+
|
|
288
|
+
return { command, args: args ? args.split( /\s+/ ) : [] };
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Checks the entries of the server in all supported clients.
|
|
293
|
+
*
|
|
294
|
+
* @param {{ name: string, file?: string }} options
|
|
295
|
+
* @param {ConnectIO} io
|
|
296
|
+
* @returns {Promise<boolean>} true if no problem was found
|
|
297
|
+
*/
|
|
298
|
+
export async function checkConnections( { name, file }, { print, run: runProgram = run } ) {
|
|
299
|
+
let healthy = true;
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* @param {string} client
|
|
303
|
+
* @param {any} entry
|
|
304
|
+
* @param {{ inheritsPath?: boolean }} [options]
|
|
305
|
+
*/
|
|
306
|
+
const report = async( client, entry, options ) => {
|
|
307
|
+
if ( !entry ) {
|
|
308
|
+
print( `${client}: no server "${name}"` );
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const problems = await entryProblems( entry, options );
|
|
313
|
+
|
|
314
|
+
print( `${client}: ${problems.length ? problems.join( "; " ) : "ok"} (${describeEntry( entry )})` );
|
|
315
|
+
healthy &&= problems.length === 0;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
try {
|
|
319
|
+
await report( "claude-code", parseCodeEntry( await runProgram( "claude", [ "mcp", "get", name ] ) ), { inheritsPath: true } );
|
|
320
|
+
} catch ( error ) {
|
|
321
|
+
print( `claude-code: ${error.code === "ENOENT" ? "the command \"claude\" isn't on the PATH" : `no server "${name}"`}` );
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
try {
|
|
325
|
+
const path = file ?? desktopConfigFile();
|
|
326
|
+
const config = await exists( path ) ? JSON.parse( await readFile( path, "utf8" ) ) : {};
|
|
327
|
+
|
|
328
|
+
await report( "claude-desktop", config.mcpServers?.[name] );
|
|
329
|
+
} catch ( error ) {
|
|
330
|
+
print( `claude-desktop: ${error.message}` );
|
|
331
|
+
healthy = false;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return healthy;
|
|
335
|
+
}
|
package/src/consent.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { DeniedError } from "./errors.js";
|
|
2
|
+
import { createSessionStore, formatUntil } from "./session.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Asks the user in a dialog of the desktop before something happens, and
|
|
6
|
+
* remembers approvals granted for the session. The agent can't answer these
|
|
7
|
+
* dialogs, so they protect against an agent acting beyond what the user wants.
|
|
8
|
+
*
|
|
9
|
+
* A session lasts as long as the server runs, unless the user started one with
|
|
10
|
+
* "mcp-gitlab session <duration>": then approvals granted for the session are
|
|
11
|
+
* kept until it ends, beyond restarts of the server.
|
|
12
|
+
*
|
|
13
|
+
* @param {import("./platform/index.js").Platform} platform
|
|
14
|
+
* @param {object} [options]
|
|
15
|
+
* @param {ReturnType<typeof createSessionStore>} [options.sessions]
|
|
16
|
+
*/
|
|
17
|
+
export function createConsent( platform, { sessions = createSessionStore() } = {} ) {
|
|
18
|
+
/** @type {Set<string>} */
|
|
19
|
+
const granted = new Set();
|
|
20
|
+
|
|
21
|
+
/** @type {Map<string, Promise<void>>} */
|
|
22
|
+
const pending = new Map();
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {string} key what is approved, e.g. "host:gitlab.com"
|
|
26
|
+
* @param {string} message
|
|
27
|
+
* @param {"session"|"always"} mode `always` asks every time, `session` once
|
|
28
|
+
* @returns {Promise<void>}
|
|
29
|
+
*/
|
|
30
|
+
async function ask( key, message, mode ) {
|
|
31
|
+
if ( mode === "session" && granted.has( key ) ) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const session = mode === "session" ? await sessions.current() : null;
|
|
36
|
+
|
|
37
|
+
if ( session?.grants.includes( key ) ) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// concurrent calls share one dialog instead of stacking several
|
|
42
|
+
const running = pending.get( key );
|
|
43
|
+
|
|
44
|
+
if ( running && mode === "session" ) {
|
|
45
|
+
await running;
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const request = ( async() => {
|
|
50
|
+
const answer = await platform.confirm( {
|
|
51
|
+
message,
|
|
52
|
+
allowSession: mode === "session",
|
|
53
|
+
...session ? { sessionLabel: `Allow until ${formatUntil( session.expires )}` } : {},
|
|
54
|
+
} );
|
|
55
|
+
|
|
56
|
+
if ( answer === "deny" ) {
|
|
57
|
+
throw new DeniedError( "the user denied this, don't try again unless the user asks for it" );
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// approvals for a session that ended meanwhile last as long as the server runs
|
|
61
|
+
if ( answer === "session" && !( session && await sessions.grant( session, key ) ) ) {
|
|
62
|
+
granted.add( key );
|
|
63
|
+
}
|
|
64
|
+
} )();
|
|
65
|
+
|
|
66
|
+
pending.set( key, request );
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
await request;
|
|
70
|
+
} finally {
|
|
71
|
+
pending.delete( key );
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
/**
|
|
77
|
+
* Asks before the token for a host is used for the first time in a session.
|
|
78
|
+
*
|
|
79
|
+
* @param {import("./hosts.js").Host} host
|
|
80
|
+
* @returns {Promise<void>}
|
|
81
|
+
*/
|
|
82
|
+
approveHost( host ) {
|
|
83
|
+
return ask( `host:${host.name}`, `An agent wants to access GitLab at ${host.url} using your access token for "${host.name}".`, "session" );
|
|
84
|
+
},
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Asks before a tool is used, as the active profile requests it.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} permission
|
|
90
|
+
* @param {string} message
|
|
91
|
+
* @param {"session"|"always"} mode
|
|
92
|
+
* @returns {Promise<void>}
|
|
93
|
+
*/
|
|
94
|
+
confirm( permission, message, mode ) {
|
|
95
|
+
return ask( `permission:${permission}`, message, mode );
|
|
96
|
+
},
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Has the user enter a secret value in a dialog, which the agent never sees.
|
|
100
|
+
*
|
|
101
|
+
* @param {string} message what the value is for, and that cancelling denies
|
|
102
|
+
* @returns {Promise<string>}
|
|
103
|
+
* @throws {DeniedError} if the user cancelled or entered nothing
|
|
104
|
+
*/
|
|
105
|
+
async enterSecret( message ) {
|
|
106
|
+
const value = await platform.promptSecret( { message } );
|
|
107
|
+
|
|
108
|
+
if ( value === null || value === "" ) {
|
|
109
|
+
throw new DeniedError( "the user didn't enter a value, don't try again unless the user asks for it" );
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return value;
|
|
113
|
+
},
|
|
114
|
+
};
|
|
115
|
+
}
|
package/src/downloads.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { mkdir, readdir, rm, rmdir, stat } from "node:fs/promises";
|
|
2
|
+
import { isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { configDirectory } from "./paths.js";
|
|
4
|
+
|
|
5
|
+
/** Days files saved by artifact_download are kept. */
|
|
6
|
+
export const DOWNLOAD_RETENTION_DAYS = 7;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Folder files are saved in for the user, below the configuration folder.
|
|
10
|
+
*
|
|
11
|
+
* @returns {string}
|
|
12
|
+
*/
|
|
13
|
+
export function downloadsDirectory() {
|
|
14
|
+
return join( configDirectory(), "downloads" );
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Turns a path into one below a folder, refusing anything that would leave it.
|
|
19
|
+
*
|
|
20
|
+
* @param {string} base
|
|
21
|
+
* @param {string[]} parts segments, each possibly containing slashes
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
export function pathBelow( base, parts ) {
|
|
25
|
+
const segments = parts.flatMap( part => String( part ).split( "/" ) ).filter( segment => segment !== "" && segment !== "." );
|
|
26
|
+
|
|
27
|
+
if ( segments.some( segment => segment === ".." || /[\0-\x1f\\:]/.test( segment ) ) ) {
|
|
28
|
+
throw new Error( "the path contains parts that aren't allowed" );
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const target = resolve( base, ...segments );
|
|
32
|
+
const inside = relative( base, target );
|
|
33
|
+
|
|
34
|
+
if ( !inside || inside === ".." || inside.startsWith( `..${sep}` ) || isAbsolute( inside ) ) {
|
|
35
|
+
throw new Error( "the path leaves the downloads folder" );
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return target;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Prepares the folder a file is saved into.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} file
|
|
45
|
+
* @returns {Promise<void>}
|
|
46
|
+
*/
|
|
47
|
+
export async function prepareFolderOf( file ) {
|
|
48
|
+
await mkdir( resolve( file, ".." ), { recursive: true, mode: 0o700 } );
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Removes saved files older than the retention time, and folders left empty.
|
|
53
|
+
*
|
|
54
|
+
* @param {{ directory?: string, now?: Date }} [options]
|
|
55
|
+
* @returns {Promise<number>} number of files removed
|
|
56
|
+
*/
|
|
57
|
+
export async function pruneDownloads( { directory = downloadsDirectory(), now = new Date() } = {} ) {
|
|
58
|
+
const oldest = now.getTime() - ( DOWNLOAD_RETENTION_DAYS * 86_400_000 );
|
|
59
|
+
let removed = 0;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* @param {string} folder
|
|
63
|
+
* @returns {Promise<void>}
|
|
64
|
+
*/
|
|
65
|
+
async function sweep( folder ) {
|
|
66
|
+
let entries;
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
entries = await readdir( folder, { withFileTypes: true } );
|
|
70
|
+
} catch ( error ) {
|
|
71
|
+
if ( error.code === "ENOENT" ) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
for ( const entry of entries ) {
|
|
79
|
+
const path = join( folder, entry.name );
|
|
80
|
+
|
|
81
|
+
if ( entry.isDirectory() ) {
|
|
82
|
+
await sweep( path );
|
|
83
|
+
await rmdir( path ).catch( () => undefined );
|
|
84
|
+
} else if ( ( await stat( path ) ).mtimeMs < oldest ) {
|
|
85
|
+
await rm( path, { force: true } );
|
|
86
|
+
removed++;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
await sweep( directory );
|
|
92
|
+
|
|
93
|
+
return removed;
|
|
94
|
+
}
|
package/src/errors.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The user or the active profile refused an action. Audited as "denied"
|
|
3
|
+
* instead of "error".
|
|
4
|
+
*/
|
|
5
|
+
export class DeniedError extends Error {
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} message
|
|
8
|
+
*/
|
|
9
|
+
constructor( message ) {
|
|
10
|
+
super( message );
|
|
11
|
+
this.name = "DeniedError";
|
|
12
|
+
}
|
|
13
|
+
}
|
package/src/git.js
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { stat } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
|
+
import { run } from "./process.js";
|
|
4
|
+
import { parseRemote } from "./refs.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Runs git in a local clone.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} directory
|
|
10
|
+
* @param {...string} args
|
|
11
|
+
* @returns {Promise<string>} trimmed output
|
|
12
|
+
*/
|
|
13
|
+
async function git( directory, ...args ) {
|
|
14
|
+
return ( await run( "git", args, { cwd: directory } ) ).trim();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Detects the repository of a local clone from its remote "origin".
|
|
19
|
+
*
|
|
20
|
+
* @param {string} directory
|
|
21
|
+
* @param {import("./hosts.js").Host[]} hosts
|
|
22
|
+
* @returns {Promise<import("./refs.js").RepoRef>}
|
|
23
|
+
*/
|
|
24
|
+
export async function detectRepo( directory, hosts ) {
|
|
25
|
+
let url;
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
url = await git( directory, "remote", "get-url", "origin" );
|
|
29
|
+
} catch ( cause ) {
|
|
30
|
+
throw new Error( `could not read remote "origin" of ${directory}, pass "repo" instead (${cause.message})` );
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return parseRemote( url, hosts );
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The branch checked out in a local clone, its commit and how many commits
|
|
38
|
+
* haven't been pushed to its upstream yet.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} directory
|
|
41
|
+
* @returns {Promise<{ branch: string, head: string, unpushed: number|null }>} `unpushed` is null without upstream
|
|
42
|
+
*/
|
|
43
|
+
export async function localBranch( directory ) {
|
|
44
|
+
const branch = await git( directory, "rev-parse", "--abbrev-ref", "HEAD" );
|
|
45
|
+
|
|
46
|
+
if ( branch === "HEAD" ) {
|
|
47
|
+
throw new Error( `${directory} has no branch checked out (detached HEAD)` );
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let unpushed = null;
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
unpushed = Number( await git( directory, "rev-list", "--count", "@{upstream}..HEAD" ) );
|
|
54
|
+
} catch {
|
|
55
|
+
// there is no upstream
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return { branch, head: await git( directory, "rev-parse", "HEAD" ), unpushed };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The top folder of the local clone a folder belongs to: the closest folder
|
|
63
|
+
* containing ".git", which is a folder in clones and a file in worktrees.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} directory absolute path
|
|
66
|
+
* @returns {Promise<string|null>} null if the folder isn't in a clone
|
|
67
|
+
*/
|
|
68
|
+
export async function cloneRoot( directory ) {
|
|
69
|
+
let folder = resolve( directory );
|
|
70
|
+
|
|
71
|
+
try {
|
|
72
|
+
if ( !( await stat( folder ) ).isDirectory() ) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
} catch {
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
for ( ;; ) {
|
|
80
|
+
try {
|
|
81
|
+
await stat( join( folder, ".git" ) );
|
|
82
|
+
|
|
83
|
+
return folder;
|
|
84
|
+
} catch {
|
|
85
|
+
const parent = dirname( folder );
|
|
86
|
+
|
|
87
|
+
if ( parent === folder ) {
|
|
88
|
+
return null;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
folder = parent;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|