/
parser.go
295 lines (277 loc) · 7.3 KB
/
parser.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
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
package report
import (
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"strings"
"github.com/konveyor/analyzer-lsp/output/v1/konveyor"
"go.lsp.dev/uri"
"gopkg.in/yaml.v2"
)
// unmarshalReportFile
func unmarshalReportFile(path string, v any) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
if strings.HasSuffix(path, ".json") {
err = json.Unmarshal(data, v)
if err != nil {
return err
}
} else {
err = yaml.Unmarshal(data, v)
if err != nil {
return err
}
}
return nil
}
// ParseFileContentJson parses json file at path as FileContent
func ParseFileContentJson(path string) (FileContent, error) {
file := FileContent{}
err := unmarshalReportFile(path, &file)
if err != nil {
return file, err
}
return file, nil
}
// ParseFilesJson parses ./api/files.json at basePath
func ParseFilesJson(basePath string) ([]File, error) {
files := []File{}
err := unmarshalReportFile(
filepath.Join(basePath, "api", "files.json"), &files)
if err != nil {
return files, err
}
for idx := range files {
file := &files[idx]
contentPath := filepath.Join(basePath, "api", "files", fmt.Sprintf("%s.json", file.ID))
content, err := ParseFileContentJson(contentPath)
if err != nil {
log.Printf("failed parsing file %s\n", contentPath)
}
file.Content = content
}
return files, nil
}
// ParseIssuesJson parses ./api/issues.json at basePath
func ParseIssuesJson(basePath string) (Issues, error) {
path := filepath.Join(basePath, "api", "issues.json")
// issues.json file has reports for multiple apps
reports := []struct {
ApplicationId string `json:"applicationId,omitempty"`
// map of issues Category -> []Issue
Issues map[string][]Issue `json:"issues,omitempty"`
}{}
err := unmarshalReportFile(path, &reports)
if err != nil {
return nil, err
}
filesMap, err := loadFiles(basePath)
if err != nil {
return nil, err
}
issues := make(Issues)
for _, report := range reports {
if report.ApplicationId == "" {
continue
}
for category, issueList := range report.Issues {
for idx := range issueList {
issue := &issueList[idx]
issue.Category = category
for _, affectedFile := range issue.AffectedFiles {
for jdx := range affectedFile.FileRefs {
fileRef := &affectedFile.FileRefs[jdx]
if val, ok := filesMap[fileRef.FileID]; ok {
fileRef.File = val
fileRef.File.Hints = filterHintsByRuleIdAndMessage(
issue.RuleID, affectedFile.Description, fileRef.File.Hints)
}
}
}
if existingIssue, ok := issues[issue.RuleID]; !ok {
issues[issue.RuleID] = issue
} else {
existingIssue.AffectedFiles = append(existingIssue.AffectedFiles, issue.AffectedFiles...)
existingIssue.TotalIncidents += issue.TotalIncidents
existingIssue.TotalStoryPoints += issue.TotalStoryPoints
}
}
}
}
return issues, nil
}
func ParseViolations(path string) (konveyor.RuleSet, error) {
rs := konveyor.RuleSet{
Name: "konveyor-analyzer",
Tags: []string{},
Violations: map[string]konveyor.Violation{},
}
rulesets := []konveyor.RuleSet{}
err := unmarshalReportFile(path, &rulesets)
if err != nil {
return rs, err
}
for _, ruleset := range rulesets {
dedupTags := map[string]bool{}
for _, tag := range ruleset.Tags {
if _, ok := dedupTags[tag]; !ok {
rs.Tags = append(rs.Tags, tag)
dedupTags[tag] = true
}
}
for ruleId, violation := range ruleset.Violations {
rs.Violations[ruleId] = violation
}
}
return rs, nil
}
func (i Issues) AsRuleset() konveyor.RuleSet {
rs := konveyor.RuleSet{
Tags: []string{},
}
violations := map[string]konveyor.Violation{}
dedupTags := map[string]bool{}
for ruleId, issue := range i {
ruleId = mappedRuleId(ruleId)
violation, tags := issue.AsViolationAndTags()
// only create incidents for non tagging rules
if len(violation.Incidents) != 0 {
violations[ruleId] = violation
} else {
if _, ok := dedupTags[issue.Name]; !ok {
rs.Tags = append(rs.Tags, issue.Name)
}
}
for _, tag := range tags {
if _, ok := dedupTags[tag]; !ok {
rs.Tags = append(rs.Tags, tag)
dedupTags[tag] = true
}
}
}
rs.Violations = violations
return rs
}
func (i Issue) AsViolationAndTags() (konveyor.Violation, []string) {
tags := []string{}
violation := konveyor.Violation{
Description: "",
Category: toAnalyzerCategory(i.Category),
Effort: &i.Effort.Points,
Incidents: []konveyor.Incident{},
Links: []konveyor.Link{},
}
for _, affFile := range i.AffectedFiles {
incidents, hintTags := affFile.AsIncidentsAndTags()
violation.Incidents = append(violation.Incidents,
incidents...)
tags = append(tags, hintTags...)
dedupLinks := map[string]bool{}
for _, ref := range affFile.FileRefs {
for _, hint := range ref.File.Hints {
for _, link := range hint.Links {
if _, ok := dedupLinks[link.Href]; !ok {
violation.Links = append(violation.Links,
konveyor.Link{URL: link.Href, Title: link.Title})
dedupLinks[link.Href] = true
}
}
}
}
}
return violation, tags
}
func (a AffectedFile) AsIncidentsAndTags() ([]konveyor.Incident, []string) {
incidents := []konveyor.Incident{}
tags := []string{}
dedupTags := map[string]bool{}
for _, fileRef := range a.FileRefs {
// only find matching incidents
incidents = append(incidents,
filterIncidentsForMessage(a.Description, fileRef.AsIncidents())...)
// Get tags
for _, windupTag := range fileRef.File.Tags {
tag := windupTag.Name
if _, ok := dedupTags[tag]; !ok {
tags = append(tags, tag)
dedupTags[tag] = true
}
}
}
return incidents, tags
}
func (f FileRef) AsIncidents() []konveyor.Incident {
// dedup line numbers
dedupLineNo := map[int]bool{}
incidents := []konveyor.Incident{}
for _, hint := range f.File.Hints {
if _, ok := dedupLineNo[hint.Line]; !ok {
incidents = append(incidents, konveyor.Incident{
URI: uri.URI(f.File.FullPath),
Message: hint.Content,
CodeSnip: f.File.Content.Content,
Variables: map[string]interface{}{
"lineNumber": hint.Line,
},
})
dedupLineNo[hint.Line] = true
}
}
return incidents
}
// returns FileId -> File
func loadFiles(basePath string) (map[string]File, error) {
files, err := ParseFilesJson(basePath)
if err != nil {
return nil, err
}
filesMap := map[string]File{}
for _, file := range files {
filesMap[file.ID] = file
}
return filesMap, nil
}
func toAnalyzerCategory(windupCat string) *konveyor.Category {
switch windupCat {
case "mandatory", "cloud-mandatory":
return &konveyor.Mandatory
case "optional", "cloud-optional":
return &konveyor.Optional
case "potential":
return &konveyor.Potential
default:
return &konveyor.Potential
}
}
func filterIncidentsForMessage(msg string, incidents []konveyor.Incident) []konveyor.Incident {
filtered := []konveyor.Incident{}
for _, inc := range incidents {
if inc.Message == msg {
filtered = append(filtered, inc)
}
}
return filtered
}
func filterHintsByRuleIdAndMessage(ruleId string, message string, hints []Hint) []Hint {
filtered := []Hint{}
for _, hint := range hints {
if hint.RuleID != ruleId || hint.Content != message {
continue
}
filtered = append(filtered, hint)
}
return filtered
}
func mappedRuleId(orig string) string {
switch orig {
case "DiscoverHardcodedIPAddressRuleProvider":
return "hardcoded-ip-address"
default:
return orig
}
}