Skip to content

Commit

Permalink
Change defaults for pull request rules (#4773)
Browse files Browse the repository at this point in the history
  • Loading branch information
eleftherias authored Oct 17, 2024
1 parent 3b93baf commit d3f88cf
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 67 deletions.
66 changes: 35 additions & 31 deletions internal/engine/eval/trusty/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,32 @@ var (
// SummaryScore is the score to use for the summary score
SummaryScore = "score"
// DefaultScore is the default score to use
DefaultScore = ""
DefaultScore = ""
defaultAction = pr_actions.ActionReviewPr
defaultEcosystemConfig = []ecosystemConfig{
{
Name: "npm",
Score: 5.0,
Provenance: 5.0,
Activity: 5.0,
AllowMalicious: false,
AllowDeprecated: false,
},
{
Name: "pypi",
Score: 5.0,
Provenance: 5.0,
Activity: 5.0,
AllowDeprecated: false,
},
{
Name: "go",
Score: 5.0,
Provenance: 5.0,
Activity: 5.0,
AllowDeprecated: false,
},
}
)

type ecosystemConfig struct {
Expand Down Expand Up @@ -50,40 +75,19 @@ type config struct {
EcosystemConfig []ecosystemConfig `json:"ecosystem_config" mapstructure:"ecosystem_config" validate:"required"`
}

func defaultConfig() *config {
return &config{
Action: pr_actions.ActionReviewPr,
EcosystemConfig: []ecosystemConfig{
{
Name: "npm",
Score: 5.0,
Provenance: 5.0,
Activity: 5.0,
AllowMalicious: false,
AllowDeprecated: false,
},
{
Name: "pypi",
Score: 5.0,
Provenance: 5.0,
Activity: 5.0,
AllowDeprecated: false,
},
{
Name: "go",
Score: 5.0,
Provenance: 5.0,
Activity: 5.0,
AllowDeprecated: false,
},
},
func populateDefaultsIfEmpty(ruleCfg map[string]any) {
if ruleCfg["ecosystem_config"] == nil {
ruleCfg["ecosystem_config"] = defaultEcosystemConfig
} else if ecoCfg, ok := ruleCfg["ecosystem_config"].([]interface{}); ok && len(ecoCfg) == 0 {
ruleCfg["ecosystem_config"] = defaultEcosystemConfig
}
if ruleCfg["action"] == nil {
ruleCfg["action"] = defaultAction
}
}

func parseConfig(ruleCfg map[string]any) (*config, error) {
if len(ruleCfg) == 0 {
return defaultConfig(), nil
}
populateDefaultsIfEmpty(ruleCfg)

var conf config
validate := validator.New(validator.WithRequiredStructEnabled())
Expand Down
11 changes: 10 additions & 1 deletion internal/engine/eval/trusty/trusty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func TestParseRuleConfig(t *testing.T) {
},
{
"invalid-config", map[string]any{
"hey": "you",
"ecosystem_config": []string{
"hey",
},
}, true,
},
} {
Expand Down Expand Up @@ -597,3 +599,10 @@ func TestEvaluationDetailRendering(t *testing.T) {
})
}
}

func defaultConfig() *config {
return &config{
Action: defaultAction,
EcosystemConfig: defaultEcosystemConfig,
}
}
78 changes: 43 additions & 35 deletions internal/engine/eval/vulncheck/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@ type vulnDbType string

const (
vulnDbTypeOsv vulnDbType = "osv"
defaultAction = pr_actions.ActionReviewPr
)

var (
defaultEcosystemConfig = []ecosystemConfig{
{
Name: "npm",
DbType: vulnDbTypeOsv,
DbEndpoint: "https://api.osv.dev/v1/query",
PackageRepository: packageRepository{
Url: "https://registry.npmjs.org",
},
},
{
Name: "pypi",
DbType: vulnDbTypeOsv,
DbEndpoint: "https://api.osv.dev/v1/query",
PackageRepository: packageRepository{
Url: "https://pypi.org/pypi",
},
},
{
Name: "go",
DbType: vulnDbTypeOsv,
DbEndpoint: "https://api.osv.dev/v1/query",
PackageRepository: packageRepository{
Url: "https://proxy.golang.org",
},
SumRepository: packageRepository{
Url: "https://sum.golang.org",
},
},
}
)

type packageRepository struct {
Expand All @@ -41,45 +74,20 @@ type config struct {
EcosystemConfig []ecosystemConfig `json:"ecosystem_config" mapstructure:"ecosystem_config" validate:"required"`
}

func defaultConfig() *config {
return &config{
Action: pr_actions.ActionReviewPr,
EcosystemConfig: []ecosystemConfig{
{
Name: "npm",
DbType: vulnDbTypeOsv,
DbEndpoint: "https://api.osv.dev/v1/query",
PackageRepository: packageRepository{
Url: "https://registry.npmjs.org",
},
},
{
Name: "pypi",
DbType: vulnDbTypeOsv,
DbEndpoint: "https://api.osv.dev/v1/query",
PackageRepository: packageRepository{
Url: "https://pypi.org/pypi",
},
},
{
Name: "go",
DbType: vulnDbTypeOsv,
DbEndpoint: "https://api.osv.dev/v1/query",
PackageRepository: packageRepository{
Url: "https://proxy.golang.org",
},
SumRepository: packageRepository{
Url: "https://sum.golang.org",
},
},
},
func populateDefaultsIfEmpty(ruleCfg map[string]any) {
if ruleCfg["ecosystem_config"] == nil {
ruleCfg["ecosystem_config"] = defaultEcosystemConfig
} else if ecoCfg, ok := ruleCfg["ecosystem_config"].([]interface{}); ok && len(ecoCfg) == 0 {
ruleCfg["ecosystem_config"] = defaultEcosystemConfig
}

if ruleCfg["action"] == nil {
ruleCfg["action"] = defaultAction
}
}

func parseConfig(ruleCfg map[string]any) (*config, error) {
if len(ruleCfg) == 0 {
return defaultConfig(), nil
}
populateDefaultsIfEmpty(ruleCfg)

var conf config
validate := validator.New(validator.WithRequiredStructEnabled())
Expand Down

0 comments on commit d3f88cf

Please sign in to comment.