BlueGit

timfa bluegit

Files

bluegit / internal / server / pipeline_page.go

pipeline_page.go

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