@@ -414,14 +414,23 @@ If the SHA is not provided, the tool will attempt to acquire it by fetching the
414414 fileContent , resp , err := client .Repositories .CreateFile (ctx , owner , repo , path , opts )
415415 if err != nil {
416416 if strings .Contains (err .Error (), `"sha" wasn't supplied` ) && sha == "" {
417+ // Close the response from the initial failed CreateFile call
418+ if resp != nil {
419+ _ = resp .Body .Close ()
420+ }
421+
417422 // attempt to get the current file SHA by fetching the file contents
418423 getOpts := & github.RepositoryContentGetOptions {
419424 Ref : branch ,
420425 }
421426 currentFileContent , _ , respContents , err := client .Repositories .GetContents (ctx , owner , repo , path , getOpts )
422- defer func () { _ = respContents .Body .Close () }()
423427
424428 if err == nil {
429+ // Close the GetContents response before making the retry call
430+ if respContents != nil {
431+ _ = respContents .Body .Close ()
432+ }
433+
425434 if currentFileContent != nil && currentFileContent .SHA != nil {
426435 opts .SHA = currentFileContent .SHA
427436 fileContent , resp , err = client .Repositories .CreateFile (ctx , owner , repo , path , opts )
@@ -438,9 +447,13 @@ If the SHA is not provided, the tool will attempt to acquire it by fetching the
438447 return utils .NewToolResultError ("file content SHA is nil, cannot update the file" ), nil , nil
439448 }
440449 } else {
450+ // Close the GetContents response before returning error
451+ if respContents != nil {
452+ _ = respContents .Body .Close ()
453+ }
441454 return ghErrors .NewGitHubAPIErrorResponse (ctx ,
442455 "failed to get file SHA for update" ,
443- resp ,
456+ respContents ,
444457 err ,
445458 ), nil , nil
446459 }
0 commit comments