Compare commits
7 Commits
v0.1.1
...
4f0f65e26a
| Author | SHA1 | Date | |
|---|---|---|---|
| 4f0f65e26a | |||
|
|
9cbb564cd9 | ||
| 47e631da23 | |||
| d35ff9781c | |||
| 052827320a | |||
| c85197ea5e | |||
|
|
c345025221 |
@@ -54,6 +54,18 @@ func main() {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
_, _ = w.Write([]byte("ok"))
|
_, _ = w.Write([]byte("ok"))
|
||||||
})
|
})
|
||||||
|
mux.HandleFunc("/.well-known/oauth-protected-resource", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write([]byte(`{"authorization_servers":[]}`))
|
||||||
|
})
|
||||||
|
mux.HandleFunc("/.well-known/oauth-authorization-server", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
})
|
||||||
|
|
||||||
addr := ":" + cfg.Port
|
addr := ":" + cfg.Port
|
||||||
logger.Info("gitea-mcp starting", "addr", addr, "version", "0.1.0")
|
logger.Info("gitea-mcp starting", "addr", addr, "version", "0.1.0")
|
||||||
|
|||||||
@@ -92,13 +92,24 @@ type FileWriteResult struct {
|
|||||||
} `json:"commit"`
|
} `json:"commit"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpsertFile creates a file when args.Sha is empty (POST) or updates an existing
|
||||||
|
// file when args.Sha is set (PUT). Gitea routes both operations by HTTP method on
|
||||||
|
// the same /contents/{path} URL, and rejects PUT without a sha.
|
||||||
func (c *Client) UpsertFile(ctx context.Context, owner, repo, path string, args UpsertFileArgs) (*FileWriteResult, error) {
|
func (c *Client) UpsertFile(ctx context.Context, owner, repo, path string, args UpsertFileArgs) (*FileWriteResult, error) {
|
||||||
p := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, path)
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/contents/%s", owner, repo, path)
|
||||||
payload, err := json.Marshal(args)
|
payload, err := json.Marshal(args)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
body, status, err := c.PutJSON(ctx, p, payload)
|
var (
|
||||||
|
body []byte
|
||||||
|
status int
|
||||||
|
)
|
||||||
|
if args.Sha == "" {
|
||||||
|
body, status, err = c.PostJSON(ctx, p, payload)
|
||||||
|
} else {
|
||||||
|
body, status, err = c.PutJSON(ctx, p, payload)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ func NewServer(opts ServerOptions) *Server {
|
|||||||
|
|
||||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
|
case http.MethodHead:
|
||||||
|
w.Header().Set("MCP-Protocol-Version", ProtocolVersion)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
s.handleGET(w, r)
|
s.handleGET(w, r)
|
||||||
case http.MethodPost:
|
case http.MethodPost:
|
||||||
|
|||||||
@@ -118,6 +118,15 @@ func TestPostBodyTooLarge(t *testing.T) {
|
|||||||
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
assert.Equal(t, http.StatusBadRequest, rr.Code)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestHEADReturnsMCPProtocolVersionHeader(t *testing.T) {
|
||||||
|
srv := newServer(t)
|
||||||
|
req := httptest.NewRequest(http.MethodHead, "/mcp", nil)
|
||||||
|
rr := httptest.NewRecorder()
|
||||||
|
srv.ServeHTTP(rr, req)
|
||||||
|
require.Equal(t, http.StatusOK, rr.Code)
|
||||||
|
assert.Equal(t, mcp.ProtocolVersion, rr.Header().Get("MCP-Protocol-Version"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestToolsCallToolNotFound(t *testing.T) {
|
func TestToolsCallToolNotFound(t *testing.T) {
|
||||||
srv := newServer(t)
|
srv := newServer(t)
|
||||||
// Initialize to get a session ID.
|
// Initialize to get a session ID.
|
||||||
|
|||||||
@@ -39,9 +39,9 @@ func TestFileWriteBranchCreatesBranchAndFile(t *testing.T) {
|
|||||||
_, _ = w.Write([]byte(createBranchResp))
|
_, _ = w.Write([]byte(createBranchResp))
|
||||||
})
|
})
|
||||||
|
|
||||||
// Upsert file → 201
|
// New file (no sha) → POST to /contents/{path}
|
||||||
mux.HandleFunc("/api/v1/repos/owner/myrepo/contents/doc.md", func(w http.ResponseWriter, r *http.Request) {
|
mux.HandleFunc("/api/v1/repos/owner/myrepo/contents/doc.md", func(w http.ResponseWriter, r *http.Request) {
|
||||||
require.Equal(t, http.MethodPut, r.Method)
|
require.Equal(t, http.MethodPost, r.Method)
|
||||||
w.WriteHeader(http.StatusCreated)
|
w.WriteHeader(http.StatusCreated)
|
||||||
_, _ = w.Write([]byte(upsertFileResp))
|
_, _ = w.Write([]byte(upsertFileResp))
|
||||||
})
|
})
|
||||||
@@ -64,6 +64,39 @@ func TestFileWriteBranchCreatesBranchAndFile(t *testing.T) {
|
|||||||
assert.Equal(t, "cmt1", result["commit_sha"])
|
assert.Equal(t, "cmt1", result["commit_sha"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFileWriteBranchUsesPutWhenShaProvided(t *testing.T) {
|
||||||
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
// Branch exists
|
||||||
|
mux.HandleFunc("/api/v1/repos/owner/myrepo/branches/feat/existing", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
_, _ = w.Write([]byte(branchCheckExistsResp))
|
||||||
|
})
|
||||||
|
|
||||||
|
// Existing file (sha provided) → PUT
|
||||||
|
mux.HandleFunc("/api/v1/repos/owner/myrepo/contents/doc.md", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
require.Equal(t, http.MethodPut, r.Method)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
_, _ = w.Write([]byte(upsertFileResp))
|
||||||
|
})
|
||||||
|
|
||||||
|
srv := httptest.NewServer(mux)
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
tool := tools.NewFileWriteBranch(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"owner"}))
|
||||||
|
out, err := tool.Call(context.Background(), json.RawMessage(`{
|
||||||
|
"owner":"owner","name":"myrepo","path":"doc.md",
|
||||||
|
"content":"hello","branch":"feat/existing",
|
||||||
|
"sha":"oldsha","message":"update doc.md"
|
||||||
|
}`))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var result map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(out, &result))
|
||||||
|
assert.Equal(t, "feat/existing", result["branch"])
|
||||||
|
assert.Equal(t, "cmt1", result["commit_sha"])
|
||||||
|
}
|
||||||
|
|
||||||
func TestFileWriteBranchUsesDefaultBaseWhenBaseEmpty(t *testing.T) {
|
func TestFileWriteBranchUsesDefaultBaseWhenBaseEmpty(t *testing.T) {
|
||||||
var createBody []byte
|
var createBody []byte
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ func splitUnifiedDiff(d []byte) map[string][]byte {
|
|||||||
|
|
||||||
flush := func() {
|
flush := func() {
|
||||||
if currentFile != "" {
|
if currentFile != "" {
|
||||||
m[currentFile] = []byte(current.String())
|
m[currentFile] = current.Bytes()
|
||||||
current.Reset()
|
current.Reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,11 +42,11 @@ func buildFilesJSON(files []string, additions int) string {
|
|||||||
func newPRFilesDiffServer(t *testing.T, filesJSON, rawDiff string) *httptest.Server {
|
func newPRFilesDiffServer(t *testing.T, filesJSON, rawDiff string) *httptest.Server {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
switch {
|
switch r.URL.Path {
|
||||||
case r.URL.Path == "/api/v1/repos/o/r/pulls/1/files":
|
case "/api/v1/repos/o/r/pulls/1/files":
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
_, _ = w.Write([]byte(filesJSON))
|
_, _ = w.Write([]byte(filesJSON))
|
||||||
case r.URL.Path == "/api/v1/repos/o/r/pulls/1.diff":
|
case "/api/v1/repos/o/r/pulls/1.diff":
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
_, _ = w.Write([]byte(rawDiff))
|
_, _ = w.Write([]byte(rawDiff))
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package tools
|
package tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
|
||||||
"gitea.d-ma.be/mathias/gitea-mcp/internal/registry"
|
"gitea.d-ma.be/mathias/gitea-mcp/internal/registry"
|
||||||
@@ -21,8 +20,6 @@ func parseArgs(raw json.RawMessage, dst any) error {
|
|||||||
return json.Unmarshal(raw, dst)
|
return json.Unmarshal(raw, dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
func _ctx(ctx context.Context) context.Context { return ctx } // stub for future hooks
|
|
||||||
|
|
||||||
// capLimit returns a sane page size: 0 or negative → def, > 50 → 50.
|
// capLimit returns a sane page size: 0 or negative → def, > 50 → 50.
|
||||||
func capLimit(in, def int) int {
|
func capLimit(in, def int) int {
|
||||||
if in <= 0 {
|
if in <= 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user