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