-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (153 loc) · 5.32 KB
/
main.go
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
// Check if there are at least two arguments
if len(os.Args) < 3 {
log.Fatal("Usage: go run main.go <baseline_file.json> <pr_file.json>")
}
// Read the baseline JSON file
baselineFile := filepath.Clean(os.Args[1])
baselineJSON, err := ioutil.ReadFile(baselineFile)
if err != nil {
log.Fatalf("Failed to read the baseline JSON file: %v", err)
}
// Read the PR JSON file
prFile := filepath.Clean(os.Args[2])
prJSON, err := ioutil.ReadFile(prFile)
if err != nil {
log.Fatalf("Failed to read the PR JSON file: %v", err)
}
// Parse the Baseline JSON scan
var baselineData map[string]interface{}
err = json.Unmarshal(baselineJSON, &baselineData)
if err != nil {
log.Fatalf("Failed to parse the Baseline JSON scan: %v", err)
}
// Parse the PR JSON scan
var prData map[string]interface{}
err = json.Unmarshal(prJSON, &prData)
if err != nil {
log.Fatalf("Failed to parse the PR JSON scan: %v", err)
}
fmt.Printf("\n")
fmt.Printf("Running Snyk Code PR Diff")
fmt.Printf("\n")
// Extract the "results" array from the Baseline scan
baselineResults, ok := extractResults(baselineData)
if !ok {
log.Fatal("Failed to extract 'results' from the Baseline scan")
}
// Extract the "results" array from the PR scan
prResults, ok := extractResults(prData)
if !ok {
log.Fatal("Failed to extract 'results' from PR scan")
}
// Find the indices of new fingerprints from the PR results
newIndices := findNewFingerprintIndices(baselineResults, prResults)
// Extract the new issues objects from the PR results
newIssues := extractNewIssues(prResults, newIndices)
// Count the number of new issues found from the PR results
issueCount := len(newIssues)
// Output the new issues from the PR results
for _, result := range newIssues {
level, message, uri, startLine := extractIssueData(result)
level = strings.Replace(level, "note", "Low", 1)
level = strings.Replace(level, "warning", "Medium", 1)
level = strings.Replace(level, "error", "High", 1)
fmt.Printf("✗ Severity: [%s]\n", level)
fmt.Printf("Path: %s\n", uri)
fmt.Printf("Start Line: %d\n", startLine)
fmt.Printf("Message: %s\n", message)
fmt.Printf("\n")
}
// Output the count new issues found from the PR results
if issueCount > 0 {
fmt.Printf("\n")
fmt.Printf("Total issues found: %d\n", issueCount)
// Replace the "results" array in the PR scan with only the new issues found
prData["runs"].([]interface{})[0].(map[string]interface{})["results"] = newIssues
// Convert the new PR data to JSON
updatedPRScan, err := json.Marshal(prData)
if err != nil {
log.Fatalf("Failed to convert updated data to JSON: %v", err)
}
// Write the updated PR diff scan to a file
err = ioutil.WriteFile("snyk_code_pr_diff_scan.json", updatedPRScan, 0644)
if err != nil {
log.Fatalf("Failed to write updated data to file: %v", err)
}
fmt.Printf("\n")
fmt.Println("Results saved in usnyk_code_pr_diff_scan.json")
os.Exit(1)
}
fmt.Printf("\n")
fmt.Println("No issues found!")
}
// Extract the "results" array from the JSON data
func extractResults(data map[string]interface{}) ([]interface{}, bool) {
runs, ok := data["runs"].([]interface{})
if !ok {
return nil, false
}
if len(runs) > 0 {
results, ok := runs[0].(map[string]interface{})["results"].([]interface{})
if !ok {
return nil, false
}
return results, true
}
return nil, false
}
// Find the indices of the new fingerprints in the PR results array
func findNewFingerprintIndices(baselineResults, prResults []interface{}) []int {
var newIndices []int
for i, prResult := range prResults {
prObject := prResult.(map[string]interface{})
if prFingerprints, ok := prObject["fingerprints"].(map[string]interface{}); ok {
matchFound := false
for _, baselineResult := range baselineResults {
baselineObject := baselineResult.(map[string]interface{})
if baselineFingerprints, ok := baselineObject["fingerprints"].(map[string]interface{}); ok {
// Ignore the "identity" key
delete(baselineFingerprints, "identity")
delete(prFingerprints, "identity")
match := fmt.Sprint(prFingerprints) == fmt.Sprint(baselineFingerprints)
if match {
matchFound = true
break
}
}
}
if !matchFound {
newIndices = append(newIndices, i)
}
}
}
return newIndices
}
// Extract new issues objects from the PR "results" array
func extractNewIssues(results []interface{}, indices []int) []interface{} {
var newIssues []interface{}
for _, idx := range indices {
newIssues = append(newIssues, results[idx])
}
return newIssues
}
// Extract new issue data from the results to output to the console
func extractIssueData(result interface{}) (string, string, string, int) {
resultObj := result.(map[string]interface{})
level := resultObj["level"].(string)
message := resultObj["message"].(map[string]interface{})["text"].(string)
locations := resultObj["locations"].([]interface{})
uri := locations[0].(map[string]interface{})["physicalLocation"].(map[string]interface{})["artifactLocation"].(map[string]interface{})["uri"].(string)
startLine := locations[0].(map[string]interface{})["physicalLocation"].(map[string]interface{})["region"].(map[string]interface{})["startLine"].(float64)
return level, message, uri, int(startLine)
}