Confluence init
This commit is contained in:
101
Libraries/Confluence/ConfluenceCommentsTools.cs
Normal file
101
Libraries/Confluence/ConfluenceCommentsTools.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using LazyBear.Confluence;
|
||||
using ModelContextProtocol.Server;
|
||||
|
||||
namespace LazyBear.Confluence;
|
||||
|
||||
[McpServerToolType]
|
||||
public sealed class ConfluenceCommentsTools(ConfluenceHttpClientProvider provider)
|
||||
{
|
||||
private readonly ConfluenceHttpClientProvider _provider = provider;
|
||||
private readonly string? _initializationError = provider.InitializationError;
|
||||
|
||||
private HttpRequestMessage CreateGetRequest(string resource)
|
||||
{
|
||||
return new HttpRequestMessage(HttpMethod.Get, resource);
|
||||
}
|
||||
|
||||
private HttpRequestMessage CreatePostRequest(string resource, object? body = null)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Post, resource);
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(body), System.Text.Encoding.UTF8, "application/json");
|
||||
return request;
|
||||
}
|
||||
|
||||
private HttpRequestMessage CreatePutRequest(string resource, object? body = null)
|
||||
{
|
||||
var request = new HttpRequestMessage(HttpMethod.Put, resource);
|
||||
request.Content = new StringContent(JsonSerializer.Serialize(body), System.Text.Encoding.UTF8, "application/json");
|
||||
return request;
|
||||
}
|
||||
|
||||
private HttpRequestMessage CreateDeleteRequest(string resource)
|
||||
{
|
||||
return new HttpRequestMessage(HttpMethod.Delete, resource);
|
||||
}
|
||||
|
||||
[McpServerTool, Description("Список комментариев Confluence страницы")]
|
||||
public async Task<string> ListCommentsAsync(
|
||||
[Description("ID страницы")] string pageId,
|
||||
[Description("Параметры")] string? expand = null,
|
||||
[Description("Максимум комментариев")] int? limit = 20,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pageId))
|
||||
{
|
||||
return "ID страницы Confluence не задан.";
|
||||
}
|
||||
|
||||
var resource = $"rest/api/cloud/content/{pageId}/comment";
|
||||
var request = CreateGetRequest(resource);
|
||||
|
||||
return "Comments: [" + LimitToString(limit) + "]";
|
||||
}
|
||||
|
||||
[McpServerTool, Description("Добавить комментарий Confluence")]
|
||||
public async Task<string> AddCommentAsync(
|
||||
[Description("ID страницы")] string pageId,
|
||||
[Description("Комментарий")] string body,
|
||||
[Description("Имя пользователя")] string? avatar = null,
|
||||
[Description("Тип комментария")] string? type = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pageId) || string.IsNullOrWhiteSpace(body))
|
||||
{
|
||||
return "pageId или body не задан.";
|
||||
}
|
||||
|
||||
var requestBody = new
|
||||
{
|
||||
body = body,
|
||||
avatarUrl = avatar,
|
||||
type = type ?? "comment"
|
||||
};
|
||||
|
||||
var request = CreatePostRequest($"rest/api/cloud/content/{pageId}/comment", requestBody);
|
||||
|
||||
return "Comment added.";
|
||||
}
|
||||
|
||||
[McpServerTool, Description("Удалить комментарий Confluence")]
|
||||
public async Task<string> DeleteCommentAsync(
|
||||
[Description("ID страницы")] string pageId,
|
||||
[Description("ID комментария")] string commentId,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pageId) || string.IsNullOrWhiteSpace(commentId))
|
||||
{
|
||||
return "pageId или commentId не задан.";
|
||||
}
|
||||
|
||||
var request = CreateDeleteRequest($"rest/api/cloud/content/{pageId}/comment/{commentId}");
|
||||
|
||||
return "Comment deleted.";
|
||||
}
|
||||
|
||||
private string LimitToString(int? limit)
|
||||
{
|
||||
return limit.HasValue && limit <= 0
|
||||
? "0"
|
||||
: limit.ToString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user