Skip to content

Commit 873a549

Browse files
feature: Add support for Trivy in codacy-cli-v2
1 parent c56872d commit 873a549

File tree

13 files changed

+490
-17
lines changed

13 files changed

+490
-17
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ runtimes:
22
33
tools:
44
5+

.examples/code.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { tryInvoke } from '@ember/utils';
2+
3+
class FooComponent extends Component {
4+
foo() {
5+
tryInvoke(this.args, 'bar', ['baz']);
6+
}
7+
}
8+
9+
10+
var token = "github_rXGj85G0qUmzPu2ijX8msJsZRMzweyUuXaF0MeTvQEmGUP6AKSHeWuYn9Ue";

.examples/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module trivy-example
2+
3+
go 1.22.3
4+
5+
require (
6+
github.com/aquasecurity/trivy v0.49.1 // MEDIUM ERROR
7+
github.com/spf13/cobra v1.8.0
8+
github.com/sirupsen/logrus v1.4.2
9+
github.com/dexidp/dex v0.0.0-20200121184102-3b39c6440888 // CRITICAL ERROR - CVE-2020-26160 - Insecure JWT implementation
10+
)

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ go.work.sum
2323

2424
.idea/
2525

26-
cli-v2
26+
cli-v2
27+
28+
# ESLint config
29+
eslint.config.mjs

README.md

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ This is a POC for what could be a new CLI for us. The idea is to rely on the nat
44

55
## Overview
66

7-
The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing code using ESLint and uploading the results in SARIF format to Codacy. It provides two main commands: `analyze` and `upload`.
7+
The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing code using ESLint, Trivy, and uploading the results in SARIF format to Codacy. It provides two main commands: `analyze` and `upload`.
88

99
### Commands
1010

11-
- **`analyze` Command**: Runs ESLint analysis on the codebase.
11+
- **`analyze` Command**: Runs analysis tools on the codebase.
1212
- `--output, -o`: Output file for the results.
13-
- `--tool, -t`: Specifies the tool to run analysis with (e.g., ESLint).
13+
- `--tool, -t`: Specifies the tool to run analysis with (e.g., ESLint, Trivy).
1414
- `--format`: Output format (use 'sarif' for SARIF format to terminal).
15-
- `--fix, -f`: Automatically fixes issues when possible.
15+
- `--fix, -f`: Automatically fixes issues when possible (only applicable to certain tools).
1616
- `--new-pr`: Creates a new GitHub PR with fixed issues.
1717

1818
- **`upload` Command With Project Token**: Uploads a SARIF file containing analysis results to Codacy.
@@ -30,14 +30,15 @@ The `codacy-cli-v2` is a command-line tool for Codacy that supports analyzing co
3030

3131
### Important Concepts
3232

33-
- **`.codacy/codacy.yaml`**: Configuration file to specify `node` and `eslint` versions for the CLI.
33+
- **`.codacy/codacy.yaml`**: Configuration file to specify runtimes and tools versions for the CLI.
3434
```yaml
3535
runtimes:
3636
3737
tools:
3838
39+
3940

40-
- **`codacy-cli-v2 install`**: Command to install the specified node and eslint versions before running analysis.
41+
- **`codacy-cli-v2 install`**: Command to install the specified runtimes and tools before running analysis.
4142

4243
## Download
4344

@@ -78,18 +79,32 @@ To run ESLint and output the results to the terminal:
7879
codacy-cli analyze --tool eslint
7980
```
8081

82+
To run Trivy vulnerability scanner:
83+
84+
```bash
85+
codacy-cli analyze --tool trivy
86+
```
87+
8188
To output results in SARIF format to the terminal:
8289

8390
```bash
8491
codacy-cli analyze --tool eslint --format sarif
8592
```
8693

94+
```bash
95+
codacy-cli analyze --tool trivy --format sarif
96+
```
97+
8798
To store the results as SARIF in a file:
8899

89100
```bash
90101
codacy-cli analyze -t eslint -o eslint.sarif
91102
```
92103

