Skip to content

Commit

Permalink
Custom functions work great across all commands now.
Browse files Browse the repository at this point in the history
html-report, lint, report, dashboard and spectral-report all support custom functions!
  • Loading branch information
daveshanley committed Jul 15, 2022
1 parent b63ad4d commit 42a39fb
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 12 deletions.
7 changes: 4 additions & 3 deletions cmd/build_results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/build_results_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import (
)

func TestBuildResults(t *testing.T) {
_, _, err := BuildResults("nuggets", nil)
_, _, err := BuildResults("nuggets", nil, nil)
assert.Error(t, err)
}
5 changes: 4 additions & 1 deletion cmd/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion cmd/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 7 additions & 2 deletions cmd/spectral_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions cmd/vacuum_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions cui/tabbed_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "🔺"
Expand Down
1 change: 1 addition & 0 deletions rulesets/rulesets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion statistics/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 42a39fb

Please sign in to comment.