All files / src/entities/milestones registry.ts

100% Statements 49/49
100% Branches 18/18
100% Functions 6/6
100% Lines 49/49

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 18716x 16x 16x 16x 16x     16x               16x                           16x     11x 1x     10x   10x   5x 5x   5x         2x         1x 1x   1x             1x 1x   1x               1x 1x   1x                                                     19x     12x 4x     8x   8x     3x   3x               2x   2x               1x 1x         2x 1x     1x                             16x 25x           16x 11x           16x     9x 5x 5x 10x     4x    
import * as z from "zod";
import { BrowseMilestonesSchema } from "./schema-readonly";
import { ManageMilestoneSchema } from "./schema";
import { gitlab, toQuery } from "../../utils/gitlab-api";
import { resolveNamespaceForAPI } from "../../utils/namespace";
import { ToolRegistry, EnhancedToolDefinition } from "../../types";
// assertDefined no longer needed - discriminated union provides type safety
import { isActionDenied } from "../../config";
 
/**
 * Milestones tools registry - 2 CQRS tools replacing 9 individual tools
 *
 * browse_milestones (Query): list, get, issues, merge_requests, burndown
 * manage_milestone (Command): create, update, delete, promote
 */
export const milestonesToolRegistry: ToolRegistry = new Map<string, EnhancedToolDefinition>([
  // ============================================================================
  // browse_milestones - CQRS Query Tool (discriminated union schema)
  // TypeScript automatically narrows types in each switch case
  // ============================================================================
  [
    "browse_milestones",
    {
      name: "browse_milestones",
      description:
        "Track milestone progress with associated issues and MRs. Actions: list (filter by state/title/search), get (milestone details), issues (items in milestone), merge_requests (MRs targeting milestone), burndown (chart data for sprint tracking). Related: manage_milestone to create/update.",
      inputSchema: z.toJSONSchema(BrowseMilestonesSchema),
      gate: { envVar: "USE_MILESTONE", defaultValue: true },
      handler: async (args: unknown) => {
        const input = BrowseMilestonesSchema.parse(args);
 
        // Runtime validation: reject denied actions even if they bypass schema filtering
        if (isActionDenied("browse_milestones", input.action)) {
          throw new Error(`Action '${input.action}' is not allowed for browse_milestones tool`);
        }
 
        const { entityType, encodedPath } = await resolveNamespaceForAPI(input.namespace);
 
        switch (input.action) {
          case "list": {
            const { action: _action, namespace: _namespace, ...rest } = input;
            const query = toQuery(rest, []);
 
            return gitlab.get(`${entityType}/${encodedPath}/milestones`, { query });
          }
 
          case "get": {
            // TypeScript knows: input has milestone_id (uses global ID, not IID)
            return gitlab.get(`${entityType}/${encodedPath}/milestones/${input.milestone_id}`);
          }
 
          case "issues": {
            // TypeScript knows: input has milestone_id (uses global ID), per_page, page (optional)
            const { action: _action, namespace: _namespace, milestone_id, ...rest } = input;
            const query = toQuery(rest, []);
 
            return gitlab.get(`${entityType}/${encodedPath}/milestones/${milestone_id}/issues`, {
              query,
            });
          }
 
          case "merge_requests": {
            // TypeScript knows: input has milestone_id (uses global ID), per_page, page (optional)
            const { action: _action, namespace: _namespace, milestone_id, ...rest } = input;
            const query = toQuery(rest, []);
 
            return gitlab.get(
              `${entityType}/${encodedPath}/milestones/${milestone_id}/merge_requests`,
              { query }
            );
          }
 
          case "burndown": {
            // TypeScript knows: input has milestone_id (uses global ID), per_page, page (optional)
            const { action: _action, namespace: _namespace, milestone_id, ...rest } = input;
            const query = toQuery(rest, []);
 
            return gitlab.get(
              `${entityType}/${encodedPath}/milestones/${milestone_id}/burndown_events`,
              { query }
            );
          }
 
          /* istanbul ignore next -- unreachable with Zod validation */
          default:
            throw new Error(`Unknown action: ${(input as { action: string }).action}`);
        }
      },
    },
  ],
 
  // ============================================================================
  // manage_milestone - CQRS Command Tool (discriminated union schema)
  // TypeScript automatically narrows types in each switch case
  // ============================================================================
  [
    "manage_milestone",
    {
      name: "manage_milestone",
      description:
        "Create, update, or delete project/group milestones. Actions: create (title + optional dates/description), update (modify properties or close/activate), delete (remove permanently), promote (elevate project milestone to group). Related: browse_milestones for progress tracking.",
      inputSchema: z.toJSONSchema(ManageMilestoneSchema),
      gate: { envVar: "USE_MILESTONE", defaultValue: true },
      handler: async (args: unknown) => {
        const input = ManageMilestoneSchema.parse(args);
 
        // Runtime validation: reject denied actions even if they bypass schema filtering
        if (isActionDenied("manage_milestone", input.action)) {
          throw new Error(`Action '${input.action}' is not allowed for manage_milestone tool`);
        }
 
        const { entityType, encodedPath } = await resolveNamespaceForAPI(input.namespace);
 
        switch (input.action) {
          case "create": {
            // TypeScript knows: input has title (required), description, due_date, start_date (optional)
            const { action: _action, namespace: _namespace, ...body } = input;
 
            return gitlab.post(`${entityType}/${encodedPath}/milestones`, {
              body,
              contentType: "json",
            });
          }
 
          case "update": {
            // TypeScript knows: input has milestone_id (uses global ID), title, description, etc. (optional)
            const { action: _action, namespace: _namespace, milestone_id, ...body } = input;
 
            return gitlab.put(`${entityType}/${encodedPath}/milestones/${milestone_id}`, {
              body,
              contentType: "json",
            });
          }
 
          case "delete": {
            // TypeScript knows: input has milestone_id (uses global ID)
            await gitlab.delete(`${entityType}/${encodedPath}/milestones/${input.milestone_id}`);
            return { deleted: true };
          }
 
          case "promote": {
            // TypeScript knows: input has milestone_id (uses global ID)
            if (entityType !== "projects") {
              throw new Error("Milestone promotion is only available for projects, not groups");
            }
 
            return gitlab.post(`projects/${encodedPath}/milestones/${input.milestone_id}/promote`);
          }
 
          /* 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 getMilestonesReadOnlyToolNames(): string[] {
  return ["browse_milestones"];
}
 
/**
 * Get all tool definitions from the registry
 */
export function getMilestonesToolDefinitions(): EnhancedToolDefinition[] {
  return Array.from(milestonesToolRegistry.values());
}
 
/**
 * Get filtered tools based on read-only mode
 */
export function getFilteredMilestonesTools(
  readOnlyMode: boolean = false
): EnhancedToolDefinition[] {
  if (readOnlyMode) {
    const readOnlyNames = getMilestonesReadOnlyToolNames();
    return Array.from(milestonesToolRegistry.values()).filter(tool =>
      readOnlyNames.includes(tool.name)
    );
  }
  return getMilestonesToolDefinitions();
}