Skip to content

Commit 5b39370

Browse files
tommaso-moroCopilot
andcommitted
Don't clear gist description on update when omitted
update_gist always sent Description as a pointer to the OptionalParam zero value (""), so omitting description would overwrite an existing gist description with an empty string. Only set UpdateGistRequest.Description when the caller actually provided the argument; an explicit empty string still clears it. Adds a test asserting the description key is absent from the PATCH body when omitted and present when set. This addresses a pre-existing behavior surfaced while migrating to the v89 gist request types. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 75e19ee commit 5b39370

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

pkg/github/gists.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,17 @@ func UpdateGist(t translations.TranslationHelperFunc) inventory.ServerTool {
331331
Content: github.Ptr(content),
332332
}
333333

334+
// Only set Description when the caller actually provided it, so
335+
// omitting it preserves the gist's existing description. Passing an
336+
// explicit empty string still clears it.
337+
var descriptionPtr *string
338+
if _, ok := args["description"]; ok {
339+
descriptionPtr = github.Ptr(description)
340+
}
341+
334342
gist := github.UpdateGistRequest{
335343
Files: files,
336-
Description: github.Ptr(description),
344+
Description: descriptionPtr,
337345
}
338346

339347
client, err := deps.GetClient(ctx)

pkg/github/gists_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,66 @@ func Test_UpdateGist(t *testing.T) {
581581
})
582582
}
583583
}
584+
585+
func Test_UpdateGist_DescriptionOnlySentWhenProvided(t *testing.T) {
586+
serverTool := UpdateGist(translations.NullTranslationHelper)
587+
588+
updatedGist := &github.Gist{
589+
ID: github.Ptr("existing-gist-id"),
590+
HTMLURL: github.Ptr("https://gist.github.com/user/existing-gist-id"),
591+
}
592+
593+
cases := []struct {
594+
name string
595+
args map[string]any
596+
wantDescPresent bool
597+
wantDescValue string
598+
}{
599+
{
600+
name: "omitted description is not sent, preserving the existing one",
601+
args: map[string]any{
602+
"gist_id": "existing-gist-id",
603+
"filename": "updated.go",
604+
"content": "package main",
605+
},
606+
wantDescPresent: false,
607+
},
608+
{
609+
name: "explicit empty description is sent to clear it",
610+
args: map[string]any{
611+
"gist_id": "existing-gist-id",
612+
"filename": "updated.go",
613+
"content": "package main",
614+
"description": "",
615+
},
616+
wantDescPresent: true,
617+
wantDescValue: "",
618+
},
619+
}
620+
621+
for _, tc := range cases {
622+
t.Run(tc.name, func(t *testing.T) {
623+
var gotBody map[string]any
624+
client := mustNewGHClient(t, MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
625+
PatchGistsByGistID: func(w http.ResponseWriter, r *http.Request) {
626+
require.NoError(t, json.NewDecoder(r.Body).Decode(&gotBody))
627+
w.WriteHeader(http.StatusOK)
628+
_, _ = w.Write(MustMarshal(updatedGist))
629+
},
630+
}))
631+
deps := BaseDeps{Client: client}
632+
handler := serverTool.Handler(deps)
633+
request := createMCPRequest(tc.args)
634+
635+
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
636+
require.NoError(t, err)
637+
require.False(t, result.IsError)
638+
639+
desc, present := gotBody["description"]
640+
assert.Equal(t, tc.wantDescPresent, present, "description key presence in PATCH body")
641+
if tc.wantDescPresent {
642+
assert.Equal(t, tc.wantDescValue, desc)
643+
}
644+
})
645+
}
646+
}

0 commit comments

Comments
 (0)