reviews.go
·
55 lines
·
1.5 KB
| 1 | package handlers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/gofiber/fiber/v2" |
| 9 | |
| 10 | "polar/internal/web/middleware" |
| 11 | ) |
| 12 | |
| 13 | // RepoPullReviewSubmit records the reviewer's verdict (approve / request changes). |
| 14 | func (h *Handler) RepoPullReviewSubmit(c *fiber.Ctx) error { |
| 15 | r, _, err := h.loadAuthorizedRepo(c, false) |
| 16 | if err != nil { |
| 17 | return nil |
| 18 | } |
| 19 | if !r.EnablePulls { |
| 20 | return h.renderErrorT(c, fiber.StatusNotFound, "err.not_found", "err.pulls_disabled") |
| 21 | } |
| 22 | |
| 23 | num, _ := strconv.Atoi(c.Params("number")) |
| 24 | pr, err := h.repoSvc.GetPullRequest(c.UserContext(), r.ID, num) |
| 25 | if err != nil { |
| 26 | return h.renderErrorT(c, fiber.StatusNotFound, "err.pr_not_found", "err.pr_not_found_desc") |
| 27 | } |
| 28 | |
| 29 | u := middleware.CurrentUser(c) |
| 30 | redirect := fmt.Sprintf("/%s/%s/pulls/%d", r.OwnerUsername, r.Name, pr.Number) |
| 31 | |
| 32 | if u == nil || u.ID == pr.AuthorID { |
| 33 | h.setFlashT(c, "flash.review_self") |
| 34 | return c.Redirect(redirect, fiber.StatusSeeOther) |
| 35 | } |
| 36 | if pr.Status != "open" { |
| 37 | h.setFlashT(c, "flash.review_closed") |
| 38 | return c.Redirect(redirect, fiber.StatusSeeOther) |
| 39 | } |
| 40 | |
| 41 | state := strings.TrimSpace(c.FormValue("state")) |
| 42 | body := strings.TrimSpace(c.FormValue("body")) |
| 43 | |
| 44 | if err := h.repoSvc.UpsertReview(c.UserContext(), pr.ID, u.ID, state, body); err != nil { |
| 45 | h.setFlashT(c, "flash.review_failed", err.Error()) |
| 46 | return c.Redirect(redirect, fiber.StatusSeeOther) |
| 47 | } |
| 48 | |
| 49 | if state == "approved" { |
| 50 | h.setFlashT(c, "flash.review_approved") |
| 51 | } else { |
| 52 | h.setFlashT(c, "flash.review_changes_requested") |
| 53 | } |
| 54 | return c.Redirect(redirect, fiber.StatusSeeOther) |
| 55 | } |
| 56 |