Skip to content

Commit 6c2f037

Browse files
authored
Merge branch 'main' into python_runtime
2 parents 845fe07 + 0f51dbf commit 6c2f037

27 files changed

+3222
-171
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+

.github/workflows/go.yml

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,30 +94,31 @@ jobs:
9494
# Write-Host "Attempting to run CLI..."
9595
# .\cli-v2.exe install
9696

97-
release:
98-
needs: [test, ittest]
99-
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.event_name == 'push'
100-
runs-on: ubuntu-latest
101-
steps:
102-
- name: Checkout
103-
uses: actions/checkout@v4
104-
with:
105-
fetch-depth: 0
106-
- name: Set up Go
107-
uses: actions/setup-go@v4
108-
- name: "Git Version"
109-
id: generate-version
110-
uses: codacy/[email protected]
111-
- name: "Tag version"
112-
run: |
113-
git tag ${{ steps.generate-version.outputs.version }}
114-
git push --tags "https://codacy:${{ secrets.GITHUB_TOKEN }}@github.com/codacy/codacy-cli-v2"
115-
- name: Run GoReleaser
116-
uses: goreleaser/goreleaser-action@v5
117-
with:
118-
distribution: goreleaser
119-
# 'latest', 'nightly', or a semver
120-
version: "latest"
121-
args: release --clean
122-
env:
123-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
97+
## For now we are not releasing the CLI, as we are making some quicker iterations
98+
# release:
99+
# needs: [test, ittest]
100+
# if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) && github.event_name == 'push'
101+
# runs-on: ubuntu-latest
102+
# steps:
103+
# - name: Checkout
104+
# uses: actions/checkout@v4
105+
# with:
106+
# fetch-depth: 0
107+
# - name: Set up Go
108+
# uses: actions/setup-go@v4
109+
# - name: "Git Version"
110+
# id: generate-version
111+
# uses: codacy/[email protected]
112+
# - name: "Tag version"
113+
# run: |
114+
# git tag ${{ steps.generate-version.outputs.version }}
115+
# git push --tags "https://codacy:${{ secrets.GITHUB_TOKEN }}@github.com/codacy/codacy-cli-v2"
116+
# - name: Run GoReleaser
117+
# uses: goreleaser/goreleaser-action@v5
118+
# with:
119+
# distribution: goreleaser
120+
# # 'latest', 'nightly', or a semver
121+
# version: "latest"
122+
# args: release --clean
123+
# env:
124+
# GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ go.work
2222
go.work.sum
2323

2424
.idea/
25+
.vscode/
2526

26-
cli-v2
27+
cli-v2

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Codacy
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

cmd/analyze.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,25 @@ func getToolName(toolName string, version string) string {
184184
return toolName
185185
}
186186

187+
func runEslintAnalysis(workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) {
188+
eslint := config.Config.Tools()["eslint"]
189+
eslintInstallationDirectory := eslint.InstallDir
190+
nodeRuntime := config.Config.Runtimes()["node"]
191+
nodeBinary := nodeRuntime.Binaries["node"]
192+
193+
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, pathsToCheck, autoFix, outputFile, outputFormat)
194+
}
195+
196+
func runTrivyAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) {
197+
trivy := config.Config.Tools()["trivy"]
198+
trivyBinary := trivy.Binaries["trivy"]
199+
200+
err := tools.RunTrivy(workDirectory, trivyBinary, pathsToCheck, outputFile, outputFormat)
201+
if err != nil {
202+
log.Fatalf("Error running Trivy: %v", err)
203+
}
204+
}
205+
187206
var analyzeCmd = &cobra.Command{
188207
Use: "analyze",
189208
Short: "Runs all linters.",
@@ -194,30 +213,23 @@ var analyzeCmd = &cobra.Command{
194213
log.Fatal(err)
195214
}
196215

197-
// TODO add more tools here
198-
switch toolToAnalyze {
199-
case "eslint":
200-
// nothing
201-
case "":
202-
log.Fatal("You need to specify a tool to run analysis with, e.g., '--tool eslint'", toolToAnalyze)
203-
default:
204-
log.Fatal("Trying to run unsupported tool: ", toolToAnalyze)
205-
}
206-
207-
eslint := config.Config.Tools()["eslint"]
208-
eslintInstallationDirectory := eslint.Info()["installDir"]
209-
nodeRuntime := config.Config.Runtimes()["node"]
210-
nodeBinary := nodeRuntime.Binaries["node"]
211-
212216
log.Printf("Running %s...\n", toolToAnalyze)
213217
if outputFormat == "sarif" {
214218
log.Println("Output will be in SARIF format")
215219
}
216-
217220
if outputFile != "" {
218221
log.Println("Output will be available at", outputFile)
219222
}
220223

221-
tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, args, autoFix, outputFile, outputFormat)
224+
switch toolToAnalyze {
225+
case "eslint":
226+
runEslintAnalysis(workDirectory, args, autoFix, outputFile, outputFormat)
227+
case "trivy":
228+
runTrivyAnalysis(workDirectory, args, outputFile, outputFormat)
229+
case "":
230+
log.Fatal("You need to specify a tool to run analysis with, e.g., '--tool eslint'")
231+
default:
232+
log.Fatal("Trying to run unsupported tool: ", toolToAnalyze)
233+
}
222234
},
223235
}

