|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/github/github-mcp-server/v2/pkg/inventory" |
| 11 | + "github.com/github/github-mcp-server/v2/pkg/translations" |
| 12 | + "github.com/modelcontextprotocol/go-sdk/mcp" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +const agentMergeEnrollmentResponse = `{"allowed_actions":["fix_ci"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}` |
| 18 | + |
| 19 | +func TestUpdatePullRequestAgentMergeEnrollment(t *testing.T) { |
| 20 | + for _, protocol := range []string{"2025-11-25", inventory.ProtocolVersionMultiRoundTrip} { |
| 21 | + for _, tc := range []struct { |
| 22 | + name, actions, method, response, expected string |
| 23 | + status int |
| 24 | + isError bool |
| 25 | + }{ |
| 26 | + {"enable", `["fix_ci"]`, http.MethodPut, agentMergeEnrollmentResponse, `{"enabled":true,"enrollment":` + agentMergeEnrollmentResponse + `}`, 201, false}, |
| 27 | + {"update", `["fix_ci","resolve_merge_conflicts"]`, http.MethodPut, |
| 28 | + `{"allowed_actions":["fix_ci","resolve_merge_conflicts"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}`, |
| 29 | + `{"enabled":true,"enrollment":{"allowed_actions":["fix_ci","resolve_merge_conflicts"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}}`, 200, false}, |
| 30 | + {"future action in response", `["fix_ci"]`, http.MethodPut, |
| 31 | + `{"allowed_actions":["future_action"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}`, |
| 32 | + `{"enabled":true,"enrollment":{"allowed_actions":["future_action"],"created_at":"2026-11-30T03:00:00Z","updated_at":"2026-11-30T03:01:00Z","user":{"id":42}}}`, 200, false}, |
| 33 | + {"disable", `[]`, http.MethodDelete, "", `{"enabled":false}`, 204, false}, |
| 34 | + {"forbidden", `["fix_ci"]`, http.MethodPut, `{"message":"not eligible"}`, "", 403, true}, |
| 35 | + {"conflict", `["fix_ci"]`, http.MethodPut, `{"message":"enrollment changed"}`, "", 409, true}, |
| 36 | + {"invalid selection", `["fix_ci"]`, http.MethodPut, `{"message":"ineligible pull request"}`, "", 422, true}, |
| 37 | + {"unavailable", `["fix_ci"]`, http.MethodPut, `{"message":"eligibility unavailable"}`, "", 503, true}, |
| 38 | + {"disable absent", `[]`, http.MethodDelete, `{"message":"Not Found"}`, "", 404, true}, |
| 39 | + {"unexpected status", `[]`, http.MethodDelete, "", "", 200, true}, |
| 40 | + {"empty enrollment", `["fix_ci"]`, http.MethodPut, "", "", 201, true}, |
| 41 | + {"incomplete enrollment", `["fix_ci"]`, http.MethodPut, `{}`, "", 200, true}, |
| 42 | + {"malformed response", `["fix_ci"]`, http.MethodPut, `{`, "", 200, true}, |
| 43 | + } { |
| 44 | + t.Run(protocol+"/"+tc.name, func(t *testing.T) { |
| 45 | + calls := 0 |
| 46 | + deps := &BaseDeps{Client: mustNewGHClient(t, MockHTTPClientWithHandler(func(w http.ResponseWriter, r *http.Request) { |
| 47 | + calls++ |
| 48 | + assert.Equal(t, tc.method+" /agents/repos/octo/widgets/pulls/42/agent-merge", r.Method+" "+r.URL.Path) |
| 49 | + assert.Equal(t, "2026-03-10", r.Header.Get("X-GitHub-Api-Version")) |
| 50 | + if tc.method == http.MethodPut { |
| 51 | + body, err := io.ReadAll(r.Body) |
| 52 | + require.NoError(t, err) |
| 53 | + assert.JSONEq(t, `{"allowed_actions":`+tc.actions+`}`, string(body)) |
| 54 | + } else { |
| 55 | + assert.Nil(t, r.Body) |
| 56 | + } |
| 57 | + w.WriteHeader(tc.status) |
| 58 | + _, _ = fmt.Fprint(w, tc.response) |
| 59 | + }))} |
| 60 | + server := mcp.NewServer(&mcp.Implementation{Name: "agent-merge-test", Version: "1"}, nil) |
| 61 | + server.AddReceivingMiddleware(InjectDepsMiddleware(deps)) |
| 62 | + tool := GranularUpdatePullRequestAgentMergeEnrollment(translations.NullTranslationHelper) |
| 63 | + tool.RegisterFunc(server, deps) |
| 64 | + session := connectCommentVisibilityClient(t, server, protocol) |
| 65 | + result, err := session.CallTool(t.Context(), &mcp.CallToolParams{ |
| 66 | + Name: tool.Tool.Name, |
| 67 | + Arguments: json.RawMessage(`{"owner":"octo","repo":"widgets","pullNumber":42,"allowedActions":` + |
| 68 | + tc.actions + `}`), |
| 69 | + }) |
| 70 | + require.NoError(t, err) |
| 71 | + assert.Equal(t, 1, calls) |
| 72 | + require.Equal(t, tc.isError, result.IsError, "%+v", result.Content) |
| 73 | + if !tc.isError { |
| 74 | + assert.JSONEq(t, tc.expected, getTextResult(t, result).Text) |
| 75 | + if protocol == inventory.ProtocolVersionMultiRoundTrip { |
| 76 | + assert.JSONEq(t, tc.expected, mustMarshalJSON(t, result.StructuredContent)) |
| 77 | + } |
| 78 | + } |
| 79 | + request, err := deps.Client.NewRequest(t.Context(), http.MethodGet, "repos/octo/widgets", nil) |
| 80 | + require.NoError(t, err) |
| 81 | + assert.Equal(t, "2022-11-28", request.Header.Get("X-GitHub-Api-Version")) |
| 82 | + }) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestUpdatePullRequestAgentMergeEnrollmentValidation(t *testing.T) { |
| 88 | + tool := GranularUpdatePullRequestAgentMergeEnrollment(translations.NullTranslationHelper) |
| 89 | + calls := 0 |
| 90 | + deps := BaseDeps{Client: mustNewGHClient(t, MockHTTPClientWithHandler(func(http.ResponseWriter, *http.Request) { |
| 91 | + calls++ |
| 92 | + }))} |
| 93 | + for _, args := range []string{ |
| 94 | + `{"owner":"octo","repo":"widgets","pullNumber":42}`, |
| 95 | + `{"owner":"octo","repo":"widgets","pullNumber":42,"allowedActions":null}`, |
| 96 | + `{"owner":"octo","repo":"widgets","pullNumber":42,"allowedActions":["unknown"]}`, |
| 97 | + `{"owner":"octo","repo":"widgets","pullNumber":42,"allowedActions":["fix_ci","fix_ci"]}`, |
| 98 | + `{"owner":"octo","repo":"widgets","pullNumber":0,"allowedActions":[]}`, |
| 99 | + `{"owner":"octo","repo":"widgets","pullNumber":1.5,"allowedActions":[]}`, |
| 100 | + `{"owner":" ","repo":"widgets","pullNumber":42,"allowedActions":[]}`, |
| 101 | + `{"owner":"octo","repo":"","pullNumber":42,"allowedActions":[]}`, |
| 102 | + } { |
| 103 | + t.Run(args, func(t *testing.T) { |
| 104 | + var input map[string]any |
| 105 | + require.NoError(t, json.Unmarshal([]byte(args), &input)) |
| 106 | + request := createMCPRequest(input) |
| 107 | + result, err := tool.Handler(deps)(ContextWithDeps(t.Context(), deps), &request) |
| 108 | + if err == nil { |
| 109 | + require.NotNil(t, result) |
| 110 | + assert.True(t, result.IsError) |
| 111 | + } |
| 112 | + }) |
| 113 | + } |
| 114 | + assert.Zero(t, calls) |
| 115 | +} |
| 116 | + |
| 117 | +func TestUpdatePullRequestAgentMergeEnrollmentFeatureGate(t *testing.T) { |
| 118 | + for _, enabled := range []bool{false, true} { |
| 119 | + t.Run(fmt.Sprint(enabled), func(t *testing.T) { |
| 120 | + checker := featureCheckerFor() |
| 121 | + if enabled { |
| 122 | + checker = featureCheckerFor(inventory.FeatureFlag(FeatureFlagPullRequestsGranular)) |
| 123 | + } |
| 124 | + inv, err := NewInventory(translations.NullTranslationHelper).WithFeatureChecker(checker).Build() |
| 125 | + require.NoError(t, err) |
| 126 | + found := false |
| 127 | + for _, tool := range inv.AvailableTools(t.Context()) { |
| 128 | + found = found || tool.Tool.Name == "update_pull_request_agent_merge_enrollment" |
| 129 | + } |
| 130 | + assert.Equal(t, enabled, found) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments