mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-27 10:37:44 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bfc1ff17da |
@@ -49,6 +49,7 @@ var (
|
|||||||
tool.Array("milestones", tool.Description("milestone name or ID filter"), tool.Items(map[string]any{"type": "string"})),
|
tool.Array("milestones", tool.Description("milestone name or ID filter"), tool.Items(map[string]any{"type": "string"})),
|
||||||
tool.String("since", tool.Description("updated after ISO 8601")),
|
tool.String("since", tool.Description("updated after ISO 8601")),
|
||||||
tool.String("before", tool.Description("updated before ISO 8601")),
|
tool.String("before", tool.Description("updated before ISO 8601")),
|
||||||
|
tool.String("assigned_by", tool.Description("filter by the user who assigned the issue")),
|
||||||
tool.Number("page", tool.Description(params.PageDesc), tool.Default(1)),
|
tool.Number("page", tool.Description(params.PageDesc), tool.Default(1)),
|
||||||
tool.Number("per_page", tool.Description(params.PaginationDesc), tool.Default(30)),
|
tool.Number("per_page", tool.Description(params.PaginationDesc), tool.Default(30)),
|
||||||
)
|
)
|
||||||
@@ -204,6 +205,9 @@ func listRepoIssuesFn(ctx context.Context, args map[string]any) (*mcp.CallToolRe
|
|||||||
if t := params.GetOptionalTime(args, "before"); t != nil {
|
if t := params.GetOptionalTime(args, "before"); t != nil {
|
||||||
opt.Before = *t
|
opt.Before = *t
|
||||||
}
|
}
|
||||||
|
if assignedBy, ok := args["assigned_by"].(string); ok {
|
||||||
|
opt.AssignedBy = assignedBy
|
||||||
|
}
|
||||||
client, err := gitea.ClientFromContext(ctx)
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
|||||||
@@ -61,12 +61,13 @@ func Test_listRepoIssuesFn_filters(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
args := map[string]any{
|
args := map[string]any{
|
||||||
"owner": owner,
|
"owner": owner,
|
||||||
"repo": repo,
|
"repo": repo,
|
||||||
"type": "issues",
|
"type": "issues",
|
||||||
"labels": []any{"bug", "enhancement"},
|
"labels": []any{"bug", "enhancement"},
|
||||||
"milestones": []any{"v1.0", "2"},
|
"milestones": []any{"v1.0", "2"},
|
||||||
"since": "2026-01-01T00:00:00Z",
|
"since": "2026-01-01T00:00:00Z",
|
||||||
|
"assigned_by": "octocat",
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := listRepoIssuesFn(context.Background(), args)
|
_, err := listRepoIssuesFn(context.Background(), args)
|
||||||
@@ -89,6 +90,9 @@ func Test_listRepoIssuesFn_filters(t *testing.T) {
|
|||||||
if !strings.Contains(gotQuery, "type=issues") {
|
if !strings.Contains(gotQuery, "type=issues") {
|
||||||
t.Fatalf("expected type query param, got %s", gotQuery)
|
t.Fatalf("expected type query param, got %s", gotQuery)
|
||||||
}
|
}
|
||||||
|
if !strings.Contains(gotQuery, "assigned_by=octocat") {
|
||||||
|
t.Fatalf("expected assigned_by query param, got %s", gotQuery)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_listRepoIssuesFn_includesMilestone(t *testing.T) {
|
func Test_listRepoIssuesFn_includesMilestone(t *testing.T) {
|
||||||
@@ -138,6 +142,52 @@ func Test_listRepoIssuesFn_includesMilestone(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_listRepoIssuesFn_includesAssignees(t *testing.T) {
|
||||||
|
const (
|
||||||
|
owner = "octo"
|
||||||
|
repo = "demo"
|
||||||
|
)
|
||||||
|
|
||||||
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/api/v1/version":
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"version":"1.12.0"}`))
|
||||||
|
case fmt.Sprintf("/api/v1/repos/%s/%s", owner, repo):
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`{"private":false}`))
|
||||||
|
case fmt.Sprintf("/api/v1/repos/%s/%s/issues", owner, repo):
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(`[
|
||||||
|
{"number": 1, "title": "with assignees", "state": "open", "assignees": [{"login": "octocat"}]}
|
||||||
|
]`))
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
origHost, origToken, origVersion := flag.Host, flag.Token, flag.Version
|
||||||
|
flag.Host, flag.Token, flag.Version = server.URL, "", "test"
|
||||||
|
defer func() { flag.Host, flag.Token, flag.Version = origHost, origToken, origVersion }()
|
||||||
|
|
||||||
|
args := map[string]any{
|
||||||
|
"owner": owner, "repo": repo,
|
||||||
|
}
|
||||||
|
res, err := listRepoIssuesFn(context.Background(), args)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("listRepoIssuesFn() error = %v", err)
|
||||||
|
}
|
||||||
|
if res.IsError {
|
||||||
|
t.Fatalf("unexpected error result: %v", res.Content)
|
||||||
|
}
|
||||||
|
body := res.Content[0].(*mcp.TextContent).Text
|
||||||
|
if !strings.Contains(body, `"assignees"`) || !strings.Contains(body, `"octocat"`) {
|
||||||
|
t.Fatalf("expected assignees in list output, got: %s", body)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Test_createIssueFn_labels(t *testing.T) {
|
func Test_createIssueFn_labels(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
owner = "octo"
|
owner = "octo"
|
||||||
|
|||||||
@@ -63,6 +63,9 @@ func slimIssues(issues []*gitea_sdk.Issue) []map[string]any {
|
|||||||
if len(i.Labels) > 0 {
|
if len(i.Labels) > 0 {
|
||||||
m["labels"] = slim.LabelNames(i.Labels)
|
m["labels"] = slim.LabelNames(i.Labels)
|
||||||
}
|
}
|
||||||
|
if len(i.Assignees) > 0 {
|
||||||
|
m["assignees"] = slim.UserLogins(i.Assignees)
|
||||||
|
}
|
||||||
if i.Milestone != nil {
|
if i.Milestone != nil {
|
||||||
m["milestone"] = map[string]any{
|
m["milestone"] = map[string]any{
|
||||||
"id": i.Milestone.ID,
|
"id": i.Milestone.ID,
|
||||||
|
|||||||
+2
-41
@@ -20,13 +20,6 @@ import (
|
|||||||
|
|
||||||
var Tool = tool.New("pull_request")
|
var Tool = tool.New("pull_request")
|
||||||
|
|
||||||
// commentWithAssets wraps the SDK Comment to capture the `assets` field that
|
|
||||||
// the SDK currently drops on the issue comments endpoint.
|
|
||||||
type commentWithAssets struct {
|
|
||||||
gitea_sdk.Comment
|
|
||||||
Assets []*gitea_sdk.Attachment `json:"assets"`
|
|
||||||
}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
ListRepoPullRequestsToolName = "list_pull_requests"
|
ListRepoPullRequestsToolName = "list_pull_requests"
|
||||||
PullRequestReadToolName = "pull_request_read"
|
PullRequestReadToolName = "pull_request_read"
|
||||||
@@ -50,9 +43,9 @@ var (
|
|||||||
|
|
||||||
PullRequestReadTool = tool.NewDefinition(
|
PullRequestReadTool = tool.NewDefinition(
|
||||||
PullRequestReadToolName,
|
PullRequestReadToolName,
|
||||||
"Read pull request: details, diff, changed files, head commit status, reviews, review comments, discussion comments.",
|
"Read pull request: details, diff, changed files, head commit status, reviews, review comments.",
|
||||||
annotation.ReadOnly("Read pull request details"),
|
annotation.ReadOnly("Read pull request details"),
|
||||||
tool.String("method", tool.Required(), tool.Enum("get", "get_diff", "get_files", "get_status", "get_reviews", "get_review", "get_review_comments", "get_comments")),
|
tool.String("method", tool.Required(), tool.Enum("get", "get_diff", "get_files", "get_status", "get_reviews", "get_review", "get_review_comments")),
|
||||||
tool.String("owner", tool.Required(), tool.Description(params.OwnerDesc)),
|
tool.String("owner", tool.Required(), tool.Description(params.OwnerDesc)),
|
||||||
tool.String("repo", tool.Required(), tool.Description(params.RepoDesc)),
|
tool.String("repo", tool.Required(), tool.Description(params.RepoDesc)),
|
||||||
tool.Number("pull_number", tool.Required()),
|
tool.Number("pull_number", tool.Required()),
|
||||||
@@ -158,8 +151,6 @@ func pullRequestReadFn(ctx context.Context, args map[string]any) (*mcp.CallToolR
|
|||||||
return getPullRequestReviewFn(ctx, args)
|
return getPullRequestReviewFn(ctx, args)
|
||||||
case "get_review_comments":
|
case "get_review_comments":
|
||||||
return listPullRequestReviewCommentsFn(ctx, args)
|
return listPullRequestReviewCommentsFn(ctx, args)
|
||||||
case "get_comments":
|
|
||||||
return listPullRequestCommentsFn(ctx, args)
|
|
||||||
default:
|
default:
|
||||||
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
return to.ErrorResult(fmt.Errorf("unknown method: %s", method))
|
||||||
}
|
}
|
||||||
@@ -612,36 +603,6 @@ func listPullRequestReviewCommentsFn(ctx context.Context, args map[string]any) (
|
|||||||
return to.TextResult(slimReviewComments(comments))
|
return to.TextResult(slimReviewComments(comments))
|
||||||
}
|
}
|
||||||
|
|
||||||
func listPullRequestCommentsFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
|
||||||
owner, err := params.GetString(args, "owner")
|
|
||||||
if err != nil {
|
|
||||||
return to.ErrorResult(err)
|
|
||||||
}
|
|
||||||
repo, err := params.GetString(args, "repo")
|
|
||||||
if err != nil {
|
|
||||||
return to.ErrorResult(err)
|
|
||||||
}
|
|
||||||
index, err := params.GetIndex(args, "pull_number")
|
|
||||||
if err != nil {
|
|
||||||
return to.ErrorResult(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// PRs are issues internally, so the regular discussion comments live on
|
|
||||||
// the issue comments endpoint rather than a pull-specific one.
|
|
||||||
var comments []commentWithAssets
|
|
||||||
path := fmt.Sprintf("repos/%s/%s/issues/%d/comments", url.PathEscape(owner), url.PathEscape(repo), index)
|
|
||||||
if _, err := gitea.DoJSON(ctx, "GET", path, nil, nil, &comments); err != nil {
|
|
||||||
return to.ErrorResult(fmt.Errorf("get %v/%v/pr/%v comments err: %v", owner, repo, index, err))
|
|
||||||
}
|
|
||||||
out := make([]map[string]any, 0, len(comments))
|
|
||||||
for i := range comments {
|
|
||||||
m := slimComment(&comments[i].Comment)
|
|
||||||
m["body"] = slim.BodyWithAttachments(comments[i].Body, comments[i].Assets)
|
|
||||||
out = append(out, m)
|
|
||||||
}
|
|
||||||
return to.TextResult(out)
|
|
||||||
}
|
|
||||||
|
|
||||||
func createPullRequestReviewFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
func createPullRequestReviewFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
||||||
owner, err := params.GetString(args, "owner")
|
owner, err := params.GetString(args, "owner")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -943,91 +943,6 @@ func Test_closePullRequestFn(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_listPullRequestCommentsFn_missingArgs(t *testing.T) {
|
|
||||||
result, err := listPullRequestCommentsFn(context.Background(), map[string]any{
|
|
||||||
"owner": "octo",
|
|
||||||
"repo": "demo",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("listPullRequestCommentsFn() error = %v", err)
|
|
||||||
}
|
|
||||||
if result == nil || !result.IsError {
|
|
||||||
t.Fatalf("listPullRequestCommentsFn() result = %#v, want an error result for missing pull_number", result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_listPullRequestCommentsFn_apiError(t *testing.T) {
|
|
||||||
const (
|
|
||||||
owner = "octo"
|
|
||||||
repo = "demo"
|
|
||||||
index = 7
|
|
||||||
)
|
|
||||||
|
|
||||||
serveStub(t, func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path != fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", owner, repo, index) {
|
|
||||||
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
http.Error(w, "boom", http.StatusInternalServerError)
|
|
||||||
})
|
|
||||||
|
|
||||||
args := map[string]any{
|
|
||||||
"owner": owner, "repo": repo, "pull_number": float64(index),
|
|
||||||
}
|
|
||||||
result, err := listPullRequestCommentsFn(context.Background(), args)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("listPullRequestCommentsFn() error = %v", err)
|
|
||||||
}
|
|
||||||
if result == nil || !result.IsError {
|
|
||||||
t.Fatalf("listPullRequestCommentsFn() result = %#v, want an error result on API failure", result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_listPullRequestCommentsFn_decodesComments(t *testing.T) {
|
|
||||||
const (
|
|
||||||
owner = "octo"
|
|
||||||
repo = "demo"
|
|
||||||
index = 7
|
|
||||||
)
|
|
||||||
|
|
||||||
serveStub(t, func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.URL.Path != fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", owner, repo, index) {
|
|
||||||
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
_, _ = w.Write([]byte(`[
|
|
||||||
{"id": 1, "body": "see this", "assets": [
|
|
||||||
{"id": 9, "name": "log.txt", "size": 200, "browser_download_url": "https://example/log.txt"}
|
|
||||||
]},
|
|
||||||
{"id": 2, "body": "no attachment", "assets": []}
|
|
||||||
]`))
|
|
||||||
})
|
|
||||||
|
|
||||||
args := map[string]any{
|
|
||||||
"method": "get_comments", "owner": owner, "repo": repo, "pull_number": float64(index),
|
|
||||||
}
|
|
||||||
result, err := pullRequestReadFn(context.Background(), args)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("pullRequestReadFn() error = %v", err)
|
|
||||||
}
|
|
||||||
if result.IsError {
|
|
||||||
t.Fatalf("unexpected error result: %v", result.Content)
|
|
||||||
}
|
|
||||||
body := result.Content[0].(*mcp.TextContent).Text
|
|
||||||
if !strings.Contains(body, `[log.txt](https://example/log.txt)`) {
|
|
||||||
t.Fatalf("expected attachment markdown inlined in body, got: %s", body)
|
|
||||||
}
|
|
||||||
if !strings.Contains(body, `"no attachment"`) {
|
|
||||||
t.Fatalf("expected second comment body preserved, got: %s", body)
|
|
||||||
}
|
|
||||||
if strings.Contains(body, `"assets"`) {
|
|
||||||
t.Fatalf("assets should be inlined into body, not a separate field: %s", body)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_reopenPullRequestFn(t *testing.T) {
|
func Test_reopenPullRequestFn(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
owner = "octo"
|
owner = "octo"
|
||||||
|
|||||||
@@ -164,17 +164,3 @@ func slimReviewComments(comments []*gitea_sdk.PullReviewComment) []map[string]an
|
|||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func slimComment(c *gitea_sdk.Comment) map[string]any {
|
|
||||||
if c == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return map[string]any{
|
|
||||||
"id": c.ID,
|
|
||||||
"body": c.Body,
|
|
||||||
"user": slim.UserLogin(c.Poster),
|
|
||||||
"html_url": c.HTMLURL,
|
|
||||||
"created_at": c.Created,
|
|
||||||
"updated_at": c.Updated,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user