Skip to content

Commit 373a0d7

Browse files
committed
feature: Only send sourceId for Trivy CF-1813
This will avoid messing with the results from other tools and respectes on how codacy cloud and the CLI v1 handles it. The downside is that will be a bit error prone when adding other tools and if forget to add it, less results will be added on Codacy, but for now it is an acceptable compromise
1 parent 3ba1156 commit 373a0d7

File tree

4 files changed

+40
-23
lines changed

4 files changed

+40
-23
lines changed

cmd/upload.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package cmd
22

33
import (
44
"bytes"
5+
"codacy/cli-v2/config"
56
"codacy/cli-v2/domain"
7+
"codacy/cli-v2/plugins"
68
"encoding/json"
79
"fmt"
810
"io"
@@ -37,11 +39,11 @@ var uploadResultsCmd = &cobra.Command{
3739
Short: "Uploads a sarif file to Codacy",
3840
Long: "YADA",
3941
Run: func(cmd *cobra.Command, args []string) {
40-
processSarifAndSendResults(sarifPath, commitUuid, projectToken, apiToken)
42+
processSarifAndSendResults(sarifPath, commitUuid, projectToken, apiToken, config.Config.Tools())
4143
},
4244
}
4345

44-
func processSarifAndSendResults(sarifPath string, commitUUID string, projectToken string, apiToken string) {
46+
func processSarifAndSendResults(sarifPath string, commitUUID string, projectToken string, apiToken string, tools map[string]*plugins.ToolInfo) {
4547
if projectToken == "" && apiToken == "" && provider == "" && repository == "" {
4648
fmt.Println("Error: api-token, provider and repository are required when project-token is not provided")
4749
os.Exit(1)
@@ -64,7 +66,7 @@ func processSarifAndSendResults(sarifPath string, commitUUID string, projectToke
6466
}
6567

6668
fmt.Println("Loading Codacy patterns...")
67-
payloads := processSarif(sarif)
69+
payloads := processSarif(sarif, tools)
6870
if projectToken != "" {
6971
for _, payload := range payloads {
7072
sendResultsWithProjectToken(payload, commitUUID, projectToken)
@@ -80,7 +82,7 @@ func processSarifAndSendResults(sarifPath string, commitUUID string, projectToke
8082

8183
}
8284

83-
func processSarif(sarif Sarif) [][]map[string]interface{} {
85+
func processSarif(sarif Sarif, tools map[string]*plugins.ToolInfo) [][]map[string]interface{} {
8486
var codacyIssues []map[string]interface{}
8587
var payloads [][]map[string]interface{}
8688

@@ -96,15 +98,21 @@ func processSarif(sarif Sarif) [][]map[string]interface{} {
9698
continue
9799
}
98100
for _, location := range result.Locations {
99-
codacyIssues = append(codacyIssues, map[string]interface{}{
101+
issue := map[string]interface{}{
100102
"source": location.PhysicalLocation.ArtifactLocation.URI,
101103
"line": location.PhysicalLocation.Region.StartLine,
102104
"type": pattern.ID,
103105
"message": result.Message.Text,
104106
"level": pattern.Level,
105107
"category": pattern.Category,
106-
"sourceId": result.RuleID,
107-
})
108+
}
109+
110+
// Only add sourceId for tools that need it
111+
if toolInfo, exists := tools[toolName]; exists && toolInfo.NeedsSourceIDUpload {
112+
issue["sourceId"] = result.RuleID
113+
}
114+
115+
codacyIssues = append(codacyIssues, issue)
108116
}
109117
}
110118
var results []map[string]interface{}
@@ -134,7 +142,11 @@ func processSarif(sarif Sarif) [][]map[string]interface{} {
134142
"line": obj["line"].(int),
135143
},
136144
},
137-
"sourceId": obj["sourceId"].(string),
145+
}
146+
147+
// Only add sourceId for tools that need it
148+
if toolInfo, exists := tools[toolName]; exists && toolInfo.NeedsSourceIDUpload {
149+
issue["sourceId"] = obj["sourceId"].(string)
138150
}
139151

140152
// Check if we already have an entry for this filename

plugins/tool-utils.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,19 @@ type RuntimeBinaries struct {
5555

5656
// ToolPluginConfig holds the structure of the tool plugin.yaml file
5757
type ToolPluginConfig struct {
58-
Name string `yaml:"name"`
59-
Description string `yaml:"description"`
60-
DefaultVersion string `yaml:"default_version"`
61-
Runtime string `yaml:"runtime"`
62-
RuntimeBinaries RuntimeBinaries `yaml:"runtime_binaries"`
63-
Installation InstallationConfig `yaml:"installation"`
64-
Download DownloadConfig `yaml:"download"`
65-
Environment map[string]string `yaml:"environment"`
66-
Binaries []ToolBinary `yaml:"binaries"`
67-
Formatters []Formatter `yaml:"formatters"`
68-
OutputOptions OutputOptions `yaml:"output_options"`
69-
AnalysisOptions AnalysisOptions `yaml:"analysis_options"`
58+
Name string `yaml:"name"`
59+
Description string `yaml:"description"`
60+
DefaultVersion string `yaml:"default_version"`
61+
Runtime string `yaml:"runtime"`
62+
RuntimeBinaries RuntimeBinaries `yaml:"runtime_binaries"`
63+
Installation InstallationConfig `yaml:"installation"`
64+
Download DownloadConfig `yaml:"download"`
65+
Environment map[string]string `yaml:"environment"`
66+
Binaries []ToolBinary `yaml:"binaries"`
67+
Formatters []Formatter `yaml:"formatters"`
68+
OutputOptions OutputOptions `yaml:"output_options"`
69+
AnalysisOptions AnalysisOptions `yaml:"analysis_options"`
70+
NeedsSourceIDUpload bool `yaml:"needs_source_id_upload"`
7071
}
7172

7273
// ToolConfig represents configuration for a tool
@@ -98,7 +99,8 @@ type ToolInfo struct {
9899
FileName string
99100
Extension string
100101
// Environment variables
101-
Environment map[string]string
102+
Environment map[string]string
103+
NeedsSourceIDUpload bool
102104
}
103105

104106
// ProcessTools processes a list of tool configurations and returns a map of tool information
@@ -151,7 +153,8 @@ func ProcessTools(configs []ToolConfig, toolDir string, runtimes map[string]*Run
151153
InstallCommand: pluginConfig.Installation.Command,
152154
RegistryCommand: pluginConfig.Installation.RegistryTemplate,
153155
// Store environment variables
154-
Environment: make(map[string]string),
156+
Environment: make(map[string]string),
157+
NeedsSourceIDUpload: pluginConfig.NeedsSourceIDUpload,
155158
}
156159

157160
// Handle download configuration for directly downloaded tools

plugins/tool-utils_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func TestProcessTools(t *testing.T) {
6666
// Assert installation command templates are correctly set
6767
assert.Equal(t, "install --prefix {{.InstallDir}} {{.PackageName}}@{{.Version}} @microsoft/eslint-formatter-sarif", eslintInfo.InstallCommand)
6868
assert.Equal(t, "config set registry {{.Registry}}", eslintInfo.RegistryCommand)
69+
assert.Equal(t, eslintInfo.NeedsSourceIDUpload, false)
6970
}
7071

7172
func TestProcessToolsWithDownload(t *testing.T) {
@@ -151,6 +152,7 @@ func TestProcessToolsWithDownload(t *testing.T) {
151152
expectedArch = runtime.GOARCH
152153
}
153154
assert.Contains(t, trivyInfo.DownloadURL, expectedArch)
155+
assert.Equal(t, trivyInfo.NeedsSourceIDUpload, true)
154156
}
155157

156158
func TestGetSupportedTools(t *testing.T) {

plugins/tools/trivy/plugin.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ download:
1616
"darwin": "macOS"
1717
"linux": "Linux"
1818
"windows": "Windows"
19-
2019
binaries:
2120
- name: trivy
2221
path: "trivy"
22+
needs_source_id_upload: true

0 commit comments

Comments
 (0)