diff --git a/internal/tools/repo_status.go b/internal/tools/repo_status.go new file mode 100644 index 0000000..ac2e939 --- /dev/null +++ b/internal/tools/repo_status.go @@ -0,0 +1,104 @@ +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 RepoStatus struct { + c *gitea.Client + a *allowlist.Allowlist +} + +func NewRepoStatus(c *gitea.Client, a *allowlist.Allowlist) *RepoStatus { + return &RepoStatus{c: c, a: a} +} + +func (t *RepoStatus) Descriptor() registry.ToolDescriptor { + return registry.ToolDescriptor{ + Name: "repo_status", + Description: "Get repo state in one call: all branches, open PRs, and protection rules for a target branch. Use this first to decide whether to use feature-branch or trunk-based development.", + InputSchema: json.RawMessage(`{ + "type":"object", + "properties":{ + "owner":{"type":"string"}, + "name":{"type":"string"}, + "branch":{"type":"string"} + }, + "required":["owner","name"] + }`), + } +} + +type repoStatusArgs struct { + Owner string `json:"owner"` + Name string `json:"name"` + Branch string `json:"branch"` +} + +func (t *RepoStatus) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) { + var args repoStatusArgs + if err := parseArgs(raw, &args); err != nil { + return nil, err + } + if err := t.a.Check(args.Owner); err != nil { + return nil, err + } + + branch := args.Branch + if branch == "" { + var err error + branch, err = t.c.DefaultBranch(ctx, args.Owner, args.Name) + if err != nil { + return nil, err + } + } + + branches, err := t.c.ListBranches(ctx, args.Owner, args.Name, 1, 50) + if err != nil { + return nil, err + } + + prs, err := t.c.ListPullRequests(ctx, args.Owner, args.Name, "open", "", 1, 50) + if err != nil { + return nil, err + } + + bp, err := t.c.GetBranchProtection(ctx, args.Owner, args.Name, branch) + if err != nil { + return nil, err + } + + branchList := make([]map[string]any, len(branches)) + for i, b := range branches { + branchList[i] = map[string]any{"name": b.Name, "sha": b.Commit.ID} + } + + prList := make([]map[string]any, len(prs)) + for i, pr := range prs { + prList[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(map[string]any{ + "branches": branchList, + "open_prs": prList, + "protection": map[string]any{ + "protected": bp.Protected, + "required_approvals": bp.RequiredApprovals, + "push_whitelist": bp.PushWhitelist, + "merge_whitelist": bp.MergeWhitelist, + }, + }) +} diff --git a/internal/tools/repo_status_test.go b/internal/tools/repo_status_test.go new file mode 100644 index 0000000..90600aa --- /dev/null +++ b/internal/tools/repo_status_test.go @@ -0,0 +1,131 @@ +package tools_test + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "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/tools" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRepoStatusComposesThreeEndpoints(t *testing.T) { + mux := http.NewServeMux() + + mux.HandleFunc("/api/v1/repos/owner/repo/branches", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[ + {"name":"main","commit":{"id":"abc","url":""}}, + {"name":"feat/x","commit":{"id":"def","url":""}} + ]`)) + }) + mux.HandleFunc("/api/v1/repos/owner/repo/pulls", func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "open", r.URL.Query().Get("state")) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[{ + "number":3,"title":"My PR","html_url":"http://example.com/pulls/3", + "state":"open","draft":false, + "head":{"ref":"feat/x"},"base":{"ref":"main"} + }]`)) + }) + mux.HandleFunc("/api/v1/repos/owner/repo/branch_protections/main", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"required_approvals":1,"push_whitelist_usernames":[],"merge_whitelist_usernames":[]}`)) + }) + + srv := httptest.NewServer(mux) + defer srv.Close() + + tool := tools.NewRepoStatus(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"owner"})) + out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"owner","name":"repo","branch":"main"}`)) + require.NoError(t, err) + + var result map[string]any + require.NoError(t, json.Unmarshal(out, &result)) + + branches := result["branches"].([]any) + assert.Len(t, branches, 2) + + openPRs := result["open_prs"].([]any) + assert.Len(t, openPRs, 1) + assert.Equal(t, float64(3), openPRs[0].(map[string]any)["number"]) + + protection := result["protection"].(map[string]any) + assert.Equal(t, true, protection["protected"]) + assert.Equal(t, float64(1), protection["required_approvals"]) +} + +func TestRepoStatusUnprotectedBranch(t *testing.T) { + mux := http.NewServeMux() + + mux.HandleFunc("/api/v1/repos/owner/repo/branches", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[{"name":"main","commit":{"id":"abc","url":""}}]`)) + }) + mux.HandleFunc("/api/v1/repos/owner/repo/pulls", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[]`)) + }) + mux.HandleFunc("/api/v1/repos/owner/repo/branch_protections/main", func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message":"not found"}`)) + }) + + srv := httptest.NewServer(mux) + defer srv.Close() + + tool := tools.NewRepoStatus(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"owner"})) + out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"owner","name":"repo","branch":"main"}`)) + require.NoError(t, err) + + var result map[string]any + require.NoError(t, json.Unmarshal(out, &result)) + protection := result["protection"].(map[string]any) + assert.Equal(t, false, protection["protected"]) +} + +func TestRepoStatusAllowlistRejects(t *testing.T) { + tool := tools.NewRepoStatus(gitea.NewClient("http://unused", ""), allowlist.New([]string{"allowed"})) + _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"repo","branch":"main"}`)) + require.Error(t, err) +} + +func TestRepoStatusDefaultsBranchFromRepo(t *testing.T) { + mux := http.NewServeMux() + + mux.HandleFunc("/api/v1/repos/owner/repo", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"name":"repo","full_name":"owner/repo","default_branch":"main","description":"","private":false,"clone_url":"","html_url":""}`)) + }) + mux.HandleFunc("/api/v1/repos/owner/repo/branches", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[{"name":"main","commit":{"id":"abc","url":""}}]`)) + }) + mux.HandleFunc("/api/v1/repos/owner/repo/pulls", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[]`)) + }) + mux.HandleFunc("/api/v1/repos/owner/repo/branch_protections/main", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"required_approvals":0,"push_whitelist_usernames":[],"merge_whitelist_usernames":[]}`)) + }) + + srv := httptest.NewServer(mux) + defer srv.Close() + + tool := tools.NewRepoStatus(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"owner"})) + // no "branch" field — triggers DefaultBranch fallback + out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"owner","name":"repo"}`)) + require.NoError(t, err) + + var result map[string]any + require.NoError(t, json.Unmarshal(out, &result)) + assert.NotNil(t, result["branches"]) + assert.NotNil(t, result["open_prs"]) + assert.NotNil(t, result["protection"]) +}