All files / src/entities/members registry.ts

98.91% Statements 91/92
97.29% Branches 36/37
100% Functions 6/6
100% Lines 83/83

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 25618x 18x 18x 18x   18x                   18x                                             11x   11x   10x   2x 2x 2x           1x 1x 1x           2x 2x 2x 2x     2x       2x 2x 2x 2x     2x       2x 2x 2x           1x 1x 1x                                                                   14x   14x   13x   2x 2x   2x       2x   2x             2x 2x   2x       2x   2x             2x 2x 2x   2x 2x 2x   2x     2x       2x 2x 2x   2x 2x 2x   2x     2x       2x 2x 2x   2x 2x   2x             3x 3x 3x   3x 3x 3x   3x                                   18x 2x           18x 3x           18x 3x 1x 1x 2x     2x    
import * as z from 'zod';
import { BrowseMembersSchema } from './schema-readonly';
import { ManageMemberSchema } from './schema';
import { gitlab, toQuery } from '../../utils/gitlab-api';
import { ToolRegistry, EnhancedToolDefinition } from '../../types';
import { assertActionAllowed } from '../utils';
 
/**
 * Members tools registry - 2 CQRS tools
 *
 * browse_members (Query): list_project, list_group, get_project, get_group,
 *                         list_all_project, list_all_group
 * manage_member (Command): add_to_project, add_to_group, remove_from_project,
 *                          remove_from_group, update_project, update_group
 */
