BlueGit

timfa bluegit

Files

bluegit / internal / server / pipeline_page.go

pipeline_page.go

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