-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgas-report-filter.go
150 lines (129 loc) · 3.41 KB
/
gas-report-filter.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
package main
import (
"bufio"
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
)
type Whitelist struct {
Issues []Issue `json:"Issues"`
}
type Issue struct {
Details string `json:"details"`
File string `json:"file"`
Code string `json:"code"`
}
type XMLReport struct {
XMLName xml.Name `xml:"testsuites"`
Testsuites []Testsuite `xml:"testsuite"`
}
type Testsuite struct {
XMLName xml.Name `xml:"testsuite"`
Name string `xml:"name,attr"`
Tests int `xml:"tests,attr"`
Testcases []Testcase `xml:"testcase"`
}
type Testcase struct {
XMLName xml.Name `xml:"testcase"`
Name string `xml:"name,attr"`
Failure Failure `xml:"failure"`
}
type Failure struct {
XMLName xml.Name `xml:"failure"`
Message string `xml:"message,attr"`
Text string `xml:",innerxml"`
}
/* #nosec */
func errorHandler(err interface{}) {
if err != nil {
log.Fatal(err)
}
}
/* #nosec */
func parseXMLstdin() XMLReport {
xmlString := ""
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
xmlString += scanner.Text() + "\n"
}
var report XMLReport
err := xml.Unmarshal([]byte(xmlString), &report)
if err != nil {
// End execution if report from gas is malformed/has changed.
log.Fatal(err)
}
return report
}
/* #nosec */
func parseWhitelistFile(filename string) Whitelist {
var whitelist Whitelist
jsonFile, err := os.Open(filename)
defer jsonFile.Close()
if err != nil {
// If json file is not found, create empty object.
json.Unmarshal([]byte("{}"), &whitelist)
}
byteValue, err := ioutil.ReadAll(jsonFile)
errorHandler(err)
err = json.Unmarshal(byteValue, &whitelist)
if err != nil {
// If JSON is malformed, treat it as empty object.
json.Unmarshal([]byte("{}"), &whitelist)
}
return whitelist
}
/* #nosec */
func retrieveCode(output string) string {
return strings.Split(output, "> ")[1]
}
/* #nosec */
func isInWhitelist(whitelist Whitelist, file string, code string) bool {
for i := range whitelist.Issues {
if whitelist.Issues[i].File == file && whitelist.Issues[i].Code == code {
return true
}
}
return false
}
/* #nosec */
func removeWhitelistedIssues(whitelist Whitelist, testcases []Testcase) []Testcase {
whitelistedTestcases := []Testcase{}
for _, testcase := range testcases {
if !isInWhitelist(whitelist, testcase.Name, retrieveCode(testcase.Failure.Text)) {
whitelistedTestcases = append(whitelistedTestcases, testcase)
}
}
return whitelistedTestcases
}
/* #nosec */
func getWhitelistedTestsuites(whitelist Whitelist, testsuites []Testsuite) []Testsuite {
var whitelistedTestsuites []Testsuite
for _, testsuite := range testsuites {
testsuite.Testcases = removeWhitelistedIssues(whitelist, testsuite.Testcases)
if len(testsuite.Testcases) != 0 {
testsuite.Tests = len(testsuite.Testcases)
whitelistedTestsuites = append(whitelistedTestsuites, testsuite)
}
}
return whitelistedTestsuites
}
/* #nosec */
func outputXMLString(xmlReport XMLReport) {
xmlString, err := xml.MarshalIndent(xmlReport, "", "\t")
errorHandler(err)
fmt.Println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + string(xmlString))
}
/* #nosec */
func main() {
whitelistPtr := flag.String("whitelist", "whitelist.json", "Path of whitelist file.")
flag.Parse()
whitelist := parseWhitelistFile(*whitelistPtr)
report := parseXMLstdin()
report.Testsuites = getWhitelistedTestsuites(whitelist, report.Testsuites)
outputXMLString(report)
}