104+
```bash
105+
codacy-cli analyze -t trivy -o trivy.sarif
106+
```
107+
93108
## Upload Results
94109

95110
To upload a SARIF file to Codacy:

cmd/analyze.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ var analyzeCmd = &cobra.Command{
202202
switch toolToAnalyze {
203203
case "eslint":
204204
// nothing
205+
case "trivy":
206+
// nothing
205207
case "":
206-
log.Fatal("You need to specify a tool to run analysis with, e.g., '--tool eslint'", toolToAnalyze)
208+
log.Fatal("You need to specify a tool to run analysis with, e.g., '--tool eslint' or '--tool trivy'")
207209
default:
208210
log.Fatal("Trying to run unsupported tool: ", toolToAnalyze)
209211
}
@@ -215,19 +217,30 @@ var analyzeCmd = &cobra.Command{
215217
failIfThereArePendingChanges()
216218
}
217219

218-
eslint := config.Config.Tools()["eslint"]
219-
eslintInstallationDirectory := eslint.Info()["installDir"]
220-
nodeRuntime := config.Config.Runtimes()["node"]
221-
nodeBinary := nodeRuntime.Info()["node"]
222-
223220
log.Printf("Running %s...\n", toolToAnalyze)
224221
if outputFile != "" {
225222
log.Println("Output will be available at", outputFile)
226223
} else if outputFormat == "sarif" {
227224
log.Println("Output will be in SARIF format")
228225
}
229226

230-
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, args, autoFix, outputFile, outputFormat)
227+
switch toolToAnalyze {
228+
case "eslint":
229+
eslint := config.Config.Tools()["eslint"]
230+
eslintInstallationDirectory := eslint.Info()["installDir"]
231+
nodeRuntime := config.Config.Runtimes()["node"]
232+
nodeBinary := nodeRuntime.Info()["node"]
233+
234+
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, args, autoFix, outputFile, outputFormat)
235+
case "trivy":
236+
trivy := config.Config.Tools()["trivy"]
237+
trivyBinary := trivy.Info()["trivy"]
238+
239+
err := tools.RunTrivy(workDirectory, trivyBinary, args, outputFile, outputFormat)
240+
if err != nil {
241+
log.Printf("Error running Trivy: %v", err)
242+
}
243+
}
231244

232245
if doNewPr {
233246
utils.CreatePr(false)

cmd/init.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,23 @@ func createConfigurationFile(tools []tools.Tool) error {
7272

7373
func configFileTemplate(tools []tools.Tool) string {
7474

75-
// Default version
75+
// Default versions
7676
eslintVersion := "9.3.0"
77+
trivyVersion := "0.50.0" // Use the latest stable version
7778

7879
for _, tool := range tools {
7980
if tool.Uuid == "f8b29663-2cb2-498d-b923-a10c6a8c05cd" {
8081
eslintVersion = tool.Version
8182
}
83+
// If Codacy API provides UUID for Trivy, you would check it here
8284
}
8385

8486
return fmt.Sprintf(`runtimes:
8587
8688
tools:
8789
- eslint@%s
88-
`, eslintVersion)
90+
- trivy@%s
91+
`, eslintVersion, trivyVersion)
8992
}
9093

9194
func buildRepositoryConfigurationFiles(token string) error {

cmd/install.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ func fetchTools(config *cfg.ConfigType) {
5252
fmt.Println(err.Error())
5353
log.Fatal(err)
5454
}
55+
case "trivy":
56+
err := cfg.InstallTrivy(tool)
57+
if err != nil {
58+
fmt.Println(err.Error())
59+
log.Fatal(err)
60+
}
5561
default:
5662
log.Fatal("Unknown tool:", tool.Name())
5763
}

config/runtime.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ func (r *Runtime) populateInfo() {
3232
r.info = genInfoNode(r)
3333
case "eslint":
3434
r.info = genInfoEslint(r)
35+
case "trivy":
36+
r.info = genInfoTrivy(r)
3537
}
3638
}
3739

0 commit comments

Comments
 (0)