feat(tools): pr_list
This commit is contained in:
80
internal/tools/pr_list.go
Normal file
80
internal/tools/pr_list.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist"
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/gitea"
|
||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/registry"
|
||||
)
|
||||
|
||||
type PRList struct {
|
||||
c *gitea.Client
|
||||
a *allowlist.Allowlist
|
||||
}
|
||||
|
||||
func NewPRList(c *gitea.Client, a *allowlist.Allowlist) *PRList {
|
||||
return &PRList{c: c, a: a}
|
||||
}
|
||||
|
||||
func (t *PRList) Descriptor() registry.ToolDescriptor {
|
||||
return registry.ToolDescriptor{
|
||||
Name: "pr_list",
|
||||
Description: "List pull requests. state: open (default), closed, or all. Optionally filter by head branch.",
|
||||
InputSchema: json.RawMessage(`{
|
||||
"type":"object",
|
||||
"properties":{
|
||||
"owner":{"type":"string"},
|
||||
"name":{"type":"string"},
|
||||
"state":{"type":"string","enum":["open","closed","all"]},
|
||||
"head":{"type":"string"},
|
||||
"page":{"type":"integer","minimum":1},
|
||||
"limit":{"type":"integer","minimum":1,"maximum":50}
|
||||
},
|
||||
"required":["owner","name"]
|
||||
}`),
|
||||
}
|
||||
}
|
||||
|
||||
type prListArgs struct {
|
||||
Owner string `json:"owner"`
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
Head string `json:"head"`
|
||||
Page int `json:"page"`
|
||||
Limit int `json:"limit"`
|
||||
}
|
||||
|
||||
func (t *PRList) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
|
||||
var args prListArgs
|
||||
if err := parseArgs(raw, &args); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := t.a.Check(args.Owner); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state := args.State
|
||||
if state == "" {
|
||||
state = "open"
|
||||
}
|
||||
|
||||
prs, err := t.c.ListPullRequests(ctx, args.Owner, args.Name, state, args.Head, args.Page, capLimit(args.Limit, 30))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := make([]map[string]any, len(prs))
|
||||
for i, pr := range prs {
|
||||
result[i] = map[string]any{
|
||||
"number": pr.Number,
|
||||
"title": pr.Title,
|
||||
"state": pr.State,
|
||||
"head_branch": pr.Head.Ref,
|
||||
"base_branch": pr.Base.Ref,
|
||||
"draft": pr.Draft,
|
||||
"html_url": pr.HTMLURL,
|
||||
}
|
||||
}
|
||||
return textOK(result)
|
||||
}
|
||||
Reference in New Issue
Block a user