@@ -2,30 +2,12 @@ package tools
22
33import (
44 "codacy/cli-v2/config"
5- "codacy/cli-v2/utils"
6- "encoding/json"
75 "fmt"
86 "os"
97 "os/exec"
108 "path/filepath"
119)
1210
13- // filterRuleDefinitions removes rule definitions from SARIF output
14- func filterRuleDefinitions (sarifData []byte ) ([]byte , error ) {
15- var report utils.SarifReport
16- if err := json .Unmarshal (sarifData , & report ); err != nil {
17- return nil , fmt .Errorf ("failed to parse SARIF data: %w" , err )
18- }
19-
20- // Remove rules from each run
21- for i := range report .Runs {
22- report .Runs [i ].Tool .Driver .Rules = nil
23- }
24-
25- // Marshal back to JSON with indentation
26- return json .MarshalIndent (report , "" , " " )
27- }
28-
2911// RunSemgrep executes Semgrep analysis on the specified directory
3012func RunSemgrep (workDirectory string , binary string , files []string , outputFile string , outputFormat string ) error {
3113 // Construct base command with -m semgrep to run semgrep module
@@ -38,17 +20,9 @@ func RunSemgrep(workDirectory string, binary string, files []string, outputFile
3820
3921 cmdArgs = append (cmdArgs , "--disable-version-check" )
4022
41- // Create a temporary file for SARIF output if needed
42- var tempFile string
23+ // Add output format if specified
4324 if outputFormat == "sarif" {
44- tmpFile , err := os .CreateTemp ("" , "semgrep-*.sarif" )
45- if err != nil {
46- return fmt .Errorf ("failed to create temporary file: %w" , err )
47- }
48- tempFile = tmpFile .Name ()
49- tmpFile .Close ()
50- defer os .Remove (tempFile )
51- cmdArgs = append (cmdArgs , "--sarif" , "--output" , tempFile )
25+ cmdArgs = append (cmdArgs , "--sarif" )
5226 }
5327
5428 // Define possible Semgrep config file names
@@ -73,8 +47,8 @@ func RunSemgrep(workDirectory string, binary string, files []string, outputFile
7347 cmd := exec .Command (binary , cmdArgs ... )
7448 cmd .Dir = workDirectory
7549
76- if outputFormat != "sarif" && outputFile != "" {
77- // If output file is specified and not SARIF , create it and redirect output
50+ if outputFile != "" {
51+ // If output file is specified, create it and redirect output
7852 var outputWriter * os.File
7953 var err error
8054 outputWriter , err = os .Create (filepath .Clean (outputFile ))
@@ -83,7 +57,7 @@ func RunSemgrep(workDirectory string, binary string, files []string, outputFile
8357 }
8458 defer outputWriter .Close ()
8559 cmd .Stdout = outputWriter
86- } else if outputFormat != "sarif" {
60+ } else {
8761 cmd .Stdout = os .Stdout
8862 }
8963 cmd .Stderr = os .Stderr
@@ -96,29 +70,5 @@ func RunSemgrep(workDirectory string, binary string, files []string, outputFile
9670 }
9771 }
9872
99- // If SARIF output was requested, process it
100- if outputFormat == "sarif" {
101- // Read the temporary SARIF file
102- sarifData , err := os .ReadFile (tempFile )
103- if err != nil {
104- return fmt .Errorf ("failed to read SARIF output: %w" , err )
105- }
106-
107- // Filter out rule definitions
108- filteredData , err := filterRuleDefinitions (sarifData )
109- if err != nil {
110- return fmt .Errorf ("failed to filter SARIF output: %w" , err )
111- }
112-
113- // Write the filtered output
114- if outputFile != "" {
115- if err := os .WriteFile (outputFile , filteredData , 0644 ); err != nil {
116- return fmt .Errorf ("failed to write filtered SARIF output: %w" , err )
117- }
118- } else {
119- fmt .Println (string (filteredData ))
120- }
121- }
122-
12373 return nil
12474}
0 commit comments