refactor(github): extract doRequestCore, address review feedback
- MAJOR: Extract doRequestCore to eliminate doRequest/doRequestWithBody duplication. Both now delegate to a shared implementation with the retry/backoff logic in a single place. - MINOR: Replace custom containsStr/containsSubstring helpers with strings.Contains in review_test.go. - MINOR: Use http.Method* constants (MethodPost, MethodDelete, MethodPut) in review.go for consistency with doGet. - MINOR: Remove redundant APPROVED/DISMISSED cases from translateGitHubReviewState that were identical to the default passthrough. - NIT: Clarify DeleteReview comment about COMMENTED being a GitHub API state name. - DismissReview Event field verified as required by GitHub API docs; kept as-is.
This commit is contained in:
+8
-8
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
||||
"gitea.weiker.me/rodin/review-bot/vcs"
|
||||
@@ -52,15 +53,13 @@ type dismissReviewRequest struct {
|
||||
// canonical vcs.Review.State value.
|
||||
func translateGitHubReviewState(state string) string {
|
||||
switch state {
|
||||
case "APPROVED":
|
||||
return "APPROVED"
|
||||
case "CHANGES_REQUESTED":
|
||||
return "REQUEST_CHANGES"
|
||||
case "COMMENTED":
|
||||
return "COMMENT"
|
||||
case "DISMISSED":
|
||||
return "DISMISSED"
|
||||
default:
|
||||
// States like APPROVED, DISMISSED, and PENDING pass through unchanged
|
||||
// as they already match the canonical vcs representation.
|
||||
return state
|
||||
}
|
||||
}
|
||||
@@ -100,7 +99,7 @@ func (c *Client) PostReview(ctx context.Context, owner, repo string, number int,
|
||||
return nil, fmt.Errorf("marshal review request: %w", err)
|
||||
}
|
||||
|
||||
body, err := c.doRequestWithBody(ctx, "POST", reqURL, data)
|
||||
body, err := c.doRequestWithBody(ctx, http.MethodPost, reqURL, data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("post review: %w", err)
|
||||
}
|
||||
@@ -150,12 +149,13 @@ func (c *Client) ListReviews(ctx context.Context, owner, repo string, number int
|
||||
|
||||
// DeleteReview deletes a pull request review.
|
||||
// Only PENDING reviews can be deleted; attempting to delete a submitted review
|
||||
// (APPROVED, CHANGES_REQUESTED, COMMENTED) returns ErrCannotDeleteSubmittedReview.
|
||||
// (APPROVED, CHANGES_REQUESTED, or COMMENTED per GitHub API naming) returns
|
||||
// ErrCannotDeleteSubmittedReview.
|
||||
func (c *Client) DeleteReview(ctx context.Context, owner, repo string, number int, reviewID int64) error {
|
||||
reqURL := fmt.Sprintf("%s/repos/%s/%s/pulls/%d/reviews/%d",
|
||||
c.baseURL, url.PathEscape(owner), url.PathEscape(repo), number, reviewID)
|
||||
|
||||
_, err := c.doRequestWithBody(ctx, "DELETE", reqURL, nil)
|
||||
_, err := c.doRequestWithBody(ctx, http.MethodDelete, reqURL, nil)
|
||||
if err != nil {
|
||||
var apiErr *APIError
|
||||
if errors.As(err, &apiErr) && apiErr.StatusCode == 422 {
|
||||
@@ -183,7 +183,7 @@ func (c *Client) DismissReview(ctx context.Context, owner, repo string, number i
|
||||
return fmt.Errorf("marshal dismiss request: %w", err)
|
||||
}
|
||||
|
||||
_, err = c.doRequestWithBody(ctx, "PUT", reqURL, data)
|
||||
_, err = c.doRequestWithBody(ctx, http.MethodPut, reqURL, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dismiss review: %w", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user