forked from github/github-mcp-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissues_delete_test.go
More file actions
439 lines (401 loc) · 13.3 KB
/
Copy pathissues_delete_test.go
File metadata and controls
439 lines (401 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package github
import (
"context"
"encoding/json"
"io"
"net/http"
"strings"
"sync"
"testing"
"github.com/github/github-mcp-server/internal/githubv4mock"
gogithub "github.com/google/go-github/v89/github"
"github.com/shurcooL/githubv4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Test_IssueRequest_EmptyFieldValues_OmittedByJSON pins the omitempty
// behaviour that makes the DELETE fallback necessary. If go-github ever drops
// the tag, the REST PATCH alone could clear field values and this test would
// fail to remind us.
func Test_IssueRequest_EmptyFieldValues_OmittedByJSON(t *testing.T) {
t.Parallel()
req := &gogithub.UpdateIssueRequest{
Title: gogithub.Ptr("still here"),
IssueFieldValues: []*gogithub.IssueRequestFieldValue{},
}
body, err := json.Marshal(req)
require.NoError(t, err)
assert.NotContains(t, string(body), "issue_field_values",
"empty IssueFieldValues should be dropped by omitempty — this is why the REST PATCH alone can't clear field values when the merged list ends up empty, and why we fall back to the dedicated DELETE endpoint")
assert.Contains(t, string(body), `"title":"still here"`,
"sanity check: other fields still serialise")
}
// Test_UpdateIssue_DeleteLastFieldValueCallsDeleteEndpoint covers the bug fix:
// when the kept set ends up empty, the PATCH alone can't clear the field
// (omitempty strips the empty slice), so UpdateIssue follows up with a DELETE
// to the dedicated endpoint.
func Test_UpdateIssue_DeleteLastFieldValueCallsDeleteEndpoint(t *testing.T) {
t.Parallel()
mockIssue := &gogithub.Issue{
Number: gogithub.Ptr(42),
Title: gogithub.Ptr("Test issue"),
State: gogithub.Ptr("open"),
HTMLURL: gogithub.Ptr("https://github.com/owner/repo/issues/42"),
}
var (
mu sync.Mutex
capturedPatchBody []byte
deletePaths []string
)
restClient := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
PatchReposIssuesByOwnerByRepoByIssueNumber: func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
mu.Lock()
capturedPatchBody = body
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(mockIssue)
},
DeleteReposIssuesIssueFieldValueByOwnerByRepoByIssueNumber: func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
deletePaths = append(deletePaths, r.URL.Path)
mu.Unlock()
w.WriteHeader(http.StatusNoContent)
},
}))
// Existing field values for the merge step. Returning the field we're
// about to delete makes the kept list empty, triggering the fallback DELETE.
existingFieldsResponse := githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"issue": map[string]any{
"issueFieldValues": map[string]any{
"nodes": []any{
map[string]any{
"__typename": "IssueFieldSingleSelectValue",
"field": map[string]any{
"fullDatabaseId": "101",
"name": "Priority",
},
"value": "P1",
},
},
},
},
},
})
gqlClient := githubv4.NewClient(githubv4mock.NewMockedHTTPClient(
githubv4mock.NewQueryMatcher(
struct {
Repository struct {
Issue struct {
IssueFieldValues struct {
Nodes []IssueFieldValueFragment
} `graphql:"issueFieldValues(first: 25)"`
} `graphql:"issue(number: $number)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}{},
map[string]any{
"owner": githubv4.String("owner"),
"repo": githubv4.String("repo"),
"number": githubv4.Int(42),
},
existingFieldsResponse,
),
))
result, err := UpdateIssue(
context.Background(),
restClient,
gqlClient,
"owner", "repo", 42,
"", "", nil, nil, 0, "",
nil,
[]int64{101},
"", "", 0,
)
require.NoError(t, err)
if result.IsError {
t.Fatalf("expected non-error result, got: %s", getTextResult(t, result).Text)
}
mu.Lock()
defer mu.Unlock()
require.NotContains(t, string(capturedPatchBody), "issue_field_values",
"REST PATCH body must not carry issue_field_values when the kept set is empty (PATCH body was: %s)", string(capturedPatchBody))
require.Equal(t, []string{"/repos/owner/repo/issues/42/issue-field-values/101"}, deletePaths,
"expected exactly one DELETE call to the dedicated endpoint for field id 101")
}
// Test_UpdateIssue_DeleteOneOfManyUsesSetSemantics: when the kept set is
// non-empty, set semantics handle the deletion implicitly via the PATCH — no
// DELETE follow-up needed.
func Test_UpdateIssue_DeleteOneOfManyUsesSetSemantics(t *testing.T) {
t.Parallel()
mockIssue := &gogithub.Issue{
Number: gogithub.Ptr(42),
Title: gogithub.Ptr("Test issue"),
State: gogithub.Ptr("open"),
HTMLURL: gogithub.Ptr("https://github.com/owner/repo/issues/42"),
}
var (
mu sync.Mutex
deletePaths []string
)
restClient := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
PatchReposIssuesByOwnerByRepoByIssueNumber: expectRequestBody(t, map[string]any{
"issue_field_values": []any{
map[string]any{"field_id": float64(202), "value": "High"},
},
}).andThen(
mockResponse(t, http.StatusOK, mockIssue),
),
DeleteReposIssuesIssueFieldValueByOwnerByRepoByIssueNumber: func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
deletePaths = append(deletePaths, r.URL.Path)
mu.Unlock()
w.WriteHeader(http.StatusNoContent)
},
}))
existingFieldsResponse := githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"issue": map[string]any{
"issueFieldValues": map[string]any{
"nodes": []any{
map[string]any{
"__typename": "IssueFieldSingleSelectValue",
"field": map[string]any{"fullDatabaseId": "101", "name": "Priority"},
"value": "P1",
},
map[string]any{
"__typename": "IssueFieldSingleSelectValue",
"field": map[string]any{"fullDatabaseId": "202", "name": "Impact"},
"value": "High",
},
},
},
},
},
})
gqlClient := githubv4.NewClient(githubv4mock.NewMockedHTTPClient(
githubv4mock.NewQueryMatcher(
struct {
Repository struct {
Issue struct {
IssueFieldValues struct {
Nodes []IssueFieldValueFragment
} `graphql:"issueFieldValues(first: 25)"`
} `graphql:"issue(number: $number)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}{},
map[string]any{
"owner": githubv4.String("owner"),
"repo": githubv4.String("repo"),
"number": githubv4.Int(42),
},
existingFieldsResponse,
),
))
result, err := UpdateIssue(
context.Background(),
restClient,
gqlClient,
"owner", "repo", 42,
"", "", nil, nil, 0, "",
nil,
[]int64{101},
"", "", 0,
)
require.NoError(t, err)
if result.IsError {
t.Fatalf("expected non-error result, got: %s", getTextResult(t, result).Text)
}
mu.Lock()
defer mu.Unlock()
require.Empty(t, deletePaths,
"no DELETE call should fire when the kept set is non-empty — the PATCH's set semantics clear the deleted field on the server side")
}
// Test_UpdateIssue_DeleteAbsentFieldIsNoOp: deleting a field that isn't set
// must not fire a DELETE (the endpoint would 404), preserving the pre-fix
// silent-no-op behaviour so idempotent delete:true callers don't break on
// retry.
func Test_UpdateIssue_DeleteAbsentFieldIsNoOp(t *testing.T) {
t.Parallel()
mockIssue := &gogithub.Issue{
Number: gogithub.Ptr(42),
Title: gogithub.Ptr("Test issue"),
State: gogithub.Ptr("open"),
HTMLURL: gogithub.Ptr("https://github.com/owner/repo/issues/42"),
}
var (
mu sync.Mutex
capturedPatchBody []byte
deletePaths []string
)
restClient := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
PatchReposIssuesByOwnerByRepoByIssueNumber: func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
mu.Lock()
capturedPatchBody = body
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(mockIssue)
},
DeleteReposIssuesIssueFieldValueByOwnerByRepoByIssueNumber: func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
deletePaths = append(deletePaths, r.URL.Path)
mu.Unlock()
// Fail loudly: if we get here, the fix is wrong.
w.WriteHeader(http.StatusNotFound)
},
}))
// Issue has no field values at all.
existingFieldsResponse := githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"issue": map[string]any{
"issueFieldValues": map[string]any{
"nodes": []any{},
},
},
},
})
gqlClient := githubv4.NewClient(githubv4mock.NewMockedHTTPClient(
githubv4mock.NewQueryMatcher(
struct {
Repository struct {
Issue struct {
IssueFieldValues struct {
Nodes []IssueFieldValueFragment
} `graphql:"issueFieldValues(first: 25)"`
} `graphql:"issue(number: $number)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}{},
map[string]any{
"owner": githubv4.String("owner"),
"repo": githubv4.String("repo"),
"number": githubv4.Int(42),
},
existingFieldsResponse,
),
))
result, err := UpdateIssue(
context.Background(),
restClient,
gqlClient,
"owner", "repo", 42,
"", "", nil, nil, 0, "",
nil,
[]int64{101}, // ask to delete a field that isn't set
"", "", 0,
)
require.NoError(t, err)
if result.IsError {
t.Fatalf("expected non-error result, got: %s", getTextResult(t, result).Text)
}
mu.Lock()
defer mu.Unlock()
require.NotContains(t, string(capturedPatchBody), "issue_field_values",
"PATCH body must not carry issue_field_values when nothing changed")
require.Empty(t, deletePaths,
"no DELETE call should fire for a field that isn't present on the issue — preserves the pre-fix silent-no-op behaviour and avoids a guaranteed 404")
}
// Test_UpdateIssue_DeleteFallbackContinuesOnPartialFailure: a failing DELETE
// must not short-circuit subsequent ones, and the error must name which IDs
// succeeded and which failed so callers can retry the right ones.
func Test_UpdateIssue_DeleteFallbackContinuesOnPartialFailure(t *testing.T) {
t.Parallel()
mockIssue := &gogithub.Issue{
Number: gogithub.Ptr(42),
Title: gogithub.Ptr("Test issue"),
State: gogithub.Ptr("open"),
HTMLURL: gogithub.Ptr("https://github.com/owner/repo/issues/42"),
}
var (
mu sync.Mutex
deletePaths []string
)
restClient := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
PatchReposIssuesByOwnerByRepoByIssueNumber: func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(mockIssue)
},
DeleteReposIssuesIssueFieldValueByOwnerByRepoByIssueNumber: func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
deletePaths = append(deletePaths, r.URL.Path)
mu.Unlock()
// Field 202 fails; 101 and 303 succeed. All three should fire and
// the error must name 202 as failed and 101/303 as cleared.
if strings.HasSuffix(r.URL.Path, "/202") {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`{"message":"simulated failure"}`))
return
}
w.WriteHeader(http.StatusNoContent)
},
}))
existingFieldsResponse := githubv4mock.DataResponse(map[string]any{
"repository": map[string]any{
"issue": map[string]any{
"issueFieldValues": map[string]any{
"nodes": []any{
map[string]any{
"__typename": "IssueFieldSingleSelectValue",
"field": map[string]any{"fullDatabaseId": "101", "name": "Priority"},
"value": "P1",
},
map[string]any{
"__typename": "IssueFieldSingleSelectValue",
"field": map[string]any{"fullDatabaseId": "202", "name": "Visibility"},
"value": "High",
},
map[string]any{
"__typename": "IssueFieldSingleSelectValue",
"field": map[string]any{"fullDatabaseId": "303", "name": "Impact"},
"value": "Critical",
},
},
},
},
},
})
gqlClient := githubv4.NewClient(githubv4mock.NewMockedHTTPClient(
githubv4mock.NewQueryMatcher(
struct {
Repository struct {
Issue struct {
IssueFieldValues struct {
Nodes []IssueFieldValueFragment
} `graphql:"issueFieldValues(first: 25)"`
} `graphql:"issue(number: $number)"`
} `graphql:"repository(owner: $owner, name: $repo)"`
}{},
map[string]any{
"owner": githubv4.String("owner"),
"repo": githubv4.String("repo"),
"number": githubv4.Int(42),
},
existingFieldsResponse,
),
))
result, err := UpdateIssue(
context.Background(),
restClient,
gqlClient,
"owner", "repo", 42,
"", "", nil, nil, 0, "",
nil,
[]int64{101, 202, 303},
"", "", 0,
)
require.NoError(t, err)
require.True(t, result.IsError, "expected an error result because field 202 failed")
mu.Lock()
defer mu.Unlock()
// All three DELETEs must have fired — the middle failure must not short-circuit the third.
require.Len(t, deletePaths, 3,
"all three DELETE calls should fire even though one fails; got paths: %v", deletePaths)
resultText := getTextResult(t, result).Text
require.Contains(t, resultText, "failed=[202]",
"error must name the failed field ID so the caller can retry it; got: %s", resultText)
require.Contains(t, resultText, "cleared=[101 303]",
"error must name the cleared field IDs so the caller knows what's already done; got: %s", resultText)
}