Skip to content

Commit 91a9a02

Browse files
refactor: streamline SARIF output handling in semgrepRunner.go
- Removed the local filterRuleDefinitions function from semgrepRunner.go, centralizing SARIF rule filtering in the utils package. - Simplified the command execution logic for SARIF output, ensuring proper handling of output files and formats. - Enhanced code clarity and maintainability by leveraging the existing FilterRuleDefinitions function in utils/sarif.go.
1 parent ab1a78c commit 91a9a02

File tree

2 files changed

+28
-56
lines changed

2 files changed

+28
-56
lines changed

tools/semgrepRunner.go

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,12 @@ package tools
22

33
import (
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
3012
func 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
}

utils/sarif.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,14 @@ func MergeSarifOutputs(inputFiles []string, outputFile string) error {
204204
return fmt.Errorf("failed to read SARIF file %s: %w", file, err)
205205
}
206206

207+
// Filter out rule definitions from each input file
208+
filteredData, err := FilterRuleDefinitions(data)
209+
if err != nil {
210+
return fmt.Errorf("failed to filter rules from SARIF file %s: %w", file, err)
211+
}
212+
207213
var sarif SimpleSarifReport
208-
if err := json.Unmarshal(data, &sarif); err != nil {
214+
if err := json.Unmarshal(filteredData, &sarif); err != nil {
209215
return fmt.Errorf("failed to parse SARIF file %s: %w", file, err)
210216
}
211217

@@ -227,3 +233,19 @@ func MergeSarifOutputs(inputFiles []string, outputFile string) error {
227233

228234
return nil
229235
}
236+
237+
// FilterRuleDefinitions removes rule definitions from SARIF output
238+
func FilterRuleDefinitions(sarifData []byte) ([]byte, error) {
239+
var report SarifReport
240+
if err := json.Unmarshal(sarifData, &report); err != nil {
241+
return nil, fmt.Errorf("failed to parse SARIF data: %w", err)
242+
}
243+
244+
// Remove rules from each run
245+
for i := range report.Runs {
246+
report.Runs[i].Tool.Driver.Rules = nil
247+
}
248+
249+
// Marshal back to JSON with indentation
250+
return json.MarshalIndent(report, "", " ")
251+
}

0 commit comments

Comments
 (0)