| 1 | // Package ci leest blue-ci-cd.yml en bepaalt welke jobs draaien. |
| 2 | // De vorm volgt GitLab CI waar dat in BlueGit past. Een oud bestand met |
| 3 | // een jobs:-map blijft geldig. Jobs mogen ook los op het hoogste niveau staan. |
| 4 | package ci |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "time" |
| 11 | |
| 12 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 13 | "gopkg.in/yaml.v3" |
| 14 | ) |
| 15 | |
| 16 | // Parse leest YAML. Jobvolgorde blijft de volgorde uit het bestand. |
| 17 | func Parse(data []byte) (Pipeline, error) { |
| 18 | return ParseIn(data, nil) |
| 19 | } |
| 20 | |
| 21 | // ParseIn leest YAML en haalt include: local via read op. |
| 22 | func ParseIn(data []byte, read func(string) ([]byte, error)) (Pipeline, error) { |
| 23 | p, issues, err := parseBytes(data, read, nil) |
| 24 | if err != nil { |
| 25 | return Pipeline{}, err |
| 26 | } |
| 27 | for _, is := range issues { |
| 28 | if !is.Warning { |
| 29 | return Pipeline{}, is |
| 30 | } |
| 31 | } |
| 32 | p.Warnings = warningsOnly(issues) |
| 33 | return p, nil |
| 34 | } |
| 35 | |
| 36 | // Lint geeft elke fout en waarschuwing, met regelnummer, zonder te stoppen bij de eerste. |
| 37 | func Lint(data []byte, read func(string) ([]byte, error)) (Pipeline, []Issue) { |
| 38 | p, issues, err := parseBytes(data, read, nil) |
| 39 | if err != nil { |
| 40 | issues = append([]Issue{{Line: 1, Key: "ci.err.yaml", Args: []any{err.Error()}}}, issues...) |
| 41 | } |
| 42 | return p, issues |
| 43 | } |
| 44 | |
| 45 | func (i Issue) Error() string { return i18n.T(i18n.EN, i.Key, i.Args...) } |
| 46 | |
| 47 | func warningsOnly(in []Issue) []Issue { |
| 48 | var out []Issue |
| 49 | for _, is := range in { |
| 50 | if is.Warning { |
| 51 | out = append(out, is) |
| 52 | } |
| 53 | } |
| 54 | return out |
| 55 | } |
| 56 | |
| 57 | var reserved = map[string]bool{ |
| 58 | "stages": true, "variables": true, "image": true, "default": true, |
| 59 | "include": true, "before_script": true, "after_script": true, "cache": true, |
| 60 | "workflow": true, "services": true, "spec": true, "pages": true, "jobs": true, |
| 61 | } |
| 62 | |
| 63 | type draft struct { |
| 64 | job Job |
| 65 | present map[string]bool |
| 66 | extends []string |
| 67 | line int |
| 68 | } |
| 69 | |
| 70 | func parseBytes(data []byte, read func(string) ([]byte, error), stack []string) (Pipeline, []Issue, error) { |
| 71 | var root yaml.Node |
| 72 | if err := yaml.Unmarshal(data, &root); err != nil { |
| 73 | return Pipeline{}, nil, i18n.E("ci.err.yaml", err.Error()) |
| 74 | } |
| 75 | if root.Kind != yaml.DocumentNode || len(root.Content) != 1 || root.Content[0].Kind != yaml.MappingNode { |
| 76 | return Pipeline{}, []Issue{{Line: 1, Key: "ci.err.mapping"}}, nil |
| 77 | } |
| 78 | body := root.Content[0] |
| 79 | var included Pipeline |
| 80 | var issues []Issue |
| 81 | for i := 0; i < len(body.Content); i += 2 { |
| 82 | if body.Content[i].Value != "include" { |
| 83 | continue |
| 84 | } |
| 85 | part, more, err := parseInclude(body.Content[i+1], read, stack) |
| 86 | if err != nil { |
| 87 | return Pipeline{}, nil, err |
| 88 | } |
| 89 | issues = append(issues, more...) |
| 90 | included = mergePipeline(included, part) |
| 91 | } |
| 92 | p := included |
| 93 | if p.Variables == nil { |
| 94 | p.Variables = map[string]string{} |
| 95 | } |
| 96 | var def draft |
| 97 | def.present = map[string]bool{} |
| 98 | var jobs []draft |
| 99 | seenLegacy := false |
| 100 | seenTop := false |
| 101 | for i := 0; i < len(body.Content); i += 2 { |
| 102 | keyNode := body.Content[i] |
| 103 | key := keyNode.Value |
| 104 | val := body.Content[i+1] |
| 105 | switch key { |
| 106 | case "include": |
| 107 | continue |
| 108 | case "stages": |
| 109 | stages, iss := scalarList(val, "ci.err.list") |
| 110 | issues = append(issues, iss...) |
| 111 | if len(stages) > 0 { |
| 112 | p.Stages = stages |
| 113 | } |
| 114 | case "variables": |
| 115 | vars, iss := scalarMap(val) |
| 116 | issues = append(issues, iss...) |
| 117 | for k, v := range vars { |
| 118 | p.Variables[k] = v |
| 119 | } |
| 120 | case "image": |
| 121 | img, iss := imageOf(val) |
| 122 | issues = append(issues, iss...) |
| 123 | if img != "" { |
| 124 | p.Image = img |
| 125 | } |
| 126 | case "before_script": |
| 127 | lines, iss := scriptList(val) |
| 128 | issues = append(issues, iss...) |
| 129 | p.Before = lines |
| 130 | def.present["before_script"] = true |
| 131 | def.job.Before = lines |
| 132 | case "after_script": |
| 133 | lines, iss := scriptList(val) |
| 134 | issues = append(issues, iss...) |
| 135 | p.After = lines |
| 136 | def.present["after_script"] = true |
| 137 | def.job.After = lines |
| 138 | case "cache": |
| 139 | c, iss := parseCache(val) |
| 140 | issues = append(issues, iss...) |
| 141 | p.Cache = c |
| 142 | def.present["cache"] = true |
| 143 | def.job.Cache = c |
| 144 | case "default": |
| 145 | d, iss := parseDefault(val) |
| 146 | issues = append(issues, iss...) |
| 147 | def = overlayDefault(def, d) |
| 148 | case "workflow", "services", "spec", "pages": |
| 149 | issues = append(issues, Issue{Line: keyNode.Line, Key: "ci.warn.ignored", Args: []any{key}, Warning: true}) |
| 150 | case "jobs": |
| 151 | seenLegacy = true |
| 152 | parsed, iss := parseJobMap(val) |
| 153 | issues = append(issues, iss...) |
| 154 | jobs = append(jobs, parsed...) |
| 155 | default: |
| 156 | if val.Kind != yaml.MappingNode { |
| 157 | issues = append(issues, Issue{Line: keyNode.Line, Key: "ci.err.unknown", Args: []any{key}}) |
| 158 | continue |
| 159 | } |
| 160 | seenTop = true |
| 161 | job, iss := parseJob(key, val) |
| 162 | issues = append(issues, iss...) |
| 163 | jobs = append(jobs, job) |
| 164 | } |
| 165 | } |
| 166 | if seenLegacy && seenTop { |
| 167 | issues = append(issues, Issue{Line: 1, Key: "ci.err.both"}) |
| 168 | } |
| 169 | if p.Image != "" && !def.present["image"] { |
| 170 | def.present["image"] = true |
| 171 | def.job.Image = p.Image |
| 172 | } |
| 173 | resolved, iss := applyExtends(jobs, def) |
| 174 | issues = append(issues, iss...) |
| 175 | byName := map[string]int{} |
| 176 | var merged []Job |
| 177 | for _, j := range p.Jobs { |
| 178 | byName[j.Name] = len(merged) |
| 179 | merged = append(merged, j) |
| 180 | } |
| 181 | for _, d := range resolved { |
| 182 | if strings.HasPrefix(d.job.Name, ".") { |
| 183 | continue |
| 184 | } |
| 185 | if i, ok := byName[d.job.Name]; ok { |
| 186 | merged[i] = overlayJob(merged[i], d) |
| 187 | continue |
| 188 | } |
| 189 | byName[d.job.Name] = len(merged) |
| 190 | merged = append(merged, d.job) |
| 191 | } |
| 192 | p.Jobs = merged |
| 193 | issues = append(issues, validate(&p)...) |
| 194 | if hasError(issues) { |
| 195 | return p, issues, nil |
| 196 | } |
| 197 | return p, issues, nil |
| 198 | } |
| 199 | |
| 200 | func parseInclude(node *yaml.Node, read func(string) ([]byte, error), stack []string) (Pipeline, []Issue, error) { |
| 201 | var items []*yaml.Node |
| 202 | switch node.Kind { |
| 203 | case yaml.SequenceNode: |
| 204 | items = node.Content |
| 205 | default: |
| 206 | items = []*yaml.Node{node} |
| 207 | } |
| 208 | var out Pipeline |
| 209 | var issues []Issue |
| 210 | for _, item := range items { |
| 211 | local, iss := includeLocal(item) |
| 212 | issues = append(issues, iss...) |
| 213 | if local == "" { |
| 214 | continue |
| 215 | } |
| 216 | if read == nil { |
| 217 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.includeRead", Args: []any{local}}) |
| 218 | continue |
| 219 | } |
| 220 | for _, prev := range stack { |
| 221 | if prev == local { |
| 222 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.includeCycle", Args: []any{local}}) |
| 223 | local = "" |
| 224 | break |
| 225 | } |
| 226 | } |
| 227 | if local == "" { |
| 228 | continue |
| 229 | } |
| 230 | raw, err := read(local) |
| 231 | if err != nil { |
| 232 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.includeRead", Args: []any{local}}) |
| 233 | continue |
| 234 | } |
| 235 | part, more, err := parseBytes(raw, read, append(append([]string{}, stack...), local)) |
| 236 | if err != nil { |
| 237 | return Pipeline{}, nil, err |
| 238 | } |
| 239 | issues = append(issues, more...) |
| 240 | out = mergePipeline(out, part) |
| 241 | } |
| 242 | return out, issues, nil |
| 243 | } |
| 244 | |
| 245 | func includeLocal(node *yaml.Node) (string, []Issue) { |
| 246 | switch node.Kind { |
| 247 | case yaml.ScalarNode: |
| 248 | return cleanLocal(node.Value, node) |
| 249 | case yaml.MappingNode: |
| 250 | for i := 0; i < len(node.Content); i += 2 { |
| 251 | key := node.Content[i].Value |
| 252 | if key == "local" { |
| 253 | if node.Content[i+1].Kind != yaml.ScalarNode { |
| 254 | return "", []Issue{{Line: node.Line, Key: "ci.err.includeLocal"}} |
| 255 | } |
| 256 | return cleanLocal(node.Content[i+1].Value, node.Content[i+1]) |
| 257 | } |
| 258 | } |
| 259 | return "", []Issue{{Line: node.Line, Key: "ci.err.includeRemote"}} |
| 260 | default: |
| 261 | return "", []Issue{{Line: node.Line, Key: "ci.err.includeLocal"}} |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | func cleanLocal(p string, node *yaml.Node) (string, []Issue) { |
| 266 | p = strings.TrimPrefix(strings.TrimSpace(p), "/") |
| 267 | if p == "" || strings.Contains(p, "\\") || strings.Contains(p, "..") { |
| 268 | return "", []Issue{{Line: node.Line, Key: "ci.err.rel", Args: []any{p}}} |
| 269 | } |
| 270 | return p, nil |
| 271 | } |
| 272 | |
| 273 | func mergePipeline(base, extra Pipeline) Pipeline { |
| 274 | if len(extra.Stages) > 0 { |
| 275 | base.Stages = extra.Stages |
| 276 | } |
| 277 | if base.Variables == nil { |
| 278 | base.Variables = map[string]string{} |
| 279 | } |
| 280 | for k, v := range extra.Variables { |
| 281 | base.Variables[k] = v |
| 282 | } |
| 283 | if extra.Image != "" { |
| 284 | base.Image = extra.Image |
| 285 | } |
| 286 | if extra.Before != nil { |
| 287 | base.Before = extra.Before |
| 288 | } |
| 289 | if extra.After != nil { |
| 290 | base.After = extra.After |
| 291 | } |
| 292 | byName := map[string]int{} |
| 293 | for i, j := range base.Jobs { |
| 294 | byName[j.Name] = i |
| 295 | } |
| 296 | for _, j := range extra.Jobs { |
| 297 | if i, ok := byName[j.Name]; ok { |
| 298 | base.Jobs[i] = j |
| 299 | continue |
| 300 | } |
| 301 | byName[j.Name] = len(base.Jobs) |
| 302 | base.Jobs = append(base.Jobs, j) |
| 303 | } |
| 304 | return base |
| 305 | } |
| 306 | |
| 307 | func parseDefault(node *yaml.Node) (draft, []Issue) { |
| 308 | d := draft{present: map[string]bool{}} |
| 309 | if node.Kind != yaml.MappingNode { |
| 310 | return d, []Issue{{Line: node.Line, Key: "ci.err.map"}} |
| 311 | } |
| 312 | var issues []Issue |
| 313 | for i := 0; i < len(node.Content); i += 2 { |
| 314 | key := node.Content[i].Value |
| 315 | val := node.Content[i+1] |
| 316 | switch key { |
| 317 | case "image": |
| 318 | img, iss := imageOf(val) |
| 319 | issues = append(issues, iss...) |
| 320 | d.job.Image = img |
| 321 | d.present["image"] = true |
| 322 | case "before_script": |
| 323 | lines, iss := scriptList(val) |
| 324 | issues = append(issues, iss...) |
| 325 | d.job.Before = lines |
| 326 | d.present["before_script"] = true |
| 327 | case "after_script": |
| 328 | lines, iss := scriptList(val) |
| 329 | issues = append(issues, iss...) |
| 330 | d.job.After = lines |
| 331 | d.present["after_script"] = true |
| 332 | case "tags": |
| 333 | tags, iss := scalarList(val, "ci.err.list") |
| 334 | issues = append(issues, iss...) |
| 335 | d.job.Tags = tags |
| 336 | d.present["tags"] = true |
| 337 | case "timeout": |
| 338 | dur, iss := durationOf(val) |
| 339 | issues = append(issues, iss...) |
| 340 | d.job.Timeout = dur |
| 341 | d.present["timeout"] = true |
| 342 | case "cache": |
| 343 | c, iss := parseCache(val) |
| 344 | issues = append(issues, iss...) |
| 345 | d.job.Cache = c |
| 346 | d.present["cache"] = true |
| 347 | default: |
| 348 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"default." + key}, Warning: true}) |
| 349 | } |
| 350 | } |
| 351 | return d, issues |
| 352 | } |
| 353 | |
| 354 | func overlayJob(base Job, d draft) Job { |
| 355 | set := func(key string, apply func()) { |
| 356 | if d.present[key] { |
| 357 | apply() |
| 358 | } |
| 359 | } |
| 360 | set("stage", func() { base.Stage = d.job.Stage }) |
| 361 | set("image", func() { base.Image = d.job.Image }) |
| 362 | set("before_script", func() { base.Before = d.job.Before }) |
| 363 | set("script", func() { base.Script = d.job.Script }) |
| 364 | set("after_script", func() { base.After = d.job.After }) |
| 365 | set("needs", func() { |
| 366 | base.Needs = d.job.Needs |
| 367 | base.Optional = d.job.Optional |
| 368 | base.NeedsSet = d.job.NeedsSet |
| 369 | }) |
| 370 | set("artifacts", func() { base.Artifacts = d.job.Artifacts }) |
| 371 | set("rules", func() { base.Rules = d.job.Rules }) |
| 372 | set("allow_failure", func() { base.AllowFailure = d.job.AllowFailure }) |
| 373 | set("when", func() { base.When = d.job.When }) |
| 374 | set("timeout", func() { base.Timeout = d.job.Timeout }) |
| 375 | set("tags", func() { base.Tags = d.job.Tags }) |
| 376 | set("cache", func() { base.Cache = d.job.Cache }) |
| 377 | set("resource_group", func() { base.ResourceGroup = d.job.ResourceGroup }) |
| 378 | set("only", func() { base.Only = d.job.Only }) |
| 379 | set("except", func() { base.Except = d.job.Except }) |
| 380 | if d.present["variables"] { |
| 381 | mergedVars := map[string]string{} |
| 382 | for k, v := range base.Variables { |
| 383 | mergedVars[k] = v |
| 384 | } |
| 385 | for k, v := range d.job.Variables { |
| 386 | mergedVars[k] = v |
| 387 | } |
| 388 | base.Variables = mergedVars |
| 389 | } |
| 390 | return base |
| 391 | } |
| 392 | |
| 393 | func overlayDefault(base, extra draft) draft { |
| 394 | if base.present == nil { |
| 395 | base.present = map[string]bool{} |
| 396 | } |
| 397 | for k, on := range extra.present { |
| 398 | if !on { |
| 399 | continue |
| 400 | } |
| 401 | base.present[k] = true |
| 402 | switch k { |
| 403 | case "image": |
| 404 | base.job.Image = extra.job.Image |
| 405 | case "before_script": |
| 406 | base.job.Before = extra.job.Before |
| 407 | case "after_script": |
| 408 | base.job.After = extra.job.After |
| 409 | case "tags": |
| 410 | base.job.Tags = extra.job.Tags |
| 411 | case "timeout": |
| 412 | base.job.Timeout = extra.job.Timeout |
| 413 | case "cache": |
| 414 | base.job.Cache = extra.job.Cache |
| 415 | } |
| 416 | } |
| 417 | return base |
| 418 | } |
| 419 | |
| 420 | func parseJobMap(node *yaml.Node) ([]draft, []Issue) { |
| 421 | if node.Kind != yaml.MappingNode { |
| 422 | return nil, []Issue{{Line: node.Line, Key: "ci.err.jobsMap"}} |
| 423 | } |
| 424 | var jobs []draft |
| 425 | var issues []Issue |
| 426 | for i := 0; i < len(node.Content); i += 2 { |
| 427 | job, iss := parseJob(node.Content[i].Value, node.Content[i+1]) |
| 428 | issues = append(issues, iss...) |
| 429 | if job.job.Name != "" { |
| 430 | jobs = append(jobs, job) |
| 431 | } |
| 432 | } |
| 433 | return jobs, issues |
| 434 | } |
| 435 | |
| 436 | func parseJob(name string, node *yaml.Node) (draft, []Issue) { |
| 437 | d := draft{present: map[string]bool{}, line: node.Line, job: Job{Name: name, Variables: map[string]string{}}} |
| 438 | if node.Kind != yaml.MappingNode { |
| 439 | return d, []Issue{{Line: node.Line, Key: "ci.err.jobMap", Args: []any{name}}} |
| 440 | } |
| 441 | var issues []Issue |
| 442 | for i := 0; i < len(node.Content); i += 2 { |
| 443 | key := node.Content[i].Value |
| 444 | val := node.Content[i+1] |
| 445 | d.present[key] = true |
| 446 | switch key { |
| 447 | case "stage": |
| 448 | if val.Kind != yaml.ScalarNode { |
| 449 | issues = append(issues, Issue{Line: val.Line, Key: "ci.err.stageText"}) |
| 450 | continue |
| 451 | } |
| 452 | d.job.Stage = val.Value |
| 453 | case "image": |
| 454 | img, iss := imageOf(val) |
| 455 | issues = append(issues, iss...) |
| 456 | d.job.Image = img |
| 457 | case "before_script": |
| 458 | lines, iss := scriptList(val) |
| 459 | issues = append(issues, iss...) |
| 460 | d.job.Before = lines |
| 461 | case "script": |
| 462 | lines, iss := scriptList(val) |
| 463 | issues = append(issues, iss...) |
| 464 | d.job.Script = lines |
| 465 | case "after_script": |
| 466 | lines, iss := scriptList(val) |
| 467 | issues = append(issues, iss...) |
| 468 | d.job.After = lines |
| 469 | case "needs": |
| 470 | needs, optional, set, iss := parseNeeds(val) |
| 471 | issues = append(issues, iss...) |
| 472 | d.job.Needs = needs |
| 473 | d.job.Optional = optional |
| 474 | d.job.NeedsSet = set |
| 475 | case "variables": |
| 476 | vars, iss := scalarMap(val) |
| 477 | issues = append(issues, iss...) |
| 478 | d.job.Variables = vars |
| 479 | case "artifacts": |
| 480 | art, iss := parseArtifacts(val) |
| 481 | issues = append(issues, iss...) |
| 482 | d.job.Artifacts = art |
| 483 | case "rules": |
| 484 | rules, iss := parseRules(val) |
| 485 | issues = append(issues, iss...) |
| 486 | d.job.Rules = rules |
| 487 | case "allow_failure": |
| 488 | b, iss := parseBool(val) |
| 489 | issues = append(issues, iss...) |
| 490 | d.job.AllowFailure = b |
| 491 | case "when": |
| 492 | w, iss := whenOf(val, false) |
| 493 | issues = append(issues, iss...) |
| 494 | d.job.When = w |
| 495 | case "timeout": |
| 496 | dur, iss := durationOf(val) |
| 497 | issues = append(issues, iss...) |
| 498 | d.job.Timeout = dur |
| 499 | case "tags": |
| 500 | tags, iss := scalarList(val, "ci.err.list") |
| 501 | issues = append(issues, iss...) |
| 502 | d.job.Tags = tags |
| 503 | case "cache": |
| 504 | c, iss := parseCache(val) |
| 505 | issues = append(issues, iss...) |
| 506 | d.job.Cache = c |
| 507 | case "resource_group": |
| 508 | if val.Kind != yaml.ScalarNode || strings.TrimSpace(val.Value) == "" { |
| 509 | issues = append(issues, Issue{Line: val.Line, Key: "ci.err.resource"}) |
| 510 | continue |
| 511 | } |
| 512 | d.job.ResourceGroup = strings.TrimSpace(val.Value) |
| 513 | case "only": |
| 514 | list, iss := scalarList(val, "ci.err.list") |
| 515 | issues = append(issues, iss...) |
| 516 | d.job.Only = list |
| 517 | case "except": |
| 518 | list, iss := scalarList(val, "ci.err.list") |
| 519 | issues = append(issues, iss...) |
| 520 | d.job.Except = list |
| 521 | case "extends": |
| 522 | list, iss := extendsOf(val) |
| 523 | issues = append(issues, iss...) |
| 524 | d.extends = list |
| 525 | default: |
| 526 | if _, ok := reserved[key]; ok { |
| 527 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{key}, Warning: true}) |
| 528 | continue |
| 529 | } |
| 530 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{name + "." + key}, Warning: true}) |
| 531 | } |
| 532 | } |
| 533 | return d, issues |
| 534 | } |
| 535 | |
| 536 | func applyExtends(jobs []draft, def draft) ([]draft, []Issue) { |
| 537 | by := map[string]*draft{} |
| 538 | var order []string |
| 539 | var issues []Issue |
| 540 | for i := range jobs { |
| 541 | name := jobs[i].job.Name |
| 542 | if by[name] != nil { |
| 543 | issues = append(issues, Issue{Line: jobs[i].line, Key: "ci.err.jobDup", Args: []any{name}}) |
| 544 | continue |
| 545 | } |
| 546 | by[name] = &jobs[i] |
| 547 | order = append(order, name) |
| 548 | } |
| 549 | visiting := map[string]bool{} |
| 550 | var walk func(string) |
| 551 | walk = func(name string) { |
| 552 | d := by[name] |
| 553 | if d == nil || d.present["__resolved"] { |
| 554 | return |
| 555 | } |
| 556 | if visiting[name] { |
| 557 | issues = append(issues, Issue{Line: d.line, Key: "ci.err.extendsCycle", Args: []any{name}}) |
| 558 | return |
| 559 | } |
| 560 | visiting[name] = true |
| 561 | for _, parent := range d.extends { |
| 562 | base := by[parent] |
| 563 | if base == nil { |
| 564 | issues = append(issues, Issue{Line: d.line, Key: "ci.err.extends", Args: []any{name, parent}}) |
| 565 | continue |
| 566 | } |
| 567 | walk(parent) |
| 568 | inherit(d, base) |
| 569 | } |
| 570 | delete(visiting, name) |
| 571 | d.present["__resolved"] = true |
| 572 | inherit(d, &def) |
| 573 | } |
| 574 | for _, name := range order { |
| 575 | walk(name) |
| 576 | } |
| 577 | return jobs, issues |
| 578 | } |
| 579 | |
| 580 | func inherit(child, parent *draft) { |
| 581 | if parent == nil { |
| 582 | return |
| 583 | } |
| 584 | copyField := func(key string, apply func()) { |
| 585 | if child.present[key] || !parent.present[key] { |
| 586 | return |
| 587 | } |
| 588 | apply() |
| 589 | child.present[key] = true |
| 590 | } |
| 591 | copyField("stage", func() { child.job.Stage = parent.job.Stage }) |
| 592 | copyField("image", func() { child.job.Image = parent.job.Image }) |
| 593 | copyField("before_script", func() { child.job.Before = parent.job.Before }) |
| 594 | copyField("script", func() { child.job.Script = parent.job.Script }) |
| 595 | copyField("after_script", func() { child.job.After = parent.job.After }) |
| 596 | copyField("needs", func() { |
| 597 | child.job.Needs = parent.job.Needs |
| 598 | child.job.Optional = parent.job.Optional |
| 599 | child.job.NeedsSet = parent.job.NeedsSet |
| 600 | }) |
| 601 | copyField("artifacts", func() { child.job.Artifacts = parent.job.Artifacts }) |
| 602 | copyField("rules", func() { child.job.Rules = parent.job.Rules }) |
| 603 | copyField("allow_failure", func() { child.job.AllowFailure = parent.job.AllowFailure }) |
| 604 | copyField("when", func() { child.job.When = parent.job.When }) |
| 605 | copyField("timeout", func() { child.job.Timeout = parent.job.Timeout }) |
| 606 | copyField("tags", func() { child.job.Tags = parent.job.Tags }) |
| 607 | copyField("cache", func() { child.job.Cache = parent.job.Cache }) |
| 608 | copyField("resource_group", func() { child.job.ResourceGroup = parent.job.ResourceGroup }) |
| 609 | copyField("only", func() { child.job.Only = parent.job.Only }) |
| 610 | copyField("except", func() { child.job.Except = parent.job.Except }) |
| 611 | if len(parent.job.Variables) > 0 { |
| 612 | merged := map[string]string{} |
| 613 | for k, v := range parent.job.Variables { |
| 614 | merged[k] = v |
| 615 | } |
| 616 | for k, v := range child.job.Variables { |
| 617 | merged[k] = v |
| 618 | } |
| 619 | child.job.Variables = merged |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | func validate(p *Pipeline) []Issue { |
| 624 | var issues []Issue |
| 625 | if len(p.Jobs) == 0 { |
| 626 | return []Issue{{Line: 1, Key: "ci.err.noJobs"}} |
| 627 | } |
| 628 | seenStage := map[string]bool{} |
| 629 | for _, s := range p.Stages { |
| 630 | if s == "" { |
| 631 | issues = append(issues, Issue{Line: 1, Key: "ci.err.stageEmpty"}) |
| 632 | continue |
| 633 | } |
| 634 | if seenStage[s] { |
| 635 | issues = append(issues, Issue{Line: 1, Key: "ci.err.stageDup", Args: []any{s}}) |
| 636 | } |
| 637 | seenStage[s] = true |
| 638 | } |
| 639 | if len(p.Stages) == 0 { |
| 640 | var order []string |
| 641 | for i := range p.Jobs { |
| 642 | if p.Jobs[i].Stage == "" { |
| 643 | p.Jobs[i].Stage = "test" |
| 644 | } |
| 645 | if !seenStage[p.Jobs[i].Stage] { |
| 646 | seenStage[p.Jobs[i].Stage] = true |
| 647 | order = append(order, p.Jobs[i].Stage) |
| 648 | } |
| 649 | } |
| 650 | p.Stages = order |
| 651 | } |
| 652 | names := map[string]bool{} |
| 653 | for i := range p.Jobs { |
| 654 | j := &p.Jobs[i] |
| 655 | if j.Name == "" || strings.ContainsAny(j.Name, " \t\n/\\") { |
| 656 | issues = append(issues, Issue{Line: 1, Key: "ci.err.jobName", Args: []any{j.Name}}) |
| 657 | continue |
| 658 | } |
| 659 | names[j.Name] = true |
| 660 | if j.Stage == "" { |
| 661 | issues = append(issues, Issue{Line: 1, Key: "ci.err.jobStage", Args: []any{j.Name}}) |
| 662 | } else if !seenStage[j.Stage] { |
| 663 | issues = append(issues, Issue{Line: 1, Key: "ci.err.stageUnknown", Args: []any{j.Name, j.Stage}}) |
| 664 | } |
| 665 | if j.Image == "" { |
| 666 | j.Image = p.Image |
| 667 | } |
| 668 | if strings.TrimSpace(j.Image) == "" || strings.ContainsAny(j.Image, " \t\n") { |
| 669 | issues = append(issues, Issue{Line: 1, Key: "ci.err.noImage", Args: []any{j.Name}}) |
| 670 | } |
| 671 | if len(j.Script) == 0 { |
| 672 | issues = append(issues, Issue{Line: 1, Key: "ci.err.noScript", Args: []any{j.Name}}) |
| 673 | } |
| 674 | for _, line := range j.Script { |
| 675 | if strings.TrimSpace(line) == "" { |
| 676 | issues = append(issues, Issue{Line: 1, Key: "ci.err.emptyLine", Args: []any{j.Name}}) |
| 677 | } |
| 678 | } |
| 679 | for _, path := range j.Artifacts.Paths { |
| 680 | if iss := safeRel(path); iss.Key != "" { |
| 681 | issues = append(issues, iss) |
| 682 | } |
| 683 | } |
| 684 | for _, path := range j.Cache.Paths { |
| 685 | if iss := safeRel(path); iss.Key != "" { |
| 686 | issues = append(issues, iss) |
| 687 | } |
| 688 | } |
| 689 | if j.When == "" { |
| 690 | j.When = "on_success" |
| 691 | } |
| 692 | } |
| 693 | for _, j := range p.Jobs { |
| 694 | for _, need := range j.Needs { |
| 695 | if !names[need] && !contains(j.Optional, need) { |
| 696 | issues = append(issues, Issue{Line: 1, Key: "ci.err.needUnknown", Args: []any{j.Name, need}}) |
| 697 | } |
| 698 | if need == j.Name { |
| 699 | issues = append(issues, Issue{Line: 1, Key: "ci.err.needSelf", Args: []any{j.Name}}) |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | if iss := detectCycles(p.Jobs); iss.Key != "" { |
| 704 | issues = append(issues, iss) |
| 705 | } |
| 706 | return issues |
| 707 | } |
| 708 | |
| 709 | func detectCycles(jobs []Job) Issue { |
| 710 | idx := map[string]int{} |
| 711 | for i, j := range jobs { |
| 712 | idx[j.Name] = i |
| 713 | } |
| 714 | const ( |
| 715 | white = 0 |
| 716 | gray = 1 |
| 717 | black = 2 |
| 718 | ) |
| 719 | color := make([]int, len(jobs)) |
| 720 | var visit func(int) Issue |
| 721 | visit = func(i int) Issue { |
| 722 | if color[i] == gray { |
| 723 | return Issue{Line: 1, Key: "ci.err.cycle", Args: []any{jobs[i].Name}} |
| 724 | } |
| 725 | if color[i] == black { |
| 726 | return Issue{} |
| 727 | } |
| 728 | color[i] = gray |
| 729 | for _, n := range jobs[i].Needs { |
| 730 | target, ok := idx[n] |
| 731 | if !ok { |
| 732 | continue |
| 733 | } |
| 734 | if iss := visit(target); iss.Key != "" { |
| 735 | return iss |
| 736 | } |
| 737 | } |
| 738 | color[i] = black |
| 739 | return Issue{} |
| 740 | } |
| 741 | for i := range jobs { |
| 742 | if iss := visit(i); iss.Key != "" { |
| 743 | return iss |
| 744 | } |
| 745 | } |
| 746 | return Issue{} |
| 747 | } |
| 748 | |
| 749 | func parseArtifacts(node *yaml.Node) (Artifacts, []Issue) { |
| 750 | if node.Kind != yaml.MappingNode { |
| 751 | return Artifacts{}, []Issue{{Line: node.Line, Key: "ci.err.artifacts"}} |
| 752 | } |
| 753 | var a Artifacts |
| 754 | var issues []Issue |
| 755 | for i := 0; i < len(node.Content); i += 2 { |
| 756 | key := node.Content[i].Value |
| 757 | val := node.Content[i+1] |
| 758 | switch key { |
| 759 | case "paths": |
| 760 | paths, iss := scalarList(val, "ci.err.list") |
| 761 | issues = append(issues, iss...) |
| 762 | a.Paths = paths |
| 763 | case "expire_in": |
| 764 | if val.Kind != yaml.ScalarNode { |
| 765 | issues = append(issues, Issue{Line: val.Line, Key: "ci.err.expire"}) |
| 766 | continue |
| 767 | } |
| 768 | d, err := parseExpire(val.Value) |
| 769 | if err != nil { |
| 770 | issues = append(issues, Issue{Line: val.Line, Key: "ci.err.expireBad", Args: []any{val.Value}}) |
| 771 | continue |
| 772 | } |
| 773 | a.ExpireIn = d |
| 774 | default: |
| 775 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"artifacts." + key}, Warning: true}) |
| 776 | } |
| 777 | } |
| 778 | return a, issues |
| 779 | } |
| 780 | |
| 781 | func parseCache(node *yaml.Node) (Cache, []Issue) { |
| 782 | if node.Kind != yaml.MappingNode { |
| 783 | return Cache{}, []Issue{{Line: node.Line, Key: "ci.err.cache"}} |
| 784 | } |
| 785 | var c Cache |
| 786 | var issues []Issue |
| 787 | for i := 0; i < len(node.Content); i += 2 { |
| 788 | key := node.Content[i].Value |
| 789 | val := node.Content[i+1] |
| 790 | switch key { |
| 791 | case "paths": |
| 792 | paths, iss := scalarList(val, "ci.err.list") |
| 793 | issues = append(issues, iss...) |
| 794 | c.Paths = paths |
| 795 | case "key": |
| 796 | if val.Kind != yaml.ScalarNode { |
| 797 | issues = append(issues, Issue{Line: val.Line, Key: "ci.err.cacheKey"}) |
| 798 | continue |
| 799 | } |
| 800 | c.Key = val.Value |
| 801 | default: |
| 802 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"cache." + key}, Warning: true}) |
| 803 | } |
| 804 | } |
| 805 | return c, issues |
| 806 | } |
| 807 | |
| 808 | func parseRules(node *yaml.Node) ([]Rule, []Issue) { |
| 809 | if node.Kind != yaml.SequenceNode { |
| 810 | return nil, []Issue{{Line: node.Line, Key: "ci.err.rulesList"}} |
| 811 | } |
| 812 | var rules []Rule |
| 813 | var issues []Issue |
| 814 | for _, item := range node.Content { |
| 815 | if item.Kind != yaml.MappingNode { |
| 816 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.ruleMap"}) |
| 817 | continue |
| 818 | } |
| 819 | var r Rule |
| 820 | for i := 0; i < len(item.Content); i += 2 { |
| 821 | key := item.Content[i].Value |
| 822 | val := item.Content[i+1] |
| 823 | switch key { |
| 824 | case "if": |
| 825 | if val.Kind != yaml.ScalarNode || strings.TrimSpace(val.Value) == "" { |
| 826 | issues = append(issues, Issue{Line: val.Line, Key: "ci.err.ruleIf"}) |
| 827 | continue |
| 828 | } |
| 829 | r.If = strings.TrimSpace(val.Value) |
| 830 | if _, err := compileRule(r.If); err != nil { |
| 831 | if ie, ok := err.(i18n.Error); ok { |
| 832 | issues = append(issues, Issue{Line: val.Line, Key: ie.Key, Args: ie.Args}) |
| 833 | } else { |
| 834 | issues = append(issues, Issue{Line: val.Line, Key: "ci.err.ruleExpr", Args: []any{r.If}}) |
| 835 | } |
| 836 | } |
| 837 | case "changes": |
| 838 | list, iss := scalarList(val, "ci.err.list") |
| 839 | issues = append(issues, iss...) |
| 840 | r.Changes = list |
| 841 | case "exists": |
| 842 | list, iss := scalarList(val, "ci.err.list") |
| 843 | issues = append(issues, iss...) |
| 844 | r.Exists = list |
| 845 | case "when": |
| 846 | w, iss := whenOf(val, true) |
| 847 | issues = append(issues, iss...) |
| 848 | r.When = w |
| 849 | default: |
| 850 | issues = append(issues, Issue{Line: item.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"rules." + key}, Warning: true}) |
| 851 | } |
| 852 | } |
| 853 | if r.If == "" && len(r.Changes) == 0 && len(r.Exists) == 0 && r.When == "" { |
| 854 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.ruleEmpty"}) |
| 855 | continue |
| 856 | } |
| 857 | rules = append(rules, r) |
| 858 | } |
| 859 | return rules, issues |
| 860 | } |
| 861 | |
| 862 | func parseNeeds(node *yaml.Node) (needs, optional []string, set bool, issues []Issue) { |
| 863 | set = true |
| 864 | if node.Kind != yaml.SequenceNode { |
| 865 | return nil, nil, true, []Issue{{Line: node.Line, Key: "ci.err.needs"}} |
| 866 | } |
| 867 | for _, item := range node.Content { |
| 868 | switch item.Kind { |
| 869 | case yaml.ScalarNode: |
| 870 | needs = append(needs, item.Value) |
| 871 | case yaml.MappingNode: |
| 872 | var name string |
| 873 | var opt bool |
| 874 | for i := 0; i < len(item.Content); i += 2 { |
| 875 | switch item.Content[i].Value { |
| 876 | case "job": |
| 877 | if item.Content[i+1].Kind == yaml.ScalarNode { |
| 878 | name = item.Content[i+1].Value |
| 879 | } |
| 880 | case "optional": |
| 881 | b, iss := parseBool(item.Content[i+1]) |
| 882 | issues = append(issues, iss...) |
| 883 | opt = b |
| 884 | default: |
| 885 | issues = append(issues, Issue{Line: item.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"needs." + item.Content[i].Value}, Warning: true}) |
| 886 | } |
| 887 | } |
| 888 | if name == "" { |
| 889 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.needs"}) |
| 890 | continue |
| 891 | } |
| 892 | needs = append(needs, name) |
| 893 | if opt { |
| 894 | optional = append(optional, name) |
| 895 | } |
| 896 | default: |
| 897 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.needs"}) |
| 898 | } |
| 899 | } |
| 900 | if needs == nil { |
| 901 | needs = []string{} |
| 902 | } |
| 903 | return needs, optional, set, issues |
| 904 | } |
| 905 | |
| 906 | func imageOf(node *yaml.Node) (string, []Issue) { |
| 907 | switch node.Kind { |
| 908 | case yaml.ScalarNode: |
| 909 | return node.Value, nil |
| 910 | case yaml.MappingNode: |
| 911 | for i := 0; i < len(node.Content); i += 2 { |
| 912 | if node.Content[i].Value == "name" && node.Content[i+1].Kind == yaml.ScalarNode { |
| 913 | return node.Content[i+1].Value, nil |
| 914 | } |
| 915 | } |
| 916 | return "", []Issue{{Line: node.Line, Key: "ci.err.image"}} |
| 917 | default: |
| 918 | return "", []Issue{{Line: node.Line, Key: "ci.err.image"}} |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | func whenOf(node *yaml.Node, allowNever bool) (string, []Issue) { |
| 923 | if node.Kind != yaml.ScalarNode { |
| 924 | return "", []Issue{{Line: node.Line, Key: "ci.err.when", Args: []any{""}}} |
| 925 | } |
| 926 | switch node.Value { |
| 927 | case "on_success", "on_failure", "always", "manual": |
| 928 | return node.Value, nil |
| 929 | case "never": |
| 930 | if allowNever { |
| 931 | return node.Value, nil |
| 932 | } |
| 933 | } |
| 934 | return "", []Issue{{Line: node.Line, Key: "ci.err.when", Args: []any{node.Value}}} |
| 935 | } |
| 936 | |
| 937 | func durationOf(node *yaml.Node) (time.Duration, []Issue) { |
| 938 | if node.Kind != yaml.ScalarNode { |
| 939 | return 0, []Issue{{Line: node.Line, Key: "ci.err.timeout", Args: []any{""}}} |
| 940 | } |
| 941 | d, err := parseExpire(node.Value) |
| 942 | if err != nil { |
| 943 | if strings.HasSuffix(node.Value, "s") { |
| 944 | n, nerr := strconv.Atoi(strings.TrimSuffix(node.Value, "s")) |
| 945 | if nerr == nil && n >= 0 { |
| 946 | return time.Duration(n) * time.Second, nil |
| 947 | } |
| 948 | } |
| 949 | return 0, []Issue{{Line: node.Line, Key: "ci.err.timeout", Args: []any{node.Value}}} |
| 950 | } |
| 951 | return d, nil |
| 952 | } |
| 953 | |
| 954 | func extendsOf(node *yaml.Node) ([]string, []Issue) { |
| 955 | if node.Kind == yaml.ScalarNode && strings.TrimSpace(node.Value) != "" { |
| 956 | return []string{node.Value}, nil |
| 957 | } |
| 958 | return scalarList(node, "ci.err.extendsList") |
| 959 | } |
| 960 | |
| 961 | func scriptList(node *yaml.Node) ([]string, []Issue) { |
| 962 | if node.Kind == yaml.ScalarNode { |
| 963 | if strings.TrimSpace(node.Value) == "" { |
| 964 | return nil, []Issue{{Line: node.Line, Key: "ci.err.script"}} |
| 965 | } |
| 966 | return splitLines(node.Value), nil |
| 967 | } |
| 968 | return scalarList(node, "ci.err.script") |
| 969 | } |
| 970 | |
| 971 | func splitLines(s string) []string { |
| 972 | parts := strings.Split(s, "\n") |
| 973 | var out []string |
| 974 | for _, p := range parts { |
| 975 | if strings.TrimSpace(p) == "" { |
| 976 | continue |
| 977 | } |
| 978 | out = append(out, p) |
| 979 | } |
| 980 | return out |
| 981 | } |
| 982 | |
| 983 | func scalarList(node *yaml.Node, key string) ([]string, []Issue) { |
| 984 | if node.Kind != yaml.SequenceNode { |
| 985 | return nil, []Issue{{Line: node.Line, Key: key}} |
| 986 | } |
| 987 | var out []string |
| 988 | var issues []Issue |
| 989 | for _, item := range node.Content { |
| 990 | if item.Kind != yaml.ScalarNode { |
| 991 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.listText"}) |
| 992 | continue |
| 993 | } |
| 994 | out = append(out, item.Value) |
| 995 | } |
| 996 | return out, issues |
| 997 | } |
| 998 | |
| 999 | func scalarMap(node *yaml.Node) (map[string]string, []Issue) { |
| 1000 | if node.Kind != yaml.MappingNode { |
| 1001 | return nil, []Issue{{Line: node.Line, Key: "ci.err.map"}} |
| 1002 | } |
| 1003 | out := map[string]string{} |
| 1004 | var issues []Issue |
| 1005 | for i := 0; i < len(node.Content); i += 2 { |
| 1006 | k := node.Content[i] |
| 1007 | v := node.Content[i+1] |
| 1008 | if k.Kind != yaml.ScalarNode { |
| 1009 | issues = append(issues, Issue{Line: k.Line, Key: "ci.err.varText"}) |
| 1010 | continue |
| 1011 | } |
| 1012 | switch v.Kind { |
| 1013 | case yaml.ScalarNode: |
| 1014 | out[k.Value] = v.Value |
| 1015 | case yaml.MappingNode: |
| 1016 | val, iss := mappingValue(v) |
| 1017 | issues = append(issues, iss...) |
| 1018 | if val != "" || len(iss) == 0 { |
| 1019 | out[k.Value] = val |
| 1020 | } |
| 1021 | default: |
| 1022 | issues = append(issues, Issue{Line: v.Line, Key: "ci.err.varText"}) |
| 1023 | } |
| 1024 | } |
| 1025 | return out, issues |
| 1026 | } |
| 1027 | |
| 1028 | func mappingValue(node *yaml.Node) (string, []Issue) { |
| 1029 | for i := 0; i < len(node.Content); i += 2 { |
| 1030 | if node.Content[i].Value == "value" && node.Content[i+1].Kind == yaml.ScalarNode { |
| 1031 | return node.Content[i+1].Value, nil |
| 1032 | } |
| 1033 | } |
| 1034 | return "", []Issue{{Line: node.Line, Key: "ci.err.varText"}} |
| 1035 | } |
| 1036 | |
| 1037 | func parseBool(node *yaml.Node) (bool, []Issue) { |
| 1038 | if node.Kind != yaml.ScalarNode { |
| 1039 | return false, []Issue{{Line: node.Line, Key: "ci.err.bool"}} |
| 1040 | } |
| 1041 | switch strings.ToLower(node.Value) { |
| 1042 | case "true", "yes": |
| 1043 | return true, nil |
| 1044 | case "false", "no": |
| 1045 | return false, nil |
| 1046 | default: |
| 1047 | return false, []Issue{{Line: node.Line, Key: "ci.err.bool"}} |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | func parseExpire(s string) (time.Duration, error) { |
| 1052 | s = strings.TrimSpace(s) |
| 1053 | if s == "" || s == "never" { |
| 1054 | return 0, nil |
| 1055 | } |
| 1056 | if len(s) < 2 { |
| 1057 | return 0, fmt.Errorf("expire") |
| 1058 | } |
| 1059 | n, err := strconv.Atoi(s[:len(s)-1]) |
| 1060 | if err != nil || n < 0 { |
| 1061 | return 0, fmt.Errorf("expire") |
| 1062 | } |
| 1063 | switch s[len(s)-1] { |
| 1064 | case 's': |
| 1065 | return time.Duration(n) * time.Second, nil |
| 1066 | case 'm': |
| 1067 | return time.Duration(n) * time.Minute, nil |
| 1068 | case 'h': |
| 1069 | return time.Duration(n) * time.Hour, nil |
| 1070 | case 'd': |
| 1071 | return time.Duration(n) * 24 * time.Hour, nil |
| 1072 | case 'w': |
| 1073 | return time.Duration(n) * 7 * 24 * time.Hour, nil |
| 1074 | default: |
| 1075 | return 0, fmt.Errorf("expire") |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | func safeRel(p string) Issue { |
| 1080 | if p == "" || strings.HasPrefix(p, "/") || strings.Contains(p, "\\") { |
| 1081 | return Issue{Line: 1, Key: "ci.err.rel", Args: []any{p}} |
| 1082 | } |
| 1083 | for _, part := range strings.Split(p, "/") { |
| 1084 | if part == ".." { |
| 1085 | return Issue{Line: 1, Key: "ci.err.dotdot", Args: []any{p}} |
| 1086 | } |
| 1087 | } |
| 1088 | return Issue{} |
| 1089 | } |
| 1090 | |
| 1091 | func hasError(issues []Issue) bool { |
| 1092 | for _, is := range issues { |
| 1093 | if !is.Warning { |
| 1094 | return true |
| 1095 | } |
| 1096 | } |
| 1097 | return false |
| 1098 | } |