This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
14-ensure-code-coverage.go
175 lines (144 loc) · 3.93 KB
/
14-ensure-code-coverage.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
package defaultRules
import (
"encoding/json"
githubConnector "github.com/allero-io/allero/pkg/connectors/github"
gitlabConnector "github.com/allero-io/allero/pkg/connectors/gitlab"
)
func EnsureCodeCoverageChecker(githubData map[string]*githubConnector.GithubOwner, gitlabData map[string]*gitlabConnector.GitlabGroup) ([]*SchemaError, error) {
schemaErrors := make([]*SchemaError, 0)
var err error
if githubData != nil {
schemaErrors, err = githubErrorsRule14(githubData)
if err != nil {
return nil, err
}
}
if gitlabData != nil {
schemaErrors, err = gitlabErrorsRule14(gitlabData)
if err != nil {
return nil, err
}
}
return schemaErrors, nil
}
func githubErrorsRule14(githubData map[string]*githubConnector.GithubOwner) ([]*SchemaError, error) {
schemaErrors := make([]*SchemaError, 0)
usesRegexExpressions := []string{
".*codecov/codecov-action@.*",
}
runRegexExpressions := []string{
".*codecov -t.*",
}
for _, owner := range githubData {
for _, repo := range owner.Repositories {
foundCodeCoverageChecker := false
for _, workflow := range repo.GithubActionsWorkflows {
content := workflow.Content
contentByteArr, err := json.Marshal(content)
if err != nil {
return nil, err
}
var workflowObj GithubWorkflow
err = json.Unmarshal(contentByteArr, &workflowObj)
if err != nil {
return nil, err
}
for _, job := range workflowObj.Jobs {
for _, step := range job.Steps {
for _, regexExpression := range usesRegexExpressions {
if matchRegex(regexExpression, step.Uses) {
foundCodeCoverageChecker = true
break
}
}
for _, regexExpression := range runRegexExpressions {
if matchRegex(regexExpression, step.Run) {
foundCodeCoverageChecker = true
break
}
}
if foundCodeCoverageChecker {
break
}
}
if foundCodeCoverageChecker {
break
}
}
if foundCodeCoverageChecker {
break
}
}
if !foundCodeCoverageChecker {
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 1,
RepositryName: repo.Name,
CiCdPlatform: "",
OwnerName: owner.Name,
ScmPlatform: "github",
})
}
}
}
return schemaErrors, nil
}
func gitlabErrorsRule14(gitlabData map[string]*gitlabConnector.GitlabGroup) ([]*SchemaError, error) {
schemaErrors := make([]*SchemaError, 0)
for _, group := range gitlabData {
for _, project := range group.Projects {
foundCodeCoverageChecker, err := findCodeCoverageCheckerRule14(project)
if err != nil {
return nil, err
}
if !foundCodeCoverageChecker {
schemaErrors = append(schemaErrors, &SchemaError{
ErrorLevel: 2,
RepositryName: project.Name,
CiCdPlatform: "",
OwnerName: group.Name,
ScmPlatform: "gitlab",
})
}
}
}
return schemaErrors, nil
}
func findCodeCoverageCheckerRule14(project *gitlabConnector.GitlabProject) (bool, error) {
scriptRegexExpressions := []string{
".*codecov -t.*",
}
for _, pipeline := range project.GitlabCi {
for _, stage := range pipeline.Content {
stageBytes, err := json.Marshal(stage)
if err != nil {
return false, err
}
var stageWithSingleScript GitlabStageScript
var stageWithScripts GitlabStageScripts
err = json.Unmarshal(stageBytes, &stageWithSingleScript)
if err != nil {
err = json.Unmarshal(stageBytes, &stageWithScripts)
if err != nil {
continue
}
}
if stageWithSingleScript.Script != "" {
for _, regexExpression := range scriptRegexExpressions {
if matchRegex(regexExpression, stageWithSingleScript.Script) {
return true, nil
}
}
}
if stageWithScripts.Scripts != nil {
for _, script := range stageWithScripts.Scripts {
for _, regexExpression := range scriptRegexExpressions {
if matchRegex(regexExpression, script) {
return true, nil
}
}
}
}
}
}
return false, nil
}