@@ -809,6 +809,7 @@ func AddSubIssue(ctx context.Context, client *github.Client, owner string, repo
809809
810810 var lastResp * github.Response
811811 var lastErr error
812+ var lastBody []byte
812813
813814 for attempt := 0 ; attempt < maxRetries ; attempt ++ {
814815 if attempt > 0 {
@@ -837,8 +838,11 @@ func AddSubIssue(ctx context.Context, client *github.Client, owner string, repo
837838 // Read the body to check for priority conflict
838839 body , readErr := io .ReadAll (resp .Body )
839840 _ = resp .Body .Close ()
840- if readErr == nil && strings .Contains (string (body ), "Priority has already been taken" ) {
841- shouldRetry = true
841+ if readErr == nil {
842+ lastBody = body
843+ if strings .Contains (string (body ), "Priority has already been taken" ) {
844+ shouldRetry = true
845+ }
842846 }
843847 }
844848
@@ -859,12 +863,19 @@ func AddSubIssue(ctx context.Context, client *github.Client, owner string, repo
859863
860864 // Handle non-201 status codes after retries exhausted
861865 if lastResp != nil && lastResp .StatusCode != http .StatusCreated {
862- defer func () { _ = lastResp .Body .Close () }()
863- body , err := io .ReadAll (lastResp .Body )
864- if err != nil {
865- return nil , fmt .Errorf ("failed to read response body: %w" , err )
866+ // Use the body we already read during retry attempts if available
867+ if len (lastBody ) > 0 {
868+ return ghErrors .NewGitHubAPIStatusErrorResponse (ctx , "failed to add sub-issue" , lastResp , lastBody ), nil
869+ }
870+ // Otherwise try to read the body if it's still available
871+ if lastResp .Body != nil {
872+ body , err := io .ReadAll (lastResp .Body )
873+ _ = lastResp .Body .Close ()
874+ if err != nil {
875+ return nil , fmt .Errorf ("failed to read response body: %w" , err )
876+ }
877+ return ghErrors .NewGitHubAPIStatusErrorResponse (ctx , "failed to add sub-issue" , lastResp , body ), nil
866878 }
867- return ghErrors .NewGitHubAPIStatusErrorResponse (ctx , "failed to add sub-issue" , lastResp , body ), nil
868879 }
869880
870881 // This should not be reached in normal operation
0 commit comments