Add GitLab Namespaces API support

This commit is contained in:
Admin
2025-03-18 00:13:28 -07:00
parent bf867b2fae
commit 29cc6fbc28
2 changed files with 175 additions and 0 deletions

130
index.ts
View File

@ -24,6 +24,8 @@ import {
GitLabSearchResponseSchema, GitLabSearchResponseSchema,
GitLabTreeSchema, GitLabTreeSchema,
GitLabCommitSchema, GitLabCommitSchema,
GitLabNamespaceSchema,
GitLabNamespaceExistsResponseSchema,
CreateRepositoryOptionsSchema, CreateRepositoryOptionsSchema,
CreateIssueOptionsSchema, CreateIssueOptionsSchema,
CreateMergeRequestOptionsSchema, CreateMergeRequestOptionsSchema,
@ -41,6 +43,9 @@ import {
GetMergeRequestSchema, GetMergeRequestSchema,
GetMergeRequestDiffsSchema, GetMergeRequestDiffsSchema,
UpdateMergeRequestSchema, UpdateMergeRequestSchema,
ListNamespacesSchema,
GetNamespaceSchema,
VerifyNamespaceSchema,
type GitLabFork, type GitLabFork,
type GitLabReference, type GitLabReference,
type GitLabRepository, type GitLabRepository,
@ -53,6 +58,8 @@ import {
type GitLabCommit, type GitLabCommit,
type FileOperation, type FileOperation,
type GitLabMergeRequestDiff, type GitLabMergeRequestDiff,
type GitLabNamespace,
type GitLabNamespaceExistsResponse,
CreateNoteSchema, CreateNoteSchema,
} from "./schemas.js"; } from "./schemas.js";
@ -811,6 +818,90 @@ async function createNote(
return await response.json(); return await response.json();
} }
/**
* List all namespaces
* 사용 가능한 모든 네임스페이스 목록 조회
*
* @param {Object} options - Options for listing namespaces
* @param {string} [options.search] - Search query to filter namespaces
* @param {boolean} [options.owned_only] - Only return namespaces owned by the authenticated user
* @param {boolean} [options.top_level_only] - Only return top-level namespaces
* @returns {Promise<GitLabNamespace[]>} List of namespaces
*/
async function listNamespaces(options: {
search?: string;
owned_only?: boolean;
top_level_only?: boolean;
}): Promise<GitLabNamespace[]> {
const url = new URL(`${GITLAB_API_URL}/namespaces`);
if (options.search) {
url.searchParams.append("search", options.search);
}
if (options.owned_only) {
url.searchParams.append("owned_only", "true");
}
if (options.top_level_only) {
url.searchParams.append("top_level_only", "true");
}
const response = await fetch(url.toString(), {
headers: DEFAULT_HEADERS,
});
await handleGitLabError(response);
const data = await response.json();
return z.array(GitLabNamespaceSchema).parse(data);
}
/**
* Get details on a namespace
* 네임스페이스 상세 정보 조회
*
* @param {string} id - The ID or URL-encoded path of the namespace
* @returns {Promise<GitLabNamespace>} The namespace details
*/
async function getNamespace(id: string): Promise<GitLabNamespace> {
const url = new URL(`${GITLAB_API_URL}/namespaces/${encodeURIComponent(id)}`);
const response = await fetch(url.toString(), {
headers: DEFAULT_HEADERS,
});
await handleGitLabError(response);
const data = await response.json();
return GitLabNamespaceSchema.parse(data);
}
/**
* Verify if a namespace exists
* 네임스페이스 존재 여부 확인
*
* @param {string} namespacePath - The path of the namespace to check
* @param {number} [parentId] - The ID of the parent namespace
* @returns {Promise<GitLabNamespaceExistsResponse>} The verification result
*/
async function verifyNamespaceExistence(
namespacePath: string,
parentId?: number
): Promise<GitLabNamespaceExistsResponse> {
const url = new URL(`${GITLAB_API_URL}/namespaces/${encodeURIComponent(namespacePath)}/exists`);
if (parentId) {
url.searchParams.append("parent_id", parentId.toString());
}
const response = await fetch(url.toString(), {
headers: DEFAULT_HEADERS,
});
await handleGitLabError(response);
const data = await response.json();
return GitLabNamespaceExistsResponseSchema.parse(data);
}
server.setRequestHandler(ListToolsRequestSchema, async () => { server.setRequestHandler(ListToolsRequestSchema, async () => {
return { return {
tools: [ tools: [
@ -882,6 +973,21 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
description: "Create a new note (comment) to an issue or merge request", description: "Create a new note (comment) to an issue or merge request",
inputSchema: zodToJsonSchema(CreateNoteSchema), inputSchema: zodToJsonSchema(CreateNoteSchema),
}, },
{
name: "list_namespaces",
description: "List all namespaces available to the current user",
inputSchema: zodToJsonSchema(ListNamespacesSchema),
},
{
name: "get_namespace",
description: "Get details on a specified namespace",
inputSchema: zodToJsonSchema(GetNamespaceSchema),
},
{
name: "verify_namespace",
description: "Verify if a specified namespace already exists",
inputSchema: zodToJsonSchema(VerifyNamespaceSchema),
},
], ],
}; };
}); });
@ -1053,6 +1159,30 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
}; };
} }
case "list_namespaces": {
const args = ListNamespacesSchema.parse(request.params.arguments);
const namespaces = await listNamespaces(args);
return {
content: [{ type: "text", text: JSON.stringify(namespaces, null, 2) }],
};
}
case "get_namespace": {
const args = GetNamespaceSchema.parse(request.params.arguments);
const namespace = await getNamespace(args.id);
return {
content: [{ type: "text", text: JSON.stringify(namespace, null, 2) }],
};
}
case "verify_namespace": {
const args = VerifyNamespaceSchema.parse(request.params.arguments);
const result = await verifyNamespaceExistence(args.namespace, args.parent_id);
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
case "create_note": { case "create_note": {
const args = CreateNoteSchema.parse(request.params.arguments); const args = CreateNoteSchema.parse(request.params.arguments);
const { project_id, noteable_type, noteable_iid, body } = args; const { project_id, noteable_type, noteable_iid, body } = args;

View File

@ -7,6 +7,33 @@ export const GitLabAuthorSchema = z.object({
date: z.string(), date: z.string(),
}); });
// Namespace related schemas
export const GitLabNamespaceSchema = z.object({
id: z.number(),
name: z.string(),
path: z.string(),
kind: z.enum(["user", "group"]),
full_path: z.string(),
parent_id: z.number().nullable(),
avatar_url: z.string().nullable(),
web_url: z.string(),
members_count_with_descendants: z.number().optional(),
billable_members_count: z.number().optional(),
max_seats_used: z.number().optional(),
seats_in_use: z.number().optional(),
plan: z.string().optional(),
end_date: z.string().nullable().optional(),
trial_ends_on: z.string().nullable().optional(),
trial: z.boolean().optional(),
root_repository_size: z.number().optional(),
projects_count: z.number().optional(),
});
export const GitLabNamespaceExistsResponseSchema = z.object({
exists: z.boolean(),
suggests: z.array(z.string()).optional(),
});
// Repository related schemas // Repository related schemas
export const GitLabOwnerSchema = z.object({ export const GitLabOwnerSchema = z.object({
username: z.string(), // Changed from login to match GitLab API username: z.string(), // Changed from login to match GitLab API
@ -407,6 +434,22 @@ export const CreateNoteSchema = z.object({
body: z.string().describe("Note content"), body: z.string().describe("Note content"),
}); });
// Add namespace-related operation schemas
export const ListNamespacesSchema = z.object({
search: z.string().optional().describe("Only returns namespaces accessible by the current user"),
owned_only: z.boolean().optional().describe("If true, only returns namespaces by the current user"),
top_level_only: z.boolean().optional().describe("In GitLab 16.8 and later, if true, only returns top-level namespaces"),
});
export const GetNamespaceSchema = z.object({
id: z.string().describe("ID or URL-encoded path of the namespace"),
});
export const VerifyNamespaceSchema = z.object({
namespace: z.string().describe("Path of the namespace"),
parent_id: z.number().optional().describe("ID of the parent namespace. If unspecified, only returns top-level namespaces"),
});
// Export types // Export types
export type GitLabAuthor = z.infer<typeof GitLabAuthorSchema>; export type GitLabAuthor = z.infer<typeof GitLabAuthorSchema>;
export type GitLabFork = z.infer<typeof GitLabForkSchema>; export type GitLabFork = z.infer<typeof GitLabForkSchema>;
@ -438,3 +481,5 @@ export type GitLabMergeRequestDiff = z.infer<
typeof GitLabMergeRequestDiffSchema typeof GitLabMergeRequestDiffSchema
>; >;
export type CreateNoteOptions = z.infer<typeof CreateNoteSchema>; export type CreateNoteOptions = z.infer<typeof CreateNoteSchema>;
export type GitLabNamespace = z.infer<typeof GitLabNamespaceSchema>;
export type GitLabNamespaceExistsResponse = z.infer<typeof GitLabNamespaceExistsResponseSchema>;