Skip to content

Commit b8f0b13

Browse files
Address code review feedback: simplify error checking and fix logic
Co-authored-by: SamMorrowDrums <4811358+SamMorrowDrums@users.noreply.github.com>
1 parent fd50a3e commit b8f0b13

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

pkg/github/issues.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -834,18 +834,12 @@ func AddSubIssue(ctx context.Context, client *github.Client, owner string, repo
834834

835835
// Check if this is a retryable priority conflict error
836836
shouldRetry := false
837-
if resp != nil && resp.StatusCode == http.StatusUnprocessableEntity {
837+
if resp != nil && resp.StatusCode == http.StatusUnprocessableEntity && resp.Body != nil {
838838
// Read the body to check for priority conflict
839-
if resp.Body != nil {
840-
body, readErr := io.ReadAll(resp.Body)
841-
_ = resp.Body.Close()
842-
if readErr == nil {
843-
bodyStr := string(body)
844-
if strings.Contains(bodyStr, "Priority has already been taken") ||
845-
(err != nil && strings.Contains(err.Error(), "Priority has already been taken")) {
846-
shouldRetry = true
847-
}
848-
}
839+
body, readErr := io.ReadAll(resp.Body)
840+
_ = resp.Body.Close()
841+
if readErr == nil && strings.Contains(string(body), "Priority has already been taken") {
842+
shouldRetry = true
849843
}
850844
}
851845

@@ -864,15 +858,14 @@ func AddSubIssue(ctx context.Context, client *github.Client, owner string, repo
864858
), nil
865859
}
866860

867-
if lastResp != nil {
861+
// Handle non-201 status codes after retries exhausted
862+
if lastResp != nil && lastResp.StatusCode != http.StatusCreated {
868863
defer func() { _ = lastResp.Body.Close() }()
869-
if lastResp.StatusCode != http.StatusCreated {
870-
body, err := io.ReadAll(lastResp.Body)
871-
if err != nil {
872-
return nil, fmt.Errorf("failed to read response body: %w", err)
873-
}
874-
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to add sub-issue", lastResp, body), nil
864+
body, err := io.ReadAll(lastResp.Body)
865+
if err != nil {
866+
return nil, fmt.Errorf("failed to read response body: %w", err)
875867
}
868+
return ghErrors.NewGitHubAPIStatusErrorResponse(ctx, "failed to add sub-issue", lastResp, body), nil
876869
}
877870

878871
return nil, fmt.Errorf("failed to add sub-issue after %d retries", maxRetries)

0 commit comments

Comments
 (0)