mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-02-27 09:05:12 +00:00
## Summary
- Add `.golangci.yml` with linter configuration matching the main gitea repo
- Add `lint`, `lint-fix`, `lint-go`, `lint-go-fix`, and `security-check` Makefile targets
- Add `tidy` Makefile target (extracts min Go version from `go.mod` for `-compat` flag)
- Bump minimum Go version to 1.26
- Update golangci-lint to v2.10.1
- Replace `golang/govulncheck-action` with `make security-check` in CI
- Add `make lint` step to CI
- Fix all lint issues across the codebase (formatting, `errors.New` vs `fmt.Errorf`, `any` vs `interface{}`, unused returns, stuttering names, Go 1.26 `new(expr)`, etc.)
- Remove unused `pkg/ptr` package (inlined by Go 1.26 `new(expr)`)
- Remove dead linter exclusions (staticcheck, gocritic, testifylint, dupl)
## Test plan
- [x] `make lint` passes
- [x] `go test ./...` passes
- [x] `make build` succeeds
Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/133
Reviewed-by: techknowlogick <techknowlogick@noreply.gitea.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-committed-by: silverwind <me@silverwind.io>
245 lines
5.9 KiB
Go
245 lines
5.9 KiB
Go
package pull
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"sync"
|
|
"testing"
|
|
|
|
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
|
"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(fmt.Appendf(nil, `{"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) {
|
|
const (
|
|
owner = "octo"
|
|
repo = "demo"
|
|
index = 12
|
|
diffRaw = "diff --git a/file.txt b/file.txt\n+line\n"
|
|
)
|
|
|
|
var (
|
|
mu sync.Mutex
|
|
diffRequested bool
|
|
binaryValue string
|
|
)
|
|
errCh := make(chan error, 1)
|
|
|
|
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("/%s/%s/pulls/%d.diff", owner, repo, index):
|
|
if r.Method != http.MethodGet {
|
|
select {
|
|
case errCh <- fmt.Errorf("unexpected method: %s", r.Method):
|
|
default:
|
|
}
|
|
}
|
|
mu.Lock()
|
|
diffRequested = true
|
|
binaryValue = r.URL.Query().Get("binary")
|
|
mu.Unlock()
|
|
w.Header().Set("Content-Type", "text/plain")
|
|
_, _ = w.Write([]byte(diffRaw))
|
|
default:
|
|
select {
|
|
case errCh <- fmt.Errorf("unexpected request path: %s", r.URL.Path):
|
|
default:
|
|
}
|
|
}
|
|
})
|
|
|
|
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),
|
|
"binary": true,
|
|
},
|
|
},
|
|
}
|
|
|
|
result, err := GetPullRequestDiffFn(context.Background(), req)
|
|
if err != nil {
|
|
t.Fatalf("GetPullRequestDiffFn() error = %v", err)
|
|
}
|
|
|
|
select {
|
|
case reqErr := <-errCh:
|
|
t.Fatalf("handler error: %v", reqErr)
|
|
default:
|
|
}
|
|
|
|
mu.Lock()
|
|
requested := diffRequested
|
|
gotBinary := binaryValue
|
|
mu.Unlock()
|
|
|
|
if !requested {
|
|
t.Fatalf("expected diff request to be made")
|
|
}
|
|
if gotBinary != "true" {
|
|
t.Fatalf("expected binary=true query param, got %q", gotBinary)
|
|
}
|
|
|
|
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, ok := parsed.Result["diff"].(string); !ok || got != diffRaw {
|
|
t.Fatalf("diff = %q, want %q", got, diffRaw)
|
|
}
|
|
if got, ok := parsed.Result["binary"].(bool); !ok || got != true {
|
|
t.Fatalf("binary = %v, want true", got)
|
|
}
|
|
if got, ok := parsed.Result["index"].(float64); !ok || int64(got) != int64(index) {
|
|
t.Fatalf("index = %v, want %d", got, index)
|
|
}
|
|
if got, ok := parsed.Result["owner"].(string); !ok || got != owner {
|
|
t.Fatalf("owner = %q, want %q", got, owner)
|
|
}
|
|
if got, ok := parsed.Result["repo"].(string); !ok || got != repo {
|
|
t.Fatalf("repo = %q, want %q", got, repo)
|
|
}
|
|
}
|