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+
4690func 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 ,
0 commit comments