export const membersToolRegistry: ToolRegistry = new Map<string, EnhancedToolDefinition>([
  // ============================================================================
  // browse_members - CQRS Query Tool (discriminated union schema)
  // ============================================================================
  [
    'browse_members',
    {
      name: 'browse_members',
      description:
        'View team members and access levels in projects or groups. Actions: list_project, list_group, get_project, get_group (direct members), list_all_project, list_all_group (includes inherited). Levels: Guest(10), Reporter(20), Developer(30), Maintainer(40), Owner(50). Related: manage_member to add/remove, browse_users to find users by name.',
      inputSchema: z.toJSONSchema(BrowseMembersSchema),
      requirements: {
        default: { tier: 'free', minVersion: '8.0' },
        actions: {
          list_all_project: {
            tier: 'free',
            minVersion: '12.4',
            notes: 'Includes inherited members',
          },
          list_all_group: { tier: 'free', minVersion: '12.4', notes: 'Includes inherited members' },
        },
      },
      handler: async (args: unknown): Promise<unknown> => {
        const input = BrowseMembersSchema.parse(args);
 
        assertActionAllowed('browse_members', input.action);
 
        switch (input.action) {
          case 'list_project': {
            const { action: _action, project_id, ...queryOptions } = input;
            const encodedProjectId = encodeURIComponent(project_id);
            return gitlab.get(`projects/${encodedProjectId}/members`, {
              query: toQuery(queryOptions, []),
            });
          }
 
          case 'list_group': {
            const { action: _action, group_id, ...queryOptions } = input;
            const encodedGroupId = encodeURIComponent(group_id);
            return gitlab.get(`groups/${encodedGroupId}/members`, {
              query: toQuery(queryOptions, []),
            });
          }
 
          case 'get_project': {
            const { project_id, user_id, include_inherited } = input;
            const encodedProjectId = encodeURIComponent(project_id);
            const encodedUserId = encodeURIComponent(user_id);
            const endpoint = include_inherited
              ? `projects/${encodedProjectId}/members/all/${encodedUserId}`
              : `projects/${encodedProjectId}/members/${encodedUserId}`;
            return gitlab.get(endpoint);
          }
 
          case 'get_group': {
            const { group_id, user_id, include_inherited } = input;
            const encodedGroupId = encodeURIComponent(group_id);
            const encodedUserId = encodeURIComponent(user_id);
            const endpoint = include_inherited
              ? `groups/${encodedGroupId}/members/all/${encodedUserId}`
              : `groups/${encodedGroupId}/members/${encodedUserId}`;
            return gitlab.get(endpoint);
          }
 
          case 'list_all_project': {
            const { action: _action, project_id, ...queryOptions } = input;
            const encodedProjectId = encodeURIComponent(project_id);
            return gitlab.get(`projects/${encodedProjectId}/members/all`, {
              query: toQuery(queryOptions, []),
            });
          }
 
          case 'list_all_group': {
            const { action: _action, group_id, ...queryOptions } = input;
            const encodedGroupId = encodeURIComponent(group_id);
            return gitlab.get(`groups/${encodedGroupId}/members/all`, {
              query: toQuery(queryOptions, []),
            });
          }
 
          /* istanbul ignore next -- unreachable with Zod discriminatedUnion */
          default:
            throw new Error(`Unknown action: ${(input as { action: string }).action}`);
        }
      },
    },
  ],
 
  // ============================================================================
  // manage_member - CQRS Command Tool (discriminated union schema)
  // ============================================================================
  [
    'manage_member',
    {
      name: 'manage_member',
      description:
        'Add, remove, or update access levels for project/group members. Actions: add_to_project, add_to_group (with access level + optional expiry), remove_from_project, remove_from_group, update_project, update_group (change access level). Related: browse_members for current membership.',
      inputSchema: z.toJSONSchema(ManageMemberSchema),
      requirements: {
        default: { tier: 'free', minVersion: '8.0' },
        actions: {
          update_group: {
            tier: 'free',
            minVersion: '8.0',
            notes: 'member_role_id requires Ultimate',
          },
        },
      },
      handler: async (args: unknown): Promise<unknown> => {
        const input = ManageMemberSchema.parse(args);
 
        assertActionAllowed('manage_member', input.action);
 
        switch (input.action) {
          case 'add_to_project': {
            const { project_id, user_id, access_level, expires_at } = input;
            const encodedProjectId = encodeURIComponent(project_id);
 
            const body: Record<string, unknown> = {
              user_id,
              access_level,
            };
            if (expires_at !== undefined) body.expires_at = expires_at;
 
            return gitlab.post(`projects/${encodedProjectId}/members`, {
              body,
              contentType: 'json',
            });
          }
 
          case 'add_to_group': {
            const { group_id, user_id, access_level, expires_at } = input;
            const encodedGroupId = encodeURIComponent(group_id);
 
            const body: Record<string, unknown> = {
              user_id,
              access_level,
            };
            if (expires_at !== undefined) body.expires_at = expires_at;
 
            return gitlab.post(`groups/${encodedGroupId}/members`, {
              body,
              contentType: 'json',
            });
          }
 
          case 'remove_from_project': {
            const { project_id, user_id, skip_subresources, unassign_issuables } = input;
            const encodedProjectId = encodeURIComponent(project_id);
            const encodedUserId = encodeURIComponent(user_id);
 
            const query: Record<string, boolean | undefined> = {};
            if (skip_subresources !== undefined) query.skip_subresources = skip_subresources;
            if (unassign_issuables !== undefined) query.unassign_issuables = unassign_issuables;
 
            await gitlab.delete(`projects/${encodedProjectId}/members/${encodedUserId}`, {
              query,
            });
            return { removed: true, project_id, user_id };
          }
 
          case 'remove_from_group': {
            const { group_id, user_id, skip_subresources, unassign_issuables } = input;
            const encodedGroupId = encodeURIComponent(group_id);
            const encodedUserId = encodeURIComponent(user_id);
 
            const query: Record<string, boolean | undefined> = {};
            if (skip_subresources !== undefined) query.skip_subresources = skip_subresources;
            Iif (unassign_issuables !== undefined) query.unassign_issuables = unassign_issuables;
 
            await gitlab.delete(`groups/${encodedGroupId}/members/${encodedUserId}`, {
              query,
            });
            return { removed: true, group_id, user_id };
          }
 
          case 'update_project': {
            const { project_id, user_id, access_level, expires_at } = input;
            const encodedProjectId = encodeURIComponent(project_id);
            const encodedUserId = encodeURIComponent(user_id);
 
            const body: Record<string, unknown> = { access_level };
            if (expires_at !== undefined) body.expires_at = expires_at;
 
            return gitlab.put(`projects/${encodedProjectId}/members/${encodedUserId}`, {
              body,
              contentType: 'json',
            });
          }
 
          case 'update_group': {
            const { group_id, user_id, access_level, expires_at, member_role_id } = input;
            const encodedGroupId = encodeURIComponent(group_id);
            const encodedUserId = encodeURIComponent(user_id);
 
            const body: Record<string, unknown> = { access_level };
            if (expires_at !== undefined) body.expires_at = expires_at;
            if (member_role_id !== undefined) body.member_role_id = member_role_id;
 
            return gitlab.put(`groups/${encodedGroupId}/members/${encodedUserId}`, {
              body,
              contentType: 'json',
            });
          }
 
          /* 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 getMembersReadOnlyToolNames(): string[] {
  return ['browse_members'];
}
 
/**
 * Get all tool definitions from the registry
 */
export function getMembersToolDefinitions(): EnhancedToolDefinition[] {
  return Array.from(membersToolRegistry.values());
}
 
/**
 * Get filtered tools based on read-only mode
 */
export function getFilteredMembersTools(readOnlyMode: boolean = false): EnhancedToolDefinition[] {
  if (readOnlyMode) {
    const readOnlyNames = getMembersReadOnlyToolNames();
    return Array.from(membersToolRegistry.values()).filter((tool) =>
      readOnlyNames.includes(tool.name),
    );
  }
  return getMembersToolDefinitions();
}