cmd/init.go

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,26 @@ 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.59.1" // 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 tool.Uuid == "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb" {
84+
trivyVersion = tool.Version
85+
}
8286
}
8387

8488
return fmt.Sprintf(`runtimes:
8589
8690
8791
tools:
8892
- eslint@%s
89-
`, eslintVersion)
93+
- trivy@%s
94+
`, eslintVersion, trivyVersion)
9095
}
9196

9297
func buildRepositoryConfigurationFiles(token string) error {
@@ -151,7 +156,24 @@ func buildRepositoryConfigurationFiles(token string) error {
151156
_, err = eslintConfigFile.WriteString(eslintConfigurationString)
152157
if err != nil {
153158
log.Fatal(err)
159+
}
154160

161+
// Create Trivy configuration after processing ESLint
162+
trivyApiConfiguration := extractTrivyConfiguration(apiToolConfigurations)
163+
if trivyApiConfiguration != nil {
164+
// Create trivy.yaml file based on API configuration
165+
err = createTrivyConfigFile(*trivyApiConfiguration)
166+
if err != nil {
167+
log.Fatal(err)
168+
}
169+
fmt.Println("Trivy configuration created based on Codacy settings")
170+
} else {
171+
// Create default trivy.yaml if no configuration from API
172+
err = createDefaultTrivyConfigFile()
173+
if err != nil {
174+
log.Fatal(err)
175+
}
176+
fmt.Println("Default Trivy configuration created")
155177
}
156178

157179
return nil
@@ -198,6 +220,20 @@ func extractESLintConfiguration(toolConfigurations []CodacyToolConfiguration) *C
198220
return nil
199221
}
200222

223+
// extractTrivyConfiguration extracts Trivy configuration from the Codacy API response
224+
func extractTrivyConfiguration(toolConfigurations []CodacyToolConfiguration) *CodacyToolConfiguration {
225+
// Trivy internal codacy uuid
226+
const TrivyUUID = "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb"
227+
228+
for _, toolConfiguration := range toolConfigurations {
229+
if toolConfiguration.Uuid == TrivyUUID {
230+
return &toolConfiguration
231+
}
232+
}
233+
234+
return nil
235+
}
236+
201237
type CodacyToolConfiguration struct {
202238
Uuid string `json:"uuid"`
203239
IsEnabled bool `json:"isEnabled"`
@@ -213,3 +249,65 @@ type ParameterConfiguration struct {
213249
name string `json:"name"`
214250
value string `json:"value"`
215251
}
252+
253+
// createTrivyConfigFile creates a trivy.yaml configuration file based on the API configuration
254+
func createTrivyConfigFile(config CodacyToolConfiguration) error {
255+
// Convert CodacyToolConfiguration to tools.ToolConfiguration
256+
trivyDomainConfiguration := convertAPIToolConfigurationForTrivy(config)
257+
258+
// Use the shared CreateTrivyConfig function to generate the config content
259+
trivyConfigurationString := tools.CreateTrivyConfig(trivyDomainConfiguration)
260+
261+
// Write to file
262+
return os.WriteFile("trivy.yaml", []byte(trivyConfigurationString), 0644)
263+
}
264+
265+
// convertAPIToolConfigurationForTrivy converts API tool configuration to domain model for Trivy
266+
func convertAPIToolConfigurationForTrivy(config CodacyToolConfiguration) tools.ToolConfiguration {
267+
var patterns []tools.PatternConfiguration
268+
269+
// Only process if tool is enabled
270+
if config.IsEnabled {
271+
for _, pattern := range config.Patterns {
272+
var parameters []tools.PatternParameterConfiguration
273+
274+
// By default patterns are enabled
275+
patternEnabled := true
276+
277+
// Check if there's an explicit enabled parameter
278+
for _, param := range pattern.Parameters {
279+
if param.name == "enabled" && param.value == "false" {
280+
patternEnabled = false
281+
}
282+
}
283+
284+
// Add enabled parameter
285+
parameters = append(parameters, tools.PatternParameterConfiguration{
286+
Name: "enabled",
287+
Value: fmt.Sprintf("%t", patternEnabled),
288+
})
289+
290+
patterns = append(
291+
patterns,
292+
tools.PatternConfiguration{
293+
PatternId: pattern.InternalId,
294+
ParamenterConfigurations: parameters,
295+
},
296+
)
297+
}
298+
}
299+
300+
return tools.ToolConfiguration{
301+
PatternsConfiguration: patterns,
302+
}
303+
}
304+
305+
// createDefaultTrivyConfigFile creates a default trivy.yaml configuration file
306+
func createDefaultTrivyConfigFile() error {
307+
// Use empty tool configuration to get default settings
308+
emptyConfig := tools.ToolConfiguration{}
309+
content := tools.CreateTrivyConfig(emptyConfig)
310+
311+
// Write to file
312+
return os.WriteFile("trivy.yaml", []byte(content), 0644)
313+
}

cmd/install.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
cfg "codacy/cli-v2/config"
5-
"fmt"
65
"log"
76

87
"github.com/spf13/cobra"
@@ -33,18 +32,9 @@ func installRuntimes(config *cfg.ConfigType) {
3332
}
3433

3534
func installTools(config *cfg.ConfigType) {
36-
for _, tool := range config.Tools() {
37-
switch tool.Name() {
38-
case "eslint":
39-
// eslint needs node runtime
40-
nodeRuntime := config.Runtimes()["node"]
41-
err := cfg.InstallEslint(nodeRuntime, tool, registry)
42-
if err != nil {
43-
fmt.Println(err.Error())
44-
log.Fatal(err)
45-
}
46-
default:
47-
log.Fatal("Unknown tool:", tool.Name())
48-
}
35+
// Use the new tools-installer instead of manual installation
36+
err := cfg.InstallTools()
37+
if err != nil {
38+
log.Fatal(err)
4939
}
50-
}
40+
}

config-file/configFile.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,22 @@ func parseConfigFile(configContents []byte) error {
3636
return err
3737
}
3838

39+
// Convert the tool strings to ToolConfig objects
40+
toolConfigs := make([]plugins.ToolConfig, 0, len(configFile.TOOLS))
3941
for _, tl := range configFile.TOOLS {
4042
ct, err := parseConfigTool(tl)
4143
if err != nil {
4244
return err
4345
}
44-
config.Config.AddTool(config.NewRuntime(ct.name, ct.version))
46+
toolConfigs = append(toolConfigs, plugins.ToolConfig{
47+
Name: ct.name,
48+
Version: ct.version,
49+
})
50+
}
51+
52+
// Add all tools at once
53+
if err := config.Config.AddTools(toolConfigs); err != nil {
54+
return err
4555
}
4656

4757
return nil

0 commit comments

Comments
 (0)