| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | "gitlab.com/Timfa/bluegit/internal/store" |
| 10 | ) |
| 11 | |
| 12 | // ApprovalView is de goedkeuringsregel zoals de instellingen hem tonen. |
| 13 | type ApprovalView struct { |
| 14 | MaintainerRequired bool |
| 15 | RequiredCount int |
| 16 | DevelopersApprove bool |
| 17 | Locked bool |
| 18 | GroupPath string |
| 19 | Maintainers int |
| 20 | Warn bool |
| 21 | WarnText string |
| 22 | } |
| 23 | |
| 24 | // ReviewRow is één reviewer op de merge request. |
| 25 | type ReviewRow struct { |
| 26 | UserID int64 |
| 27 | Username string |
| 28 | Status string |
| 29 | Label string |
| 30 | } |
| 31 | |
| 32 | func (s *Server) fillApproval(r *http.Request, v *View, repo store.Repo) { |
| 33 | pol, groupPath, err := s.Store.EffectiveApproval(r.Context(), repo) |
| 34 | if err != nil { |
| 35 | return |
| 36 | } |
| 37 | n, _ := s.Store.ProjectMaintainerCount(r.Context(), repo) |
| 38 | v.Approval = approvalView(pol, groupPath != "", groupPath, n) |
| 39 | s.paintApprovalWarn(r, v.Approval) |
| 40 | } |
| 41 | |
| 42 | func (s *Server) fillGroupApproval(r *http.Request, v *View, g store.Group) { |
| 43 | pol, err := s.Store.GetApproval(r.Context(), "group", g.ID) |
| 44 | if err != nil { |
| 45 | return |
| 46 | } |
| 47 | n, _ := s.Store.GroupMaintainerCount(r.Context(), g.ID) |
| 48 | v.Approval = approvalView(pol, false, "", n) |
| 49 | s.paintApprovalWarn(r, v.Approval) |
| 50 | } |
| 51 | |
| 52 | func approvalView(pol store.ApprovalPolicy, locked bool, groupPath string, maintainers int) *ApprovalView { |
| 53 | if pol.RequiredCount < 1 { |
| 54 | pol.RequiredCount = 1 |
| 55 | } |
| 56 | return &ApprovalView{ |
| 57 | MaintainerRequired: pol.MaintainerRequired, |
| 58 | RequiredCount: pol.RequiredCount, |
| 59 | DevelopersApprove: pol.DevelopersApprove, |
| 60 | Locked: locked, |
| 61 | GroupPath: groupPath, |
| 62 | Maintainers: maintainers, |
| 63 | Warn: pol.MaintainerRequired && pol.RequiredCount > maintainers, |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (s *Server) paintApprovalWarn(r *http.Request, a *ApprovalView) { |
| 68 | if a == nil || !a.Warn { |
| 69 | return |
| 70 | } |
| 71 | a.WarnText = s.tr(r, approvalWarnKey(a.RequiredCount, a.Maintainers), a.RequiredCount, a.Maintainers) |
| 72 | } |
| 73 | |
| 74 | func approvalWarnKey(required, maintainers int) string { |
| 75 | req, who := "many", "many" |
| 76 | if required == 1 { |
| 77 | req = "one" |
| 78 | } |
| 79 | if maintainers == 1 { |
| 80 | who = "one" |
| 81 | } |
| 82 | return "appr.warn." + req + "." + who |
| 83 | } |
| 84 | |
| 85 | func (s *Server) saveRepoApproval(w http.ResponseWriter, r *http.Request) { |
| 86 | _, repo, _, ok := s.guard(w, r, access.RoleMaintainer) |
| 87 | if !ok || !s.csrfOK(w, r) { |
| 88 | return |
| 89 | } |
| 90 | _, groupPath, err := s.Store.EffectiveApproval(r.Context(), repo) |
| 91 | if err != nil { |
| 92 | s.redirect(w, r, repoURL(repo)+"/-/settings?tab=approvals", "", s.trErr(r, err)) |
| 93 | return |
| 94 | } |
| 95 | if groupPath != "" { |
| 96 | s.redirect(w, r, repoURL(repo)+"/-/settings?tab=approvals", "", s.tr(r, "appr.locked")) |
| 97 | return |
| 98 | } |
| 99 | if err := s.Store.SaveApproval(r.Context(), "repo", repo.ID, approvalFromForm(r)); err != nil { |
| 100 | s.redirect(w, r, repoURL(repo)+"/-/settings?tab=approvals", "", s.trErr(r, err)) |
| 101 | return |
| 102 | } |
| 103 | s.redirect(w, r, repoURL(repo)+"/-/settings?tab=approvals", s.tr(r, "flash.saved"), "") |
| 104 | } |
| 105 | |
| 106 | func (s *Server) saveGroupApproval(w http.ResponseWriter, r *http.Request) { |
| 107 | g, _, ok := s.guardGroup(w, r, access.RoleMaintainer) |
| 108 | if !ok || !s.csrfOK(w, r) { |
| 109 | return |
| 110 | } |
| 111 | if err := s.Store.SaveApproval(r.Context(), "group", g.ID, approvalFromForm(r)); err != nil { |
| 112 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", "", s.trErr(r, err)) |
| 113 | return |
| 114 | } |
| 115 | s.redirect(w, r, "/groups/"+g.Path+"?tab=settings", s.tr(r, "flash.saved"), "") |
| 116 | } |
| 117 | |
| 118 | func approvalFromForm(r *http.Request) store.ApprovalPolicy { |
| 119 | n, _ := strconv.Atoi(r.FormValue("required_count")) |
| 120 | return store.ApprovalPolicy{ |
| 121 | MaintainerRequired: r.FormValue("maintainer_required") == "1", |
| 122 | RequiredCount: n, |
| 123 | DevelopersApprove: r.FormValue("developers_approve") == "1", |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | func (s *Server) reviewBlocks(r *http.Request, repo store.Repo, mr store.MergeRequest) []string { |
| 128 | if mr.Status != "open" { |
| 129 | return nil |
| 130 | } |
| 131 | pol, _, err := s.Store.EffectiveApproval(r.Context(), repo) |
| 132 | if err != nil { |
| 133 | return nil |
| 134 | } |
| 135 | var out []string |
| 136 | if pol.MaintainerRequired { |
| 137 | n := s.countApprovals(r.Context(), repo, mr, pol) |
| 138 | if n < pol.RequiredCount { |
| 139 | key := "rev.summary.approvals" |
| 140 | if pol.RequiredCount == 1 { |
| 141 | key = "rev.summary.approvals.one" |
| 142 | } |
| 143 | out = append(out, s.tr(r, key, n, pol.RequiredCount)) |
| 144 | } |
| 145 | } |
| 146 | reviews, err := s.Store.ListReviews(r.Context(), mr.ID) |
| 147 | if err != nil { |
| 148 | return out |
| 149 | } |
| 150 | for _, rev := range reviews { |
| 151 | switch rev.Status { |
| 152 | case "changes": |
| 153 | out = append(out, s.tr(r, "rev.summary.changes", rev.Username)) |
| 154 | case "rejected": |
| 155 | out = append(out, s.tr(r, "rev.summary.rejected", rev.Username)) |
| 156 | } |
| 157 | } |
| 158 | return out |
| 159 | } |
| 160 | |
| 161 | func (s *Server) countApprovals(ctx context.Context, repo store.Repo, mr store.MergeRequest, pol store.ApprovalPolicy) int { |
| 162 | reviews, err := s.Store.ListReviews(ctx, mr.ID) |
| 163 | if err != nil { |
| 164 | return 0 |
| 165 | } |
| 166 | n := 0 |
| 167 | for _, rev := range reviews { |
| 168 | if rev.Status != "approved" || rev.UserID == mr.AuthorID { |
| 169 | continue |
| 170 | } |
| 171 | role, err := s.Store.EffectiveRole(ctx, rev.UserID, repo.ID) |
| 172 | if err != nil { |
| 173 | continue |
| 174 | } |
| 175 | if approvalCounts(role, pol) { |
| 176 | n++ |
| 177 | } |
| 178 | } |
| 179 | return n |
| 180 | } |
| 181 | |
| 182 | func approvalCounts(role access.Role, pol store.ApprovalPolicy) bool { |
| 183 | if role.CanManageSettings() { |
| 184 | return true |
| 185 | } |
| 186 | return pol.DevelopersApprove && role.CanWriteCode() |
| 187 | } |
| 188 | |
| 189 | func (s *Server) fillReviews(r *http.Request, v *View, repo store.Repo, mr store.MergeRequest, role access.Role) { |
| 190 | reviews, _ := s.Store.ListReviews(r.Context(), mr.ID) |
| 191 | for _, rev := range reviews { |
| 192 | v.Reviews = append(v.Reviews, ReviewRow{ |
| 193 | UserID: rev.UserID, Username: rev.Username, Status: rev.Status, |
| 194 | Label: s.tr(r, "rev."+rev.Status), |
| 195 | }) |
| 196 | } |
| 197 | if v.User == nil || mr.Status != "open" { |
| 198 | return |
| 199 | } |
| 200 | v.CanReview = role.CanWriteCode() |
| 201 | pol, _, _ := s.Store.EffectiveApproval(r.Context(), repo) |
| 202 | v.CanApprove = v.CanReview && v.User.ID != mr.AuthorID && approvalCounts(role, pol) |
| 203 | v.CanDismiss = role.CanManageSettings() |
| 204 | } |
| 205 | |
| 206 | func (s *Server) postReview(w http.ResponseWriter, r *http.Request) { |
| 207 | u, repo, role, ok := s.guard(w, r, access.RoleDeveloper) |
| 208 | if !ok || !s.csrfOK(w, r) { |
| 209 | return |
| 210 | } |
| 211 | mr, err := s.mrFromRequest(r, repo.ID) |
| 212 | if err != nil || mr.Status != "open" { |
| 213 | http.NotFound(w, r) |
| 214 | return |
| 215 | } |
| 216 | back := repoURL(repo) + "/-/merge-requests/" + strconv.FormatInt(mr.Number, 10) |
| 217 | if !role.CanWriteCode() { |
| 218 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 219 | return |
| 220 | } |
| 221 | status := r.FormValue("status") |
| 222 | switch status { |
| 223 | case "approved", "changes", "rejected": |
| 224 | default: |
| 225 | s.redirect(w, r, back, "", s.tr(r, "rev.bad")) |
| 226 | return |
| 227 | } |
| 228 | if status == "approved" { |
| 229 | if u.ID == mr.AuthorID { |
| 230 | s.redirect(w, r, back, "", s.tr(r, "rev.own")) |
| 231 | return |
| 232 | } |
| 233 | pol, _, err := s.Store.EffectiveApproval(r.Context(), repo) |
| 234 | if err != nil || !approvalCounts(role, pol) { |
| 235 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 236 | return |
| 237 | } |
| 238 | } |
| 239 | if err := s.Store.UpsertReview(r.Context(), repo.ID, mr.ID, u.ID, status); err != nil { |
| 240 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 241 | return |
| 242 | } |
| 243 | s.redirect(w, r, back, s.tr(r, "flash.saved"), "") |
| 244 | } |
| 245 | |
| 246 | func (s *Server) dismissReview(w http.ResponseWriter, r *http.Request) { |
| 247 | _, repo, role, ok := s.guard(w, r, access.RoleMaintainer) |
| 248 | if !ok || !s.csrfOK(w, r) { |
| 249 | return |
| 250 | } |
| 251 | mr, err := s.mrFromRequest(r, repo.ID) |
| 252 | if err != nil { |
| 253 | http.NotFound(w, r) |
| 254 | return |
| 255 | } |
| 256 | back := repoURL(repo) + "/-/merge-requests/" + strconv.FormatInt(mr.Number, 10) |
| 257 | if !role.CanManageSettings() { |
| 258 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 259 | return |
| 260 | } |
| 261 | userID, _ := strconv.ParseInt(r.FormValue("user_id"), 10, 64) |
| 262 | if userID == 0 { |
| 263 | s.redirect(w, r, back, "", s.tr(r, "rev.bad")) |
| 264 | return |
| 265 | } |
| 266 | if err := s.Store.UpsertReview(r.Context(), repo.ID, mr.ID, userID, "dismissed"); err != nil { |
| 267 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 268 | return |
| 269 | } |
| 270 | s.redirect(w, r, back, s.tr(r, "flash.saved"), "") |
| 271 | } |
| 272 | |
| 273 | func (s *Server) reopenMR(w http.ResponseWriter, r *http.Request) { |
| 274 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 275 | if !ok || !s.csrfOK(w, r) { |
| 276 | return |
| 277 | } |
| 278 | mr, err := s.mrFromRequest(r, repo.ID) |
| 279 | if err != nil || mr.Status != "closed" { |
| 280 | http.NotFound(w, r) |
| 281 | return |
| 282 | } |
| 283 | if u.ID != mr.AuthorID && !role.CanManageSettings() { |
| 284 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 285 | return |
| 286 | } |
| 287 | _ = s.Store.SetMergeRequestStatus(r.Context(), mr.ID, "open") |
| 288 | s.redirect(w, r, repoURL(repo)+"/-/merge-requests/"+strconv.FormatInt(mr.Number, 10), s.tr(r, "flash.reopened"), "") |
| 289 | } |
| 290 | |
| 291 | // noteRequestsChanges zet Request changes als de reactie een suggestie bevat. |
| 292 | func (s *Server) noteRequestsChanges(r *http.Request, repo store.Repo, mr store.MergeRequest, userID int64, suggestion string) { |
| 293 | if suggestion == "" || mr.Status != "open" { |
| 294 | return |
| 295 | } |
| 296 | _ = s.Store.UpsertReview(r.Context(), repo.ID, mr.ID, userID, "changes") |
| 297 | } |