Merge pull request #38 from ncrum/feat/merge-request-note

Adds threaded comment support for merge requests
This commit is contained in:
zereight
2025-05-15 10:51:48 +09:00
committed by GitHub
3 changed files with 203 additions and 27 deletions

139
index.ts
View File

@ -75,6 +75,7 @@ import {
UpdateLabelSchema,
DeleteLabelSchema,
CreateNoteSchema,
CreateMergeRequestThreadSchema,
ListGroupProjectsSchema,
ListWikiPagesSchema,
GetWikiPageSchema,
@ -86,6 +87,7 @@ import {
GitLabDiscussionNoteSchema, // Added
GitLabDiscussionSchema,
UpdateMergeRequestNoteSchema, // Added
CreateMergeRequestNoteSchema, // Added
ListMergeRequestDiscussionsSchema,
type GitLabFork,
type GitLabReference,
@ -108,6 +110,7 @@ import {
// Discussion Types
type GitLabDiscussionNote, // Added
type GitLabDiscussion,
type MergeRequestThreadPosition,
type GetWikiPageOptions,
type CreateWikiPageOptions,
type UpdateWikiPageOptions,
@ -263,6 +266,11 @@ const allTools = [
description: "Create a new note (comment) to an issue or merge request",
inputSchema: zodToJsonSchema(CreateNoteSchema),
},
{
name: "create_merge_request_thread",
description: "Create a new thread on a merge request",
inputSchema: zodToJsonSchema(CreateMergeRequestThreadSchema),
},
{
name: "mr_discussions",
description: "List discussion items for a merge request",
@ -273,6 +281,11 @@ const allTools = [
description: "Modify an existing merge request thread note",
inputSchema: zodToJsonSchema(UpdateMergeRequestNoteSchema),
},
{
name: "create_merge_request_note",
description: "Add a new note to an existing merge request thread",
inputSchema: zodToJsonSchema(CreateMergeRequestNoteSchema),
},
{
name: "list_issues",
description: "List issues in a GitLab project with filtering options",
@ -1056,6 +1069,47 @@ async function updateMergeRequestNote(
return GitLabDiscussionNoteSchema.parse(data);
}
/**
* Add a new note to an existing merge request thread
* 기존 병합 요청 스레드에 새 노트 추가
*
* @param {string} projectId - The ID or URL-encoded path of the project
* @param {number} mergeRequestIid - The IID of a merge request
* @param {string} discussionId - The ID of a thread
* @param {string} body - The content of the new note
* @param {string} [createdAt] - The creation date of the note (ISO 8601 format)
* @returns {Promise<GitLabDiscussionNote>} The created note
*/
async function createMergeRequestNote(
projectId: string,
mergeRequestIid: number,
discussionId: string,
body: string,
createdAt?: string
): Promise<GitLabDiscussionNote> {
projectId = decodeURIComponent(projectId); // Decode project ID
const url = new URL(
`${GITLAB_API_URL}/projects/${encodeURIComponent(
projectId
)}/merge_requests/${mergeRequestIid}/discussions/${discussionId}/notes`
);
const payload: { body: string; created_at?: string } = { body };
if (createdAt) {
payload.created_at = createdAt;
}
const response = await fetch(url.toString(), {
...DEFAULT_FETCH_CONFIG,
method: "POST",
body: JSON.stringify(payload),
});
await handleGitLabError(response);
const data = await response.json();
return GitLabDiscussionNoteSchema.parse(data);
}
/**
* Create or update a file in a GitLab project
* 파일 생성 또는 업데이트
@ -1523,6 +1577,59 @@ async function createNote(
return await response.json();
}
/**
* Create a new thread on a merge request
* 📦 새로운 함수: createMergeRequestThread - 병합 요청에 새로운 스레드(토론)를 생성하는 함수
* (New function: createMergeRequestThread - Function to create a new thread (discussion) on a merge request)
*
* This function provides more capabilities than createNote, including the ability to:
* - Create diff notes (comments on specific lines of code)
* - Specify exact positions for comments
* - Set creation timestamps
*
* @param {string} projectId - The ID or URL-encoded path of the project
* @param {number} mergeRequestIid - The internal ID of the merge request
* @param {string} body - The content of the thread
* @param {MergeRequestThreadPosition} [position] - Position information for diff notes
* @param {string} [createdAt] - ISO 8601 formatted creation date
* @returns {Promise<GitLabDiscussion>} The created discussion thread
*/
async function createMergeRequestThread(
projectId: string,
mergeRequestIid: number,
body: string,
position?: MergeRequestThreadPosition,
createdAt?: string
): Promise<GitLabDiscussion> {
projectId = decodeURIComponent(projectId); // Decode project ID
const url = new URL(
`${GITLAB_API_URL}/projects/${encodeURIComponent(
projectId
)}/merge_requests/${mergeRequestIid}/discussions`
);
const payload: Record<string, any> = { body };
// Add optional parameters if provided
if (position) {
payload.position = position;
}
if (createdAt) {
payload.created_at = createdAt;
}
const response = await fetch(url.toString(), {
...DEFAULT_FETCH_CONFIG,
method: "POST",
body: JSON.stringify(payload),
});
await handleGitLabError(response);
const data = await response.json();
return GitLabDiscussionSchema.parse(data);
}
/**
* List all namespaces
* 사용 가능한 모든 네임스페이스 목록 조회
@ -2280,6 +2387,22 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
content: [{ type: "text", text: JSON.stringify(note, null, 2) }],
};
}
case "create_merge_request_note": {
const args = CreateMergeRequestNoteSchema.parse(
request.params.arguments
);
const note = await createMergeRequestNote(
args.project_id,
args.merge_request_iid,
args.discussion_id,
args.body,
args.created_at
);
return {
content: [{ type: "text", text: JSON.stringify(note, null, 2) }],
};
}
case "get_merge_request": {
const args = GetMergeRequestSchema.parse(request.params.arguments);
@ -2457,6 +2580,22 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
};
}
case "create_merge_request_thread": {
const args = CreateMergeRequestThreadSchema.parse(request.params.arguments);
const { project_id, merge_request_iid, body, position, created_at } = args;
const thread = await createMergeRequestThread(
project_id,
merge_request_iid,
body,
position,
created_at
);
return {
content: [{ type: "text", text: JSON.stringify(thread, null, 2) }],
};
}
case "list_issues": {
const args = ListIssuesSchema.parse(request.params.arguments);
const { project_id, ...options } = args;