GiteaHelpMCP/Base/GiteaHelp.cs
2025-05-06 18:16:49 +08:00

85 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Flurl.Http;
using Newtonsoft.Json.Linq;
namespace GiteaHelpMCP.Base
{
public class GiteaHelp
{
[McpTool("GetReposFullName", "得到当前用户所有的仓库返回仓库的full_name列表")]
public static string GetReposFullName()
{
var url = MsBase.url + "/api/v1/user/repos?page=1&limit=100";
var resultStr = url.WithHeader("accept", "application/json")
.WithHeader("Authorization", $"token {MsBase.token}")
.GetStringAsync().GetAwaiter().GetResult();
var result = JArray.Parse(resultStr);
var resultPath = "$[*].full_name";
IEnumerable<JToken> resultTokens = result.SelectTokens(resultPath);
var newresultObjects = resultTokens.Select(result =>
{
return result.ToString();
}).ToList();
return JArray.FromObject(newresultObjects).ToString(Newtonsoft.Json.Formatting.None);
}
[McpTool("GetCommits", "得到指定仓库的commit返回详细的信息json")]
public static string GetCommits(
[McpParameter(true, "仓库")] string repoFullName,
[McpParameter(true, "page 默认是1")] int page = 1,
[McpParameter(true, "limit 默认是20")] int limit = 20)
{
var url = MsBase.url + $"/api/v1/repos/{repoFullName}/commits?page={page}&limit={limit}";
var resultStr = url.WithHeader("accept", "application/json")
.WithHeader("Authorization", $"token {MsBase.token}")
.GetStringAsync().GetAwaiter().GetResult();
//var result = JArray.Parse(resultStr);
return resultStr;
}
[McpTool("GetCommitsSimple", "得到仓库的commit,返回简单信息json[仓库,创建时间,提交信息]")]
public static string GetCommitsSimple(
[McpParameter(true, "仓库")] string repoFullName,
[McpParameter(true, "page 默认是1")] int page = 1,
[McpParameter(true, "limit 默认是20")] int limit = 20)
{
var resultStr = GetCommits(repoFullName, page, limit);
var result = JArray.Parse(resultStr);
var json = new JArray();
foreach (JObject item in result)
{
// 获取 created 字段
string created = item["created"].ToString();
// 获取 commit 对象的 message 字段
string message = item["commit"]["message"].ToString();
json.Add(new JObject {
{"repoFullName",repoFullName },
{ "created", created },
{ "message", message }});
}
return json.ToString(Newtonsoft.Json.Formatting.None);
}
[McpTool("GetAllReposCommitsSimple", "得到所有仓库的commit,返回简单信息json[[仓库,创建时间,提交信息]]limit调整返回的数量20100")]
public static string GetAllReposCommitsSimple(
[McpParameter(true, "page 默认是1")] int page = 1,
[McpParameter(true, "limit 默认是20")] int limit = 20)
{
if (limit > 100) { limit = 100; }
var AllReposstr = GetReposFullName();
var AllRepos = JArray.Parse(AllReposstr);
var json = new JArray();
foreach (var item in AllRepos)
{
string Commits = GetCommitsSimple(item.ToString(), page, limit);
//判断Commits是否为空
var Commitsjs = JArray.Parse(Commits);
if (Commitsjs.Any())
{
json.Add(Commitsjs);
}
}
return json.ToString(Newtonsoft.Json.Formatting.None);
}
}
}