check-and-test / check-and-test (pull_request) Successful in 2m21s
Expose Gitea /issues/{i}/dependencies endpoints: list_dependencies on
issue_read and add_dependency/remove_dependency on issue_write via the
blocking_issue param.
Assisted-by: Devin:SWE-2 Max
135 lines
4.3 KiB
Go
135 lines
4.3 KiB
Go
package issue
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
|
"gitea.com/gitea/gitea-mcp/pkg/params"
|
|
"gitea.com/gitea/gitea-mcp/pkg/to"
|
|
|
|
gitea_sdk "gitea.dev/sdk"
|
|
"github.com/modelcontextprotocol/go-sdk/mcp"
|
|
)
|
|
|
|
func listIssueBlocksFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
|
owner, repo, index, err := issueRef(args)
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
page, pageSize := params.GetPagination(args, 30)
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
listOpt := gitea_sdk.ListOptions{Page: page, PageSize: pageSize}
|
|
issues, _, err := client.Issues.ListIssueBlocks(ctx, owner, repo, index, gitea_sdk.ListIssueBlocksOptions{
|
|
ListOptions: listOpt,
|
|
})
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("list %v/%v/issue/%v/blocks err: %v", owner, repo, index, err))
|
|
}
|
|
return to.TextResult(slimIssues(issues))
|
|
}
|
|
|
|
func blockIssueFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
|
return setIssueBlockingFn(ctx, args, true)
|
|
}
|
|
|
|
func unblockIssueFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
|
return setIssueBlockingFn(ctx, args, false)
|
|
}
|
|
|
|
func setIssueBlockingFn(ctx context.Context, args map[string]any, block bool) (*mcp.CallToolResult, error) {
|
|
owner, repo, index, err := issueRef(args)
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
blocked, err := params.GetIndex(args, "blocked_issue")
|
|
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))
|
|
}
|
|
meta := gitea_sdk.IssueMeta{Index: blocked}
|
|
var issue *gitea_sdk.Issue
|
|
if block {
|
|
issue, _, err = client.Issues.CreateIssueBlocking(ctx, owner, repo, index, meta)
|
|
} else {
|
|
issue, _, err = client.Issues.RemoveIssueBlocking(ctx, owner, repo, index, meta)
|
|
}
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("update %v/%v/issue/%v/blocks err: %v", owner, repo, index, err))
|
|
}
|
|
return to.TextResult(slimIssue(issue))
|
|
}
|
|
|
|
func listIssueDependenciesFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
|
owner, repo, index, err := issueRef(args)
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
page, pageSize := params.GetPagination(args, 30)
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
listOpt := gitea_sdk.ListOptions{Page: page, PageSize: pageSize}
|
|
issues, _, err := client.Issues.ListIssueDependencies(ctx, owner, repo, index, gitea_sdk.ListIssueDependenciesOptions{
|
|
ListOptions: listOpt,
|
|
})
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("list %v/%v/issue/%v/dependencies err: %v", owner, repo, index, err))
|
|
}
|
|
return to.TextResult(slimIssues(issues))
|
|
}
|
|
|
|
func addIssueDependencyFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
|
return setIssueDependencyFn(ctx, args, true)
|
|
}
|
|
|
|
func removeIssueDependencyFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
|
return setIssueDependencyFn(ctx, args, false)
|
|
}
|
|
|
|
func setIssueDependencyFn(ctx context.Context, args map[string]any, add bool) (*mcp.CallToolResult, error) {
|
|
owner, repo, index, err := issueRef(args)
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
blocking, err := params.GetIndex(args, "blocking_issue")
|
|
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))
|
|
}
|
|
meta := gitea_sdk.IssueMeta{Index: blocking}
|
|
var issue *gitea_sdk.Issue
|
|
if add {
|
|
issue, _, err = client.Issues.CreateIssueDependency(ctx, owner, repo, index, meta)
|
|
} else {
|
|
issue, _, err = client.Issues.RemoveIssueDependency(ctx, owner, repo, index, meta)
|
|
}
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("update %v/%v/issue/%v/dependencies err: %v", owner, repo, index, err))
|
|
}
|
|
return to.TextResult(slimIssue(issue))
|
|
}
|
|
|
|
func issueRef(args map[string]any) (owner, repo string, index int64, err error) {
|
|
if owner, err = params.GetString(args, "owner"); err != nil {
|
|
return "", "", 0, err
|
|
}
|
|
if repo, err = params.GetString(args, "repo"); err != nil {
|
|
return "", "", 0, err
|
|
}
|
|
if index, err = params.GetIndex(args, "issue_number"); err != nil {
|
|
return "", "", 0, err
|
|
}
|
|
return owner, repo, index, nil
|
|
}
|