-
Notifications
You must be signed in to change notification settings - Fork 10
[PLUTO-1396] Handle tool errors gracefully #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
f225ba3
f9d4372
235225c
2bc5e47
011554f
484c72c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,42 +188,33 @@ func getToolName(toolName string, version string) string { | |
| return toolName | ||
| } | ||
|
|
||
| func runEslintAnalysis(workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) { | ||
| func runEslintAnalysis(workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) error { | ||
| eslint := config.Config.Tools()["eslint"] | ||
| eslintInstallationDirectory := eslint.InstallDir | ||
| nodeRuntime := config.Config.Runtimes()["node"] | ||
| nodeBinary := nodeRuntime.Binaries["node"] | ||
|
|
||
| tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, pathsToCheck, autoFix, outputFile, outputFormat) | ||
| return tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, pathsToCheck, autoFix, outputFile, outputFormat) | ||
| } | ||
|
|
||
| func runTrivyAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) { | ||
| func runTrivyAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error { | ||
| trivy := config.Config.Tools()["trivy"] | ||
| trivyBinary := trivy.Binaries["trivy"] | ||
|
|
||
| err := tools.RunTrivy(workDirectory, trivyBinary, pathsToCheck, outputFile, outputFormat) | ||
| if err != nil { | ||
| log.Fatalf("Error running Trivy: %v", err) | ||
| } | ||
| return tools.RunTrivy(workDirectory, trivyBinary, pathsToCheck, outputFile, outputFormat) | ||
| } | ||
|
|
||
| func runPmdAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) { | ||
| func runPmdAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error { | ||
| pmd := config.Config.Tools()["pmd"] | ||
| pmdBinary := pmd.Binaries["pmd"] | ||
|
|
||
| err := tools.RunPmd(workDirectory, pmdBinary, pathsToCheck, outputFile, outputFormat, config.Config) | ||
| if err != nil { | ||
| log.Fatalf("Error running PMD: %v", err) | ||
| } | ||
| return tools.RunPmd(workDirectory, pmdBinary, pathsToCheck, outputFile, outputFormat, config.Config) | ||
| } | ||
|
|
||
| func runPylintAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) { | ||
| func runPylintAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error { | ||
| pylint := config.Config.Tools()["pylint"] | ||
|
|
||
| err := tools.RunPylint(workDirectory, pylint, pathsToCheck, outputFile, outputFormat) | ||
| if err != nil { | ||
| log.Fatalf("Error running Pylint: %v", err) | ||
| } | ||
| return tools.RunPylint(workDirectory, pylint, pathsToCheck, outputFile, outputFormat) | ||
| } | ||
|
|
||
| var analyzeCmd = &cobra.Command{ | ||
|
|
@@ -262,13 +253,22 @@ var analyzeCmd = &cobra.Command{ | |
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| var sarifOutputs []string | ||
| failedTools := make(map[string]error) | ||
| for toolName := range toolsToRun { | ||
| log.Printf("Running %s...\n", toolName) | ||
| tmpFile := filepath.Join(tmpDir, fmt.Sprintf("%s.sarif", toolName)) | ||
| runTool(workDirectory, toolName, args, tmpFile) | ||
| if err := runTool(workDirectory, toolName, args, tmpFile); err != nil { | ||
| log.Printf("Warning: Tool %s failed: %v\n", toolName, err) | ||
| failedTools[toolName] = err | ||
| continue | ||
| } | ||
| sarifOutputs = append(sarifOutputs, tmpFile) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming that this line works even if tmpFile foes not exist, LGTM!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, in the mergesarif function non existent files should be handled 👍 |
||
| } | ||
|
|
||
| if len(sarifOutputs) == 0 && len(failedTools) > 0 { | ||
| log.Fatal("All tools failed to run. No analysis results available.") | ||
| } | ||
|
|
||
| // create output file tmp file | ||
| tmpOutputFile := filepath.Join(tmpDir, "merged.sarif") | ||
|
|
||
|
|
@@ -277,6 +277,22 @@ var analyzeCmd = &cobra.Command{ | |
| log.Fatalf("Failed to merge SARIF outputs: %v", err) | ||
| } | ||
|
|
||
| // Add error runs to the merged SARIF | ||
| if len(failedTools) > 0 { | ||
| mergedSarif, err := utils.ReadSarifFile(tmpOutputFile) | ||
| if err != nil { | ||
| log.Fatalf("Failed to read merged SARIF: %v", err) | ||
| } | ||
|
|
||
| for toolName, err := range failedTools { | ||
| utils.AddErrorRun(&mergedSarif, toolName, err.Error()) | ||
|
||
| } | ||
|
|
||
| if err := utils.WriteSarifFile(mergedSarif, tmpOutputFile); err != nil { | ||
| log.Fatalf("Failed to write updated SARIF: %v", err) | ||
| } | ||
| } | ||
|
|
||
| if outputFile != "" { | ||
| // copy tmpOutputFile to outputFile | ||
| content, err := os.ReadFile(tmpOutputFile) | ||
|
|
@@ -302,17 +318,17 @@ var analyzeCmd = &cobra.Command{ | |
| }, | ||
| } | ||
|
|
||
| func runTool(workDirectory string, toolName string, args []string, outputFile string) { | ||
| func runTool(workDirectory string, toolName string, args []string, outputFile string) error { | ||
| switch toolName { | ||
| case "eslint": | ||
| runEslintAnalysis(workDirectory, args, autoFix, outputFile, outputFormat) | ||
| return runEslintAnalysis(workDirectory, args, autoFix, outputFile, outputFormat) | ||
| case "trivy": | ||
| runTrivyAnalysis(workDirectory, args, outputFile, outputFormat) | ||
| return runTrivyAnalysis(workDirectory, args, outputFile, outputFormat) | ||
| case "pmd": | ||
| runPmdAnalysis(workDirectory, args, outputFile, outputFormat) | ||
| return runPmdAnalysis(workDirectory, args, outputFile, outputFormat) | ||
| case "pylint": | ||
| runPylintAnalysis(workDirectory, args, outputFile, outputFormat) | ||
| return runPylintAnalysis(workDirectory, args, outputFile, outputFormat) | ||
| default: | ||
| log.Printf("Warning: Unsupported tool: %s\n", toolName) | ||
| return fmt.Errorf("unsupported tool: %s", toolName) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would see us not having these continues. I thing that the 'append' of sarifs, should be ready to receive a file that does not exist or something.
But would like not to miss any possible generated sarif from the tool
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, will remove 👍