Skip to content

Commit ee92327

Browse files
fix upload sarif file
1 parent 6b54110 commit ee92327

File tree

6 files changed

+75
-24
lines changed

6 files changed

+75
-24
lines changed

cmd/analyze.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func loadsToolAndPatterns(toolName string, onlyEnabledPatterns bool) (domain.Too
251251
}
252252
}
253253
var patterns []domain.PatternConfiguration
254-
patterns, err = codacyclient.GetDefaultToolPatternsConfig(domain.InitFlags{}, tool.Uuid, onlyEnabledPatterns)
254+
patterns, err = codacyclient.GetToolPatternsConfig(domain.InitFlags{}, tool.Uuid, onlyEnabledPatterns)
255255
if err != nil {
256256
fmt.Println("Error:", err)
257257
return domain.Tool{}, []domain.PatternConfiguration{}

cmd/configsetup/default_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func createDefaultConfigurationsForSpecificTools(discoveredToolNames map[string]
200200
// createToolConfigurationsForUUIDs creates tool configurations for specific UUIDs
201201
func createToolConfigurationsForUUIDs(uuids []string, toolsConfigDir string, initFlags domain.InitFlags) error {
202202
for _, uuid := range uuids {
203-
patternsConfig, err := codacyclient.GetDefaultToolPatternsConfig(initFlags, uuid, true)
203+
patternsConfig, err := codacyclient.GetToolPatternsConfig(initFlags, uuid, true)
204204
if err != nil {
205205
logToolConfigWarning(uuid, "Failed to get default patterns", err)
206206
continue

cmd/configsetup/repository_config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func CreateToolConfigurationFile(toolName string, flags domain.InitFlags) error
9898
return fmt.Errorf("tool '%s' not found in supported tools", toolName)
9999
}
100100

101-
patternsConfig, err := codacyclient.GetDefaultToolPatternsConfig(flags, toolUUID, true)
101+
patternsConfig, err := codacyclient.GetToolPatternsConfig(flags, toolUUID, true)
102102
if err != nil {
103103
return fmt.Errorf("failed to get default patterns: %w", err)
104104
}

cmd/upload.go

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"fmt"
1010
"io"
1111
"net/http"
12+
"net/url"
1213
"os"
14+
"path/filepath"
1315
"strconv"
1416
"strings"
1517

@@ -43,6 +45,48 @@ var uploadResultsCmd = &cobra.Command{
4345
},
4446
}
4547

48+
var sarifShortNameMap = map[string]string{
49+
// The keys here MUST match the exact string found in run.Tool.Driver.Name
50+
"ESLint (deprecated)": "eslint",
51+
"ESLint": "eslint-8",
52+
"ESLint9": "eslint-9",
53+
"PMD": "pmd",
54+
"PMD7": "pmd-7",
55+
"Trivy": "trivy",
56+
"Pylint": "pylintpython3",
57+
"dartanalyzer": "dartanalyzer",
58+
"Semgrep": "semgrep",
59+
"Lizard": "lizard",
60+
"revive": "revive",
61+
}
62+
63+
// Helper to look up the short name
64+
func getToolShortName(fullName string) string {
65+
if shortName, ok := sarifShortNameMap[fullName]; ok {
66+
return shortName
67+
}
68+
// Fallback: Use the original name if no mapping is found
69+
return fullName
70+
}
71+
72+
func getRelativePath(baseDir string, fullURI string) string {
73+
74+
localPath := fullURI
75+
u, err := url.Parse(fullURI)
76+
if err == nil && u.Scheme == "file" {
77+
// url.Path extracts the local path component correctly
78+
localPath = u.Path
79+
}
80+
relativePath, err := filepath.Rel(baseDir, localPath)
81+
if err != nil {
82+
// Fallback to the normalized absolute path if calculation fails
83+
fmt.Printf("Warning: Could not get relative path for '%s' relative to '%s': %v. Using absolute path.\n", localPath, baseDir, err)
84+
return localPath
85+
}
86+
87+
return relativePath
88+
}
89+
4690
func processSarifAndSendResults(sarifPath string, commitUUID string, projectToken string, apiToken string, tools map[string]*plugins.ToolInfo) {
4791
if projectToken == "" && apiToken == "" && provider == "" && repository == "" {
4892
fmt.Println("Error: api-token, provider and repository are required when project-token is not provided")
@@ -86,6 +130,12 @@ func processSarif(sarif Sarif, tools map[string]*plugins.ToolInfo) [][]map[strin
86130
var codacyIssues []map[string]interface{}
87131
var payloads [][]map[string]interface{}
88132

133+
baseDir, err := os.Getwd()
134+
if err != nil {
135+
fmt.Printf("Error getting current working directory: %v\n", err)
136+
os.Exit(1)
137+
}
138+
89139
for _, run := range sarif.Runs {
90140
var toolName = getToolName(strings.ToLower(run.Tool.Driver.Name), run.Tool.Driver.Version)
91141
tool, patterns := loadsToolAndPatterns(toolName, false)
@@ -98,8 +148,12 @@ func processSarif(sarif Sarif, tools map[string]*plugins.ToolInfo) [][]map[strin
98148
continue
99149
}
100150
for _, location := range result.Locations {
151+
152+
fullURI := location.PhysicalLocation.ArtifactLocation.URI
153+
relativePath := getRelativePath(baseDir, fullURI)
154+
101155
issue := map[string]interface{}{
102-
"source": location.PhysicalLocation.ArtifactLocation.URI,
156+
"source": relativePath,
103157
"line": location.PhysicalLocation.Region.StartLine,
104158
"type": pattern.ID,
105159
"message": result.Message.Text,
@@ -119,8 +173,12 @@ func processSarif(sarif Sarif, tools map[string]*plugins.ToolInfo) [][]map[strin
119173
// Iterate through run.Artifacts and create entries in the results object
120174
for _, artifact := range run.Artifacts {
121175
if artifact.Location.URI != "" {
176+
177+
fullURI := artifact.Location.URI
178+
relativePath := getRelativePath(baseDir, fullURI)
179+
122180
results = append(results, map[string]interface{}{
123-
"filename": artifact.Location.URI,
181+
"filename": relativePath,
124182
"results": []map[string]interface{}{},
125183
})
126184
}
@@ -169,10 +227,10 @@ func processSarif(sarif Sarif, tools map[string]*plugins.ToolInfo) [][]map[strin
169227
}
170228

171229
}
172-
230+
var toolShortName = getToolShortName(toolName)
173231
payload := []map[string]interface{}{
174232
{
175-
"tool": toolName,
233+
"tool": toolShortName,
176234
"issues": map[string]interface{}{
177235
"Success": map[string]interface{}{
178236
"results": results,

codacy-client/client.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ func parsePatternConfigurations(response []byte) ([]domain.PatternConfiguration,
170170
return patternConfigurations, pagination.Cursor, nil
171171
}
172172

173-
// GetDefaultToolPatternsConfig fetches the default patterns for a tool
174-
func GetDefaultToolPatternsConfig(initFlags domain.InitFlags, toolUUID string, onlyEnabledPatterns bool) ([]domain.PatternConfiguration, error) {
175-
return GetDefaultToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, toolUUID, onlyEnabledPatterns)
173+
// GetToolPatternsConfig fetches the default patterns for a tool
174+
func GetToolPatternsConfig(initFlags domain.InitFlags, toolUUID string, onlyEnabledPatterns bool) ([]domain.PatternConfiguration, error) {
175+
return GetToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, toolUUID, onlyEnabledPatterns)
176176
}
177177

178-
// GetDefaultToolPatternsConfigWithCodacyAPIBase fetches the default patterns for a tool, and a base api url
179-
func GetDefaultToolPatternsConfigWithCodacyAPIBase(codacyAPIBaseURL string, initFlags domain.InitFlags, toolUUID string, onlyEnabledPatterns bool) ([]domain.PatternConfiguration, error) {
178+
// GetToolPatternsConfigWithCodacyAPIBase fetches the default patterns for a tool, and a base api url
179+
func GetToolPatternsConfigWithCodacyAPIBase(codacyAPIBaseURL string, initFlags domain.InitFlags, toolUUID string, onlyEnabledPatterns bool) ([]domain.PatternConfiguration, error) {
180180
baseURL := fmt.Sprintf("%s/api/v3/tools/%s/patterns", codacyAPIBaseURL, toolUUID)
181181
if onlyEnabledPatterns {
182182
baseURL += "?enabled=true"
@@ -187,14 +187,7 @@ func GetDefaultToolPatternsConfigWithCodacyAPIBase(codacyAPIBaseURL string, init
187187
return nil, err
188188
}
189189

190-
onlyRecommendedPatterns := make([]domain.PatternConfiguration, 0)
191-
for _, pattern := range allPaterns {
192-
if pattern.PatternDefinition.Enabled {
193-
onlyRecommendedPatterns = append(onlyRecommendedPatterns, pattern)
194-
}
195-
}
196-
197-
return onlyRecommendedPatterns, nil
190+
return allPaterns, nil
198191
}
199192

200193
// GetRepositoryToolPatterns fetches the patterns for a tool in a repository

codacy-client/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestGetPageAndGetAllPages(t *testing.T) {
9191
assert.Len(t, allItems, 3)
9292
}
9393

94-
func TestGetDefaultToolPatternsConfig_Empty(t *testing.T) {
94+
func TestGetToolPatternsConfig_Empty(t *testing.T) {
9595
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
9696
resp := map[string]interface{}{
9797
"data": []interface{}{},
@@ -104,12 +104,12 @@ func TestGetDefaultToolPatternsConfig_Empty(t *testing.T) {
104104
CodacyApiBase = ts.URL
105105

106106
initFlags := domain.InitFlags{ApiToken: "dummy"}
107-
patterns, err := GetDefaultToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, "tool-uuid", true)
107+
patterns, err := GetToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, "tool-uuid", true)
108108
assert.NoError(t, err)
109109
assert.Empty(t, patterns)
110110
}
111111

112-
func TestGetDefaultToolPatternsConfig_WithNonRecommended(t *testing.T) {
112+
func TestGetToolPatternsConfig_WithNonRecommended(t *testing.T) {
113113

114114
config := []domain.PatternDefinition{
115115
{
@@ -146,7 +146,7 @@ func TestGetDefaultToolPatternsConfig_WithNonRecommended(t *testing.T) {
146146
CodacyApiBase = ts.URL
147147

148148
initFlags := domain.InitFlags{ApiToken: "dummy"}
149-
patterns, err := GetDefaultToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, "tool-uuid", true)
149+
patterns, err := GetToolPatternsConfigWithCodacyAPIBase(CodacyApiBase, initFlags, "tool-uuid", true)
150150

151151
fmt.Println(len(patterns))
152152

0 commit comments

Comments
 (0)