mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-02-27 17:15:13 +00:00
Compare commits
3 Commits
feat/accep
...
v0.8.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4d5fa3ab2c | ||
|
|
21e4e1b42b | ||
|
|
4aacfe348a |
@@ -221,6 +221,7 @@ The Gitea MCP Server supports the following tools:
|
|||||||
| submit_pull_request_review | Pull Request | Submit a pending review |
|
| submit_pull_request_review | Pull Request | Submit a pending review |
|
||||||
| delete_pull_request_review | Pull Request | Delete a review |
|
| delete_pull_request_review | Pull Request | Delete a review |
|
||||||
| dismiss_pull_request_review | Pull Request | Dismiss a review with optional message |
|
| dismiss_pull_request_review | Pull Request | Dismiss a review with optional message |
|
||||||
|
| merge_pull_request | Pull Request | Merge a pull request |
|
||||||
| search_users | User | Search for users |
|
| search_users | User | Search for users |
|
||||||
| search_org_teams | Organization | Search for teams in an organization |
|
| search_org_teams | Organization | Search for teams in an organization |
|
||||||
| list_org_labels | Organization | List labels defined at organization level |
|
| list_org_labels | Organization | List labels defined at organization level |
|
||||||
|
|||||||
@@ -220,6 +220,7 @@ Gitea MCP 服务器支持以下工具:
|
|||||||
| submit_pull_request_review | 拉取请求 | 提交待处理的审查 |
|
| submit_pull_request_review | 拉取请求 | 提交待处理的审查 |
|
||||||
| delete_pull_request_review | 拉取请求 | 删除审查 |
|
| delete_pull_request_review | 拉取请求 | 删除审查 |
|
||||||
| dismiss_pull_request_review | 拉取请求 | 驳回审查(可附消息) |
|
| dismiss_pull_request_review | 拉取请求 | 驳回审查(可附消息) |
|
||||||
|
| merge_pull_request | 拉取请求 | 合并拉取请求 |
|
||||||
| search_users | 用户 | 搜索用户 |
|
| search_users | 用户 | 搜索用户 |
|
||||||
| search_org_teams | 组织 | 搜索组织团队 |
|
| search_org_teams | 组织 | 搜索组织团队 |
|
||||||
| list_org_labels | 组织 | 列出组织标签 |
|
| list_org_labels | 组织 | 列出组织标签 |
|
||||||
|
|||||||
@@ -220,6 +220,7 @@ Gitea MCP 伺服器支援以下工具:
|
|||||||
| submit_pull_request_review | 拉取請求 | 提交待處理的審查 |
|
| submit_pull_request_review | 拉取請求 | 提交待處理的審查 |
|
||||||
| delete_pull_request_review | 拉取請求 | 刪除審查 |
|
| delete_pull_request_review | 拉取請求 | 刪除審查 |
|
||||||
| dismiss_pull_request_review | 拉取請求 | 駁回審查(可附訊息) |
|
| dismiss_pull_request_review | 拉取請求 | 駁回審查(可附訊息) |
|
||||||
|
| merge_pull_request | 拉取請求 | 合併拉取請求 |
|
||||||
| search_users | 用戶 | 搜尋用戶 |
|
| search_users | 用戶 | 搜尋用戶 |
|
||||||
| search_org_teams | 組織 | 搜尋組織團隊 |
|
| search_org_teams | 組織 | 搜尋組織團隊 |
|
||||||
| list_org_labels | 組織 | 列出組織標籤 |
|
| list_org_labels | 組織 | 列出組織標籤 |
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
"gitea.com/gitea/gitea-mcp/pkg/gitea"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/log"
|
"gitea.com/gitea/gitea-mcp/pkg/log"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/params"
|
"gitea.com/gitea/gitea-mcp/pkg/params"
|
||||||
|
"gitea.com/gitea/gitea-mcp/pkg/ptr"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/to"
|
"gitea.com/gitea/gitea-mcp/pkg/to"
|
||||||
"gitea.com/gitea/gitea-mcp/pkg/tool"
|
"gitea.com/gitea/gitea-mcp/pkg/tool"
|
||||||
|
|
||||||
@@ -31,6 +32,8 @@ const (
|
|||||||
SubmitPullRequestReviewToolName = "submit_pull_request_review"
|
SubmitPullRequestReviewToolName = "submit_pull_request_review"
|
||||||
DeletePullRequestReviewToolName = "delete_pull_request_review"
|
DeletePullRequestReviewToolName = "delete_pull_request_review"
|
||||||
DismissPullRequestReviewToolName = "dismiss_pull_request_review"
|
DismissPullRequestReviewToolName = "dismiss_pull_request_review"
|
||||||
|
MergePullRequestToolName = "merge_pull_request"
|
||||||
|
EditPullRequestToolName = "edit_pull_request"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -171,6 +174,33 @@ var (
|
|||||||
mcp.WithNumber("review_id", mcp.Required(), mcp.Description("review ID")),
|
mcp.WithNumber("review_id", mcp.Required(), mcp.Description("review ID")),
|
||||||
mcp.WithString("message", mcp.Description("dismissal reason")),
|
mcp.WithString("message", mcp.Description("dismissal reason")),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
MergePullRequestTool = mcp.NewTool(
|
||||||
|
MergePullRequestToolName,
|
||||||
|
mcp.WithDescription("merge a pull request"),
|
||||||
|
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("pull request index")),
|
||||||
|
mcp.WithString("merge_style", mcp.Description("merge style: merge, rebase, rebase-merge, squash, fast-forward-only"), mcp.Enum("merge", "rebase", "rebase-merge", "squash", "fast-forward-only"), mcp.DefaultString("merge")),
|
||||||
|
mcp.WithString("message", mcp.Description("custom merge commit message (optional)")),
|
||||||
|
mcp.WithBoolean("delete_branch", mcp.Description("delete the branch after merge"), mcp.DefaultBool(false)),
|
||||||
|
)
|
||||||
|
|
||||||
|
EditPullRequestTool = mcp.NewTool(
|
||||||
|
EditPullRequestToolName,
|
||||||
|
mcp.WithDescription("edit a pull request"),
|
||||||
|
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("pull request index")),
|
||||||
|
mcp.WithString("title", mcp.Description("pull request title")),
|
||||||
|
mcp.WithString("body", mcp.Description("pull request body content")),
|
||||||
|
mcp.WithString("base", mcp.Description("pull request base branch")),
|
||||||
|
mcp.WithString("assignee", mcp.Description("username to assign")),
|
||||||
|
mcp.WithArray("assignees", mcp.Description("usernames to assign"), mcp.Items(map[string]interface{}{"type": "string"})),
|
||||||
|
mcp.WithNumber("milestone", mcp.Description("milestone number")),
|
||||||
|
mcp.WithString("state", mcp.Description("pull request state"), mcp.Enum("open", "closed")),
|
||||||
|
mcp.WithBoolean("allow_maintainer_edit", mcp.Description("allow maintainer to edit the pull request")),
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@@ -226,6 +256,14 @@ func init() {
|
|||||||
Tool: DismissPullRequestReviewTool,
|
Tool: DismissPullRequestReviewTool,
|
||||||
Handler: DismissPullRequestReviewFn,
|
Handler: DismissPullRequestReviewFn,
|
||||||
})
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: MergePullRequestTool,
|
||||||
|
Handler: MergePullRequestFn,
|
||||||
|
})
|
||||||
|
Tool.RegisterWrite(server.ServerTool{
|
||||||
|
Tool: EditPullRequestTool,
|
||||||
|
Handler: EditPullRequestFn,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPullRequestByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func GetPullRequestByIndexFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
@@ -796,3 +834,131 @@ func DismissPullRequestReviewFn(ctx context.Context, req mcp.CallToolRequest) (*
|
|||||||
|
|
||||||
return to.TextResult(successMsg)
|
return to.TextResult(successMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MergePullRequestFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called MergePullRequestFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(fmt.Errorf("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(fmt.Errorf("repo is required"))
|
||||||
|
}
|
||||||
|
index, ok := req.GetArguments()["index"].(float64)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(fmt.Errorf("index is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
mergeStyle := "merge"
|
||||||
|
if style, exists := req.GetArguments()["merge_style"].(string); exists && style != "" {
|
||||||
|
mergeStyle = style
|
||||||
|
}
|
||||||
|
|
||||||
|
message := ""
|
||||||
|
if msg, exists := req.GetArguments()["message"].(string); exists {
|
||||||
|
message = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
deleteBranch := false
|
||||||
|
if del, exists := req.GetArguments()["delete_branch"].(bool); exists {
|
||||||
|
deleteBranch = del
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.MergePullRequestOption{
|
||||||
|
Style: gitea_sdk.MergeStyle(mergeStyle),
|
||||||
|
Message: message,
|
||||||
|
DeleteBranchAfterMerge: deleteBranch,
|
||||||
|
}
|
||||||
|
|
||||||
|
merged, resp, err := client.MergePullRequest(owner, repo, int64(index), opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("merge %v/%v/pr/%v err: %v", owner, repo, int64(index), err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !merged && resp != nil && resp.StatusCode >= 400 {
|
||||||
|
return to.ErrorResult(fmt.Errorf("merge %v/%v/pr/%v failed: HTTP %d %s", owner, repo, int64(index), resp.StatusCode, resp.Status))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !merged {
|
||||||
|
return to.ErrorResult(fmt.Errorf("merge %v/%v/pr/%v returned merged=false", owner, repo, int64(index)))
|
||||||
|
}
|
||||||
|
|
||||||
|
successMsg := map[string]interface{}{
|
||||||
|
"merged": merged,
|
||||||
|
"pr_index": int64(index),
|
||||||
|
"repository": fmt.Sprintf("%s/%s", owner, repo),
|
||||||
|
"merge_style": mergeStyle,
|
||||||
|
"branch_deleted": deleteBranch,
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(successMsg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func EditPullRequestFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
|
log.Debugf("Called EditPullRequestFn")
|
||||||
|
owner, ok := req.GetArguments()["owner"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(fmt.Errorf("owner is required"))
|
||||||
|
}
|
||||||
|
repo, ok := req.GetArguments()["repo"].(string)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(fmt.Errorf("repo is required"))
|
||||||
|
}
|
||||||
|
index, ok := req.GetArguments()["index"].(float64)
|
||||||
|
if !ok {
|
||||||
|
return to.ErrorResult(fmt.Errorf("index is required"))
|
||||||
|
}
|
||||||
|
|
||||||
|
opt := gitea_sdk.EditPullRequestOption{}
|
||||||
|
|
||||||
|
if title, ok := req.GetArguments()["title"].(string); ok {
|
||||||
|
opt.Title = title
|
||||||
|
}
|
||||||
|
if body, ok := req.GetArguments()["body"].(string); ok {
|
||||||
|
opt.Body = ptr.To(body)
|
||||||
|
}
|
||||||
|
if base, ok := req.GetArguments()["base"].(string); ok {
|
||||||
|
opt.Base = base
|
||||||
|
}
|
||||||
|
if assignee, ok := req.GetArguments()["assignee"].(string); ok {
|
||||||
|
opt.Assignee = assignee
|
||||||
|
}
|
||||||
|
if assigneesArg, exists := req.GetArguments()["assignees"]; exists {
|
||||||
|
if assigneesSlice, ok := assigneesArg.([]interface{}); ok {
|
||||||
|
var assignees []string
|
||||||
|
for _, a := range assigneesSlice {
|
||||||
|
if s, ok := a.(string); ok {
|
||||||
|
assignees = append(assignees, s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
opt.Assignees = assignees
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if milestone, ok := req.GetArguments()["milestone"].(float64); ok {
|
||||||
|
opt.Milestone = int64(milestone)
|
||||||
|
}
|
||||||
|
if state, ok := req.GetArguments()["state"].(string); ok {
|
||||||
|
opt.State = ptr.To(gitea_sdk.StateType(state))
|
||||||
|
}
|
||||||
|
if allowMaintainerEdit, ok := req.GetArguments()["allow_maintainer_edit"].(bool); ok {
|
||||||
|
opt.AllowMaintainerEdit = ptr.To(allowMaintainerEdit)
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := gitea.ClientFromContext(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
pr, _, err := client.EditPullRequest(owner, repo, int64(index), opt)
|
||||||
|
if err != nil {
|
||||||
|
return to.ErrorResult(fmt.Errorf("edit %v/%v/pr/%v err: %v", owner, repo, int64(index), err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return to.TextResult(pr)
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,6 +13,110 @@ import (
|
|||||||
"github.com/mark3labs/mcp-go/mcp"
|
"github.com/mark3labs/mcp-go/mcp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestEditPullRequestFn(t *testing.T) {
|
||||||
|
const (
|
||||||
|
owner = "octo"
|
||||||
|
repo = "demo"
|
||||||
|
index = 7
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
mu sync.Mutex
|
||||||
|
gotMethod string
|
||||||
|
gotPath string
|
||||||
|
gotBody map[string]any
|
||||||
|
)
|
||||||
|
|
||||||
|
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/pulls/%d", owner, repo, index):
|
||||||
|
mu.Lock()
|
||||||
|
gotMethod = r.Method
|
||||||
|
gotPath = r.URL.Path
|
||||||
|
var body map[string]any
|
||||||
|
_ = json.NewDecoder(r.Body).Decode(&body)
|
||||||
|
gotBody = body
|
||||||
|
mu.Unlock()
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(fmt.Sprintf(`{"number":%d,"title":"%s","state":"open"}`, index, body["title"])))
|
||||||
|
default:
|
||||||
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
origHost := flag.Host
|
||||||
|
origToken := flag.Token
|
||||||
|
origVersion := flag.Version
|
||||||
|
flag.Host = server.URL
|
||||||
|
flag.Token = ""
|
||||||
|
flag.Version = "test"
|
||||||
|
defer func() {
|
||||||
|
flag.Host = origHost
|
||||||
|
flag.Token = origToken
|
||||||
|
flag.Version = origVersion
|
||||||
|
}()
|
||||||
|
|
||||||
|
req := mcp.CallToolRequest{
|
||||||
|
Params: mcp.CallToolParams{
|
||||||
|
Arguments: map[string]any{
|
||||||
|
"owner": owner,
|
||||||
|
"repo": repo,
|
||||||
|
"index": float64(index),
|
||||||
|
"title": "WIP: my feature",
|
||||||
|
"state": "open",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := EditPullRequestFn(context.Background(), req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("EditPullRequestFn() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
mu.Lock()
|
||||||
|
defer mu.Unlock()
|
||||||
|
|
||||||
|
if gotMethod != http.MethodPatch {
|
||||||
|
t.Fatalf("expected PATCH request, got %s", gotMethod)
|
||||||
|
}
|
||||||
|
if gotPath != fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d", owner, repo, index) {
|
||||||
|
t.Fatalf("unexpected path: %s", gotPath)
|
||||||
|
}
|
||||||
|
if gotBody["title"] != "WIP: my feature" {
|
||||||
|
t.Fatalf("expected title 'WIP: my feature', got %v", gotBody["title"])
|
||||||
|
}
|
||||||
|
if gotBody["state"] != "open" {
|
||||||
|
t.Fatalf("expected state 'open', got %v", gotBody["state"])
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(result.Content) == 0 {
|
||||||
|
t.Fatalf("expected content in result")
|
||||||
|
}
|
||||||
|
textContent, ok := mcp.AsTextContent(result.Content[0])
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("expected text content, got %T", result.Content[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
var parsed struct {
|
||||||
|
Result map[string]any `json:"Result"`
|
||||||
|
}
|
||||||
|
if err := json.Unmarshal([]byte(textContent.Text), &parsed); err != nil {
|
||||||
|
t.Fatalf("unmarshal result text: %v", err)
|
||||||
|
}
|
||||||
|
if got := parsed.Result["title"].(string); got != "WIP: my feature" {
|
||||||
|
t.Fatalf("result title = %q, want %q", got, "WIP: my feature")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetPullRequestDiffFn(t *testing.T) {
|
func TestGetPullRequestDiffFn(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
owner = "octo"
|
owner = "octo"
|
||||||
|
|||||||
Reference in New Issue
Block a user