From 42a39fb57706368b9514ff4e04cadecd07fb3602 Mon Sep 17 00:00:00 2001 From: Dave Shanley Date: Fri, 15 Jul 2022 08:20:00 -0400 Subject: [PATCH] Custom functions work great across all commands now. html-report, lint, report, dashboard and spectral-report all support custom functions! --- cmd/build_results.go | 7 ++++--- cmd/build_results_test.go | 2 +- cmd/dashboard.go | 5 ++++- cmd/html_report.go | 5 ++++- cmd/lint.go | 1 - cmd/spectral_report.go | 9 +++++++-- cmd/vacuum_report.go | 10 ++++++++-- cui/tabbed_view.go | 3 +++ rulesets/rulesets.go | 1 + statistics/statistics.go | 2 +- 10 files changed, 33 insertions(+), 12 deletions(-) diff --git a/cmd/build_results.go b/cmd/build_results.go index c2a1c7e9..b19fcd62 100644 --- a/cmd/build_results.go +++ b/cmd/build_results.go @@ -8,7 +8,7 @@ import ( "io/ioutil" ) -func BuildResults(rulesetFlag string, specBytes []byte) (*model.RuleResultSet, *motor.RuleSetExecutionResult, error) { +func BuildResults(rulesetFlag string, specBytes []byte, customFunctions map[string]model.RuleFunction) (*model.RuleResultSet, *motor.RuleSetExecutionResult, error) { // read spec and parse defaultRuleSets := rulesets.BuildDefaultRuleSets() @@ -33,8 +33,9 @@ func BuildResults(rulesetFlag string, specBytes []byte) (*model.RuleResultSet, * pterm.Info.Printf("Linting against %d rules: %s\n", len(selectedRS.Rules), selectedRS.DocumentationURI) ruleset := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{ - RuleSet: selectedRS, - Spec: specBytes, + RuleSet: selectedRS, + Spec: specBytes, + CustomFunctions: customFunctions, }) resultSet := model.NewRuleResultSet(ruleset.Results) diff --git a/cmd/build_results_test.go b/cmd/build_results_test.go index bf67ba94..dc1d5dbf 100644 --- a/cmd/build_results_test.go +++ b/cmd/build_results_test.go @@ -6,6 +6,6 @@ import ( ) func TestBuildResults(t *testing.T) { - _, _, err := BuildResults("nuggets", nil) + _, _, err := BuildResults("nuggets", nil, nil) assert.Error(t, err) } diff --git a/cmd/dashboard.go b/cmd/dashboard.go index 69e5bb18..17711054 100644 --- a/cmd/dashboard.go +++ b/cmd/dashboard.go @@ -43,8 +43,11 @@ func GetDashboardCommand() *cobra.Command { // if we have a pre-compiled report, jump straight to the end and collect $500 if vacuumReport == nil { + functionsFlag, _ := cmd.Flags().GetString("functions") + customFunctions, _ := LoadCustomFunctions(functionsFlag) + rulesetFlag, _ := cmd.Flags().GetString("ruleset") - resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes) + resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes, customFunctions) specIndex = ruleset.Index specInfo = ruleset.SpecInfo specInfo.Generated = time.Now() diff --git a/cmd/html_report.go b/cmd/html_report.go index aa1ed07f..c6366e1f 100644 --- a/cmd/html_report.go +++ b/cmd/html_report.go @@ -62,8 +62,11 @@ func GetHTMLReportCommand() *cobra.Command { // if we have a pre-compiled report, jump straight to the end and collect $500 if vacuumReport == nil { + functionsFlag, _ := cmd.Flags().GetString("functions") + customFunctions, _ := LoadCustomFunctions(functionsFlag) + rulesetFlag, _ := cmd.Flags().GetString("ruleset") - resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes) + resultSet, ruleset, err = BuildResults(rulesetFlag, specBytes, customFunctions) specIndex = ruleset.Index specInfo = ruleset.SpecInfo specInfo.Generated = time.Now() diff --git a/cmd/lint.go b/cmd/lint.go index 66d75fb3..604773dd 100644 --- a/cmd/lint.go +++ b/cmd/lint.go @@ -34,7 +34,6 @@ func GetLintCommand() *cobra.Command { categoryFlag, _ := cmd.Flags().GetString("category") rulesetFlag, _ := cmd.Flags().GetString("ruleset") silent, _ := cmd.Flags().GetBool("silent") - functionsFlag, _ := cmd.Flags().GetString("functions") if !silent { diff --git a/cmd/spectral_report.go b/cmd/spectral_report.go index df638fd2..6aa9187f 100644 --- a/cmd/spectral_report.go +++ b/cmd/spectral_report.go @@ -65,10 +65,14 @@ func GetSpectralReportCommand() *cobra.Command { // default is recommended rules, based on spectral (for now anyway) selectedRS := defaultRuleSets.GenerateOpenAPIRecommendedRuleSet() + functionsFlag, _ := cmd.Flags().GetString("functions") + var customFunctions map[string]model.RuleFunction + // if ruleset has been supplied, lets make sure it exists, then load it in // and see if it's valid. If so - let's go! if rulesetFlag != "" { + customFunctions, _ = LoadCustomFunctions(functionsFlag) rsBytes, rsErr := ioutil.ReadFile(rulesetFlag) if rsErr != nil { pterm.Error.Printf("Unable to read ruleset file '%s': %s\n", rulesetFlag, rsErr.Error()) @@ -84,8 +88,9 @@ func GetSpectralReportCommand() *cobra.Command { pterm.Info.Printf("Linting against %d rules: %s\n", len(selectedRS.Rules), selectedRS.DocumentationURI) ruleset := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{ - RuleSet: selectedRS, - Spec: specBytes, + RuleSet: selectedRS, + Spec: specBytes, + CustomFunctions: customFunctions, }) resultSet := model.NewRuleResultSet(ruleset.Results) diff --git a/cmd/vacuum_report.go b/cmd/vacuum_report.go index 2d143457..4015067a 100644 --- a/cmd/vacuum_report.go +++ b/cmd/vacuum_report.go @@ -73,10 +73,15 @@ func GetVacuumReportCommand() *cobra.Command { // default is recommended rules, based on spectral (for now anyway) selectedRS := defaultRuleSets.GenerateOpenAPIRecommendedRuleSet() + functionsFlag, _ := cmd.Flags().GetString("functions") + var customFunctions map[string]model.RuleFunction + // if ruleset has been supplied, lets make sure it exists, then load it in // and see if it's valid. If so - let's go! if rulesetFlag != "" { + customFunctions, _ = LoadCustomFunctions(functionsFlag) + rsBytes, rsErr := ioutil.ReadFile(rulesetFlag) if rsErr != nil { pterm.Error.Printf("Unable to read ruleset file '%s': %s\n", rulesetFlag, rsErr.Error()) @@ -92,8 +97,9 @@ func GetVacuumReportCommand() *cobra.Command { pterm.Info.Printf("Linting against %d rules: %s\n", len(selectedRS.Rules), selectedRS.DocumentationURI) ruleset := motor.ApplyRulesToRuleSet(&motor.RuleSetExecution{ - RuleSet: selectedRS, - Spec: specBytes, + RuleSet: selectedRS, + Spec: specBytes, + CustomFunctions: customFunctions, }) resultSet := model.NewRuleResultSet(ruleset.Results) diff --git a/cui/tabbed_view.go b/cui/tabbed_view.go index 7b3dc16b..92d9f11b 100644 --- a/cui/tabbed_view.go +++ b/cui/tabbed_view.go @@ -124,6 +124,9 @@ func (t *TabbedView) generateRulesInCategory() { sev := result.Rule.GetSeverityAsIntValue() ruleType := "" ruleName := result.Rule.Name + if ruleName == "" { + ruleName = result.Rule.Id + } switch sev { case 0: ruleType = "🔺" diff --git a/rulesets/rulesets.go b/rulesets/rulesets.go index 4dd2fe9a..610454fb 100644 --- a/rulesets/rulesets.go +++ b/rulesets/rulesets.go @@ -235,6 +235,7 @@ func (rsm ruleSetsModel) GenerateRuleSetFromSuppliedRuleSet(ruleset *RuleSet) *R // add to validation category if it's not supplied if rc.Id == "" { nr.RuleCategory = model.RuleCategories[model.CategoryValidation] + nr.Id = k } else { if model.RuleCategories[rc.Id] != nil { nr.RuleCategory = model.RuleCategories[rc.Id] diff --git a/statistics/statistics.go b/statistics/statistics.go index e4423d06..3aacedab 100644 --- a/statistics/statistics.go +++ b/statistics/statistics.go @@ -42,7 +42,7 @@ func CreateReportStatistics(index *model.SpecIndex, info *model.SpecInfo, result total := 100.0 score := total - float64(results.GetInfoCount())*0.1 score = score - (0.4 * float64(results.GetWarnCount())) - score = score - (2.0 * float64(results.GetErrorCount())) + score = score - (15.0 * float64(results.GetErrorCount())) // errors are failures they should be judged harshly. if results.GetErrorCount() <= 0 && score < 0 { // floor at 25% if there are no errors, but a ton of warnings lowering the score