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