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