-
Notifications
You must be signed in to change notification settings - Fork 0
/
results.go
104 lines (83 loc) · 2.17 KB
/
results.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
package plugin
import (
"fmt"
"path/filepath"
"strings"
"github.com/cocov-ci/go-plugin-kit/cocov"
)
// Those constants represents official semgrep results categories.
const (
bestPractice = "best-practice"
correctness = "correctness"
maintainability = "maintainability"
performance = "performance"
portability = "portability"
security = "security"
)
var cocovIssues = map[string]cocov.IssueKind{
correctness: cocov.IssueKindQuality,
portability: cocov.IssueKindQuality,
maintainability: cocov.IssueKindQuality,
bestPractice: cocov.IssueKindConvention,
performance: cocov.IssueKindPerformance,
security: cocov.IssueKindSecurity,
}
type opResult struct {
results *results
error error
}
type results struct {
Results []*result `json:"results"`
}
func (r *results) renderResults(rootPath string) *results {
if len(r.Results) > 0 {
for _, res := range r.Results {
v, ok := cocovIssues[res.Extra.Metadata.Category]
res.valid = ok
if !ok {
continue
}
res.kind = v
refs := res.Extra.Metadata.References
if len(refs) > 0 {
res.Extra.Message = renderMessage(res.Extra.Message, refs)
}
if filepath.Dir(res.Path) == rootPath {
res.Path = filepath.Base(res.Path)
continue
}
res.Path = strings.TrimPrefix(res.Path, rootPath)[1:]
}
}
return r
}
type result struct {
Path string `json:"path"`
Extra extra `json:"extra"`
Start start `json:"start"`
kind cocov.IssueKind
valid bool
}
type extra struct {
Message string `json:"message"`
Metadata metadata `json:"metadata"`
}
type metadata struct {
Category string `json:"category"`
References []string `json:"references"`
}
type start struct {
Line int `json:"line"`
}
func (r result) message() string { return r.Extra.Message }
func (r result) startLine() uint { return uint(r.Start.Line) }
func (r result) hashID(commitSha string) string {
input := fmt.Sprintf(
"%s-%s-%s-%s",
r.kind, fmt.Sprintf("%d", r.Start), r.Path, commitSha,
)
return cocov.SHA1([]byte(input))
}
func renderMessage(message string, references []string) string {
return fmt.Sprintf("%s\nreferences:\n%s", message, strings.Join(references, "\n"))
}