Some checks failed
release-nightly / release-image (push) Failing after 2m17s
Fork the official gitea-mcp and massively extend API coverage: - Organization: orgs, members, teams, hooks, blocks, activity (34 tools) - User: profile, followers, keys, emails, repos, blocks (30 tools) - Repository: collaborators, webhooks, branch/tag protection, deploy keys, topics, git refs/trees/notes, commit status, stars/watchers, forks, transfers, mirrors, templates (53 tools) - Issue: reactions, pins, subscriptions, timeline, templates (16 tools) - Notifications: list, check, read, repo-scoped (7 tools) - Settings: API, attachment, repo, UI settings (4 tools) - Packages: list, get, delete, files, latest, link/unlink (7 tools) - Miscellaneous: server version, gitignore/label/license templates, markdown/markup rendering, node info, signing keys (12 tools) - Admin: user/org/repo CRUD, system webhooks, cron tasks, unadopted repos, emails, badges (23 tools) Module path updated to git.lethalbits.com/lethalbits/gitea-mcp.
373 lines
12 KiB
Go
373 lines
12 KiB
Go
package issue
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/gitea"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/log"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/params"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/to"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp/pkg/tool"
|
|
|
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
var Tool = tool.New()
|
|
|
|
const (
|
|
GetIssueByIndexToolName = "get_issue_by_index"
|
|
ListRepoIssuesToolName = "list_repo_issues"
|
|
CreateIssueToolName = "create_issue"
|
|
CreateIssueCommentToolName = "create_issue_comment"
|
|
EditIssueToolName = "edit_issue"
|
|
EditIssueCommentToolName = "edit_issue_comment"
|
|
GetIssueCommentsByIndexToolName = "get_issue_comments_by_index"
|
|
)
|
|
|
|
var (
|
|
GetIssueByIndexTool = mcp.NewTool(
|
|
GetIssueByIndexToolName,
|
|
mcp.WithDescription("get issue by index"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository issue index")),
|
|
)
|
|
|
|
ListRepoIssuesTool = mcp.NewTool(
|
|
ListRepoIssuesToolName,
|
|
mcp.WithDescription("List repository issues"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithString("state", mcp.Description("issue state"), mcp.DefaultString("all")),
|
|
mcp.WithNumber("page", mcp.Description("page number"), mcp.DefaultNumber(1)),
|
|
mcp.WithNumber("pageSize", mcp.Description("page size"), mcp.DefaultNumber(100)),
|
|
)
|
|
|
|
CreateIssueTool = mcp.NewTool(
|
|
CreateIssueToolName,
|
|
mcp.WithDescription("create issue"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithString("title", mcp.Required(), mcp.Description("issue title")),
|
|
mcp.WithString("body", mcp.Required(), mcp.Description("issue body")),
|
|
)
|
|
|
|
CreateIssueCommentTool = mcp.NewTool(
|
|
CreateIssueCommentToolName,
|
|
mcp.WithDescription("create issue comment"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository issue index")),
|
|
mcp.WithString("body", mcp.Required(), mcp.Description("issue comment body")),
|
|
)
|
|
|
|
EditIssueTool = mcp.NewTool(
|
|
EditIssueToolName,
|
|
mcp.WithDescription("edit issue"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository issue index")),
|
|
mcp.WithString("title", mcp.Description("issue title"), mcp.DefaultString("")),
|
|
mcp.WithString("body", mcp.Description("issue body content")),
|
|
mcp.WithArray("assignees", mcp.Description("usernames to assign to this issue"), mcp.Items(map[string]any{"type": "string"})),
|
|
mcp.WithNumber("milestone", mcp.Description("milestone number")),
|
|
mcp.WithString("state", mcp.Description("issue state, one of open, closed, all")),
|
|
)
|
|
|
|
EditIssueCommentTool = mcp.NewTool(
|
|
EditIssueCommentToolName,
|
|
mcp.WithDescription("edit issue comment"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithNumber("commentID", mcp.Required(), mcp.Description("id of issue comment")),
|
|
mcp.WithString("body", mcp.Required(), mcp.Description("issue comment body")),
|
|
)
|
|
|
|
GetIssueCommentsByIndexTool = mcp.NewTool(
|
|
GetIssueCommentsByIndexToolName,
|
|
mcp.WithDescription("get issue comment by index"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithNumber("index", mcp.Required(), mcp.Description("repository issue index")),
|
|
)
|
|
)
|
|
|
|
func init() {
|
|
Tool.RegisterRead(server.ServerTool{
|
|
Tool: GetIssueByIndexTool,
|
|
Handler: GetIssueByIndexFn,
|
|
})
|
|
Tool.RegisterRead(server.ServerTool{
|
|
Tool: ListRepoIssuesTool,
|
|
Handler: ListRepoIssuesFn,
|
|
})
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: CreateIssueTool,
|
|
Handler: CreateIssueFn,
|
|
})
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: CreateIssueCommentTool,
|
|
Handler: CreateIssueCommentFn,
|
|
})
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: EditIssueTool,
|
|
Handler: EditIssueFn,
|
|
})
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: EditIssueCommentTool,
|
|
Handler: EditIssueCommentFn,
|
|
})
|
|
Tool.RegisterRead(server.ServerTool{
|
|
Tool: GetIssueCommentsByIndexTool,
|
|
Handler: GetIssueCommentsByIndexFn,
|
|
})
|
|
}
|
|
|
|
func GetIssueByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called GetIssueByIndexFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
issue, _, err := client.GetIssue(owner, repo, index)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get %v/%v/issue/%v err: %v", owner, repo, index, err))
|
|
}
|
|
|
|
return to.TextResult(issue)
|
|
}
|
|
|
|
func ListRepoIssuesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called ListIssuesFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
state, ok := req.GetArguments()["state"].(string)
|
|
if !ok {
|
|
state = "all"
|
|
}
|
|
page := params.GetOptionalInt(req.GetArguments(), "page", 1)
|
|
pageSize := params.GetOptionalInt(req.GetArguments(), "pageSize", 100)
|
|
opt := gitea_sdk.ListIssueOption{
|
|
State: gitea_sdk.StateType(state),
|
|
ListOptions: gitea_sdk.ListOptions{
|
|
Page: int(page),
|
|
PageSize: int(pageSize),
|
|
},
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
issues, _, err := client.ListRepoIssues(owner, repo, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get %v/%v/issues err: %v", owner, repo, err))
|
|
}
|
|
return to.TextResult(issues)
|
|
}
|
|
|
|
func CreateIssueFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called CreateIssueFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
title, ok := req.GetArguments()["title"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("title is required"))
|
|
}
|
|
body, ok := req.GetArguments()["body"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("body is required"))
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
issue, _, err := client.CreateIssue(owner, repo, gitea_sdk.CreateIssueOption{
|
|
Title: title,
|
|
Body: body,
|
|
})
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("create %v/%v/issue err: %v", owner, repo, err))
|
|
}
|
|
|
|
return to.TextResult(issue)
|
|
}
|
|
|
|
func CreateIssueCommentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called CreateIssueCommentFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
body, ok := req.GetArguments()["body"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("body is required"))
|
|
}
|
|
opt := gitea_sdk.CreateIssueCommentOption{
|
|
Body: body,
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
issueComment, _, err := client.CreateIssueComment(owner, repo, index, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("create %v/%v/issue/%v/comment err: %v", owner, repo, index, err))
|
|
}
|
|
|
|
return to.TextResult(issueComment)
|
|
}
|
|
|
|
func EditIssueFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called EditIssueFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
|
|
opt := gitea_sdk.EditIssueOption{}
|
|
|
|
title, ok := req.GetArguments()["title"].(string)
|
|
if ok {
|
|
opt.Title = title
|
|
}
|
|
body, ok := req.GetArguments()["body"].(string)
|
|
if ok {
|
|
opt.Body = new(body)
|
|
}
|
|
var assignees []string
|
|
if assigneesArg, exists := req.GetArguments()["assignees"]; exists {
|
|
if assigneesSlice, ok := assigneesArg.([]any); ok {
|
|
for _, assignee := range assigneesSlice {
|
|
if assigneeStr, ok := assignee.(string); ok {
|
|
assignees = append(assignees, assigneeStr)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
opt.Assignees = assignees
|
|
if val, exists := req.GetArguments()["milestone"]; exists {
|
|
if milestone, ok := params.ToInt64(val); ok {
|
|
opt.Milestone = new(milestone)
|
|
}
|
|
}
|
|
state, ok := req.GetArguments()["state"].(string)
|
|
if ok {
|
|
opt.State = new(gitea_sdk.StateType(state))
|
|
}
|
|
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
issue, _, err := client.EditIssue(owner, repo, index, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("edit %v/%v/issue/%v err: %v", owner, repo, index, err))
|
|
}
|
|
|
|
return to.TextResult(issue)
|
|
}
|
|
|
|
func EditIssueCommentFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called EditIssueCommentFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
commentID, err := params.GetIndex(req.GetArguments(), "commentID")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
body, ok := req.GetArguments()["body"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("body is required"))
|
|
}
|
|
opt := gitea_sdk.EditIssueCommentOption{
|
|
Body: body,
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
issueComment, _, err := client.EditIssueComment(owner, repo, commentID, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("edit %v/%v/issues/comments/%v err: %v", owner, repo, commentID, err))
|
|
}
|
|
|
|
return to.TextResult(issueComment)
|
|
}
|
|
|
|
func GetIssueCommentsByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called GetIssueCommentsByIndexFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
index, err := params.GetIndex(req.GetArguments(), "index")
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
opt := gitea_sdk.ListIssueCommentOptions{}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
issue, _, err := client.ListIssueComments(owner, repo, index, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get %v/%v/issues/%v/comments err: %v", owner, repo, index, err))
|
|
}
|
|
|
|
return to.TextResult(issue)
|
|
}
|