|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/github/github-mcp-server/pkg/translations" |
| 9 | + "github.com/google/go-github/v89/github" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_MergePullRequestSHAPinning(t *testing.T) { |
| 15 | + serverTool := MergePullRequest(translations.NullTranslationHelper) |
| 16 | + |
| 17 | + const pinnedSHA = "0123456789abcdef0123456789abcdef01234567" |
| 18 | + |
| 19 | + mockMergeResult := &github.PullRequestMergeResult{ |
| 20 | + SHA: github.Ptr("merge-result-sha"), |
| 21 | + Merged: github.Ptr(true), |
| 22 | + Message: github.Ptr("Pull Request successfully merged"), |
| 23 | + } |
| 24 | + |
| 25 | + t.Run("expected head SHA is sent as REST sha", func(t *testing.T) { |
| 26 | + client := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 27 | + PutReposPullsMergeByOwnerByRepoByPullNumber: expectRequestBody(t, map[string]any{ |
| 28 | + "merge_method": "merge", |
| 29 | + "sha": pinnedSHA, |
| 30 | + }).andThen( |
| 31 | + mockResponse(t, http.StatusOK, mockMergeResult), |
| 32 | + ), |
| 33 | + })) |
| 34 | + |
| 35 | + deps := BaseDeps{Client: client} |
| 36 | + handler := serverTool.Handler(deps) |
| 37 | + |
| 38 | + request := createMCPRequest(map[string]any{ |
| 39 | + "owner": "owner", |
| 40 | + "repo": "repo", |
| 41 | + "pullNumber": float64(42), |
| 42 | + "merge_method": "merge", |
| 43 | + "expectedHeadSha": pinnedSHA, |
| 44 | + }) |
| 45 | + |
| 46 | + result, err := handler( |
| 47 | + ContextWithDeps(context.Background(), deps), |
| 48 | + &request, |
| 49 | + ) |
| 50 | + |
| 51 | + require.NoError(t, err) |
| 52 | + require.False(t, result.IsError) |
| 53 | + }) |
| 54 | + |
| 55 | + t.Run("head mismatch fails closed without retry", func(t *testing.T) { |
| 56 | + calls := 0 |
| 57 | + |
| 58 | + client := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ |
| 59 | + PutReposPullsMergeByOwnerByRepoByPullNumber: expectRequestBody(t, map[string]any{ |
| 60 | + "merge_method": "merge", |
| 61 | + "sha": pinnedSHA, |
| 62 | + }).andThen(func(w http.ResponseWriter, _ *http.Request) { |
| 63 | + calls++ |
| 64 | + w.Header().Set("Content-Type", "application/json") |
| 65 | + w.WriteHeader(http.StatusConflict) |
| 66 | + _, _ = w.Write([]byte( |
| 67 | + `{"message":"Head branch was modified. Review and try the merge again."}`, |
| 68 | + )) |
| 69 | + }), |
| 70 | + })) |
| 71 | + |
| 72 | + deps := BaseDeps{Client: client} |
| 73 | + handler := serverTool.Handler(deps) |
| 74 | + |
| 75 | + request := createMCPRequest(map[string]any{ |
| 76 | + "owner": "owner", |
| 77 | + "repo": "repo", |
| 78 | + "pullNumber": float64(42), |
| 79 | + "merge_method": "merge", |
| 80 | + "expectedHeadSha": pinnedSHA, |
| 81 | + }) |
| 82 | + |
| 83 | + result, err := handler( |
| 84 | + ContextWithDeps(context.Background(), deps), |
| 85 | + &request, |
| 86 | + ) |
| 87 | + |
| 88 | + require.NoError(t, err) |
| 89 | + require.True(t, result.IsError) |
| 90 | + assert.Contains(t, getErrorResult(t, result).Text, "Head branch was modified") |
| 91 | + assert.Equal(t, 1, calls, "merge must not retry after a head-SHA mismatch") |
| 92 | + }) |
| 93 | +} |
0 commit comments