Runs the Regal linter on the given rego content and records any violations
(filePath, content string)
| 269 | |
| 270 | // Runs the Regal linter on the given rego content and records any violations |
| 271 | func (p *PolicyToLint) runRegalLinter(filePath, content string) { |
| 272 | inputModules, err := rules.InputFromText(filePath, content) |
| 273 | if err != nil { |
| 274 | // Cast to OPA AST errors for better formatting |
| 275 | var astErrs opaAst.Errors |
| 276 | if errors.As(err, &astErrs) { |
| 277 | for _, e := range astErrs { |
| 278 | line := 0 |
| 279 | if e.Location != nil { |
| 280 | line = e.Location.Row |
| 281 | } |
| 282 | |
| 283 | p.AddError(filePath, e.Message, line) |
| 284 | } |
| 285 | return |
| 286 | } |
| 287 | // Fallback if it's not an ast.Errors type |
| 288 | p.AddError(filePath, err.Error(), 0) |
| 289 | return |
| 290 | } |
| 291 | |
| 292 | // Initialize linter with input modules |
| 293 | lntr := linter.NewLinter().WithInputModules(&inputModules) |
| 294 | |
| 295 | // Load and apply configuration |
| 296 | cfg, err := p.loadConfig() |
| 297 | if err != nil { |
| 298 | p.AddError(filePath, fmt.Sprintf("%s", err), 0) |
| 299 | } |
| 300 | if cfg != nil { |
| 301 | lntr = lntr.WithUserConfig(*cfg) |
| 302 | } |
| 303 | |
| 304 | report, err := lntr.Lint(context.Background()) |
| 305 | if err != nil { |
| 306 | p.AddError(filePath, err.Error(), 0) |
| 307 | return |
| 308 | } |
| 309 | |
| 310 | // Parse the Rego AST to map line numbers to rule names |
| 311 | regoRuleMap := p.buildRegoRuleMap(content) |
| 312 | |
| 313 | // Add violations to the policy errors |
| 314 | for _, v := range report.Violations { |
| 315 | errorStr := p.formatViolationError(v, regoRuleMap) |
| 316 | p.AddError(filePath, errorStr, v.Location.Row) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Creates a formatted error message from a Regal violation |
| 321 | // Follows format <file>:<line>: [<ruleName>] <errorMsg> - <docLinks> |
no test coverage detected