| | | @@ -0,0 +1,157 @@ |
| | | | package repo |
| | | | |
| | | | import ( |
| | | | "context" |
| | | | "fmt" |
| | | | |
| | | | gitea_sdk "code.gitea.io/sdk/gitea" |
| | | | "gitea.com/gitea/gitea-mcp/pkg/gitea" |
| | | | "gitea.com/gitea/gitea-mcp/pkg/log" |
| | | | "gitea.com/gitea/gitea-mcp/pkg/to" |
| | | | "github.com/mark3labs/mcp-go/mcp" |
| | | | ) |
| | | | |
| | | | const ( |
| | | | CreateTagToolName = "create_tag" |
| | | | DeleteTagToolName = "delete_tag" |
| | | | GetTagToolName = "get_tag" |
| | | | ListTagsToolName = "list_tags" |
| | | | ) |
| | | | |
| | | | var ( |
| | | | CreateTagTool = mcp.NewTool( |
| | | | CreateTagToolName, |
| | | | mcp.WithDescription("Create tag"), |
| | | | mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")), |
| | | | mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")), |
| | | | mcp.WithString("tag_name", mcp.Required(), mcp.Description("tag name")), |
| | | | mcp.WithString("target", mcp.Description("target commitish"), mcp.DefaultString("")), |
| | | | mcp.WithString("message", mcp.Description("tag message"), mcp.DefaultString("")), |
| | | | ) |
| | | | |
| | | | DeleteTagTool = mcp.NewTool( |
| | | | DeleteTagToolName, |
| | | | mcp.WithDescription("Delete tag"), |
| | | | mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")), |
| | | | mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")), |
| | | | mcp.WithString("tag_name", mcp.Required(), mcp.Description("tag name")), |
| | | | ) |
| | | | |
| | | | GetTagTool = mcp.NewTool( |
| | | | GetTagToolName, |
| | | | mcp.WithDescription("Get tag"), |
| | | | mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")), |
| | | | mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")), |
| | | | mcp.WithString("tag_name", mcp.Required(), mcp.Description("tag name")), |
| | | | ) |
| | | | |
| | | | ListTagsTool = mcp.NewTool( |
| | | | ListTagsToolName, |
| | | | mcp.WithDescription("List tags"), |
| | | | mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")), |
| | | | mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")), |
| | | | mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1), mcp.Min(1)), |
| | | | mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100), mcp.Min(1)), |
| | | | ) |
| | | | ) |
| | | | |
| | | | func CreateTagFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| | | | log.Debugf("Called CreateTagFn") |
| | | | owner, ok := req.Params.Arguments["owner"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("owner is required") |
| | | | } |
| | | | repo, ok := req.Params.Arguments["repo"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("repo is required") |
| | | | } |
| | | | tagName, ok := req.Params.Arguments["tag_name"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("tag_name is required") |
| | | | } |
| | | | target, _ := req.Params.Arguments["target"].(string) |
| | | | message, _ := req.Params.Arguments["message"].(string) |
| | | | |
| | | | _, _, err := gitea.Client().CreateTag(owner, repo, gitea_sdk.CreateTagOption{ |
| | | | TagName: tagName, |
| | | | Target: target, |
| | | | Message: message, |
| | | | }) |
| | | | if err != nil { |
| | | | return nil, fmt.Errorf("create tag error: %v", err) |
| | | | } |
| | | | |
| | | | return mcp.NewToolResultText("Tag Created"), nil |
| | | | } |
| | | | |
| | | | func DeleteTagFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| | | | log.Debugf("Called DeleteTagFn") |
| | | | owner, ok := req.Params.Arguments["owner"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("owner is required") |
| | | | } |
| | | | repo, ok := req.Params.Arguments["repo"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("repo is required") |
| | | | } |
| | | | tagName, ok := req.Params.Arguments["tag_name"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("tag_name is required") |
| | | | } |
| | | | |
| | | | _, err := gitea.Client().DeleteTag(owner, repo, tagName) |
| | | | if err != nil { |
| | | | return nil, fmt.Errorf("delete tag error: %v", err) |
| | | | } |
| | | | |
| | | | return to.TextResult("Tag deleted") |
| | | | } |
| | | | |
| | | | func GetTagFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| | | | log.Debugf("Called GetTagFn") |
| | | | owner, ok := req.Params.Arguments["owner"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("owner is required") |
| | | | } |
| | | | repo, ok := req.Params.Arguments["repo"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("repo is required") |
| | | | } |
| | | | tagName, ok := req.Params.Arguments["tag_name"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("tag_name is required") |
| | | | } |
| | | | |
| | | | tag, _, err := gitea.Client().GetTag(owner, repo, tagName) |
| | | | if err != nil { |
| | | | return nil, fmt.Errorf("get tag error: %v", err) |
| | | | } |
| | | | |
| | | | return to.TextResult(tag) |
| | | | } |
| | | | |
| | | | func ListTagsFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) { |
| | | | log.Debugf("Called ListTagsFn") |
| | | | owner, ok := req.Params.Arguments["owner"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("owner is required") |
| | | | } |
| | | | repo, ok := req.Params.Arguments["repo"].(string) |
| | | | if !ok { |
| | | | return nil, fmt.Errorf("repo is required") |
| | | | } |
| | | | page, _ := req.Params.Arguments["page"].(int) |
| | | | pageSize, _ := req.Params.Arguments["pageSize"].(int) |
| | | | |
| | | | tags, _, err := gitea.Client().ListRepoTags(owner, repo, gitea_sdk.ListRepoTagsOptions{ |
| | | | ListOptions: gitea_sdk.ListOptions{ |
| | | | Page: page, |
| | | | PageSize: pageSize, |
| | | | }, |
| | | | }) |
| | | | if err != nil { |
| | | | return nil, fmt.Errorf("list tags error: %v", err) |
| | | | } |
| | | | |
| | | | return to.TextResult(tags) |
| | | | } |