-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathupload.go
More file actions
306 lines (271 loc) · 9.07 KB
/
upload.go
File metadata and controls
306 lines (271 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package cmd
import (
"bytes"
"codacy/cli-v2/domain"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
)
var apiToken string
var provider string
var owner string
var repository string
func init() {
uploadResultsCmd.Flags().StringVarP(&sarifPath, "sarif-path", "s", "", "Path to the SARIF report")
uploadResultsCmd.MarkPersistentFlagRequired("sarif-path")
uploadResultsCmd.Flags().StringVarP(&commitUuid, "commit-uuid", "c", "", "Commit UUID")
uploadResultsCmd.Flags().StringVarP(&projectToken, "project-token", "t", "", "Project token for Codacy API")
uploadResultsCmd.Flags().StringVarP(&apiToken, "api-token", "a", "", "API token for Codacy API")
uploadResultsCmd.Flags().StringVarP(&provider, "provider", "p", "", "Provider (gh, gl, bb)")
uploadResultsCmd.Flags().StringVarP(&owner, "owner", "o", "", "Owner/Organization")
uploadResultsCmd.Flags().StringVarP(&repository, "repository", "r", "", "Repository")
rootCmd.AddCommand(uploadResultsCmd)
}
var uploadResultsCmd = &cobra.Command{
Use: "upload",
Short: "Uploads a sarif file to Codacy",
Long: "YADA",
Run: func(cmd *cobra.Command, args []string) {
processSarifAndSendResults(sarifPath, commitUuid, projectToken, apiToken)
},
}
func processSarifAndSendResults(sarifPath string, commitUUID string, projectToken string, apiToken string) {
if projectToken == "" && apiToken == "" && provider == "" && repository == "" {
fmt.Println("Error: api-token, provider and repository are required when project-token is not provided")
os.Exit(1)
}
//Load SARIF file
fmt.Printf("Loading SARIF file from path: %s\n", sarifPath)
sarifFile, err := os.Open(sarifPath)
if err != nil {
fmt.Printf("Error opening SARIF file: %v\n", err)
panic("panic")
}
defer sarifFile.Close()
var sarif Sarif
fmt.Println("Parsing SARIF file...")
err = json.NewDecoder(sarifFile).Decode(&sarif)
if err != nil {
fmt.Printf("Error parsing SARIF file: %v\n", err)
os.Exit(1)
}
fmt.Println("Loading Codacy patterns...")
payloads := processSarif(sarif)
if projectToken != "" {
for _, payload := range payloads {
sendResultsWithProjectToken(payload, commitUUID, projectToken)
}
resultsFinalWithProjectToken(commitUUID, projectToken)
} else {
for _, payload := range payloads {
sendResultsWithAPIToken(payload, commitUUID, apiToken, provider, owner, repository)
}
resultsFinalWithAPIToken(commitUUID, apiToken, provider, owner, repository)
}
}
func processSarif(sarif Sarif) [][]map[string]interface{} {
var codacyIssues []map[string]interface{}
var payloads [][]map[string]interface{}
for _, run := range sarif.Runs {
var toolName = getToolName(strings.ToLower(run.Tool.Driver.Name), run.Tool.Driver.Version)
tool, patterns := loadsToolAndPatterns(toolName, false)
for _, result := range run.Results {
modifiedType := tool.Prefix + strings.Replace(result.RuleID, "/", "_", -1)
pattern := getPatternByID(patterns, modifiedType)
if pattern == nil {
fmt.Printf("Rule '%s' doesn't have a direct mapping on Codacy\n", modifiedType)
continue
}
for _, location := range result.Locations {
codacyIssues = append(codacyIssues, map[string]interface{}{
"source": location.PhysicalLocation.ArtifactLocation.URI,
"line": location.PhysicalLocation.Region.StartLine,
"type": pattern.ID,
"message": result.Message.Text,
"level": pattern.Level,
"category": pattern.Category,
})
}
}
var results []map[string]interface{}
// Iterate through run.Artifacts and create entries in the results object
for _, artifact := range run.Artifacts {
if artifact.Location.URI != "" {
results = append(results, map[string]interface{}{
"filename": artifact.Location.URI,
"results": []map[string]interface{}{},
})
}
}
for _, obj := range codacyIssues {
source := obj["source"].(string)
issue := map[string]interface{}{
"patternId": map[string]string{
"value": obj["type"].(string),
},
"filename": source,
"message": map[string]string{
"text": obj["message"].(string),
},
"level": obj["level"].(string),
//"category": obj["category"].(string),
"location": map[string]interface{}{
"LineLocation": map[string]int{
"line": obj["line"].(int),
},
},
}
// Check if we already have an entry for this filename
found := false
for i, result := range results {
if result["filename"] == source {
// If we do, append this issue to its results
results[i]["results"] = append(results[i]["results"].([]map[string]interface{}), map[string]interface{}{"Issue": issue})
found = true
break
}
}
// If we don't, create a new entry
if !found {
results = append(results, map[string]interface{}{
"filename": source,
"results": []map[string]interface{}{{"Issue": issue}},
})
}
}
payload := []map[string]interface{}{
{
"tool": toolName,
"issues": map[string]interface{}{
"Success": map[string]interface{}{
"results": results,
},
},
},
}
payloads = append(payloads, payload)
}
return payloads
}
func resultsFinalWithProjectToken(commitUUID string, projectToken string) {
url := fmt.Sprintf("https://api.codacy.com/2.0/commit/%s/resultsFinal", commitUUID)
req, _ := http.NewRequest("POST", url, nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("project-token", projectToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println("Response:", resp.Status)
fmt.Println("Response Body:", string(body))
}
func resultsFinalWithAPIToken(commitUUID string, apiToken string, provider string, owner string, repository string) {
url := fmt.Sprintf("https://api.codacy.com/2.0/%s/%s/%s/commit/%s/resultsFinal", provider, owner, repository, commitUUID)
req, _ := http.NewRequest("POST", url, nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("api-token", apiToken)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
fmt.Println("Response:", resp.Status)
fmt.Println("Response Body:", string(body))
}
func getPatternByID(patterns []domain.PatternConfiguration, patternID string) *domain.SarifPatternConfiguration {
var sarifPatterns []domain.SarifPatternConfiguration
for _, p := range patterns {
sarifPatterns = append(sarifPatterns, domain.SarifPatternConfiguration{
UUID: p.PatternDefinition.Id,
ID: p.PatternDefinition.Id,
Category: p.PatternDefinition.Category,
Description: p.PatternDefinition.Description,
Level: p.PatternDefinition.Level,
})
}
for _, p := range sarifPatterns {
if strings.EqualFold(p.ID, patternID) {
return &p
}
}
return nil
}
func getMajorVersion(version string) int {
parts := strings.Split(version, ".")
if len(parts) > 0 {
major, err := strconv.Atoi(parts[0])
if err != nil {
fmt.Println("Error converting major version to integer:", err)
return -1
}
return major
}
return -1
}
func sendResultsWithProjectToken(payload []map[string]interface{}, commitUUID string, projectToken string) {
payloadBytes, err := json.Marshal(payload)
if err != nil {
fmt.Printf("Error marshaling payload: %v\n", err)
panic("panic")
}
url := fmt.Sprintf("https://api.codacy.com/2.0/commit/%s/issuesRemoteResults", commitUUID)
fmt.Printf("Sending results to URL: %s\n", url)
fmt.Println("Payload:", string(payloadBytes))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
os.Exit(1)
}
req.Header.Set("content-type", "application/json")
req.Header.Set("project-token", projectToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("Error sending results: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Error sending results, status code: %d\n", resp.StatusCode)
os.Exit(1)
}
}
func sendResultsWithAPIToken(payload []map[string]interface{}, commitUUID string, apiToken string, provider string, owner string, repository string) {
payloadBytes, err := json.Marshal(payload)
if err != nil {
fmt.Printf("Error marshaling payload: %v\n", err)
panic("panic")
}
url := fmt.Sprintf("https://api.codacy.com/2.0/%s/%s/%s/commit/%s/issuesRemoteResults", provider, owner, repository, commitUUID)
fmt.Printf("Sending results to URL: %s\n", url)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payloadBytes))
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
os.Exit(1)
}
req.Header.Set("content-type", "application/json")
req.Header.Set("api-token", apiToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("Error sending results: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
fmt.Printf("Error sending results, status code: %d\n", resp.StatusCode)
os.Exit(1)
} else {
fmt.Println("Results sent successfully")
}
}