Skip to content

Commit 5ca127d

Browse files
authored
Merge pull request #21 from codacy/fix-payload-format
fix payload generator
2 parents e042228 + f406b00 commit 5ca127d

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

cmd/upload.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,40 +104,53 @@ func processSarif(sarif Sarif) [][]map[string]interface{} {
104104
})
105105
}
106106
}
107-
groups := make(map[string]map[string]interface{})
107+
var results []map[string]interface{}
108108
for _, obj := range codacyIssues {
109109
source := obj["source"].(string)
110-
if _, ok := groups[source]; !ok {
111-
groups[source] = map[string]interface{}{
112-
"filename": source,
113-
"results": []interface{}{},
110+
issue := map[string]interface{}{
111+
"patternId": map[string]string{
112+
"value": obj["type"].(string),
113+
},
114+
"filename": source,
115+
"message": map[string]string{
116+
"text": obj["message"].(string),
117+
},
118+
"level": obj["level"].(string),
119+
//"category": obj["category"].(string),
120+
"location": map[string]interface{}{
121+
"LineLocation": map[string]int{
122+
"line": obj["line"].(int),
123+
},
124+
},
125+
}
126+
127+
// Check if we already have an entry for this filename
128+
found := false
129+
for i, result := range results {
130+
if result["filename"] == source {
131+
// If we do, append this issue to its results
132+
results[i]["results"] = append(results[i]["results"].([]map[string]interface{}), map[string]interface{}{"Issue": issue})
133+
found = true
134+
break
114135
}
115136
}
116-
groups[source]["results"] = append(groups[source]["results"].([]interface{}), map[string]interface{}{
117-
"Issue": map[string]interface{}{
118-
"patternId": map[string]string{
119-
"value": obj["type"].(string),
120-
},
137+
138+
// If we don't, create a new entry
139+
if !found {
140+
results = append(results, map[string]interface{}{
121141
"filename": source,
122-
"message": map[string]string{
123-
"text": obj["message"].(string),
124-
},
125-
"level": obj["level"].(string),
126-
"category": obj["category"].(string),
127-
"location": map[string]interface{}{
128-
"LineLocation": map[string]int{
129-
"line": obj["line"].(int),
130-
},
131-
},
132-
},
133-
})
142+
"results": []map[string]interface{}{{"Issue": issue}},
143+
})
144+
}
145+
134146
}
147+
135148
payload := []map[string]interface{}{
136149
{
137150
"tool": toolName,
138151
"issues": map[string]interface{}{
139152
"Success": map[string]interface{}{
140-
"results": groups,
153+
"results": results,
141154
},
142155
},
143156
},
@@ -216,6 +229,7 @@ func sendResultsWithProjectToken(payload []map[string]interface{}, commitUUID st
216229

217230
url := fmt.Sprintf("https://api.codacy.com/2.0/commit/%s/issuesRemoteResults", commitUUID)
218231
fmt.Printf("Sending results to URL: %s\n", url)
232+
fmt.Println("Payload:", string(payloadBytes))
219233
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
220234
if err != nil {
221235
fmt.Printf("Error creating request: %v\n", err)
@@ -265,5 +279,7 @@ func sendResultsWithAPIToken(payload []map[string]interface{}, commitUUID string
265279
if resp.StatusCode != http.StatusOK {
266280
fmt.Printf("Error sending results, status code: %d\n", resp.StatusCode)
267281
os.Exit(1)
282+
} else {
283+
fmt.Println("Results sent successfully")
268284
}
269285
}

0 commit comments

Comments
 (0)