@therealchristhomas/gitlab-mcp-server
Version:
MCP Server for GitLab API operations
67 lines (66 loc) • 2.49 kB
JavaScript
import { z } from "zod";
import { gitlabGet, gitlabPost, gitlabPut, encodeProjectId, buildSearchParams } from "../utils/gitlab-client.js";
import { GitLabIssueSchema, GitLabCommentSchema } from "../types/index.js";
export async function listIssues(projectId, options = {}) {
if (!projectId?.trim()) {
throw new Error("Project ID is required");
}
const endpoint = `/projects/${encodeProjectId(projectId)}/issues`;
const params = buildSearchParams(options);
const rawIssues = await gitlabGet(endpoint, params);
return z.array(GitLabIssueSchema).parse(rawIssues);
}
export async function createIssue(projectId, options) {
if (!projectId?.trim()) {
throw new Error("Project ID is required");
}
if (!options?.title?.trim()) {
throw new Error("Issue title is required");
}
const endpoint = `/projects/${encodeProjectId(projectId)}/issues`;
const issue = await gitlabPost(endpoint, {
title: options.title,
description: options.description,
assignee_ids: options.assignee_ids,
milestone_id: options.milestone_id,
labels: options.labels?.join(",")
});
return GitLabIssueSchema.parse(issue);
}
export async function updateIssue(projectId, issueIid, options) {
if (!projectId?.trim()) {
throw new Error("Project ID is required");
}
if (!issueIid || issueIid < 1) {
throw new Error("Valid issue IID is required");
}
const endpoint = `/projects/${encodeProjectId(projectId)}/issues/${issueIid}`;
const issue = await gitlabPut(endpoint, {
...options,
labels: options.labels?.join(",")
});
return GitLabIssueSchema.parse(issue);
}
export async function searchIssues(projectId, searchTerm, options = {}) {
if (!searchTerm?.trim()) {
throw new Error("Search term is required");
}
return listIssues(projectId, {
search: searchTerm,
...options
});
}
export async function addIssueComment(projectId, issueIid, body) {
if (!projectId?.trim()) {
throw new Error("Project ID is required");
}
if (!issueIid || issueIid < 1) {
throw new Error("Valid issue IID is required");
}
if (!body?.trim()) {
throw new Error("Comment body is required");
}
const endpoint = `/projects/${encodeProjectId(projectId)}/issues/${issueIid}/notes`;
const comment = await gitlabPost(endpoint, { body });
return GitLabCommentSchema.parse(comment);
}