1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import (
6 "context"
7 "errors"
8 "net/http"
9 "strconv"
10 "strings"
11
12 "github.com/bnema/zerowrap"
13
14 "bnema.dev/ermite/internal/domain"
15 "bnema.dev/ermite/internal/ports"
16)
17
18// The writes of the merge request pages. Each is a POST that passed the gate
19// (an Origin of this site and the owner's session), calls the use case as the
20// owner, and answers 303 to the page that shows the result, with
21// "Cache-Control: no-store". A request the use case refuses is answered with a
22// state page that says why, in the words of the use case for the rules the
23// owner can fix (a text rule, a moved branch, a full merge request), and
24// without detail for anything else.
25
26// maxFormBytes bounds the body of a form: a description of 8,000 characters is
27// at most 32,000 bytes.
28const maxFormBytes = 128 << 10
29
30// form reads the form of a POST, bounded.
31func (s *Server) form(w http.ResponseWriter, r *http.Request) bool {
32 r.Body = http.MaxBytesReader(w, r.Body, maxFormBytes)
33 if err := r.ParseForm(); err != nil {
34 s.reject(r.Context(), w, r, http.StatusRequestEntityTooLarge, "form too large", "the form is larger than a merge request can be.", mrBase(r.PathValue("repo")))
35 return false
36 }
37 return true
38}
39
40// seeOther redirects to a page, never cached.
41func seeOther(w http.ResponseWriter, r *http.Request, to string) {
42 w.Header().Set("Cache-Control", "no-store")
43 http.Redirect(w, r, to, http.StatusSeeOther)
44}
45
46// mrAction wraps a POST handler: it bounds the use case calls, reads the form
47// and maps the error of the use case to a page.
48func (s *Server) mrAction(h func(ctx context.Context, r *http.Request) (to string, back string, err error)) http.HandlerFunc {
49 return func(w http.ResponseWriter, r *http.Request) {
50 ctx, cancel := context.WithTimeout(r.Context(), requestTimeout)
51 defer cancel()
52 if !s.form(w, r) {
53 return
54 }
55 to, back, err := h(ctx, r)
56 if err != nil {
57 s.actionFailed(ctx, w, r, err, back)
58 return
59 }
60 seeOther(w, r, to)
61 }
62}
63
64// actionFailed answers a refused action.
65func (s *Server) actionFailed(ctx context.Context, w http.ResponseWriter, r *http.Request, err error, back string) {
66 // The words of the use case, for the rules the owner can fix.
67 msg := err.Error()
68 // The order is that of the API's mapping of the domain errors to codes
69 // (rpc.codeOf), so that a web action answers what the API would.
70 switch {
71 case errors.Is(err, context.Canceled):
72 // The browser is gone.
73 case errors.Is(err, domain.ErrNotFound):
74 w.Header().Set("Cache-Control", "no-store")
75 s.write(ctx, w, r, http.StatusNotFound, s.notFoundPage())
76 case errors.Is(err, domain.ErrAlreadyExists):
77 s.reject(ctx, w, r, http.StatusConflict, "already there", rule(msg, "this already exists."), back)
78 case errors.Is(err, domain.ErrUnauthenticated), errors.Is(err, domain.ErrForbidden), errors.Is(err, domain.ErrRejected):
79 s.reject(ctx, w, r, http.StatusForbidden, "forbidden", "the session is not allowed to do this.", back)
80 case errors.Is(err, domain.ErrPreconditionFailed), errors.Is(err, domain.ErrConflict):
81 s.reject(ctx, w, r, http.StatusConflict, "not possible now", rule(msg, "the merge request is not in a state that allows this."), back)
82 case errors.Is(err, domain.ErrResourceExhausted):
83 s.reject(ctx, w, r, http.StatusConflict, "limit reached", rule(msg, "a limit of the merge request is reached."), back)
84 case domain.IsInvalid(err):
85 s.reject(ctx, w, r, http.StatusBadRequest, "not accepted", rule(msg, "the server refused what was sent."), back)
86 default:
87 s.internalFailure(ctx, w, r, err)
88 }
89}
90
91// internalFailure answers a failure of an action that is not the owner's to
92// fix: the 500 page with an id, and the cause in the log.
93func (s *Server) internalFailure(ctx context.Context, w http.ResponseWriter, r *http.Request, err error) {
94 id := requestID()
95 log := zerowrap.FromCtx(ctx)
96 log.Error().Err(err).Str("request_id", id).Str("path", logPath(r.URL.Path)).Msg("web action failed")
97 w.Header().Set("Cache-Control", "no-store")
98 s.write(ctx, w, r, http.StatusInternalServerError, s.errorPage(id))
99}
100
101// rule is a message of the use case fit to show: the invisible characters marked,
102// bounded, and a fallback for an empty one.
103func rule(msg, fallback string) string {
104 msg = strings.TrimSpace(visibleText(msg))
105 if msg == "" {
106 return fallback
107 }
108 return cutRunes(msg, 400)
109}
110
111// reject writes the page of a refused action, with the way back.
112func (s *Server) reject(ctx context.Context, w http.ResponseWriter, r *http.Request, status int, title, line, back string) {
113 if back == "" {
114 back = "/"
115 }
116 l := s.stateLayout(title, []Crumb{{Label: title, Current: true}})
117 w.Header().Set("Cache-Control", "no-store")
118 s.write(ctx, w, r, status, State(l, StatePage{
119 Code: itoa(status), Title: title,
120 Lines: []string{line, "nothing was changed by this request."},
121 Back: []Link{{Label: "back", Href: back}, {Label: "home", Href: "/"}},
122 }))
123}
124
125// number reads the merge request number of the path.
126func number(r *http.Request) (int64, error) { return mrNumber(r) }
127
128// mrCreate is POST /<repo>/merge-requests: it opens a merge request.
129func (s *Server) mrCreate(ctx context.Context, r *http.Request) (string, string, error) {
130 repo := r.PathValue("repo")
131 source := r.PostFormValue("source")
132 back := repoHref(repo, "compare", source)
133 name, err := domain.ParseRepoName(repo)
134 if err != nil {
135 return "", back, err
136 }
137 mr, err := s.cfg.MergeRequests.Create(ctx, principal(ctx), name, ports.CreateMergeRequestParams{
138 Title: r.PostFormValue("title"), Description: normalizeNewlines(r.PostFormValue("description")),
139 Source: source, Target: r.PostFormValue("target"),
140 })
141 if err != nil {
142 return "", back, err
143 }
144 return mrHref(repo, mr.Number), back, nil
145}
146
147// formError is a form the server refuses before asking a use case: it matches
148// domain.ErrInvalid and says just what is wrong.
149type formError string
150
151func (e formError) Error() string { return string(e) }
152
153func (formError) Unwrap() error { return domain.ErrInvalid }
154
155// mrComment is POST /<repo>/merge-requests/<n>/comments: a general comment, or
156// one on a line (path, line and side).
157func (s *Server) mrComment(ctx context.Context, r *http.Request) (string, string, error) {
158 repo := r.PathValue("repo")
159 n, err := number(r)
160 if err != nil {
161 return "", "", err
162 }
163 back := mrHref(repo, n)
164 params := ports.AddCommentParams{Text: normalizeNewlines(r.PostFormValue("text")), Path: r.PostFormValue("path")}
165 if params.Path != "" || r.PostFormValue("line") != "" || r.PostFormValue("side") != "" {
166 line, err := strconv.ParseInt(r.PostFormValue("line"), 10, 32)
167 if err != nil {
168 return "", back, formError("line is not a number")
169 }
170 params.Line = int(line)
171 switch r.PostFormValue("side") {
172 case "new":
173 params.Side = domain.SideNew
174 case "old":
175 params.Side = domain.SideOld
176 default:
177 return "", back, formError("side must be new or old")
178 }
179 }
180 name, err := domain.ParseRepoName(repo)
181 if err != nil {
182 return "", back, err
183 }
184 c, err := s.cfg.MergeRequests.AddComment(ctx, principal(ctx), name, n, params)
185 if err != nil {
186 return "", back, err
187 }
188 return back + "#c" + strconv.FormatInt(c.ID, 10), back, nil
189}
190
191// mrResolve is POST /<repo>/merge-requests/<n>/comments/<id>/resolve.
192//
193//hexcheck:ignore suspicious-business-logic-in-adapter reads two form fields into a use case call; the state of the comment is decided by the use case
194func (s *Server) mrResolve(ctx context.Context, r *http.Request) (string, string, error) {
195 repo := r.PathValue("repo")
196 n, err := number(r)
197 if err != nil {
198 return "", "", err
199 }
200 back := mrHref(repo, n)
201 resolved, err := formBool(r.PostFormValue("resolved"))
202 if err != nil {
203 return "", back, formError("resolved: " + err.Error())
204 }
205 name, err := domain.ParseRepoName(repo)
206 if err != nil {
207 return "", back, err
208 }
209 // An id this server never issued is 0: the use case answers not found,
210 // after the repository and the scope were checked, as for any unknown
211 // comment.
212 c, err := s.cfg.MergeRequests.ResolveComment(ctx, principal(ctx), name, n, commentID(r.PathValue("id")), resolved)
213 if err != nil {
214 return "", back, err
215 }
216 return back + "#c" + strconv.FormatInt(c.ID, 10), back, nil
217}
218
219// commentID parses the id of a comment in a path: a positive decimal number,
220// written without sign or leading zeros. Anything else is 0, which names no
221// comment.
222func commentID(s string) int64 {
223 n, err := strconv.ParseInt(s, 10, 64)
224 if err != nil || n < 1 || strconv.FormatInt(n, 10) != s {
225 return 0
226 }
227 return n
228}
229
230// headID parses a commit id of a form; the empty string is the zero id.
231//
232//hexcheck:ignore suspicious-business-logic-in-adapter parses a form field into the domain id and names the field in the error; no rule is decided here
233func headID(s string) (domain.ObjectID, error) {
234 if s == "" {
235 return domain.ObjectID{}, nil
236 }
237 var v domain.Violations
238 id, err := domain.ParseObjectID(s)
239 if err != nil {
240 v.Add("head_id", "is not a full commit id")
241 }
242 return id, v.Err()
243}
244
245// mrClose is POST /<repo>/merge-requests/<n>/close.
246func (s *Server) mrClose(ctx context.Context, r *http.Request) (string, string, error) {
247 repo := r.PathValue("repo")
248 n, err := number(r)
249 if err != nil {
250 return "", "", err
251 }
252 back := mrHref(repo, n)
253 name, err := domain.ParseRepoName(repo)
254 if err != nil {
255 return "", back, err
256 }
257 if _, err := s.cfg.MergeRequests.Close(ctx, principal(ctx), name, n); err != nil {
258 return "", back, err
259 }
260 return back, back, nil
261}
262
263// mrMerge is POST /<repo>/merge-requests/<n>/merge. The form carries the mode,
264// the head the owner was shown (head_id): the server merges only if the
265// branch still points to it, and delete_branch when the owner asked to delete
266// the source branch after the merge.
267func (s *Server) mrMerge(ctx context.Context, r *http.Request) (string, string, error) {
268 repo := r.PathValue("repo")
269 n, err := number(r)
270 if err != nil {
271 return "", "", err
272 }
273 back := mrHref(repo, n)
274 var mode domain.MergeMode
275 switch r.PostFormValue("mode") {
276 case "fast-forward":
277 mode = domain.MergeModeFastForward
278 case "squash":
279 mode = domain.MergeModeSquash
280 default:
281 return "", back, formError("mode must be fast-forward or squash")
282 }
283 name, err := domain.ParseRepoName(repo)
284 if err != nil {
285 return "", back, err
286 }
287 head, err := headID(r.PostFormValue("head_id"))
288 if err != nil {
289 return "", back, err
290 }
291 if _, err := s.cfg.MergeRequests.Merge(ctx, principal(ctx), name, n, ports.MergeParams{
292 Mode: mode, HeadID: head, DeleteSource: r.PostFormValue("delete_branch") == "1",
293 }); err != nil {
294 return "", back, err
295 }
296 return back, back, nil
297}
298
299// mrRead is POST /<repo>/merge-requests/<n>/read: the owner marks the head
300// they were shown as read. Reading a page never does it (see the top of mr.go).
301func (s *Server) mrRead(ctx context.Context, r *http.Request) (string, string, error) {
302 repo := r.PathValue("repo")
303 n, err := number(r)
304 if err != nil {
305 return "", "", err
306 }
307 back := mrHref(repo, n)
308 name, err := domain.ParseRepoName(repo)
309 if err != nil {
310 return "", back, err
311 }
312 head, err := headID(r.PostFormValue("head_id"))
313 if err != nil {
314 return "", back, err
315 }
316 if _, err := s.cfg.MergeRequests.MarkRead(ctx, principal(ctx), name, n, head); err != nil {
317 return "", back, err
318 }
319 return back, back, nil
320}
321
322// formBool reads the "true" or "false" a form field carries.
323func formBool(s string) (bool, error) {
324 switch s {
325 case "true":
326 return true, nil
327 case "false":
328 return false, nil
329 }
330 return false, errors.New("must be true or false")
331}
332
333// normalizeNewlines turns the CRLF of a browser textarea into LF, the line
334// break the use case stores.
335func normalizeNewlines(s string) string {
336 return strings.ReplaceAll(s, "\r\n", "\n")
337}