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 | 3x 3x 3x 3x 3x 3x 3x 13x 11x 11x 11x 8x 8x 8x 3x 3x 1x 3x 14x 12x 12x 12x 4x 4x 4x 4x 4x 1x 4x 4x 4x 1x 4x 2x 3x 14x 3x 2x 3x 3x 2x 2x 4x 1x | import * as z from "zod";
import { BrowseVariablesSchema } from "./schema-readonly";
import { ManageVariableSchema } from "./schema";
import { gitlab, toQuery } from "../../utils/gitlab-api";
import { resolveNamespaceForAPI } from "../../utils/namespace";
import { ToolRegistry, EnhancedToolDefinition } from "../../types";
import { isActionDenied } from "../../config";
/**
* Variables tools registry - 2 CQRS tools replacing 5 individual tools
*
* browse_variables (Query): list, get
* manage_variable (Command): create, update, delete
*/
export const variablesToolRegistry: ToolRegistry = new Map<string, EnhancedToolDefinition>([
// ============================================================================
// browse_variables - CQRS Query Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"browse_variables",
{
name: "browse_variables",
description:
'BROWSE CI/CD variables. Actions: "list" shows all variables in project/group with pagination, "get" retrieves single variable details by key with optional environment scope filter.',
inputSchema: z.toJSONSchema(BrowseVariablesSchema),
gate: { envVar: "USE_VARIABLES", defaultValue: true },
handler: async (args: unknown) => {
const input = BrowseVariablesSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("browse_variables", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for browse_variables tool`);
}
const { entityType, encodedPath } = await resolveNamespaceForAPI(input.namespace);
switch (input.action) {
case "list": {
// TypeScript knows: input has per_page, page (optional)
const { action: _action, namespace: _namespace, ...rest } = input;
const query = toQuery(rest, []);
return gitlab.get(`${entityType}/${encodedPath}/variables`, { query });
}
case "get": {
// TypeScript knows: input has key (required), filter (optional)
const query: Record<string, string | undefined> = {};
if (input.filter?.environment_scope) {
query["filter[environment_scope]"] = input.filter.environment_scope;
}
return gitlab.get(
`${entityType}/${encodedPath}/variables/${encodeURIComponent(input.key)}`,
{ query }
);
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
},
},
],
// ============================================================================
// manage_variable - CQRS Command Tool (discriminated union schema)
// TypeScript automatically narrows types in each switch case
// ============================================================================
[
"manage_variable",
{
name: "manage_variable",
description:
'MANAGE CI/CD variables. Actions: "create" adds new variable (requires key and value), "update" modifies existing variable, "delete" removes variable permanently. Supports environment scoping and protection settings.',
inputSchema: z.toJSONSchema(ManageVariableSchema),
gate: { envVar: "USE_VARIABLES", defaultValue: true },
handler: async (args: unknown) => {
const input = ManageVariableSchema.parse(args);
// Runtime validation: reject denied actions even if they bypass schema filtering
Iif (isActionDenied("manage_variable", input.action)) {
throw new Error(`Action '${input.action}' is not allowed for manage_variable tool`);
}
const { entityType, encodedPath } = await resolveNamespaceForAPI(input.namespace);
switch (input.action) {
case "create": {
// TypeScript knows: input has key, value (required), variable_type, environment_scope, etc. (optional)
const { action: _action, namespace: _namespace, ...body } = input;
return gitlab.post(`${entityType}/${encodedPath}/variables`, {
body,
contentType: "json",
});
}
case "update": {
// TypeScript knows: input has key (required), value, filter, etc. (optional)
const { action: _action, namespace: _namespace, key, filter, ...body } = input;
const query: Record<string, string | undefined> = {};
if (filter?.environment_scope) {
query["filter[environment_scope]"] = filter.environment_scope;
}
return gitlab.put(`${entityType}/${encodedPath}/variables/${encodeURIComponent(key)}`, {
query,
body,
contentType: "json",
});
}
case "delete": {
// TypeScript knows: input has key (required), filter (optional)
const query: Record<string, string | undefined> = {};
if (input.filter?.environment_scope) {
query["filter[environment_scope]"] = input.filter.environment_scope;
}
await gitlab.delete(
`${entityType}/${encodedPath}/variables/${encodeURIComponent(input.key)}`,
{ query }
);
return { deleted: true };
}
/* istanbul ignore next -- unreachable with Zod discriminatedUnion */
default:
throw new Error(`Unknown action: ${(input as { action: string }).action}`);
}
},
},
],
]);
/**
* Get read-only tool names from the registry
*/
export function getVariablesReadOnlyToolNames(): string[] {
return ["browse_variables"];
}
/**
* Get all tool definitions from the registry
*/
export function getVariablesToolDefinitions(): EnhancedToolDefinition[] {
return Array.from(variablesToolRegistry.values());
}
/**
* Get filtered tools based on read-only mode
*/
export function getFilteredVariablesTools(readOnlyMode: boolean = false): EnhancedToolDefinition[] {
if (readOnlyMode) {
const readOnlyNames = getVariablesReadOnlyToolNames();
return Array.from(variablesToolRegistry.values()).filter(tool =>
readOnlyNames.includes(tool.name)
);
}
return getVariablesToolDefinitions();
}
|