Skip to content

Commit 08979c7

Browse files
Runners use conf files form .codacy
- Removed cli-config.yaml from the repository and updated .gitignore accordingly. - Added ESLint and Trivy configuration files to the tools-configs directory. - Modified ESLint and Trivy runner functions to utilize the new configuration files. - Cleaned up PMD runner to default to the new ruleset configuration if none is specified.
1 parent 32902e5 commit 08979c7

File tree

12 files changed

+51
-21
lines changed

12 files changed

+51
-21
lines changed

.codacy/cli-config.yaml

Lines changed: 0 additions & 1 deletion
This file was deleted.

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,6 @@ go.work.sum
2626

2727
# Codacy CLI
2828
cli-v2
29-
.codacy/tools-configs
29+
.codacy/cli-config.yaml
30+
.codacy/tools-configs
31+
.codacy/tools-configs/*

cmd/analyze.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ var outputFormat string
2020
var sarifPath string
2121
var commitUuid string
2222
var projectToken string
23-
var pmdRulesetFile string
2423

2524
type Sarif struct {
2625
Runs []struct {
@@ -96,7 +95,6 @@ func init() {
9695
analyzeCmd.Flags().StringVarP(&toolToAnalyze, "tool", "t", "", "Which tool to run analysis with")
9796
analyzeCmd.Flags().StringVar(&outputFormat, "format", "", "Output format (use 'sarif' for SARIF format)")
9897
analyzeCmd.Flags().BoolVar(&autoFix, "fix", false, "Apply auto fix to your issues when available")
99-
analyzeCmd.Flags().StringVar(&pmdRulesetFile, "rulesets", "", "Path to PMD ruleset file")
10098
rootCmd.AddCommand(analyzeCmd)
10199
}
102100

@@ -209,7 +207,7 @@ func runPmdAnalysis(workDirectory string, pathsToCheck []string, outputFile stri
209207
pmd := config.Config.Tools()["pmd"]
210208
pmdBinary := pmd.Binaries["pmd"]
211209

212-
err := tools.RunPmd(workDirectory, pmdBinary, pathsToCheck, outputFile, outputFormat, pmdRulesetFile)
210+
err := tools.RunPmd(workDirectory, pmdBinary, pathsToCheck, outputFile, outputFormat, "")
213211
if err != nil {
214212
log.Fatalf("Error running PMD: %v", err)
215213
}

cmd/init.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ func buildRepositoryConfigurationFiles(token string) error {
143143
fmt.Println("Building repository configuration files ...")
144144
fmt.Println("Fetching repository configuration from codacy ...")
145145

146-
localCodacyDir := config.Config.LocalCodacyDirectory()
147-
toolsConfigDir := filepath.Join(localCodacyDir, "tools-configs")
146+
toolsConfigDir := config.Config.ToolsConfigDirectory()
148147

149148
// Create tools-configs directory if it doesn't exist
150149
if err := os.MkdirAll(toolsConfigDir, 0777); err != nil {

config/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type ConfigType struct {
1515
runtimesDirectory string
1616
toolsDirectory string
1717
localCodacyDirectory string
18+
toolsConfigDirectory string
1819
projectConfigFile string
1920
cliConfigFile string
2021

@@ -88,11 +89,16 @@ func (c *ConfigType) AddTools(configs []plugins.ToolConfig) error {
8889
return nil
8990
}
9091

92+
func (c *ConfigType) ToolsConfigDirectory() string {
93+
return c.toolsConfigDirectory
94+
}
95+
9196
func (c *ConfigType) setupCodacyPaths() {
9297
c.globalCacheDirectory = filepath.Join(c.homePath, ".cache", "codacy")
9398
c.runtimesDirectory = filepath.Join(c.globalCacheDirectory, "runtimes")
9499
c.toolsDirectory = filepath.Join(c.globalCacheDirectory, "tools")
95100
c.localCodacyDirectory = ".codacy"
101+
c.toolsConfigDirectory = filepath.Join(c.localCodacyDirectory, "tools-configs")
96102

97103
c.projectConfigFile = filepath.Join(c.localCodacyDirectory, "codacy.yaml")
98104
c.cliConfigFile = filepath.Join(c.localCodacyDirectory, "cli-config.yaml")

tools/eslintConfigCreator.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func CreateEslintConfig(configuration ToolConfiguration) string {
2626

2727
for _, patternConfiguration := range configuration.PatternsConfiguration {
2828
rule := strings.TrimPrefix(patternConfiguration.PatternId, "ESLint8_")
29-
fmt.Println("Rule:", rule)
3029

3130
const tempstring = "TEMPORARYSTRING"
3231
rule = strings.ReplaceAll(rule, "__", tempstring)

tools/eslintRunner.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ func RunEslint(repositoryToAnalyseDirectory string, eslintInstallationDirectory
1414
eslintJsPath := filepath.Join(eslintInstallationNodeModules, ".bin", "eslint")
1515

1616
cmd := exec.Command(nodeBinary, eslintJsPath)
17+
18+
// For Eslint compatibility with version 8.
19+
// https://eslint.org/docs/v8.x/use/configure/configuration-files-new
20+
cmd.Env = append(cmd.Env, "ESLINT_USE_FLAT_CONFIG=true")
21+
22+
// Add config file from tools-configs directory
23+
configFile := filepath.Join(".codacy", "tools-configs", "eslint.config.mjs")
24+
cmd.Args = append(cmd.Args, "-c", configFile)
25+
1726
if autoFix {
1827
cmd.Args = append(cmd.Args, "--fix")
1928
}

tools/pmdRunner.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,52 @@ package tools
33
import (
44
"os"
55
"os/exec"
6+
"path/filepath"
67
"strings"
78
)
89

910
// RunPmd executes PMD static code analyzer with the specified options
11+
//
12+
// Parameters:
13+
// - repositoryToAnalyseDirectory: The root directory of the repository to analyze
14+
// - pmdBinary: Path to the PMD executable
15+
// - pathsToCheck: List of specific paths to analyze, if empty analyzes whole repository
16+
// - outputFile: Path where analysis results should be written
17+
// - outputFormat: Format for the output (e.g. "sarif")
18+
// - rulesetFile: Path to custom ruleset XML file, if empty uses default ruleset
19+
//
20+
// Returns:
21+
// - error: nil if analysis succeeds or violations found, error otherwise
1022
func RunPmd(repositoryToAnalyseDirectory string, pmdBinary string, pathsToCheck []string, outputFile string, outputFormat string, rulesetFile string) error {
11-
cmdArgs := []string{"pmd"}
23+
cmd := exec.Command(pmdBinary, "pmd")
24+
25+
// Add config file from tools-configs directory if not specified
26+
if rulesetFile == "" {
27+
configFile := filepath.Join(".codacy", "tools-configs", "pmd-ruleset.xml")
28+
cmd.Args = append(cmd.Args, "-R", configFile)
29+
} else {
30+
cmd.Args = append(cmd.Args, "-R", rulesetFile)
31+
}
1232

1333
// Add source directories (comma-separated list for PMD)
1434
if len(pathsToCheck) > 0 {
1535
dirArg := strings.Join(pathsToCheck, ",")
16-
cmdArgs = append(cmdArgs, "-d", dirArg)
36+
cmd.Args = append(cmd.Args, "-d", dirArg)
1737
} else {
1838
// Fall back to whole repo if no specific paths given
19-
cmdArgs = append(cmdArgs, "-d", repositoryToAnalyseDirectory)
20-
}
21-
22-
// Add ruleset
23-
if rulesetFile != "" {
24-
cmdArgs = append(cmdArgs, "-R", rulesetFile)
39+
cmd.Args = append(cmd.Args, "-d", repositoryToAnalyseDirectory)
2540
}
2641

2742
// Format
2843
if outputFormat != "" {
29-
cmdArgs = append(cmdArgs, "-f", outputFormat)
44+
cmd.Args = append(cmd.Args, "-f", outputFormat)
3045
}
3146

3247
// Output file
3348
if outputFile != "" {
34-
cmdArgs = append(cmdArgs, "-r", outputFile)
49+
cmd.Args = append(cmd.Args, "-r", outputFile)
3550
}
3651

37-
cmd := exec.Command(pmdBinary, cmdArgs...)
38-
3952
cmd.Dir = repositoryToAnalyseDirectory
4053
cmd.Stderr = os.Stderr
4154
cmd.Stdout = os.Stdout

tools/testdata/repositories/test1/expected.sarif

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"artifacts": [
1515
{
1616
"location": {
17-
"uri": "testdata/repositories/test1/src/eslint.config.mjs"
17+
"uri": "testdata/repositories/test1/src/.codacy/tools-configs/eslint.config.mjs"
1818
}
1919
},
2020
{

tools/testdata/repositories/test1/src/eslint.config.mjs renamed to tools/testdata/repositories/test1/src/.codacy/tools-configs/eslint.config.mjs

File renamed without changes.

0 commit comments

Comments
 (0)