1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package web
4
5import "strings"
6
7// MRList is the list of the merge requests of a repository: the open ones,
8// then the merged and closed ones.
9templ MRList(l Layout, p MRListPage) {
10 @Shell(l, mrListRail(p)) {
11 if p.Live {
12 @LiveSlot("mr-notice")
13 }
14 <div class="filter">
15 <label for="mr-filter" class="sr">filter</label>
16 <input
17 id="mr-filter"
18 type="search"
19 data-filter=".mrs tbody tr"
20 placeholder="Filter by title, branch, author or state…"
21 autocomplete="off"
22 hx-on:input="ermite.filter(this)"
23 />
24 </div>
25 if p.Newest == "" {
26 <h2 class="rule-h"><span>Open</span><span class="count">{ itoa(len(p.Open)) }</span></h2>
27 @mrRows(p.Open, "Open merge requests")
28 if len(p.Open) == 0 {
29 <p class="lede dim">no open merge requests.</p>
30 }
31 if p.OpenNote != "" {
32 <p class="note">{ p.OpenNote }</p>
33 }
34 }
35 <h2 class="rule-h"><span>Merged and closed</span><span class="count">{ itoa(len(p.Done)) }</span></h2>
36 @mrRows(p.Done, "Merged and closed merge requests")
37 if len(p.Done) == 0 {
38 <p class="lede dim">nothing merged or closed yet.</p>
39 }
40 if p.Older != "" || p.Newest != "" {
41 <nav class="pager" aria-label="Pages">
42 if p.Newest != "" {
43 <a class="btn" href={ templ.SafeURL(p.Newest) }>← Newest</a>
44 } else {
45 <span class="btn" aria-disabled="true">← Newest</span>
46 }
47 if p.Older != "" {
48 <a class="btn" href={ templ.SafeURL(p.Older) } rel="next">Older →</a>
49 } else {
50 <span class="btn" aria-disabled="true">Older →</span>
51 }
52 </nav>
53 }
54 }
55}
56
57templ mrRows(rows []MRRow, label string) {
58 if len(rows) > 0 {
59 <table class="list mrs" aria-label={ label }>
60 <tbody data-rows>
61 for _, m := range rows {
62 <tr data-cursor?={ m.Cursor } data-state={ m.State }>
63 <td class="cur" aria-hidden="true"></td>
64 <td class="c-n mute">{ m.Number }</td>
65 <td class="c-state">{ m.State }</td>
66 <td class="c-subject"><a href={ templ.SafeURL(m.Href) }>{ m.Title }</a> <span class="mute">{ m.Branches }</span></td>
67 <td class="c-who dim">{ m.Author }</td>
68 <td class="c-when nowrap dim">
69 if m.WhenAt != "" {
70 <time datetime={ m.WhenAt } title={ m.WhenAt }>{ m.When }</time>
71 }
72 </td>
73 </tr>
74 }
75 </tbody>
76 </table>
77 }
78}
79
80templ mrListRail(p MRListPage) {
81 <section aria-labelledby="r-states">
82 <h2 id="r-states">States</h2>
83 <dl class="kv">
84 <dt>open</dt>
85 <dd>awaits review</dd>
86 <dt>merged</dt>
87 <dd>the target took the branch</dd>
88 <dt>closed</dt>
89 <dd>refused, branch kept</dd>
90 </dl>
91 </section>
92 <section aria-labelledby="r-new">
93 <h2 id="r-new">Open one</h2>
94 if len(p.Branches) > 0 {
95 <p class="dim">compare a branch with { p.Default }:</p>
96 <nav class="outline" aria-label="Branches">
97 for _, b := range p.Branches {
98 <a href={ templ.SafeURL(b.Href) }>{ b.Name }</a>
99 }
100 </nav>
101 } else {
102 <p class="dim">there is no other branch to merge yet.</p>
103 }
104 @CloneBlock(p.CLI)
105 </section>
106}
107
108// MergeRequest is a merge request: its facts, its description, the diff with
109// the notes under their lines, the general comments; and in the rail the merge
110// form, the checks, the settings, the commits and the files.
111templ MergeRequest(l Layout, p MRPage) {
112 @Shell(l, mrRail(p)) {
113 <header class="show mr-h">
114 <h1><span class="mute">{ p.Number }</span> { p.Title }</h1>
115 <dl class="kv show-kv">
116 <dt>state</dt>
117 <dd class="mr-state" data-state={ p.State }>
118 { p.State }
119 if p.MergedShort != "" {
120 { " · " }<a href={ templ.SafeURL(p.MergedHref) }>{ p.MergedShort }</a>{ " · " + p.MergeMode }
121 }
122 </dd>
123 <dt>branch</dt>
124 <dd>
125 @branchLink(p.Source, p.SourceHref)
126 { " → " }
127 @branchLink(p.Target, p.TargetHref)
128 </dd>
129 <dt>opened</dt>
130 <dd>
131 { p.Opened.Name }
132 if p.Opened.WhenAt != "" {
133 { " · " }<time datetime={ p.Opened.WhenAt } title={ p.Opened.Abs }>{ p.Opened.When }</time>
134 }
135 </dd>
136 if p.UpdatedAt != "" {
137 <dt>updated</dt>
138 <dd><time datetime={ p.UpdatedAt }>{ p.Updated }</time></dd>
139 }
140 </dl>
141 </header>
142 @LiveSlot("mr-notice")
143 if len(p.Desc) > 0 {
144 <p class="mr-desc">@userText(p.Desc)</p>
145 }
146 if p.Since != nil {
147 <nav class="since" aria-label="Diff range">
148 <a
149 href={ templ.SafeURL(p.Since.AllHref) }
150 if !p.Since.Current {
151 aria-current="page"
152 }
153 >all changes <span class="mute">vs { p.Target }</span></a>
154 <a
155 href={ templ.SafeURL(p.Since.SinceHref) }
156 if p.Since.Current {
157 aria-current="page"
158 }
159 >since your last read <span class="mute">{ p.Since.ReadShort } → { p.Since.HeadShort }</span></a>
160 </nav>
161 }
162 if p.SinceNote != "" {
163 <p class="notice">{ p.SinceNote }</p>
164 }
165 @mrBar(p)
166 if p.Invisible {
167 <p class="notice">
168 this diff holds invisible characters (direction controls, zero-width characters). They are shown as
169 <span class="inv"><U+XXXX></span> markers, never applied.
170 </p>
171 }
172 if p.Note != "" {
173 <p class="notice">{ p.Note }</p>
174 }
175 <div
176 id="diffs"
177 data-mr
178 if p.Both {
179 data-diff={ p.View }
180 } else {
181 data-view={ p.View }
182 }
183 if p.Both && !p.Pinned {
184 hx-live="this.dataset.diff = sessionStorage.getItem('diff') || this.dataset.diff"
185 }
186 >
187 for _, f := range p.Files {
188 @DiffFileView(f, p.DiffView)
189 }
190 if len(p.Files) == 0 {
191 <p class="note">no changed files.</p>
192 }
193 </div>
194 <h2 class="rule-h" id="comments"><span>Comments</span><span class="count">{ itoa(p.NUnresolved) } open / { itoa(p.NComments) }</span></h2>
195 for _, c := range p.Loose {
196 @commentView(c, "")
197 }
198 for _, c := range p.General {
199 @commentView(c, "")
200 }
201 if p.NComments == 0 {
202 <p class="dim">no comments yet.</p>
203 }
204 if p.GeneralForm != nil {
205 @commentForm(*p.GeneralForm, "")
206 }
207 }
208}
209
210templ branchLink(name, href string) {
211 if href != "" {
212 <a href={ templ.SafeURL(href) }>{ name }</a>
213 } else {
214 { name }
215 }
216}
217
218// lineNotes are the comments under a line of the diff, and the form to write
219// one when the reader asked for it. The diff is in the page twice (unified and
220// split); the suffix keeps the ids unique, the unified table having none.
221templ lineNotes(notes []CommentView, form *CommentForm, suffix string) {
222 for _, c := range notes {
223 @commentView(c, suffix)
224 }
225 if form != nil {
226 @commentForm(*form, suffix)
227 }
228}
229
230// commentView is a note: a ruled block, under its line in the diff or in the
231// list of comments. The text is plain: escaped, its line breaks kept, its
232// http(s) addresses linked.
233templ commentView(c CommentView, suffix string) {
234 <div class="cmt" id={ "c" + c.ID + suffix } data-resolved={ boolText(c.Resolved) }>
235 <p class="c-head">
236 <span class="c-who">{ c.Author }</span>
237 if c.WhenAt != "" {
238 { " " }<time class="mute" datetime={ c.WhenAt } title={ c.Abs }>{ c.When }</time>
239 }
240 if c.Resolved {
241 { " " }<span class="c-state">resolved</span>
242 }
243 if c.Outdated {
244 { " " }<span class="c-state" title="written on an earlier head; the line may have moved">outdated</span>
245 }
246 if c.Where != "" {
247 { " " }<span class="c-where mute">{ c.Where }</span>
248 }
249 </p>
250 <p class="c-text">@userText(c.Parts)</p>
251 if c.Resolve != nil {
252 <form class="c-act" method="post" action={ templ.SafeURL(c.Resolve.Action) }>
253 <input type="hidden" name="resolved" value={ boolText(c.Resolve.Value) }/>
254 if c.Resolve.From > 0 {
255 <input type="hidden" name="from" value={ itoa(c.Resolve.From) }/>
256 }
257 <button type="submit" class="btn">{ c.Resolve.Label }</button>
258 </form>
259 }
260 </div>
261}
262
263func boolText(b bool) string {
264 if b {
265 return "true"
266 }
267 return "false"
268}
269
270// commentForm writes a comment, general or on a line.
271templ commentForm(f CommentForm, suffix string) {
272 <form class="cform" method="post" action={ templ.SafeURL(f.Action) }>
273 <label for={ f.ID + suffix }>{ f.Title }</label>
274 <textarea
275 id={ f.ID + suffix }
276 name="text"
277 rows="3"
278 maxlength="8000"
279 required
280 placeholder="plain text, 8000 characters at most"
281 if f.Focus {
282 autofocus
283 }
284 ></textarea>
285 if f.Path != "" {
286 <input type="hidden" name="path" value={ f.Path }/>
287 <input type="hidden" name="line" value={ f.Line }/>
288 <input type="hidden" name="side" value={ f.Side }/>
289 }
290 if f.From > 0 {
291 <input type="hidden" name="from" value={ itoa(f.From) }/>
292 }
293 <div class="f-btns">
294 <button type="submit" class="btn primary">Comment</button>
295 if f.Cancel != "" {
296 <a class="btn" href={ templ.SafeURL(f.Cancel) }>Cancel</a>
297 }
298 </div>
299 </form>
300}
301
302templ mrRail(p MRPage) {
303 <section aria-labelledby="r-files" class="r-files">
304 <h2 id="r-files">
305 Files <span class="count">{ itoa(len(p.Files)) }</span><kbd aria-hidden="true">g f</kbd>
306 <button type="button" class="btn icon fold" data-js aria-expanded="true" title="fold the list of files" hx-on:click="ermiteMR.foldFiles()">
307 <span class="ico i-chevron" aria-hidden="true"></span><span class="sr">fold the list of files</span>
308 </button>
309 </h2>
310 @FileList(p.Files)
311 </section>
312 @MRChecks(p.Checks)
313 <section aria-labelledby="r-commits">
314 <h2 id="r-commits">Commits <span class="count">{ itoa(len(p.Commits)) }</span></h2>
315 <ul class="pcommits">
316 for _, c := range p.Commits {
317 <li data-new?={ c.New }>
318 <a href={ templ.SafeURL(c.Href) }>{ c.Short }</a>
319 <span class="s">{ c.Subject }</span>
320 <span class="mute">
321 { c.Author }
322 if c.WhenAt != "" {
323 { " · " }<time datetime={ c.WhenAt }>{ c.When }</time>
324 }
325 if c.New {
326 { " · new" }
327 }
328 </span>
329 </li>
330 }
331 </ul>
332 if len(p.Commits) == 0 {
333 <p class="dim">no commit to merge.</p>
334 }
335 if p.CommitsNote != "" {
336 <p class="note">{ p.CommitsNote }</p>
337 }
338 </section>
339 <section aria-labelledby="r-settings">
340 <h2 id="r-settings">Settings</h2>
341 <dl class="kv">
342 <dt>modes</dt>
343 <dd>{ strings.Join(p.Modes, ", ") }</dd>
344 <dt>checks</dt>
345 <dd>
346 if p.ChecksBlock {
347 failing ones block the merge
348 } else {
349 do not block the merge
350 }
351 </dd>
352 </dl>
353 </section>
354}
355
356// mrBar is the bar above the diff, which follows the review: the totals, the
357// layout of the diff, the state of the checks and, for the owner of an open
358// request, the merge menu and the other actions. Each menu is a <details>: it
359// works without scripts; with them, M and the keys of the menu drive it.
360templ mrBar(p MRPage) {
361 <div class="diff-tools mr-bar" role="toolbar" aria-label="Diff view and merge">
362 <span class="dim">{ plural(p.NFiles, "file", "files") } · <span class="plus">+{ itoa(p.Adds) }</span> <span class="minus">-{ itoa(p.Dels) }</span></span>
363 @viewLink(p.DiffView, "unified", "Unified", "u", p.UnifiedHref)
364 @viewLink(p.DiffView, "split", "Split", "d", p.SplitHref)
365 @collapseButton()
366 <span class="grow"></span>
367 @MRChecksSum(p.ChecksSum)
368 if p.Box != nil {
369 @mergeMenu(p)
370 @moreMenu(p)
371 }
372 </div>
373}
374
375// mergeMenu is the merge of an open request, for the owner: the form (it
376// carries the head that was read), or "Ready for review" for a draft. The
377// mode and the branch deletion have keys (1-3, D) while it is open.
378templ mergeMenu(p MRPage) {
379 <details class="menu mmenu" id="merge-menu" data-state="open">
380 <summary class="btn primary" aria-keyshortcuts="Shift+M">
381 if p.Box.ReadyAction != "" {
382 Draft
383 } else {
384 Merge
385 }
386 <kbd aria-hidden="true">M</kbd>
387 </summary>
388 <div class="menu-pop" role="group" aria-label="Merge">
389 <dl class="kv">
390 <dt>head</dt>
391 <dd>
392 if p.Box.HeadGone {
393 gone: the source branch was deleted
394 } else {
395 { p.HeadShort }
396 }
397 </dd>
398 <dt>into</dt>
399 <dd>{ p.Target }</dd>
400 </dl>
401 if p.Box.ReadyAction != "" {
402 <p class="d-draft">this is a draft: it cannot be merged until it is ready for review.</p>
403 <form class="d-btns" method="post" action={ templ.SafeURL(p.Box.ReadyAction) }>
404 <button type="submit" class="btn primary" data-default title="remove the draft prefix from the title">Ready for review</button>
405 </form>
406 } else if !p.Box.HeadGone {
407 <form class="d-acts" method="post" action={ templ.SafeURL(p.Box.MergeAction) }>
408 <input type="hidden" name="head_id" value={ p.Box.Head }/>
409 <fieldset class="d-modes">
410 <legend class="sr">merge mode</legend>
411 for i, m := range p.Box.Modes {
412 <label>
413 <input type="radio" name="mode" value={ m.Value } checked?={ i == 0 }/>
414 { m.Label }
415 if i < 9 {
416 <kbd aria-hidden="true">{ itoa(i + 1) }</kbd>
417 }
418 </label>
419 }
420 </fieldset>
421 if p.Box.CanDeleteBranch {
422 <label class="d-check"><input type="checkbox" name="delete_branch" value="1"/> delete { p.Source } after the merge <kbd aria-hidden="true">D</kbd></label>
423 }
424 <div class="d-btns">
425 <button type="submit" class="btn primary" data-default>Merge into { p.Target }</button>
426 </div>
427 </form>
428 }
429 </div>
430 </details>
431}
432
433// moreMenu is what else the owner does with an open request: mark the head
434// read, close it, merge it from a terminal.
435templ moreMenu(p MRPage) {
436 <details class="menu omenu">
437 <summary class="btn icon" aria-label="More actions" title="more actions">⋯</summary>
438 <div class="menu-pop" role="group" aria-label="More actions">
439 if p.Box.CanMarkRead {
440 <form method="post" action={ templ.SafeURL(p.Box.ReadAction) }>
441 <input type="hidden" name="head_id" value={ p.Box.Head }/>
442 <button type="submit" title="the next visit can show only what changed after this head" class="btn" aria-keyshortcuts="Shift+R" data-read>Mark read <kbd aria-hidden="true">R</kbd></button>
443 </form>
444 }
445 <form method="post" action={ templ.SafeURL(p.Box.CloseAction) }>
446 <button type="submit" title="close without merging; the branch is kept" class="btn danger">Close</button>
447 </form>
448 if !p.Box.HeadGone && p.Box.ReadyAction == "" {
449 <details class="d-or">
450 <summary class="mute">or from a terminal</summary>
451 @CloneBlock(p.Box.CLI)
452 </details>
453 }
454 </div>
455 </details>
456}
457
458templ Compare(l Layout, p ComparePage) {
459 @Shell(l, compareRail(p)) {
460 <header class="show mr-h">
461 <h1>{ p.Source } → { p.Target }</h1>
462 <dl class="kv show-kv">
463 <dt>branch</dt>
464 <dd><a href={ templ.SafeURL(p.SourceHref) }>{ p.Source }</a></dd>
465 <dt>target</dt>
466 <dd><a href={ templ.SafeURL(p.TargetHref) }>{ p.Target }</a></dd>
467 <dt>ahead</dt>
468 <dd>{ p.AheadText }</dd>
469 </dl>
470 </header>
471 if p.Note != "" {
472 <p class="notice">{ p.Note }</p>
473 }
474 if p.Existing != nil {
475 <p class="notice">an open merge request already covers these branches: <a href={ templ.SafeURL(p.Existing.Href) }>{ p.Existing.Label }</a></p>
476 }
477 if p.Create != nil {
478 <form class="cform create" method="post" action={ templ.SafeURL(p.Create.Action) }>
479 <input type="hidden" name="source" value={ p.Create.Source }/>
480 <input type="hidden" name="target" value={ p.Create.Target }/>
481 <label for="mr-title">title</label>
482 <input id="mr-title" name="title" type="text" maxlength="120" required value={ p.Create.Title } autocomplete="off"/>
483 <label for="mr-desc">description</label>
484 <textarea id="mr-desc" name="description" rows="4" maxlength="8000" placeholder="plain text, optional"></textarea>
485 <div class="f-btns">
486 <button type="submit" class="btn primary">Create merge request</button>
487 </div>
488 </form>
489 }
490 if p.HasDiff {
491 <div class="diff-tools" role="toolbar" aria-label="Diff view">
492 <span class="dim">{ plural(p.NFiles, "file", "files") } · <span class="plus">+{ itoa(p.Adds) }</span> <span class="minus">-{ itoa(p.Dels) }</span></span>
493 <span class="grow"></span>
494 @viewLink(p.DiffView, "unified", "Unified", "u", p.UnifiedHref)
495 @viewLink(p.DiffView, "split", "Split", "d", p.SplitHref)
496 @collapseButton()
497 </div>
498 if p.Invisible {
499 <p class="notice">
500 this diff holds invisible characters (direction controls, zero-width characters). They are shown as
501 <span class="inv"><U+XXXX></span> markers, never applied.
502 </p>
503 }
504 <div
505 id="diffs"
506 if p.Both {
507 data-diff={ p.View }
508 } else {
509 data-view={ p.View }
510 }
511 if p.Both && !p.Pinned {
512 hx-live="this.dataset.diff = sessionStorage.getItem('diff') || this.dataset.diff"
513 }
514 >
515 for _, f := range p.Files {
516 @DiffFileView(f, p.DiffView)
517 }
518 </div>
519 }
520 }
521}
522
523templ compareRail(p ComparePage) {
524 <section aria-labelledby="r-ahead">
525 <h2 id="r-ahead">Commits <span class="count">{ itoa(len(p.Ahead)) }</span></h2>
526 <ul class="pcommits">
527 for _, c := range p.Ahead {
528 <li>
529 <a href={ templ.SafeURL(c.CHref) }>{ c.Short }</a>
530 <span class="s">{ c.Subject }</span>
531 <span class="mute">
532 { c.Author }
533 if c.WhenAt != "" {
534 { " · " }<time datetime={ c.WhenAt }>{ c.When }</time>
535 }
536 </span>
537 </li>
538 }
539 </ul>
540 if len(p.Ahead) == 0 {
541 <p class="dim">no commit ahead of { p.Target }.</p>
542 }
543 </section>
544 if p.HasDiff {
545 <section aria-labelledby="r-files">
546 <h2 id="r-files">Files <span class="count">{ itoa(len(p.Files)) }</span></h2>
547 @FileList(p.Files)
548 </section>
549 }
550 <section aria-labelledby="r-cli">
551 <h2 id="r-cli">From a terminal</h2>
552 @CloneBlock(p.CLI)
553 </section>
554}