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