@@ -18,7 +18,6 @@ import (
1818 "time"
1919
2020 "github.com/spf13/cobra"
21- "gopkg.in/yaml.v3"
2221)
2322
2423const CodacyApiBase = "https://app.codacy.com"
@@ -459,119 +458,50 @@ type SemgrepRulesFile struct {
459458
460459// createSemgrepConfigFile creates a semgrep.yaml configuration file based on the API configuration
461460func createSemgrepConfigFile (config []domain.PatternConfiguration , toolsConfigDir string ) error {
462- // When specific patterns are configured, filter rules from rules.yaml
463- if len (config ) > 0 {
464- // First try to read the rules.yaml file
465- rulesFile := filepath .Join ("plugins" , "tools" , "semgrep" , "rules.yaml" )
466- if _ , err := os .Stat (rulesFile ); err == nil {
467- // Read and parse the rules.yaml file
468- data , err := os .ReadFile (rulesFile )
469- if err != nil {
470- fmt .Printf ("Warning: Failed to read rules.yaml: %v\n " , err )
471- // Fall back to the old method
472- semgrepConfigurationString := tools .CreateSemgrepConfig (config )
473- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (semgrepConfigurationString ), utils .DefaultFilePerms )
474- }
475-
476- // Parse the YAML file just enough to get the rules array
477- var allRules SemgrepRulesFile
478- if err := yaml .Unmarshal (data , & allRules ); err != nil {
479- fmt .Printf ("Warning: Failed to parse rules.yaml: %v\n " , err )
480- // Fall back to the old method
481- semgrepConfigurationString := tools .CreateSemgrepConfig (config )
482- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (semgrepConfigurationString ), utils .DefaultFilePerms )
483- }
484-
485- // Create a map of enabled pattern IDs for faster lookup
486- enabledPatterns := make (map [string ]bool )
487- for _ , pattern := range config {
488- if pattern .Enabled && pattern .PatternDefinition .Enabled {
489- // Extract rule ID from pattern ID
490- parts := strings .SplitN (pattern .PatternDefinition .Id , "_" , 2 )
491- if len (parts ) == 2 {
492- ruleID := parts [1 ]
493- enabledPatterns [ruleID ] = true
494- }
495- }
496- }
497-
498- // Filter the rules based on enabled patterns
499- var filteredRules SemgrepRulesFile
500- filteredRules .Rules = []map [string ]interface {}{}
501-
502- for _ , rule := range allRules .Rules {
503- // Get the rule ID
504- if ruleID , ok := rule ["id" ].(string ); ok && enabledPatterns [ruleID ] {
505- // If this rule is enabled, include it
506- filteredRules .Rules = append (filteredRules .Rules , rule )
507- }
508- }
509-
510- // If no rules match, use the old method
511- if len (filteredRules .Rules ) == 0 {
512- fmt .Println ("Warning: No matching rules found in rules.yaml" )
513- semgrepConfigurationString := tools .CreateSemgrepConfig (config )
514- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (semgrepConfigurationString ), utils .DefaultFilePerms )
515- }
516-
517- // Marshal the filtered rules back to YAML
518- filteredData , err := yaml .Marshal (filteredRules )
519- if err != nil {
520- fmt .Printf ("Warning: Failed to marshal filtered rules: %v\n " , err )
521- // Fall back to the old method
522- semgrepConfigurationString := tools .CreateSemgrepConfig (config )
523- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (semgrepConfigurationString ), utils .DefaultFilePerms )
524- }
525-
526- // Write the filtered rules to semgrep.yaml
527- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), filteredData , utils .DefaultFilePerms )
528- }
529-
530- // If rules.yaml doesn't exist, fall back to the old method
531- semgrepConfigurationString := tools .CreateSemgrepConfig (config )
532- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (semgrepConfigurationString ), utils .DefaultFilePerms )
533- }
534-
535- // For default case with no specific patterns, use the entire rules.yaml
536- rulesFile := filepath .Join ("plugins" , "tools" , "semgrep" , "rules.yaml" )
537- if _ , err := os .Stat (rulesFile ); err == nil {
538- data , err := os .ReadFile (rulesFile )
539- if err != nil {
540- fmt .Printf ("Warning: Failed to read rules.yaml: %v\n " , err )
541- // Fall back to the old method for default config
542- emptyConfig := []domain.PatternConfiguration {}
543- content := tools .CreateSemgrepConfig (emptyConfig )
544- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (content ), utils .DefaultFilePerms )
545- }
546- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), data , utils .DefaultFilePerms )
461+ // Use the refactored function from tools package
462+ configData , err := tools .GetSemgrepConfig (config )
463+ if err != nil {
464+ // Log the error but continue with a minimal configuration
465+ fmt .Printf ("Warning: %v. Creating a minimal configuration.\n " , err )
466+
467+ // Create a minimal configuration
468+ minimalConfig := []byte (`rules:
469+ - id: all
470+ pattern: |
471+ $X
472+ message: "Semgrep analysis"
473+ languages: [generic]
474+ severity: INFO
475+ ` )
476+ return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), minimalConfig , utils .DefaultFilePerms )
547477 }
548478
549- // Fall back to default config
550- emptyConfig := []domain.PatternConfiguration {}
551- content := tools .CreateSemgrepConfig (emptyConfig )
552- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (content ), utils .DefaultFilePerms )
479+ // Write to file
480+ return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), configData , utils .DefaultFilePerms )
553481}
554482
555483// createDefaultSemgrepConfigFile creates a default semgrep.yaml configuration file
556484func createDefaultSemgrepConfigFile (toolsConfigDir string ) error {
557- // Use rules.yaml as the default
558- rulesFile := filepath .Join ("plugins" , "tools" , "semgrep" , "rules.yaml" )
559- if _ , err := os .Stat (rulesFile ); err == nil {
560- data , err := os .ReadFile (rulesFile )
561- if err != nil {
562- fmt .Printf ("Warning: Failed to read rules.yaml: %v\n " , err )
563- // Fall back to the old method
564- emptyConfig := []domain.PatternConfiguration {}
565- content := tools .CreateSemgrepConfig (emptyConfig )
566- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (content ), utils .DefaultFilePerms )
567- }
568- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), data , utils .DefaultFilePerms )
485+ // Use the refactored function from tools package
486+ configData , err := tools .GetDefaultSemgrepConfig ()
487+ if err != nil {
488+ // Log the error but continue with a minimal configuration
489+ fmt .Printf ("Warning: %v. Creating a minimal configuration.\n " , err )
490+
491+ // Create a minimal configuration
492+ minimalConfig := []byte (`rules:
493+ - id: all
494+ pattern: |
495+ $X
496+ message: "Semgrep analysis"
497+ languages: [generic]
498+ severity: INFO
499+ ` )
500+ return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), minimalConfig , utils .DefaultFilePerms )
569501 }
570502
571- // Fall back to the old method if rules.yaml doesn't exist
572- emptyConfig := []domain.PatternConfiguration {}
573- content := tools .CreateSemgrepConfig (emptyConfig )
574- return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), []byte (content ), utils .DefaultFilePerms )
503+ // Write to file
504+ return os .WriteFile (filepath .Join (toolsConfigDir , "semgrep.yaml" ), configData , utils .DefaultFilePerms )
575505}
576506
577507// cleanConfigDirectory removes all previous configuration files in the tools-configs directory
0 commit comments