-
Notifications
You must be signed in to change notification settings - Fork 1
/
eslintHandling.go
126 lines (107 loc) · 3.69 KB
/
eslintHandling.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
)
//Mapping for eslint Severity
func getSeverityString(severityInteger int) string {
if severityInteger == 0 {
return "LOW"
} else if severityInteger == 1 {
return "MEDIUM"
} else if severityInteger == 2 {
return "HIGH"
} else {
fmt.Println("invalid severityInteger")
return "LOW"
}
}
func getStatsFromIssues(issues []Issues) ([]Annotation, int, int) {
var totalErrorCount = 0
var totalWarningCount = 0
var annotations []Annotation
// The filePath is absolute, but Bitbucket Server requires it to be relative to the git repository
basePath := os.Getenv("BITRISE_SOURCE_DIR") + "/"
for i := 0; i < len(issues); i++ {
totalErrorCount += issues[i].ErrorCount
totalWarningCount += issues[i].WarningCount
for x := 0; x < len(issues[i].Messages); x++ {
var anno = issues[i].Messages[x]
var path string
path = strings.ReplaceAll(issues[i].FilePath, basePath, "")
annotations = append(annotations, Annotation{Path: path, Line: anno.Line, Message: anno.Message, Severity: getSeverityString(anno.Severity)})
if anno.Severity == 2 {fmt.Println("ERRORS :", issues[i].Messages[x])}
}
}
return annotations, totalErrorCount, totalWarningCount
}
func createReport(totalErrorCount int, totalWarningCount int) Report {
var report Report
report.Title = "ESLint Report"
report.Vendor = "WookieFPV"
report.LogoURL = "https://upload.wikimedia.org/wikipedia/en/e/e3/ESLint_logo.svg"
report.Data = append(report.Data, EslintReportData{Title: "warnings", Value: totalWarningCount})
report.Data = append(report.Data, EslintReportData{Title: "errors", Value: totalErrorCount})
report.Link = "https://www.youtube.com/watch?v=oHg5SJYRHA0"
report.Details = "Hello World, here could be alot of text"
if totalErrorCount == 0 {
report.Result = "PASS"
} else {
report.Result = "FAIL"
}
return report
}
func reportEslintErrors() error {
var fileNames []string
// get all fileNames and store in Array for iterating
files, err := ioutil.ReadDir(os.Getenv("BITRISE_SOURCE_DIR") + "/lint-results")
if err != nil {
fmt.Println(err)
}
for _, f := range files {
fileNames = append(fileNames,f.Name())
}
fmt.Println(fileNames)
fmt.Println(len(fileNames))
var combinedIssues []Issues
for i := 0; i< len(fileNames); i++ {
lintFile := os.Getenv("BITRISE_SOURCE_DIR") + "/lint-results/" + fileNames[i]
jsonFile, err := os.Open(lintFile)
if err != nil {
fmt.Println("os.Open(lintFile) failed", err, lintFile)
return err
}
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var issues []Issues
json.Unmarshal(byteValue, &issues)
fmt.Println(fileNames[i],"has",len(issues), "issues")
combinedIssues = append(combinedIssues, issues...)
}
fmt.Println(len(combinedIssues), "combined issues found")
annotations, totalErrorCount, totalWarningCount := getStatsFromIssues(combinedIssues)
// COMPUTED VALUES
var token string = "Bearer " + os.Getenv("BITBUCKET_SERVER_TOKEN")
var url string = os.Getenv("BITBUCKET_SERVER_URL") + "rest/insights/1.0/projects/" + os.Getenv("PROJECT_ID") + "/repos/" + os.Getenv("BITRISEIO_GIT_REPOSITORY_SLUG") + "/commits/" + os.Getenv("BITRISE_GIT_COMMIT") + "/reports/" + os.Getenv("REPORT_NAME")
var annotationsURL string = url + "/annotations"
report := createReport(totalErrorCount, totalWarningCount)
fmt.Println("token", token)
fmt.Println("url", url)
err = putReport(url, token, report)
if err != nil {
return err
}
err = deleteAnnotations(annotationsURL, token)
if err != nil {
return err
}
err = postAnnotations(annotationsURL, token, annotations)
if err != nil {
return err
}
return nil
}