BlueGit

timfa bluegit

Files

bluegit / internal / server / pipeline_page.go

pipeline_page.go

feat: pipelines-tab en jobconsole
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed 5d0db17c History
pipeline_page.go 12.1 KiB Download Raw
1package server
2
3import (
4 "encoding/json"
5 "net/http"
6 "strconv"
7 "strings"
8 "time"
9
10 "gitlab.com/Timfa/bluegit/internal/access"
11 "gitlab.com/Timfa/bluegit/internal/ci"
12 "gitlab.com/Timfa/bluegit/internal/i18n"
13 "gitlab.com/Timfa/bluegit/internal/store"
14)
15
16// PipeRow is één rij in het pipeline-overzicht.
17type PipeRow struct {
18 ID int64
19 Status string
20 Label string
21 Duration string
22 When string
23 Title string
24 SHA string
25 Short string
26 Ref string
27 RefKind string
28 Actor string
29 Avatar string
30 Initial string
31 Stages []StageView
32}
33
34// StageView is één bolletje in de stages-kolom.
35type StageView struct {
36 Name string
37 Status string
38 Label string
39 Tip string
40 Jobs []JobLink
41}
42
43// JobLink is een job in een stage-popover of in de zijbalk.
44type JobLink struct {
45 ID int64
46 Name string
47 Status string
48 Label string
49 Current bool
50 Stage string
51}
52
53// JobPage is de console van één job.
54type JobPage struct {
55 Job store.Job
56 Pipe store.Pipeline
57 Name string
58 Status string
59 Label string
60 Actor string
61 Avatar string
62 Initial string
63 When string
64 Duration string
65 Finished string
66 Queued string
67 Timeout string
68 Source string
69 Ref string
70 RefKind string
71 Short string
72 Title string
73 Stage string
74 PipeStatus string
75 PipeLabel string
76 Stages []string
77 Related []JobLink
78 Blocks []LogBlock
79 Running bool
80 LogBytes int
81 CanPlay bool
82}
83
84// LogBlock is een losse regel of een inklapbare sectie.
85type LogBlock struct {
86 Section bool
87 Title string
88 Duration string
89 Open bool
90 Lines []LogLine
91}
92
93// LogLine is één regel in de console.
94type LogLine struct {
95 N int
96 Time string
97 Text string
98}
99
100func (s *Server) fillPipelines(r *http.Request, v *View, repo store.Repo) {
101 lang := s.langOf(r)
102 list, _ := s.Store.ListPipelines(r.Context(), repo.ID)
103 q := strings.ToLower(strings.TrimSpace(r.URL.Query().Get("q")))
104 v.Query = r.URL.Query().Get("q")
105 users := map[string]store.User{}
106 for _, p := range list {
107 row := s.pipeRow(r, lang, repo, p, users)
108 if q != "" && !pipeMatches(row, q) {
109 continue
110 }
111 v.PipeRows = append(v.PipeRows, row)
112 }
113}
114
115func (s *Server) pipeRow(r *http.Request, lang i18n.Lang, repo store.Repo, p store.Pipeline, users map[string]store.User) PipeRow {
116 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
117 name, kind := ci.SplitRef(p.Ref)
118 status := displayStatus(p.Status, jobs)
119 actor, avatar := s.actorOf(r, p.Actor, users)
120 initial := ""
121 if actor != "" {
122 initial = strings.ToUpper(actor[:1])
123 }
124 end := p.FinishedAt
125 if end.IsZero() {
126 end = time.Now()
127 }
128 start := p.StartedAt
129 if start.IsZero() {
130 start = p.CreatedAt
131 }
132 return PipeRow{
133 ID: p.ID, Status: status, Label: s.tr(r, "pipe."+status),
134 Duration: formatClock(end.Sub(start)), When: i18n.Ago(lang, p.CreatedAt.Unix()),
135 Title: p.CommitTitle, SHA: p.CommitSHA, Short: shortSHA(p.CommitSHA),
136 Ref: name, RefKind: kind, Actor: actor, Avatar: avatar, Initial: initial,
137 Stages: stageViews(lang, jobs, func(key string) string { return s.tr(r, key) }),
138 }
139}
140
141func (s *Server) actorOf(r *http.Request, name string, cache map[string]store.User) (string, string) {
142 name = strings.TrimSpace(name)
143 if name == "" {
144 return "", ""
145 }
146 u, ok := cache[name]
147 if !ok {
148 found, err := s.Store.UserByUsername(r.Context(), name)
149 if err == nil {
150 u = found
151 }
152 cache[name] = u
153 }
154 if u.ID == 0 {
155 return name, ""
156 }
157 return u.Username, u.AvatarURL
158}
159
160func pipeMatches(row PipeRow, q string) bool {
161 bits := []string{row.Label, row.Title, row.SHA, row.Short, row.Ref, row.Actor, strconv.FormatInt(row.ID, 10), row.Status}
162 for _, st := range row.Stages {
163 bits = append(bits, st.Name)
164 for _, j := range st.Jobs {
165 bits = append(bits, j.Name)
166 }
167 }
168 for _, b := range bits {
169 if strings.Contains(strings.ToLower(b), q) {
170 return true
171 }
172 }
173 return false
174}
175
176func displayStatus(pipe string, jobs []store.Job) string {
177 switch pipe {
178 case "failed":
179 return "failed"
180 case "running":
181 return "running"
182 case "manual":
183 return "manual"
184 case "queued":
185 return "pending"
186 }
187 warn := false
188 for _, j := range jobs {
189 if j.Status == "failed" && j.AllowFailure {
190 warn = true
191 }
192 }
193 if warn {
194 return "warning"
195 }
196 if pipe == "success" {
197 return "passed"
198 }
199 if pipe == "" {
200 return "pending"
201 }
202 return pipe
203}
204
205func stageViews(lang i18n.Lang, jobs []store.Job, tr func(string) string) []StageView {
206 var order []string
207 by := map[string][]store.Job{}
208 for _, j := range jobs {
209 if _, ok := by[j.Stage]; !ok {
210 order = append(order, j.Stage)
211 }
212 by[j.Stage] = append(by[j.Stage], j)
213 }
214 var out []StageView
215 for _, name := range order {
216 group := by[name]
217 status := stageStatus(group)
218 label := tr("pipe." + status)
219 var links []JobLink
220 for _, j := range group {
221 st := jobDisplay(j)
222 links = append(links, JobLink{ID: j.ID, Name: j.Name, Status: st, Label: tr("pipe." + st), Stage: j.Stage})
223 }
224 out = append(out, StageView{Name: name, Status: status, Label: label, Tip: name + ": " + strings.ToLower(label), Jobs: links})
225 }
226 _ = lang
227 return out
228}
229
230func stageStatus(jobs []store.Job) string {
231 pending, failed, warning, running := false, false, false, false
232 seen := false
233 for _, j := range jobs {
234 seen = true
235 switch {
236 case j.Status == "running":
237 running = true
238 case j.Status == "failed" && !j.AllowFailure:
239 failed = true
240 case j.Status == "failed" && j.AllowFailure:
241 warning = true
242 case j.Status == "queued" || j.Status == "manual":
243 pending = true
244 }
245 }
246 if !seen {
247 return "skipped"
248 }
249 if running {
250 return "running"
251 }
252 if failed {
253 return "failed"
254 }
255 if warning {
256 return "warning"
257 }
258 if pending {
259 return "pending"
260 }
261 allSkip := true
262 for _, j := range jobs {
263 if j.Status != "skipped" {
264 allSkip = false
265 }
266 }
267 if allSkip {
268 return "skipped"
269 }
270 return "passed"
271}
272
273func jobDisplay(j store.Job) string {
274 if j.Status == "failed" && j.AllowFailure {
275 return "warning"
276 }
277 switch j.Status {
278 case "success":
279 return "passed"
280 case "queued":
281 return "pending"
282 case "":
283 return "pending"
284 default:
285 return j.Status
286 }
287}
288
289func formatClock(d time.Duration) string {
290 if d < 0 {
291 d = 0
292 }
293 s := int(d.Seconds())
294 return pad2(s/3600) + ":" + pad2((s%3600)/60) + ":" + pad2(s%60)
295}
296
297func pad2(n int) string {
298 if n < 10 {
299 return "0" + strconv.Itoa(n)
300 }
301 return strconv.Itoa(n)
302}
303
304func shortSHA(sha string) string {
305 if len(sha) > 8 {
306 return sha[:8]
307 }
308 return sha
309}
310
311func (s *Server) showJob(w http.ResponseWriter, r *http.Request) {
312 _, repo, role, ok := s.guard(w, r, access.RoleReadonly)
313 if !ok {
314 return
315 }
316 p, job, ok := s.jobFromRequest(w, r, repo)
317 if !ok {
318 return
319 }
320 v := s.repoView(w, r, repo, role, job.Name)
321 v.Wide = true
322 v.Page = "pipelines"
323 v.Pipe = &p
324 v.JobPage = s.jobPage(r, repo, p, job, role)
325 s.render(w, "job", v)
326}
327
328func (s *Server) jobLog(w http.ResponseWriter, r *http.Request) {
329 _, repo, _, ok := s.guard(w, r, access.RoleReadonly)
330 if !ok {
331 return
332 }
333 _, job, ok := s.jobFromRequest(w, r, repo)
334 if !ok {
335 return
336 }
337 if r.URL.Query().Get("raw") == "1" {
338 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
339 _, _ = w.Write([]byte(job.Log))
340 return
341 }
342 offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
343 if offset < 0 || offset > len(job.Log) {
344 offset = 0
345 }
346 w.Header().Set("Content-Type", "application/json")
347 _ = json.NewEncoder(w).Encode(map[string]any{
348 "offset": len(job.Log),
349 "status": job.Status,
350 "text": job.Log[offset:],
351 })
352}
353
354func (s *Server) playJob(w http.ResponseWriter, r *http.Request) {
355 _, repo, _, ok := s.guard(w, r, access.RoleDeveloper)
356 if !ok || !s.csrfOK(w, r) {
357 return
358 }
359 p, job, ok := s.jobFromRequest(w, r, repo)
360 if !ok {
361 return
362 }
363 _, _ = s.Store.QueueJob(r.Context(), job.ID)
364 s.redirect(w, r, repoURL(repo)+"/-/pipelines/"+strconv.FormatInt(p.ID, 10)+"/jobs/"+strconv.FormatInt(job.ID, 10), s.tr(r, "flash.pipeline"), "")
365}
366
367func (s *Server) jobFromRequest(w http.ResponseWriter, r *http.Request, repo store.Repo) (store.Pipeline, store.Job, bool) {
368 pid, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
369 if err != nil {
370 http.NotFound(w, r)
371 return store.Pipeline{}, store.Job{}, false
372 }
373 jid, err := strconv.ParseInt(r.PathValue("job"), 10, 64)
374 if err != nil {
375 http.NotFound(w, r)
376 return store.Pipeline{}, store.Job{}, false
377 }
378 p, err := s.Store.PipelineByID(r.Context(), repo.ID, pid)
379 if err != nil {
380 http.NotFound(w, r)
381 return store.Pipeline{}, store.Job{}, false
382 }
383 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
384 for _, j := range jobs {
385 if j.ID == jid {
386 return p, j, true
387 }
388 }
389 http.NotFound(w, r)
390 return store.Pipeline{}, store.Job{}, false
391}
392
393func (s *Server) jobPage(r *http.Request, repo store.Repo, p store.Pipeline, job store.Job, role access.Role) *JobPage {
394 lang := s.langOf(r)
395 jobs, _ := s.Store.JobsByPipeline(r.Context(), p.ID)
396 users := map[string]store.User{}
397 actor, avatar := s.actorOf(r, p.Actor, users)
398 initial := ""
399 if actor != "" {
400 initial = strings.ToUpper(actor[:1])
401 }
402 name, kind := ci.SplitRef(p.Ref)
403 status := jobDisplay(job)
404 pipeStatus := displayStatus(p.Status, jobs)
405 end := job.FinishedAt
406 if end.IsZero() && (job.Status == "success" || job.Status == "failed" || job.Status == "skipped") {
407 end = p.FinishedAt
408 }
409 start := job.StartedAt
410 dur := ""
411 if !start.IsZero() {
412 until := end
413 if until.IsZero() {
414 until = time.Now()
415 }
416 dur = formatClock(until.Sub(start))
417 }
418 queued := ""
419 if !start.IsZero() && !p.CreatedAt.IsZero() {
420 queued = formatClock(start.Sub(p.CreatedAt))
421 }
422 timeout := formatClock(job.Timeout)
423 if job.Timeout == 0 && s.Cfg.CI.TimeoutSec > 0 {
424 timeout = formatClock(time.Duration(s.Cfg.CI.TimeoutSec) * time.Second)
425 }
426 finished := ""
427 if !end.IsZero() {
428 finished = i18n.Ago(lang, end.Unix())
429 }
430 var stages []string
431 seen := map[string]bool{}
432 var related []JobLink
433 for _, j := range jobs {
434 if !seen[j.Stage] {
435 seen[j.Stage] = true
436 stages = append(stages, j.Stage)
437 }
438 st := jobDisplay(j)
439 related = append(related, JobLink{ID: j.ID, Name: j.Name, Status: st, Label: s.tr(r, "pipe."+st), Current: j.ID == job.ID, Stage: j.Stage})
440 }
441 sourceKey := "pipe.src." + p.Source
442 source := s.tr(r, sourceKey)
443 if source == sourceKey {
444 source = p.Source
445 }
446 return &JobPage{
447 Job: job, Pipe: p, Name: job.Name, Status: status, Label: s.tr(r, "pipe."+status),
448 Actor: actor, Avatar: avatar, Initial: initial, When: i18n.Ago(lang, p.CreatedAt.Unix()),
449 Duration: dur, Finished: finished, Queued: queued, Timeout: timeout, Source: source,
450 Ref: name, RefKind: kind, Short: shortSHA(p.CommitSHA), Title: p.CommitTitle, Stage: job.Stage,
451 PipeStatus: pipeStatus, PipeLabel: s.tr(r, "pipe."+pipeStatus),
452 Stages: stages, Related: related, Blocks: parseLog(job.Log), Running: job.Status == "running" || job.Status == "queued",
453 LogBytes: len(job.Log), CanPlay: job.Status == "manual" && role.CanWriteCode(),
454 }
455}
456
457func parseLog(raw string) []LogBlock {
458 var blocks []LogBlock
459 var cur *LogBlock
460 n := 0
461 flush := func() {
462 if cur == nil {
463 return
464 }
465 blocks = append(blocks, *cur)
466 cur = nil
467 }
468 for _, row := range strings.Split(raw, "\n") {
469 if row == "" {
470 continue
471 }
472 ts, text := row, row
473 if i := strings.IndexByte(row, '\t'); i >= 0 {
474 ts, text = row[:i], row[i+1:]
475 }
476 if strings.HasPrefix(text, "section_start:") {
477 flush()
478 body := strings.TrimPrefix(text, "section_start:")
479 title := body
480 if i := strings.IndexByte(body, ':'); i >= 0 {
481 title = body[i+1:]
482 }
483 cur = &LogBlock{Section: true, Title: title, Open: true}
484 continue
485 }
486 if strings.HasPrefix(text, "section_end:") {
487 dur := ""
488 body := strings.TrimPrefix(text, "section_end:")
489 if i := strings.IndexByte(body, '\t'); i >= 0 {
490 dur = body[i+1:]
491 }
492 if cur != nil {
493 cur.Duration = dur
494 flush()
495 }
496 continue
497 }
498 n++
499 line := LogLine{N: n, Time: clockOf(ts), Text: text}
500 if cur != nil {
501 cur.Lines = append(cur.Lines, line)
502 continue
503 }
504 blocks = append(blocks, LogBlock{Lines: []LogLine{line}})
505 }
506 if cur != nil {
507 flush()
508 }
509 return blocks
510}
511
512func clockOf(ts string) string {
513 if t, err := time.Parse(time.RFC3339, ts); err == nil {
514 return t.UTC().Format("15:04:05")
515 }
516 if len(ts) >= 19 && strings.Contains(ts, "T") {
517 return ts[11:19]
518 }
519 return ""
520}