Append ordered rules when extending (#1304)

This commit is contained in:
Jarek Porzucek authored and GitHub committed 2024-03-22 11:21:26 -05:00
1 parent 6c52f878cc
commit 59c0cc7183
2 files changed
+10 -5

No files matched your search

+9 -4
View File
@@ -4,6 +4,7 @@ import (
_ "embed" _ "embed"
"fmt" "fmt"
"regexp" "regexp"
"sort"
"strings" "strings"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@@ -62,7 +63,7 @@ type Config struct {
Keywords []string Keywords []string
// used to keep sarif results consistent // used to keep sarif results consistent
orderedRules []string OrderedRules []string
} }
// Extend is a struct that allows users to define how they want their // Extend is a struct that allows users to define how they want their
@@ -158,7 +159,7 @@ func (vc *ViperConfig) Translate() (Config, error) {
StopWords: vc.Allowlist.StopWords, StopWords: vc.Allowlist.StopWords,
}, },
Keywords: keywords, Keywords: keywords,
orderedRules: orderedRules, OrderedRules: orderedRules,
} }
if maxExtendDepth != extendDepth { if maxExtendDepth != extendDepth {
@@ -177,9 +178,9 @@ func (vc *ViperConfig) Translate() (Config, error) {
return c, nil return c, nil
} }
func (c *Config) OrderedRules() []Rule { func (c *Config) GetOrderedRules() []Rule {
var orderedRules []Rule var orderedRules []Rule
for _, id := range c.orderedRules { for _, id := range c.OrderedRules {
if _, ok := c.Rules[id]; ok { if _, ok := c.Rules[id]; ok {
orderedRules = append(orderedRules, c.Rules[id]) orderedRules = append(orderedRules, c.Rules[id])
} }
@@ -240,6 +241,7 @@ func (c *Config) extend(extensionConfig Config) {
log.Trace().Msgf("adding %s to base config", ruleID) log.Trace().Msgf("adding %s to base config", ruleID)
c.Rules[ruleID] = rule c.Rules[ruleID] = rule
c.Keywords = append(c.Keywords, rule.Keywords...) c.Keywords = append(c.Keywords, rule.Keywords...)
c.OrderedRules = append(c.OrderedRules, ruleID)
} }
} }
@@ -250,4 +252,7 @@ func (c *Config) extend(extensionConfig Config) {
extensionConfig.Allowlist.Paths...) extensionConfig.Allowlist.Paths...)
c.Allowlist.Regexes = append(c.Allowlist.Regexes, c.Allowlist.Regexes = append(c.Allowlist.Regexes,
extensionConfig.Allowlist.Regexes...) extensionConfig.Allowlist.Regexes...)
// sort to keep extended rules in order
sort.Strings(c.OrderedRules)
} }
+1 -1
View File
@@ -55,7 +55,7 @@ func hasEmptyRules(tool Tool) bool {
func getRules(cfg config.Config) []Rules { func getRules(cfg config.Config) []Rules {
// TODO for _, rule := range cfg.Rules { // TODO for _, rule := range cfg.Rules {
var rules []Rules var rules []Rules
for _, rule := range cfg.OrderedRules() { for _, rule := range cfg.GetOrderedRules() {
shortDescription := ShortDescription{ shortDescription := ShortDescription{
Text: rule.Description, Text: rule.Description,
} }