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 p.Desc != "" {
144 <article class="mr-desc prose" aria-label="description" hx-ignore>
145 @templ.Raw(string(p.Desc))
146 </article>
147 }
148 if p.Since != nil {
149 <nav class="since" aria-label="Diff range">
150 <a
151 href={ templ.SafeURL(p.Since.AllHref) }
152 if !p.Since.Current {
153 aria-current="page"
154 }
155 >all changes <span class="mute">vs { p.Target }</span></a>
156 <a
157 href={ templ.SafeURL(p.Since.SinceHref) }
158 if p.Since.Current {
159 aria-current="page"
160 }
161 >since your last read <span class="mute">{ p.Since.ReadShort } → { p.Since.HeadShort }</span></a>
162 </nav>
163 }
164 if p.SinceNote != "" {
165 <p class="notice">{ p.SinceNote }</p>
166 }
167 @mrBar(p)
168 if p.Invisible {
169 <p class="notice">
170 this diff holds invisible characters (direction controls, zero-width characters). They are shown as
171 <span class="inv"><U+XXXX></span> markers, never applied.
172 </p>
173 }
174 if p.Note != "" {
175 <p class="notice">{ p.Note }</p>
176 }
177 <div
178 id="diffs"
179 data-mr
180 if p.Both {
181 data-diff={ p.View }
182 } else {
183 data-view={ p.View }
184 }
185 if p.Both && !p.Pinned {
186 hx-live="this.dataset.diff = sessionStorage.getItem('diff') || this.dataset.diff"
187 }
188 >
189 for _, f := range p.Files {
190 @DiffFileView(f, p.DiffView)
191 }
192 if len(p.Files) == 0 {
193 <p class="note">no changed files.</p>
194 }
195 </div>
196 <h2 class="rule-h" id="comments"><span>Comments</span><span class="count">{ itoa(p.NUnresolved) } open / { itoa(p.NComments) }</span></h2>
197 for _, c := range p.Loose {
198 @commentView(c, "")
199 }
200 for _, c := range p.General {
201 @commentView(c, "")
202 }
203 if p.NComments == 0 {
204 <p class="dim">no comments yet.</p>
205 }
206 if p.GeneralForm != nil {
207 @commentForm(*p.GeneralForm, "")
208 }
209 }
210}
211
212templ branchLink(name, href string) {
213 if href != "" {
214 <a href={ templ.SafeURL(href) }>{ name }</a>
215 } else {
216 { name }
217 }
218}
219
220// lineNotes are the comments under a line of the diff, and the form to write
221// one when the reader asked for it. The diff is in the page twice (unified and
222// split); the suffix keeps the ids unique, the unified table having none.
223templ lineNotes(notes []CommentView, form *CommentForm, suffix string) {
224 for _, c := range notes {
225 @commentView(c, suffix)
226 }
227 if form != nil {
228 @commentForm(*form, suffix)
229 }
230}
231
232// commentView is a note: a ruled block, under its line in the diff or in the
233// list of comments. The text is plain: escaped, its line breaks kept, its
234// http(s) addresses linked.
235templ commentView(c CommentView, suffix string) {
236 <div class="cmt" id={ "c" + c.ID + suffix } data-resolved={ boolText(c.Resolved) }>
237 <p class="c-head">
238 <span class="c-who">{ c.Author }</span>
239 if c.WhenAt != "" {
240 { " " }<time class="mute" datetime={ c.WhenAt } title={ c.Abs }>{ c.When }</time>
241 }
242 if c.Resolved {
243 { " " }<span class="c-state">resolved</span>
244 }
245 if c.Outdated {
246 { " " }<span class="c-state" title="written on an earlier head; the line may have moved">outdated</span>
247 }
248 if c.Where != "" {
249 { " " }<span class="c-where mute">{ c.Where }</span>
250 }
251 </p>
252 <p class="c-text">@userText(c.Parts)</p>
253 if c.Resolve != nil {
254 <form class="c-act" method="post" action={ templ.SafeURL(c.Resolve.Action) }>
255 <input type="hidden" name="resolved" value={ boolText(c.Resolve.Value) }/>
256 if c.Resolve.From > 0 {
257 <input type="hidden" name="from" value={ itoa(c.Resolve.From) }/>
258 }
259 <button type="submit" class="btn">{ c.Resolve.Label }</button>
260 </form>
261 }
262 </div>
263}
264
265func boolText(b bool) string {
266 if b {
267 return "true"
268 }
269 return "false"
270}
271
272// commentForm writes a comment, general or on a line.
273templ commentForm(f CommentForm, suffix string) {
274 <form class="cform" method="post" action={ templ.SafeURL(f.Action) }>
275 <label for={ f.ID + suffix }>{ f.Title }</label>
276 <textarea
277 id={ f.ID + suffix }
278 name="text"
279 rows="3"
280 maxlength="8000"
281 required
282 placeholder="plain text, 8000 characters at most"
283 if f.Focus {
284 autofocus
285 }
286 ></textarea>
287 if f.Path != "" {
288 <input type="hidden" name="path" value={ f.Path }/>
289 <input type="hidden" name="line" value={ f.Line }/>
290 <input type="hidden" name="side" value={ f.Side }/>
291 }
292 if f.From > 0 {
293 <input type="hidden" name="from" value={ itoa(f.From) }/>
294 }
295 <div class="f-btns">
296 <button type="submit" class="btn primary">Comment</button>
297 if f.Cancel != "" {
298 <a class="btn" href={ templ.SafeURL(f.Cancel) }>Cancel</a>
299 }
300 </div>
301 </form>
302}
303
304templ mrRail(p MRPage) {
305 <section aria-labelledby="r-files" class="r-files">
306 <h2 id="r-files">
307 Files <span class="count">{ itoa(len(p.Files)) }</span><kbd aria-hidden="true">g f</kbd>
308 <button type="button" class="btn icon fold" data-js aria-expanded="true" title="fold the list of files" hx-on:click="ermiteMR.foldFiles()">
309 <span class="ico i-chevron" aria-hidden="true"></span><span class="sr">fold the list of files</span>
310 </button>
311 </h2>
312 @FileList(p.Files)
313 </section>
314 @MRChecks(p.Checks)
315 <section aria-labelledby="r-commits">
316 <h2 id="r-commits">Commits <span class="count">{ itoa(len(p.Commits)) }</span></h2>
317 <ul class="pcommits">
318 for _, c := range p.Commits {
319 <li data-new?={ c.New }>
320 <a href={ templ.SafeURL(c.Href) }>{ c.Short }</a>
321 <span class="s">{ c.Subject }</span>
322 <span class="mute">
323 { c.Author }
324 if c.WhenAt != "" {
325 { " · " }<time datetime={ c.WhenAt }>{ c.When }</time>
326 }
327 if c.New {
328 { " · new" }
329 }
330 </span>
331 </li>
332 }
333 </ul>
334 if len(p.Commits) == 0 {
335 <p class="dim">no commit to merge.</p>
336 }
337 if p.CommitsNote != "" {
338 <p class="note">{ p.CommitsNote }</p>
339 }
340 </section>
341 <section aria-labelledby="r-settings">
342 <h2 id="r-settings">Settings</h2>
343 <dl class="kv">
344 <dt>modes</dt>
345 <dd>{ strings.Join(p.Modes, ", ") }</dd>
346 <dt>checks</dt>
347 <dd>
348 if p.ChecksBlock {
349 failing ones block the merge
350 } else {
351 do not block the merge
352 }
353 </dd>
354 </dl>
355 </section>
356}
357
358// mrBar is the bar above the diff, which follows the review: the totals, the
359// layout of the diff, the state of the checks and, for the owner of an open
360// request, the merge menu and the other actions. Each menu is a <details>: it
361// works without scripts; with them, M and the keys of the menu drive it.
362templ mrBar(p MRPage) {
363 <div class="diff-tools mr-bar" role="group" aria-label="Diff view and merge">
364 <span class="dim">{ plural(p.NFiles, "file", "files") } · <span class="plus">+{ itoa(p.Adds) }</span> <span class="minus">-{ itoa(p.Dels) }</span></span>
365 @viewLink(p.DiffView, "unified", "Unified", "u", p.UnifiedHref)
366 @viewLink(p.DiffView, "split", "Split", "d", p.SplitHref)
367 @collapseButton()
368 <span class="grow"></span>
369 @MRChecksSum(p.ChecksSum)
370 if p.Box != nil {
371 @mergeMenu(p)
372 @moreMenu(p)
373 }
374 </div>
375}
376
377// mergeMenu is the merge of an open request, for the owner: the form (it
378// carries the head that was read), or "Ready for review" for a draft. The
379// mode and the branch deletion have keys (1-3, D) while it is open.
380templ mergeMenu(p MRPage) {
381 <details class="menu mmenu" id="merge-menu" data-state="open">
382 <summary class="btn primary" aria-keyshortcuts="Shift+M">
383 if p.Box.ReadyAction != "" {
384 Draft
385 } else {
386 Merge
387 }
388 <kbd aria-hidden="true">M</kbd>
389 </summary>
390 <div class="menu-pop" role="group" aria-label="Merge">
391 <dl class="kv">
392 <dt>head</dt>
393 <dd>
394 if p.Box.HeadGone {
395 gone: the source branch was deleted
396 } else {
397 { p.HeadShort }
398 }
399 </dd>
400 <dt>into</dt>
401 <dd>{ p.Target }</dd>
402 </dl>
403 if p.Box.ReadyAction != "" {
404 <p class="d-draft">this is a draft: it cannot be merged until it is ready for review.</p>
405 <form class="d-btns" method="post" action={ templ.SafeURL(p.Box.ReadyAction) }>
406 <button type="submit" class="btn primary" data-default title="remove the draft prefix from the title">Ready for review</button>
407 </form>
408 } else if !p.Box.HeadGone {
409 <form class="d-acts" method="post" action={ templ.SafeURL(p.Box.MergeAction) }>
410 <input type="hidden" name="head_id" value={ p.Box.Head }/>
411 <fieldset class="d-modes">
412 <legend class="sr">merge mode</legend>
413 for i, m := range p.Box.Modes {
414 <label>
415 <input type="radio" name="mode" value={ m.Value } checked?={ i == 0 }/>
416 { m.Label }
417 if i < 9 {
418 <kbd aria-hidden="true">{ itoa(i + 1) }</kbd>
419 }
420 </label>
421 }
422 </fieldset>
423 if p.Box.CanDeleteBranch {
424 <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>
425 }
426 <div class="d-btns">
427 <button type="submit" class="btn primary" data-default>Merge into { p.Target }</button>
428 </div>
429 </form>
430 }
431 </div>
432 </details>
433}
434
435// moreMenu is what else the owner does with an open request: mark the head
436// read, close it, merge it from a terminal.
437templ moreMenu(p MRPage) {
438 <details class="menu omenu">
439 <summary class="btn icon" aria-label="More actions" title="more actions">⋯</summary>
440 <div class="menu-pop" role="group" aria-label="More actions">
441 if p.Box.CanMarkRead {
442 <form method="post" action={ templ.SafeURL(p.Box.ReadAction) }>
443 <input type="hidden" name="head_id" value={ p.Box.Head }/>
444 <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>
445 </form>
446 }
447 <form method="post" action={ templ.SafeURL(p.Box.CloseAction) }>
448 <button type="submit" title="close without merging; the branch is kept" class="btn danger">Close</button>
449 </form>
450 if !p.Box.HeadGone && p.Box.ReadyAction == "" {
451 <details class="d-or">
452 <summary class="mute">or from a terminal</summary>
453 @CloneBlock(p.Box.CLI)
454 </details>
455 }
456 </div>
457 </details>
458}
459
460templ Compare(l Layout, p ComparePage) {
461 @Shell(l, compareRail(p)) {
462 <header class="show mr-h">
463 <h1>{ p.Source } → { p.Target }</h1>
464 <dl class="kv show-kv">
465 <dt>branch</dt>
466 <dd><a href={ templ.SafeURL(p.SourceHref) }>{ p.Source }</a></dd>
467 <dt>target</dt>
468 <dd><a href={ templ.SafeURL(p.TargetHref) }>{ p.Target }</a></dd>
469 <dt>ahead</dt>
470 <dd>{ p.AheadText }</dd>
471 </dl>
472 </header>
473 if p.Note != "" {
474 <p class="notice">{ p.Note }</p>
475 }
476 if p.Existing != nil {
477 <p class="notice">an open merge request already covers these branches: <a href={ templ.SafeURL(p.Existing.Href) }>{ p.Existing.Label }</a></p>
478 }
479 if p.Create != nil {
480 <form class="cform create" method="post" action={ templ.SafeURL(p.Create.Action) }>
481 <input type="hidden" name="source" value={ p.Create.Source }/>
482 <input type="hidden" name="target" value={ p.Create.Target }/>
483 <label for="mr-title">title</label>
484 <input id="mr-title" name="title" type="text" maxlength="120" required value={ p.Create.Title } autocomplete="off"/>
485 <label for="mr-desc">description</label>
486 <textarea id="mr-desc" name="description" rows="4" maxlength="8000" placeholder="Markdown, optional"></textarea>
487 <div class="f-btns">
488 <button type="submit" class="btn primary">Create merge request</button>
489 </div>
490 </form>
491 }
492 if p.HasDiff {
493 <div class="diff-tools" role="group" aria-label="Diff view">
494 <span class="dim">{ plural(p.NFiles, "file", "files") } · <span class="plus">+{ itoa(p.Adds) }</span> <span class="minus">-{ itoa(p.Dels) }</span></span>
495 <span class="grow"></span>
496 @viewLink(p.DiffView, "unified", "Unified", "u", p.UnifiedHref)
497 @viewLink(p.DiffView, "split", "Split", "d", p.SplitHref)
498 @collapseButton()
499 </div>
500 if p.Invisible {
501 <p class="notice">
502 this diff holds invisible characters (direction controls, zero-width characters). They are shown as
503 <span class="inv"><U+XXXX></span> markers, never applied.
504 </p>
505 }
506 <div
507 id="diffs"
508 if p.Both {
509 data-diff={ p.View }
510 } else {
511 data-view={ p.View }
512 }
513 if p.Both && !p.Pinned {
514 hx-live="this.dataset.diff = sessionStorage.getItem('diff') || this.dataset.diff"
515 }
516 >
517 for _, f := range p.Files {
518 @DiffFileView(f, p.DiffView)
519 }
520 </div>
521 }
522 }
523}
524
525templ compareRail(p ComparePage) {
526 <section aria-labelledby="r-ahead">
527 <h2 id="r-ahead">Commits <span class="count">{ itoa(len(p.Ahead)) }</span></h2>
528 <ul class="pcommits">
529 for _, c := range p.Ahead {
530 <li>
531 <a href={ templ.SafeURL(c.CHref) }>{ c.Short }</a>
532 <span class="s">{ c.Subject }</span>
533 <span class="mute">
534 { c.Author }
535 if c.WhenAt != "" {
536 { " · " }<time datetime={ c.WhenAt }>{ c.When }</time>
537 }
538 </span>
539 </li>
540 }
541 </ul>
542 if len(p.Ahead) == 0 {
543 <p class="dim">no commit ahead of { p.Target }.</p>
544 }
545 </section>
546 if p.HasDiff {
547 <section aria-labelledby="r-files">
548 <h2 id="r-files">Files <span class="count">{ itoa(len(p.Files)) }</span></h2>
549 @FileList(p.Files)
550 </section>
551 }
552 <section aria-labelledby="r-cli">
553 <h2 id="r-cli">From a terminal</h2>
554 @CloneBlock(p.CLI)
555 </section>
556}