| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/access" |
| 9 | "gitlab.com/Timfa/bluegit/internal/desctpl" |
| 10 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | "gitlab.com/Timfa/bluegit/internal/markdown" |
| 13 | "gitlab.com/Timfa/bluegit/internal/store" |
| 14 | ) |
| 15 | |
| 16 | func (s *Server) taskOpt(r *http.Request, csrf string, repo store.Repo, kind, id string, can bool) markdown.Tasks { |
| 17 | return markdown.Tasks{ |
| 18 | Post: repoURL(repo) + "/-/tasks", CSRF: csrf, Kind: kind, ID: id, |
| 19 | CanToggle: can && csrf != "", Count: s.tr(r, "tpl.tasks"), Toggle: s.tr(r, "tpl.toggle"), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func (s *Server) descriptionTemplates(r *http.Request, repo store.Repo, kind string) []desctpl.Template { |
| 24 | if s.StoragePath() == "" { |
| 25 | return nil |
| 26 | } |
| 27 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 28 | list, err := desctpl.List(r.Context(), s.Git, dir, repo.DefaultBranch, kind) |
| 29 | if err != nil { |
| 30 | return nil |
| 31 | } |
| 32 | return list |
| 33 | } |
| 34 | |
| 35 | func templateNames(list []desctpl.Template) []string { |
| 36 | if len(list) == 0 { |
| 37 | return nil |
| 38 | } |
| 39 | out := make([]string, len(list)) |
| 40 | for i, t := range list { |
| 41 | out[i] = t.Name |
| 42 | } |
| 43 | return out |
| 44 | } |
| 45 | |
| 46 | // fillTpl zet een sjabloon in een lege beschrijving, of het gekozen sjabloon |
| 47 | // als het formulier ?template= meestuurt. Een lege keuze maakt de tekst leeg. |
| 48 | func (s *Server) fillTpl(r *http.Request, repo store.Repo, kind string, body, picked *string, names *[]string) { |
| 49 | list := s.descriptionTemplates(r, repo, kind) |
| 50 | *names = templateNames(list) |
| 51 | if r.URL.Query().Has("template") { |
| 52 | name := r.URL.Query().Get("template") |
| 53 | if name == "" { |
| 54 | *body = "" |
| 55 | *picked = "" |
| 56 | return |
| 57 | } |
| 58 | if t, ok := desctpl.Find(list, name); ok { |
| 59 | *body = t.Body |
| 60 | *picked = t.Name |
| 61 | } |
| 62 | return |
| 63 | } |
| 64 | if strings.TrimSpace(*body) == "" { |
| 65 | if t, ok := desctpl.Default(list); ok { |
| 66 | *body = t.Body |
| 67 | *picked = t.Name |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func (s *Server) mrVars(r *http.Request, repo store.Repo, source, target string) desctpl.Vars { |
| 73 | v := desctpl.Vars{Source: source, Target: target} |
| 74 | if s.StoragePath() == "" || source == "" || target == "" { |
| 75 | return v |
| 76 | } |
| 77 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 78 | msgs, err := gitrepo.Messages(r.Context(), s.Git, dir, target+".."+source, 100) |
| 79 | if err != nil { |
| 80 | return v |
| 81 | } |
| 82 | for _, m := range msgs { |
| 83 | v.Commits = append(v.Commits, desctpl.Commit{ |
| 84 | Subject: m.Subject, Body: m.Body, Author: m.Author, Email: m.Email, Merge: m.Merge, |
| 85 | }) |
| 86 | } |
| 87 | return v |
| 88 | } |
| 89 | |
| 90 | func applyToggle(body string, index int, checked bool) (string, error) { |
| 91 | next, ok := markdown.SetTask(body, index, checked) |
| 92 | if !ok { |
| 93 | return "", i18n.E("err.task") |
| 94 | } |
| 95 | return next, nil |
| 96 | } |
| 97 | |
| 98 | func (s *Server) toggleTask(w http.ResponseWriter, r *http.Request) { |
| 99 | u, repo, role, ok := s.guard(w, r, access.RoleReadonly) |
| 100 | if !ok || !s.csrfOK(w, r) { |
| 101 | return |
| 102 | } |
| 103 | if u.ID == 0 { |
| 104 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 105 | return |
| 106 | } |
| 107 | index, err := strconv.Atoi(r.FormValue("index")) |
| 108 | if err != nil || index < 0 { |
| 109 | http.Error(w, s.tr(r, "err.task"), http.StatusBadRequest) |
| 110 | return |
| 111 | } |
| 112 | id, err := strconv.ParseInt(r.FormValue("id"), 10, 64) |
| 113 | if err != nil || id < 1 { |
| 114 | http.Error(w, s.tr(r, "err.task"), http.StatusBadRequest) |
| 115 | return |
| 116 | } |
| 117 | checked := r.FormValue("checked") == "1" |
| 118 | switch r.FormValue("kind") { |
| 119 | case "issue": |
| 120 | s.toggleIssue(w, r, u, repo, role, id, index, checked) |
| 121 | case "issue-note": |
| 122 | s.toggleIssueNote(w, r, u, repo, role, id, index, checked) |
| 123 | case "mr": |
| 124 | s.toggleMR(w, r, u, repo, role, id, index, checked) |
| 125 | case "mr-note": |
| 126 | s.toggleMRNote(w, r, u, repo, id, index, checked) |
| 127 | default: |
| 128 | http.Error(w, s.tr(r, "err.task"), http.StatusBadRequest) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | func (s *Server) toggleIssue(w http.ResponseWriter, r *http.Request, u store.User, repo store.Repo, role access.Role, number int64, index int, checked bool) { |
| 133 | iss, err := s.Store.IssueByNumber(r.Context(), repo.ID, number) |
| 134 | if err != nil || !issueVisible(iss, u, role) { |
| 135 | http.NotFound(w, r) |
| 136 | return |
| 137 | } |
| 138 | back := issuePath(repo, iss.Number) |
| 139 | if u.ID != iss.AuthorID && !role.CanTriageIssue() { |
| 140 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 141 | return |
| 142 | } |
| 143 | next, err := applyToggle(iss.Description, index, checked) |
| 144 | if err != nil { |
| 145 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 146 | return |
| 147 | } |
| 148 | if err := s.Store.UpdateIssueText(r.Context(), iss.ID, iss.Title, next); err != nil { |
| 149 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 150 | return |
| 151 | } |
| 152 | s.issueHook(repo, iss.Number, u.Username, "updated", "") |
| 153 | s.redirect(w, r, back, "", "") |
| 154 | } |
| 155 | |
| 156 | func (s *Server) toggleIssueNote(w http.ResponseWriter, r *http.Request, u store.User, repo store.Repo, role access.Role, id int64, index int, checked bool) { |
| 157 | note, err := s.Store.IssueNoteByID(r.Context(), repo.ID, id) |
| 158 | if err != nil { |
| 159 | http.NotFound(w, r) |
| 160 | return |
| 161 | } |
| 162 | iss, err := s.Store.IssueByID(r.Context(), repo.ID, note.IssueID) |
| 163 | if err != nil || !issueVisible(iss, u, role) { |
| 164 | http.NotFound(w, r) |
| 165 | return |
| 166 | } |
| 167 | back := issuePath(repo, iss.Number) |
| 168 | if u.ID != note.AuthorID { |
| 169 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 170 | return |
| 171 | } |
| 172 | next, err := applyToggle(note.Body, index, checked) |
| 173 | if err != nil { |
| 174 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 175 | return |
| 176 | } |
| 177 | if err := s.Store.UpdateIssueNoteBody(r.Context(), repo.ID, note.ID, next); err != nil { |
| 178 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 179 | return |
| 180 | } |
| 181 | s.redirect(w, r, back, "", "") |
| 182 | } |
| 183 | |
| 184 | func (s *Server) toggleMR(w http.ResponseWriter, r *http.Request, u store.User, repo store.Repo, role access.Role, number int64, index int, checked bool) { |
| 185 | mr, err := s.Store.MergeRequestByNumber(r.Context(), repo.ID, number) |
| 186 | if err != nil { |
| 187 | http.NotFound(w, r) |
| 188 | return |
| 189 | } |
| 190 | back := repoURL(repo) + "/-/merge-requests/" + strconv.FormatInt(mr.Number, 10) |
| 191 | if mr.Status != "open" || (u.ID != mr.AuthorID && !role.CanWriteCode()) { |
| 192 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 193 | return |
| 194 | } |
| 195 | next, err := applyToggle(mr.Description, index, checked) |
| 196 | if err != nil { |
| 197 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 198 | return |
| 199 | } |
| 200 | if err := s.Store.UpdateMergeRequest(r.Context(), mr.ID, mr.Title, next, mr.Labels); err != nil { |
| 201 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 202 | return |
| 203 | } |
| 204 | s.redirect(w, r, back, "", "") |
| 205 | } |
| 206 | |
| 207 | func (s *Server) toggleMRNote(w http.ResponseWriter, r *http.Request, u store.User, repo store.Repo, id int64, index int, checked bool) { |
| 208 | note, err := s.Store.NoteByID(r.Context(), repo.ID, id) |
| 209 | if err != nil { |
| 210 | http.NotFound(w, r) |
| 211 | return |
| 212 | } |
| 213 | mr, err := s.Store.MergeRequest(r.Context(), repo.ID, note.MRID) |
| 214 | if err != nil { |
| 215 | http.NotFound(w, r) |
| 216 | return |
| 217 | } |
| 218 | back := repoURL(repo) + "/-/merge-requests/" + strconv.FormatInt(mr.Number, 10) |
| 219 | if u.ID != note.AuthorID { |
| 220 | http.Error(w, s.tr(r, "err.forbidden"), http.StatusForbidden) |
| 221 | return |
| 222 | } |
| 223 | next, err := applyToggle(note.Body, index, checked) |
| 224 | if err != nil { |
| 225 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 226 | return |
| 227 | } |
| 228 | if err := s.Store.UpdateNoteBody(r.Context(), repo.ID, note.ID, next); err != nil { |
| 229 | s.redirect(w, r, back, "", s.trErr(r, err)) |
| 230 | return |
| 231 | } |
| 232 | s.redirect(w, r, back, "", "") |
| 233 | } |