Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 20 additions & 25 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -265,7 +256,9 @@ var analyzeCmd = &cobra.Command{
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("Tool failed to run: %s: %v\n", toolName, err)
}
sarifOutputs = append(sarifOutputs, tmpFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Assuming that this line works even if tmpFile foes not exist, LGTM!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in the mergesarif function non existent files should be handled 👍

		data, err := os.ReadFile(file)
		if err != nil {
			if os.IsNotExist(err) {
				// Skip if file doesn't exist (tool might have failed)
				continue
			}
			return fmt.Errorf("failed to read SARIF file %s: %w", file, err)
		}```

}

Expand Down Expand Up @@ -296,23 +289,25 @@ var analyzeCmd = &cobra.Command{
// Run tools without merging outputs
for toolName := range toolsToRun {
log.Printf("Running %s...\n", toolName)
runTool(workDirectory, toolName, args, outputFile)
if err := runTool(workDirectory, toolName, args, outputFile); err != nil {
log.Printf("Tool failed to run: %s: %v\n", toolName, err)
}
}
}
},
}

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)
}
}
15 changes: 12 additions & 3 deletions tools/eslintRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import (
"codacy/cli-v2/config"
"fmt"
"os"
"os/exec"
"path/filepath"
Expand All @@ -10,7 +11,7 @@
// * Run from the root of the repo we want to analyse
// * NODE_PATH="<the installed eslint path>/node_modules"
// * The local installed ESLint should have the @microsoft/eslint-formatter-sarif installed
func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) {
func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory string, nodeBinary string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) error {

Check warning on line 14 in tools/eslintRunner.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tools/eslintRunner.go#L14

Method RunEslint has 7 parameters (limit is 5)

Check warning on line 14 in tools/eslintRunner.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tools/eslintRunner.go#L14

Method RunEslint has a cyclomatic complexity of 9 (limit is 7)
eslintInstallationNodeModules := filepath.Join(eslintInstallationDirectory, "node_modules")
eslintJsPath := filepath.Join(eslintInstallationNodeModules, ".bin", "eslint")

Expand Down Expand Up @@ -54,6 +55,14 @@
// fmt.Println(cmd.Env)
// fmt.Println(cmd)

// TODO eslint returns 1 when it finds errors, so we're not propagating it
cmd.Run()
// Run the command and handle errors
err := cmd.Run()
if err != nil {
// ESLint returns 1 when it finds errors, which is not a failure
if exitErr, ok := err.(*exec.ExitError); ok && exitErr.ExitCode() == 1 {
return nil
}
return fmt.Errorf("failed to run ESLint: %w", err)
}
return nil
}
3 changes: 2 additions & 1 deletion tools/pmdRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tools

import (
"codacy/cli-v2/config"
"fmt"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -56,7 +57,7 @@ func RunPmd(repositoryToAnalyseDirectory string, pmdBinary string, pathsToCheck
// Exit code 4 means violations found – treat as success
return nil
}
return err
return fmt.Errorf("failed to run PMD: %w", err)
}
return nil
}
2 changes: 1 addition & 1 deletion tools/pylintRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func RunPylint(workDirectory string, toolInfo *plugins.ToolInfo, files []string,
// Pylint returns non-zero exit code when it finds issues
// We should not treat this as an error
if _, ok := err.(*exec.ExitError); !ok {
return fmt.Errorf("failed to run pylint: %w", err)
return fmt.Errorf("failed to run Pylint: %w", err)
}
}

Expand Down
7 changes: 6 additions & 1 deletion tools/trivyRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tools

import (
"codacy/cli-v2/config"
"fmt"
"os"
"os/exec"
)
Expand Down Expand Up @@ -35,5 +36,9 @@ func RunTrivy(repositoryToAnalyseDirectory string, trivyBinary string, pathsToCh
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

return cmd.Run()
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to run Trivy: %w", err)
}
return nil
}