| 1 | package ci |
| 2 | |
| 3 | import ( |
| 4 | "strconv" |
| 5 | "strings" |
| 6 | |
| 7 | "gopkg.in/yaml.v3" |
| 8 | ) |
| 9 | |
| 10 | func parseWorkflow(node *yaml.Node) ([]Rule, []Issue) { |
| 11 | if node.Kind != yaml.MappingNode { |
| 12 | return nil, []Issue{{Line: node.Line, Key: "ci.err.map"}} |
| 13 | } |
| 14 | var rules []Rule |
| 15 | var issues []Issue |
| 16 | for i := 0; i < len(node.Content); i += 2 { |
| 17 | key := node.Content[i].Value |
| 18 | val := node.Content[i+1] |
| 19 | if key == "rules" { |
| 20 | parsed, iss := parseRules(val) |
| 21 | issues = append(issues, iss...) |
| 22 | rules = parsed |
| 23 | continue |
| 24 | } |
| 25 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"workflow." + key}, Warning: true}) |
| 26 | } |
| 27 | return rules, issues |
| 28 | } |
| 29 | |
| 30 | func parseRetry(node *yaml.Node) (Retry, []Issue) { |
| 31 | switch node.Kind { |
| 32 | case yaml.ScalarNode: |
| 33 | n, iss := retryMax(node) |
| 34 | if len(iss) > 0 && n == 0 && iss[0].Key != "ci.warn.retryMax" { |
| 35 | return Retry{}, iss |
| 36 | } |
| 37 | return Retry{Max: n}, iss |
| 38 | case yaml.MappingNode: |
| 39 | var retry Retry |
| 40 | var issues []Issue |
| 41 | for i := 0; i < len(node.Content); i += 2 { |
| 42 | key := node.Content[i].Value |
| 43 | val := node.Content[i+1] |
| 44 | switch key { |
| 45 | case "max": |
| 46 | n, iss := retryMax(val) |
| 47 | issues = append(issues, iss...) |
| 48 | retry.Max = n |
| 49 | case "when": |
| 50 | list, iss := retryWhen(val) |
| 51 | issues = append(issues, iss...) |
| 52 | retry.When = list |
| 53 | default: |
| 54 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"retry." + key}, Warning: true}) |
| 55 | } |
| 56 | } |
| 57 | return retry, issues |
| 58 | default: |
| 59 | return Retry{}, []Issue{{Line: node.Line, Key: "ci.err.retry"}} |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | func retryMax(node *yaml.Node) (int, []Issue) { |
| 64 | if node.Kind != yaml.ScalarNode { |
| 65 | return 0, []Issue{{Line: node.Line, Key: "ci.err.retry"}} |
| 66 | } |
| 67 | n, err := strconv.Atoi(strings.TrimSpace(node.Value)) |
| 68 | if err != nil || n < 0 { |
| 69 | return 0, []Issue{{Line: node.Line, Key: "ci.err.retry"}} |
| 70 | } |
| 71 | if n > 2 { |
| 72 | return 2, []Issue{{Line: node.Line, Key: "ci.warn.retryMax", Args: []any{n, 2}, Warning: true}} |
| 73 | } |
| 74 | return n, nil |
| 75 | } |
| 76 | |
| 77 | func retryWhen(node *yaml.Node) ([]string, []Issue) { |
| 78 | if node.Kind == yaml.ScalarNode { |
| 79 | if strings.TrimSpace(node.Value) == "" { |
| 80 | return nil, []Issue{{Line: node.Line, Key: "ci.err.retry"}} |
| 81 | } |
| 82 | return []string{node.Value}, nil |
| 83 | } |
| 84 | return scalarList(node, "ci.err.list") |
| 85 | } |
| 86 | |
| 87 | func parseParallel(node *yaml.Node) (int, []MatrixSpec, []Issue) { |
| 88 | if node.Kind == yaml.ScalarNode { |
| 89 | n, err := strconv.Atoi(strings.TrimSpace(node.Value)) |
| 90 | if err != nil || n < 1 { |
| 91 | return 0, nil, []Issue{{Line: node.Line, Key: "ci.err.parallelForm", Args: []any{maxParallel}}} |
| 92 | } |
| 93 | if n > maxParallel { |
| 94 | return 0, nil, []Issue{{Line: node.Line, Key: "ci.err.parallel", Args: []any{n, maxParallel}}} |
| 95 | } |
| 96 | return n, nil, nil |
| 97 | } |
| 98 | if node.Kind != yaml.MappingNode { |
| 99 | return 0, nil, []Issue{{Line: node.Line, Key: "ci.err.parallelForm", Args: []any{maxParallel}}} |
| 100 | } |
| 101 | var specs []MatrixSpec |
| 102 | var issues []Issue |
| 103 | seen := false |
| 104 | for i := 0; i < len(node.Content); i += 2 { |
| 105 | key := node.Content[i].Value |
| 106 | val := node.Content[i+1] |
| 107 | if key != "matrix" { |
| 108 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"parallel." + key}, Warning: true}) |
| 109 | continue |
| 110 | } |
| 111 | seen = true |
| 112 | parsed, iss := parseMatrix(val) |
| 113 | issues = append(issues, iss...) |
| 114 | specs = parsed |
| 115 | } |
| 116 | if !seen { |
| 117 | issues = append(issues, Issue{Line: node.Line, Key: "ci.err.parallelForm", Args: []any{maxParallel}}) |
| 118 | } |
| 119 | count := 0 |
| 120 | for _, spec := range specs { |
| 121 | count += len(cartesian(spec)) |
| 122 | } |
| 123 | if count > maxParallel { |
| 124 | issues = append(issues, Issue{Line: node.Line, Key: "ci.err.parallel", Args: []any{count, maxParallel}}) |
| 125 | } |
| 126 | return 0, specs, issues |
| 127 | } |
| 128 | |
| 129 | func parseMatrix(node *yaml.Node) ([]MatrixSpec, []Issue) { |
| 130 | if node.Kind != yaml.SequenceNode || len(node.Content) == 0 { |
| 131 | return nil, []Issue{{Line: node.Line, Key: "ci.err.parallelForm", Args: []any{maxParallel}}} |
| 132 | } |
| 133 | var specs []MatrixSpec |
| 134 | var issues []Issue |
| 135 | for _, item := range node.Content { |
| 136 | if item.Kind != yaml.MappingNode { |
| 137 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.parallelForm", Args: []any{maxParallel}}) |
| 138 | continue |
| 139 | } |
| 140 | spec := MatrixSpec{} |
| 141 | for i := 0; i < len(item.Content); i += 2 { |
| 142 | key := item.Content[i].Value |
| 143 | vals, iss := matrixValues(item.Content[i+1]) |
| 144 | issues = append(issues, iss...) |
| 145 | if len(vals) == 0 { |
| 146 | continue |
| 147 | } |
| 148 | spec.Keys = append(spec.Keys, key) |
| 149 | spec.Vals = append(spec.Vals, vals) |
| 150 | } |
| 151 | if len(spec.Keys) == 0 { |
| 152 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.parallelForm", Args: []any{maxParallel}}) |
| 153 | continue |
| 154 | } |
| 155 | specs = append(specs, spec) |
| 156 | } |
| 157 | return specs, issues |
| 158 | } |
| 159 | |
| 160 | func matrixValues(node *yaml.Node) ([]string, []Issue) { |
| 161 | if node.Kind == yaml.ScalarNode { |
| 162 | return []string{node.Value}, nil |
| 163 | } |
| 164 | return scalarList(node, "ci.err.list") |
| 165 | } |
| 166 | |
| 167 | func parseServices(node *yaml.Node) ([]Service, []Issue) { |
| 168 | var items []*yaml.Node |
| 169 | switch node.Kind { |
| 170 | case yaml.SequenceNode: |
| 171 | items = node.Content |
| 172 | default: |
| 173 | items = []*yaml.Node{node} |
| 174 | } |
| 175 | var out []Service |
| 176 | var issues []Issue |
| 177 | for _, item := range items { |
| 178 | switch item.Kind { |
| 179 | case yaml.ScalarNode: |
| 180 | if strings.TrimSpace(item.Value) == "" { |
| 181 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.service"}) |
| 182 | continue |
| 183 | } |
| 184 | out = append(out, Service{Name: item.Value, Alias: serviceAlias(item.Value)}) |
| 185 | case yaml.MappingNode: |
| 186 | svc := Service{Variables: map[string]string{}} |
| 187 | for i := 0; i < len(item.Content); i += 2 { |
| 188 | key := item.Content[i].Value |
| 189 | val := item.Content[i+1] |
| 190 | switch key { |
| 191 | case "name": |
| 192 | if val.Kind == yaml.ScalarNode { |
| 193 | svc.Name = val.Value |
| 194 | } |
| 195 | case "alias": |
| 196 | if val.Kind == yaml.ScalarNode { |
| 197 | svc.Alias = val.Value |
| 198 | } |
| 199 | case "command": |
| 200 | list, iss := scriptList(val) |
| 201 | issues = append(issues, iss...) |
| 202 | svc.Command = list |
| 203 | case "entrypoint": |
| 204 | list, iss := scriptList(val) |
| 205 | issues = append(issues, iss...) |
| 206 | svc.Entrypoint = list |
| 207 | case "variables": |
| 208 | vars, iss := scalarMap(val) |
| 209 | issues = append(issues, iss...) |
| 210 | svc.Variables = vars |
| 211 | default: |
| 212 | issues = append(issues, Issue{Line: item.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"services." + key}, Warning: true}) |
| 213 | } |
| 214 | } |
| 215 | if strings.TrimSpace(svc.Name) == "" { |
| 216 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.service"}) |
| 217 | continue |
| 218 | } |
| 219 | if svc.Alias == "" { |
| 220 | svc.Alias = serviceAlias(svc.Name) |
| 221 | } |
| 222 | out = append(out, svc) |
| 223 | default: |
| 224 | issues = append(issues, Issue{Line: item.Line, Key: "ci.err.service"}) |
| 225 | } |
| 226 | } |
| 227 | return out, issues |
| 228 | } |
| 229 | |
| 230 | func serviceAlias(image string) string { |
| 231 | name := image |
| 232 | if i := strings.LastIndex(name, "/"); i >= 0 { |
| 233 | name = name[i+1:] |
| 234 | } |
| 235 | if i := strings.IndexAny(name, ":@"); i >= 0 { |
| 236 | name = name[:i] |
| 237 | } |
| 238 | name = strings.TrimSpace(name) |
| 239 | if name == "" { |
| 240 | return "service" |
| 241 | } |
| 242 | return name |
| 243 | } |
| 244 | |
| 245 | func parseEnvironment(node *yaml.Node) (Environment, []Issue) { |
| 246 | switch node.Kind { |
| 247 | case yaml.ScalarNode: |
| 248 | if strings.TrimSpace(node.Value) == "" { |
| 249 | return Environment{}, []Issue{{Line: node.Line, Key: "ci.err.environment"}} |
| 250 | } |
| 251 | return Environment{Name: node.Value}, nil |
| 252 | case yaml.MappingNode: |
| 253 | var env Environment |
| 254 | var issues []Issue |
| 255 | for i := 0; i < len(node.Content); i += 2 { |
| 256 | key := node.Content[i].Value |
| 257 | val := node.Content[i+1] |
| 258 | switch key { |
| 259 | case "name": |
| 260 | if val.Kind == yaml.ScalarNode { |
| 261 | env.Name = val.Value |
| 262 | } |
| 263 | case "url": |
| 264 | if val.Kind == yaml.ScalarNode { |
| 265 | env.URL = val.Value |
| 266 | } |
| 267 | default: |
| 268 | issues = append(issues, Issue{Line: node.Content[i].Line, Key: "ci.warn.ignored", Args: []any{"environment." + key}, Warning: true}) |
| 269 | } |
| 270 | } |
| 271 | if strings.TrimSpace(env.Name) == "" { |
| 272 | issues = append(issues, Issue{Line: node.Line, Key: "ci.err.environment"}) |
| 273 | } |
| 274 | return env, issues |
| 275 | default: |
| 276 | return Environment{}, []Issue{{Line: node.Line, Key: "ci.err.environment"}} |
| 277 | } |
| 278 | } |