85 lines
3.9 KiB
C#
85 lines
3.9 KiB
C#
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调整返回的数量,默认是20,最大是100;注意这里的分页是单个仓库的提交记录分页,不是所有仓库的提交记录分页")]
|
||
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);
|
||
}
|
||
}
|
||
}
|