BlueGit

timfa bluegit

Files

bluegit / internal / server / pipeline_page.go

pipeline_page.go

feat: toon repositories en groepen op het profiel
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Passed 69db8c2f History
pipeline_page.go 19.5 KiB Download Raw
1package server
2
3import (
4 "bytes"
5 "encoding/json"
6 "net/http"
7 "net/url"
8 "strconv"
9 "strings"
10 "time"
11
12 "gitlab.com/Timfa/bluegit/internal/access"
13 "gitlab.com/Timfa/bluegit/internal/ci"
14 "gitlab.com/Timfa/bluegit/internal/i18n"
15 "gitlab.com/Timfa/bluegit/internal/store"
16)
17
18// PipeRow is één rij in het pipeline-overzicht.
19type PipeRow struct {
20 ID int64
21 Status string
22 Label string
23 Duration string
24 When string
25 Title string
26 SHA string
27 Short string
28 Ref string
29 RefKind string
30 Source string
31 SourceLabel string
32 MRNumber int64
33 Actor string
34 ActorURL string
35 Avatar string
36 Initial string
37 Stages []StageView
38}
39
40// StageView is één bolletje in de stages-kolom.
41type StageView struct {
42 Name string
43 Status string
44 Label string
45 Tip string
46 Jobs []JobLink
47}
48
49// JobLink is een job in een stage-popover of in de zijbalk.
50type JobLink struct {
51 ID int64
52 Name string
53 Status string
54 Label string
55 Queue string
56 Current bool
57 Stage string
58}
59
60// JobPage is de console van één job.
61type JobPage struct {
62 Job store.Job
63 Pipe store.Pipeline
64 Name string
65 Status string
66 Label string
67 Actor string
68 ActorURL string
69 Avatar string
70 Initial string
71 When string
72 Duration string
73 Finished string
74 Queued string
75 Timeout string
76 Source string
77 Ref string
78 RefKind string
79 Short string
80 Title string
81 Stage string
82 PipeStatus string
83 PipeLabel string
84 Stages []string
85 Related []JobLink
86 Blocks []LogBlock
87 Running bool
88 LogBytes int
89 CanPlay bool
90 Environment string
91 EnvironmentURL string
92 EnvironmentLink string
93 Queue string
94}
95
96// LogBlock is een losse regel of een inklapbare sectie.
97type LogBlock struct {
98 Section bool
99 Title string
100 Duration string
101 Open bool
102 Lines []LogLine
103}
104
105// LogLine is één regel in de console.
106type LogLine struct {
107 N int
108 Time string
109 Text string
110 Spans []LogSpan
111}
112
113func (s *Server) fillPipelines(r *http.Request, v *View, repo store.Repo) {
114 lang := s.langOf(r)
115 list, _ := s.Store.ListPipelines(r.Context(), repo.ID)
116 q := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("q")))
117 v.Query = r.URL.Query().Get("q")
118 ranks := s.queueRanks(r.Context())
119 users := map[string]store.User{}
120 for _, p := range list {
121 row := s.pipeRow(r, lang, repo, p, users, ranks)
122 if q != "" && !pipeMatches(row, q) {
123 continue
124 }
125 if rowLive(row) {
126 v.Live = true
127 }
128 v.PipeRows = append(v.PipeRows, row)
129 }
130 v.LiveURL = repoURL(repo) + "/-/pipelines"
131 if raw := strings.TrimSpace(r.URL.Query().Get("q")); raw != "" {
132 v.LiveURL += "?q=" + url.QueryEscape(raw)
133 }
134}
135
136func rowLive(row PipeRow) bool {
137 if row.Status == "pending" || row.Status == "running" {
138 return true
139 }
140 for _, st := range row.Stages {
141 if st.Status == "pending" || st.Status == "running" {
142 return true
143 }
144 for _, j := range st.Jobs {
145 if j.Status == "pending" || j.Status == "running" {
146 return true
147 }
148 }
149 }
150 return false
151}
152
153func (s *Server) pipeRow(r *http.Request, lang i18n.Lang, repo store.Repo, p store.Pipeline, users map[string]store.User, ranks map[int64]int) PipeRow {
154 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
155 name, kind := ci.SplitRef(p.Ref)
156 status := displayStatus(p.Status, jobs)
157 actor, avatar, actorURL := s.actorOf(r, p.Actor, users)
158 initial := ""
159 if actor != "" {
160 initial = strings.ToUpper(actor[:1])
161 }
162 sourceLabel, mrNumber := s.pipelineSourceLabel(r, repo, p)
163 return PipeRow{
164 ID: p.ID, Status: status, Label: s.tr(r, "pipe."+status),
165 Duration: pipelineClock(p), When: i18n.Ago(lang, p.CreatedAt.Unix()),
166 Title: p.CommitTitle, SHA: p.CommitSHA, Short: shortSHA(p.CommitSHA),
167 Ref: name, RefKind: kind, Source: p.Source, SourceLabel: sourceLabel, MRNumber: mrNumber,
168 Actor: actor, ActorURL: actorURL, Avatar: avatar, Initial: initial,
169 Stages: stageViews(jobs, ranks, func(key string, args ...any) string { return s.tr(r, key, args...) }),
170 }
171}
172
173// pipelineSourceLabel benoemt een pipeline die bij een merge request hoort,
174// ook wanneer de rij als push is gestart en daarna gekoppeld is.
175// Een gewone push zonder merge request krijgt geen extra label.
176func (s *Server) pipelineSourceLabel(r *http.Request, repo store.Repo, p store.Pipeline) (string, int64) {
177 if p.MRID != 0 {
178 var number int64
179 if mr, err := s.Store.MergeRequest(r.Context(), repo.ID, p.MRID); err == nil {
180 number = mr.Number
181 }
182 return s.tr(r, "pipe.src.merge_request_event"), number
183 }
184 if p.Source == "" || p.Source == "push" {
185 return "", 0
186 }
187 key := "pipe.src." + p.Source
188 label := s.tr(r, key)
189 if label == key {
190 label = p.Source
191 }
192 return label, 0
193}
194
195func (s *Server) pipelineSourceText(r *http.Request, repo store.Repo, p store.Pipeline) string {
196 label, number := s.pipelineSourceLabel(r, repo, p)
197 if label == "" {
198 return ""
199 }
200 if number == 0 {
201 return label
202 }
203 return label + " !" + strconv.FormatInt(number, 10)
204}
205
206func (s *Server) actorOf(r *http.Request, name string, cache map[string]store.User) (string, string, string) {
207 name = strings.TrimSpace(name)
208 if name == "" {
209 return "", "", ""
210 }
211 u, ok := cache[name]
212 if !ok {
213 found, err := s.Store.UserByUsername(r.Context(), name)
214 if err == nil {
215 u = found
216 }
217 cache[name] = u
218 }
219 if u.ID == 0 {
220 return name, "", ""
221 }
222 return u.Username, u.AvatarURL, "/users/" + u.Username
223}
224
225func pipeMatches(row PipeRow, q string) bool {
226 bits := []string{row.Label, row.Title, row.SHA, row.Short, row.Ref, row.Actor, row.SourceLabel, strconv.FormatInt(row.ID, 10), row.Status}
227 for _, st := range row.Stages {
228 bits = append(bits, st.Name)
229 for _, j := range st.Jobs {
230 bits = append(bits, j.Name)
231 }
232 }
233 for _, b := range bits {
234 if strings.Contains(strings.ToLower(b), q) {
235 return true
236 }
237 }
238 return false
239}
240
241func displayStatus(pipe string, jobs []store.Job) string {
242 switch pipe {
243 case "failed":
244 return "failed"
245 case "running":
246 return "running"
247 case "manual":
248 return "manual"
249 case "queued":
250 return "pending"
251 case "canceled":
252 return "canceled"
253 }
254 warn := false
255 for _, j := range jobs {
256 if j.Status == "failed" && j.AllowFailure {
257 warn = true
258 }
259 }
260 if warn {
261 return "warning"
262 }
263 if pipe == "success" {
264 return "passed"
265 }
266 if pipe == "" {
267 return "pending"
268 }
269 return pipe
270}
271
272func stageViews(jobs []store.Job, ranks map[int64]int, tr func(string, ...any) string) []StageView {
273 var order []string
274 by := map[string][]store.Job{}
275 for _, j := range jobs {
276 if _, ok := by[j.Stage]; !ok {
277 order = append(order, j.Stage)
278 }
279 by[j.Stage] = append(by[j.Stage], j)
280 }
281 var out []StageView
282 for _, name := range order {
283 group := by[name]
284 status := stageStatus(group)
285 label := tr("pipe." + status)
286 var links []JobLink
287 for _, j := range group {
288 st := jobDisplay(j)
289 links = append(links, JobLink{
290 ID: j.ID, Name: j.Name, Status: st, Label: tr("pipe." + st), Stage: j.Stage,
291 Queue: queueText(tr, ranks, j.ID),
292 })
293 }
294 out = append(out, StageView{Name: name, Status: status, Label: label, Tip: name + ": " + strings.ToLower(label), Jobs: links})
295 }
296 return out
297}
298
299func stageStatus(jobs []store.Job) string {
300 pending, failed, warning, running, canceled := false, false, false, false, false
301 seen := false
302 for _, j := range jobs {
303 seen = true
304 switch {
305 case j.Status == "running":
306 running = true
307 case j.Status == "failed" && !j.AllowFailure:
308 failed = true
309 case j.Status == "canceled" && !j.AllowFailure:
310 canceled = true
311 case j.Status == "failed" && j.AllowFailure:
312 warning = true
313 case j.Status == "queued" || j.Status == "manual":
314 pending = true
315 }
316 }
317 if !seen {
318 return "skipped"
319 }
320 if running {
321 return "running"
322 }
323 if failed {
324 return "failed"
325 }
326 if canceled {
327 return "canceled"
328 }
329 if warning {
330 return "warning"
331 }
332 if pending {
333 return "pending"
334 }
335 allSkip := true
336 for _, j := range jobs {
337 if j.Status != "skipped" {
338 allSkip = false
339 }
340 }
341 if allSkip {
342 return "skipped"
343 }
344 return "passed"
345}
346
347func jobDisplay(j store.Job) string {
348 if j.Status == "failed" && j.AllowFailure {
349 return "warning"
350 }
351 switch j.Status {
352 case "success":
353 return "passed"
354 case "queued":
355 return "pending"
356 case "":
357 return "pending"
358 default:
359 return j.Status
360 }
361}
362
363func pipelineClock(p store.Pipeline) string {
364 end := p.FinishedAt
365 if end.IsZero() {
366 end = time.Now()
367 }
368 start := p.StartedAt
369 if start.IsZero() {
370 start = p.CreatedAt
371 }
372 if start.IsZero() {
373 return ""
374 }
375 return formatClock(end.Sub(start))
376}
377
378func formatClock(d time.Duration) string {
379 if d < 0 {
380 d = 0
381 }
382 s := int(d.Seconds())
383 return pad2(s/3600) + ":" + pad2((s%3600)/60) + ":" + pad2(s%60)
384}
385
386func pad2(n int) string {
387 if n < 10 {
388 return "0" + strconv.Itoa(n)
389 }
390 return strconv.Itoa(n)
391}
392
393func shortSHA(sha string) string {
394 if len(sha) > 8 {
395 return sha[:8]
396 }
397 return sha
398}
399
400func (s *Server) showJob(w http.ResponseWriter, r *http.Request) {
401 _, repo, role, ok := s.guard(w, r, access.RoleReadonly)
402 if !ok {
403 return
404 }
405 p, job, ok := s.jobFromRequest(w, r, repo)
406 if !ok {
407 return
408 }
409 v := s.repoView(w, r, repo, role, job.Name)
410 v.Wide = true
411 v.Page = "pipelines"
412 v.Pipe = &p
413 v.JobPage = s.jobPage(r, repo, p, job, role)
414 s.render(w, "job", v)
415}
416
417func (s *Server) jobLog(w http.ResponseWriter, r *http.Request) {
418 _, repo, _, ok := s.guard(w, r, access.RoleReadonly)
419 if !ok {
420 return
421 }
422 _, job, ok := s.jobFromRequest(w, r, repo)
423 if !ok {
424 return
425 }
426 if r.URL.Query().Get("raw") == "1" {
427 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
428 _, _ = w.Write([]byte(job.Log))
429 return
430 }
431 offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
432 if offset < 0 || offset > len(job.Log) {
433 offset = 0
434 }
435 queue := ""
436 if job.Status == "queued" {
437 tr := func(key string, args ...any) string { return s.tr(r, key, args...) }
438 queue = queueText(tr, s.queueRanks(r.Context()), job.ID)
439 }
440 w.Header().Set("Content-Type", "application/json")
441 w.Header().Set("Cache-Control", "no-store")
442 _ = json.NewEncoder(w).Encode(map[string]any{
443 "offset": len(job.Log),
444 "status": job.Status,
445 "text": job.Log[offset:],
446 "queue": queue,
447 })
448}
449
450func (s *Server) playJob(w http.ResponseWriter, r *http.Request) {
451 u, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
452 if !ok || !s.csrfOK(w, r) {
453 return
454 }
455 p, job, ok := s.jobFromRequest(w, r, repo)
456 if !ok {
457 return
458 }
459 _, _ = s.opJobAction(r.Context(), u, repo, p.ID, job.ID, "play")
460 s.redirect(w, r, repoURL(repo)+"/-/pipelines/"+strconv.FormatInt(p.ID, 10)+"/jobs/"+strconv.FormatInt(job.ID, 10), s.tr(r, "flash.pipeline"), "")
461}
462
463func (s *Server) jobFromRequest(w http.ResponseWriter, r *http.Request, repo store.Repo) (store.Pipeline, store.Job, bool) {
464 pid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
465 if err != nil {
466 http.NotFound(w, r)
467 return store.Pipeline{}, store.Job{}, false
468 }
469 jid, err := strconv.ParseInt(r.PathValue("job"), 10, 64)
470 if err != nil {
471 http.NotFound(w, r)
472 return store.Pipeline{}, store.Job{}, false
473 }
474 p, err := s.Store.PipelineByID(r.Context(), repo.ID, pid)
475 if err != nil {
476 http.NotFound(w, r)
477 return store.Pipeline{}, store.Job{}, false
478 }
479 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
480 for _, j := range jobs {
481 if j.ID == jid {
482 return p, j, true
483 }
484 }
485 http.NotFound(w, r)
486 return store.Pipeline{}, store.Job{}, false
487}
488
489func (s *Server) jobPage(r *http.Request, repo store.Repo, p store.Pipeline, job store.Job, role access.Role) *JobPage {
490 lang := s.langOf(r)
491 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
492 users := map[string]store.User{}
493 actor, avatar, actorURL := s.actorOf(r, p.Actor, users)
494 initial := ""
495 if actor != "" {
496 initial = strings.ToUpper(actor[:1])
497 }
498 name, kind := ci.SplitRef(p.Ref)
499 status := jobDisplay(job)
500 pipeStatus := displayStatus(p.Status, jobs)
501 end := job.FinishedAt
502 if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") {
503 end = p.FinishedAt
504 }
505 start := job.StartedAt
506 dur := ""
507 if !start.IsZero() {
508 until := end
509 if until.IsZero() {
510 until = time.Now()
511 }
512 dur = formatClock(until.Sub(start))
513 }
514 queued := ""
515 if !start.IsZero() && !p.CreatedAt.IsZero() {
516 queued = formatClock(start.Sub(p.CreatedAt))
517 }
518 timeout := formatClock(job.Timeout)
519 if job.Timeout == 0 && s.Cfg.CI.TimeoutSec > 0 {
520 timeout = formatClock(time.Duration(s.Cfg.CI.TimeoutSec) * time.Second)
521 }
522 finished := ""
523 if !end.IsZero() {
524 finished = i18n.Ago(lang, end.Unix())
525 }
526 ranks := s.queueRanks(r.Context())
527 tr := func(key string, args ...any) string { return s.tr(r, key, args...) }
528 var stages []string
529 seen := map[string]bool{}
530 var related []JobLink
531 for _, j := range jobs {
532 if !seen[j.Stage] {
533 seen[j.Stage] = true
534 stages = append(stages, j.Stage)
535 }
536 st := jobDisplay(j)
537 related = append(related, JobLink{
538 ID: j.ID, Name: j.Name, Status: st, Label: tr("pipe." + st), Current: j.ID == job.ID, Stage: j.Stage,
539 Queue: queueText(tr, ranks, j.ID),
540 })
541 }
542 sourceKey := "pipe.src." + p.Source
543 source := s.tr(r, sourceKey)
544 if source == sourceKey {
545 source = p.Source
546 }
547 return &JobPage{
548 Job: job, Pipe: p, Name: job.Name, Status: status, Label: s.tr(r, "pipe."+status),
549 Actor: actor, ActorURL: actorURL, Avatar: avatar, Initial: initial, When: i18n.Ago(lang, p.CreatedAt.Unix()),
550 Duration: dur, Finished: finished, Queued: queued, Timeout: timeout, Source: source,
551 Ref: name, RefKind: kind, Short: shortSHA(p.CommitSHA), Title: p.CommitTitle, Stage: job.Stage,
552 PipeStatus: pipeStatus, PipeLabel: s.tr(r, "pipe."+pipeStatus),
553 Stages: stages, Related: related, Blocks: parseLog(job.Log), Running: job.Status == "running" || job.Status == "queued",
554 LogBytes: len(job.Log), CanPlay: job.Status == "manual" && role.CanWriteCode(),
555 Queue: queueText(tr, ranks, job.ID),
556 Environment: job.Environment, EnvironmentURL: job.EnvironmentURL, EnvironmentLink: safeEnvironmentURL(job.EnvironmentURL),
557 }
558}
559
560func safeEnvironmentURL(raw string) string {
561 if strings.Contains(raw, "$") {
562 return ""
563 }
564 if strings.HasPrefix(raw, "https://") || strings.HasPrefix(raw, "http://") {
565 return raw
566 }
567 return ""
568}
569
570func parseLog(raw string) []LogBlock {
571 var blocks []LogBlock
572 var cur *LogBlock
573 n := 0
574 flush := func() {
575 if cur == nil {
576 return
577 }
578 blocks = append(blocks, *cur)
579 cur = nil
580 }
581 for _, row := range strings.Split(raw, "\n") {
582 if row == "" {
583 continue
584 }
585 ts, text := row, row
586 if i := strings.IndexByte(row, '\t'); i >= 0 {
587 ts, text = row[:i], row[i+1:]
588 }
589 if strings.HasPrefix(text, "section_start:") {
590 flush()
591 body := strings.TrimPrefix(text, "section_start:")
592 title := body
593 if i := strings.IndexByte(body, ':'); i >= 0 {
594 title = body[i+1:]
595 }
596 cur = &LogBlock{Section: true, Title: title, Open: true}
597 continue
598 }
599 if strings.HasPrefix(text, "section_end:") {
600 dur := ""
601 body := strings.TrimPrefix(text, "section_end:")
602 if i := strings.IndexByte(body, '\t'); i >= 0 {
603 dur = body[i+1:]
604 }
605 if cur != nil {
606 cur.Duration = dur
607 flush()
608 }
609 continue
610 }
611 n++
612 line := LogLine{N: n, Time: clockOf(ts), Text: text, Spans: ansiSpans(text)}
613 if cur != nil {
614 cur.Lines = append(cur.Lines, line)
615 continue
616 }
617 blocks = append(blocks, LogBlock{Lines: []LogLine{line}})
618 }
619 if cur != nil {
620 flush()
621 }
622 return blocks
623}
624
625func wantsJSON(r *http.Request) bool {
626 return strings.Contains(r.Header.Get("Accept"), "application/json")
627}
628
629func jobsLive(pipe string, jobs []store.Job) bool {
630 if pipe == "queued" || pipe == "running" {
631 return true
632 }
633 for _, j := range jobs {
634 if j.Status == "queued" || j.Status == "running" {
635 return true
636 }
637 }
638 return false
639}
640
641func linksFor(jobs []store.Job, ranks map[int64]int, tr func(string, ...any) string) []JobLink {
642 out := make([]JobLink, 0, len(jobs))
643 for _, j := range jobs {
644 st := jobDisplay(j)
645 out = append(out, JobLink{
646 ID: j.ID, Name: j.Name, Status: st, Label: tr("pipe." + st), Stage: j.Stage,
647 Queue: queueText(tr, ranks, j.ID),
648 })
649 }
650 return out
651}
652
653func (s *Server) partial(name string, v View) string {
654 var buf bytes.Buffer
655 tmpl := s.byLang[v.Lang]
656 if tmpl == nil {
657 tmpl = s.tmpl
658 }
659 if tmpl == nil {
660 return ""
661 }
662 if err := tmpl.ExecuteTemplate(&buf, name, v); err != nil && s.Log != nil {
663 s.Log.Error("sjabloon", "name", name, "err", err)
664 }
665 return buf.String()
666}
667
668func (s *Server) writePipeList(w http.ResponseWriter, v View) {
669 type job struct {
670 ID int64 `json:"id"`
671 Name string `json:"name"`
672 Status string `json:"status"`
673 Label string `json:"label"`
674 Queue string `json:"queue"`
675 }
676 type stage struct {
677 Name string `json:"name"`
678 Status string `json:"status"`
679 Tip string `json:"tip"`
680 Jobs []job `json:"jobs"`
681 }
682 type row struct {
683 ID int64 `json:"id"`
684 Status string `json:"status"`
685 Label string `json:"label"`
686 Duration string `json:"duration"`
687 When string `json:"when"`
688 Stages []stage `json:"stages"`
689 HTML string `json:"html"`
690 }
691 rows := make([]row, 0, len(v.PipeRows))
692 for _, pr := range v.PipeRows {
693 one := v
694 one.PipeRows = []PipeRow{pr}
695 item := row{
696 ID: pr.ID, Status: pr.Status, Label: pr.Label, Duration: pr.Duration, When: pr.When,
697 HTML: s.partial("pipe-rows", one),
698 }
699 for _, st := range pr.Stages {
700 jobs := make([]job, 0, len(st.Jobs))
701 for _, j := range st.Jobs {
702 jobs = append(jobs, job{ID: j.ID, Name: j.Name, Status: j.Status, Label: j.Label, Queue: j.Queue})
703 }
704 item.Stages = append(item.Stages, stage{Name: st.Name, Status: st.Status, Tip: st.Tip, Jobs: jobs})
705 }
706 rows = append(rows, item)
707 }
708 w.Header().Set("Content-Type", "application/json")
709 w.Header().Set("Cache-Control", "no-store")
710 _ = json.NewEncoder(w).Encode(map[string]any{"active": v.Live, "rows": rows})
711}
712
713func (s *Server) writePipeDetail(w http.ResponseWriter, v View) {
714 type job struct {
715 ID int64 `json:"id"`
716 Name string `json:"name"`
717 Status string `json:"status"`
718 Label string `json:"label"`
719 Stage string `json:"stage"`
720 Queue string `json:"queue"`
721 }
722 type stageJob struct {
723 ID int64 `json:"id"`
724 Name string `json:"name"`
725 Status string `json:"status"`
726 Label string `json:"label"`
727 Queue string `json:"queue"`
728 }
729 type stage struct {
730 Name string `json:"name"`
731 Status string `json:"status"`
732 Tip string `json:"tip"`
733 Jobs []stageJob `json:"jobs"`
734 }
735 jobs := make([]job, 0, len(v.PipeJobs))
736 for _, j := range v.PipeJobs {
737 jobs = append(jobs, job{ID: j.ID, Name: j.Name, Status: j.Status, Label: j.Label, Stage: j.Stage, Queue: j.Queue})
738 }
739 stages := make([]stage, 0, len(v.PipeStages))
740 for _, st := range v.PipeStages {
741 item := stage{Name: st.Name, Status: st.Status, Tip: st.Tip, Jobs: make([]stageJob, 0, len(st.Jobs))}
742 for _, j := range st.Jobs {
743 item.Jobs = append(item.Jobs, stageJob{ID: j.ID, Name: j.Name, Status: j.Status, Label: j.Label, Queue: j.Queue})
744 }
745 stages = append(stages, item)
746 }
747 dur := ""
748 html := ""
749 if v.Pipe != nil {
750 dur = pipelineClock(*v.Pipe)
751 if v.Repo != nil {
752 head := CommitRow{PipeID: v.Pipe.ID, PipeStages: v.PipeStages}
753 html = s.partial("stage-dots", View{Lang: v.Lang, Repo: v.Repo, Head: &head})
754 }
755 }
756 w.Header().Set("Content-Type", "application/json")
757 w.Header().Set("Cache-Control", "no-store")
758 _ = json.NewEncoder(w).Encode(map[string]any{
759 "active": v.Live, "status": v.PipeStatus, "label": v.PipeLabel,
760 "duration": dur, "jobs": jobs, "stages": stages, "html": html,
761 })
762}
763
764func clockOf(ts string) string {
765 if t, err := time.Parse(time.RFC3339, ts); err == nil {
766 return t.UTC().Format("15:04:05")
767 }
768 if len(ts) >= 19 && strings.Contains(ts, "T") {
769 return ts[11:19]
770 }
771 return ""
772}