@therealchristhomas/gitlab-mcp-server
Version:
MCP Server for GitLab API operations
75 lines (74 loc) • 2.72 kB
JavaScript
import { z } from "zod";
import { gitlabGet, gitlabPost, gitlabPut, gitlabDelete, encodeProjectId, buildSearchParams } from "../utils/gitlab-client.js";
import { GitLabLabelSchema } from "../types/index.js";
export async function listLabels(projectId, page = 1, perPage = 20) {
if (!projectId?.trim()) {
throw new Error("Project ID is required");
}
if (page < 1) {
throw new Error("Page number must be 1 or greater");
}
if (perPage < 1 || perPage > 100) {
throw new Error("Per page must be between 1 and 100");
}
const endpoint = `/projects/${encodeProjectId(projectId)}/labels`;
const params = buildSearchParams({
page: page.toString(),
per_page: perPage.toString()
});
const labels = await gitlabGet(endpoint, params);
return z.array(GitLabLabelSchema).parse(labels);
}
export async function createLabel(projectId, name, color, description, priority) {
if (!projectId?.trim()) {
throw new Error("Project ID is required");
}
if (!name?.trim()) {
throw new Error("Label name is required");
}
if (!color?.trim()) {
throw new Error("Label color is required");
}
if (!color.match(/^#[0-9a-fA-F]{6}$/)) {
throw new Error("Label color must be a valid hex color (e.g., #ff0000)");
}
const endpoint = `/projects/${encodeProjectId(projectId)}/labels`;
const label = await gitlabPost(endpoint, {
name,
color,
description,
priority
});
return GitLabLabelSchema.parse(label);
}
export async function updateLabel(projectId, name, options) {
if (!projectId?.trim()) {
throw new Error("Project ID is required");
}
if (!name?.trim()) {
throw new Error("Label name is required");
}
if (options.color && !options.color.match(/^#[0-9a-fA-F]{6}$/)) {
throw new Error("Label color must be a valid hex color (e.g., #ff0000)");
}
const encodedName = encodeURIComponent(name);
const endpoint = `/projects/${encodeProjectId(projectId)}/labels/${encodedName}`;
const label = await gitlabPut(endpoint, {
new_name: options.new_name,
color: options.color,
description: options.description,
priority: options.priority
});
return GitLabLabelSchema.parse(label);
}
export async function deleteLabel(projectId, name) {
if (!projectId?.trim()) {
throw new Error("Project ID is required");
}
if (!name?.trim()) {
throw new Error("Label name is required");
}
const encodedName = encodeURIComponent(name);
const endpoint = `/projects/${encodeProjectId(projectId)}/labels/${encodedName}`;
await gitlabDelete(endpoint);
}