/
action_eslint.go
241 lines (227 loc) · 7.19 KB
/
action_eslint.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
package action
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/hamster-shared/aline-engine/consts"
"github.com/hamster-shared/aline-engine/logger"
"github.com/hamster-shared/aline-engine/model"
"github.com/hamster-shared/aline-engine/output"
"github.com/tidwall/gjson"
"os"
"os/exec"
path2 "path"
"strconv"
"strings"
)
// EslintAction 前端代码检查
type EslintAction struct {
path string
ctx context.Context
output *output.Output
}
func NewEslintAction(step model.Step, ctx context.Context, output *output.Output) *EslintAction {
return &EslintAction{
path: step.With["path"],
ctx: ctx,
output: output,
}
}
func (a *EslintAction) Pre() error {
eslintFile := ".eslintrc.*"
packageFile := "package.json"
stack := a.ctx.Value(STACK).(map[string]interface{})
workdir, ok := stack["workdir"].(string)
if !ok {
return errors.New("workdir is empty")
}
files, _ := os.ReadDir(workdir)
eslintFileFlag := false
packageFileFlag := false
for _, file := range files {
matched, err := path2.Match(eslintFile, file.Name())
if err != nil {
return errors.New(".eslintrc.* not exist")
}
if matched {
eslintFileFlag = matched
}
if file.Name() == packageFile {
packageFileFlag = true
}
}
if !eslintFileFlag {
return errors.New(".eslintrc.* not exist")
}
if !packageFileFlag {
return errors.New("package.json not exist")
}
packagePath := path2.Join(workdir, packageFile)
readFile, err := os.ReadFile(packagePath)
if err != nil {
return err
}
if !gjson.GetBytes(readFile, "scripts.lint").Exists() {
return errors.New("lint cli not exist")
}
if !strings.Contains(gjson.GetBytes(readFile, "scripts.lint").String(), "json") {
return errors.New("please add '- f json' in 'lint' cli")
}
return nil
}
func (a *EslintAction) Hook() (*model.ActionResult, error) {
// a.output.NewStep("eslint-check")
stack := a.ctx.Value(STACK).(map[string]interface{})
workdir, ok := stack["workdir"].(string)
if !ok {
return nil, errors.New("workdir is empty")
}
jobName, ok := stack["name"].(string)
if !ok {
return nil, errors.New("get job name error")
}
jobId, ok := stack["id"].(string)
if !ok {
return nil, errors.New("get job id error")
}
userHomeDir, err := os.UserHomeDir()
if err != nil {
logger.Errorf("Failed to get home directory, the file will be saved to the current directory, err is %s", err.Error())
userHomeDir = "."
}
destDir := path2.Join(userHomeDir, consts.ArtifactoryDir, jobName, consts.CheckName, jobId, consts.EslintCheckOutputDir)
_, err = os.Stat(destDir)
if os.IsNotExist(err) {
err := os.MkdirAll(destDir, os.ModePerm)
if err != nil {
return nil, err
}
}
dest := path2.Join(destDir, consts.EslintCheckOutputDir+consts.SuffixType)
out, err := a.ExecuteCommand(strings.Fields("npm run lint"), workdir)
if out == "" && err != nil {
return nil, err
}
firstIndex := strings.Index(out, "\n")
afterTheFirstLine := out[firstIndex+1:]
secondIndex := strings.Index(afterTheFirstLine, "\n")
afterTheSecondLine := afterTheFirstLine[secondIndex+1:]
thirdIndex := strings.Index(afterTheSecondLine, "\n")
afterTheThirdLine := afterTheSecondLine[thirdIndex+1:]
fourIndex := strings.Index(afterTheThirdLine, "\n")
create, err := os.Create(dest)
if err != nil {
return nil, err
}
_, err = create.WriteString(afterTheThirdLine[fourIndex+1:])
if err != nil {
return nil, err
}
create.Close()
a.path = destDir
id, err := strconv.Atoi(jobId)
if err != nil {
return nil, err
}
actionResult := model.ActionResult{
Artifactorys: nil,
Reports: []model.Report{
{
Id: id,
Url: "",
Type: 2,
},
},
}
return &actionResult, err
}
func (a *EslintAction) Post() error {
file, err := os.ReadFile(path2.Join(a.path, consts.EslintCheckOutputDir+consts.SuffixType))
if err != nil {
return errors.New("check result path is err")
}
var eslintCheckResultList []EslintCheckResult
err = json.Unmarshal(file, &eslintCheckResultList)
if err != nil {
return errors.New("get check result is fail")
}
if len(eslintCheckResultList) < 1 {
return nil
}
var checkResultDetailsList []model.ContractCheckResultDetails[[]model.EslintCheckReportDetails]
var total int
for _, eslintCheckResult := range eslintCheckResultList {
if len(eslintCheckResult.Messages) < 1 {
continue
}
var checkResultDetails model.ContractCheckResultDetails[[]model.EslintCheckReportDetails]
checkResultDetails.Name = eslintCheckResult.FilePath
checkResultDetails.Issue = len(eslintCheckResult.Messages)
var eslintCheckReportDetailsList []model.EslintCheckReportDetails
for _, message := range eslintCheckResult.Messages {
var eslintCheckReportDetails model.EslintCheckReportDetails
eslintCheckReportDetails.Tool = message.RuleId
eslintCheckReportDetails.Note = message.Message
eslintCheckReportDetails.Line = strconv.Itoa(message.Line)
eslintCheckReportDetails.Column = strconv.Itoa(message.Column)
if message.Severity == 1 {
eslintCheckReportDetails.Level = "error"
} else {
eslintCheckReportDetails.Level = "waring"
}
eslintCheckReportDetailsList = append(eslintCheckReportDetailsList, eslintCheckReportDetails)
}
checkResultDetails.Message = eslintCheckReportDetailsList
total = total + checkResultDetails.Issue
checkResultDetailsList = append(checkResultDetailsList, checkResultDetails)
}
checkResult := model.NewContractCheckResult(consts.FrontEndCheckReport.Name, consts.CheckSuccess.Result, consts.FrontEndCheckReport.Tool, checkResultDetailsList, total)
create, err := os.Create(path2.Join(a.path, consts.CheckResult))
if err != nil {
return err
}
marshal, err := json.Marshal(checkResult)
if err != nil {
return err
}
_, err = create.WriteString(string(marshal))
if err != nil {
return err
}
create.Close()
return nil
}
func (a *EslintAction) ExecuteCommand(commands []string, workdir string) (string, error) {
c := exec.CommandContext(a.ctx, commands[0], commands[1:]...) // mac linux
c.Dir = workdir
logger.Debugf("execute mythril check command: %s", strings.Join(commands, " "))
a.output.WriteCommandLine(strings.Join(commands, " "))
out, err := c.CombinedOutput()
fmt.Println(string(out))
a.output.WriteCommandLine(string(out))
if err != nil {
a.output.WriteLine(err.Error())
}
return string(out), err
}
type EslintCheckResult struct {
FilePath string `json:"filePath"`
Messages []EslintCheckResultMessage `json:"messages"`
SuppressedMessages []interface{} `json:"suppressedMessages"`
ErrorCount int `json:"errorCount"`
FatalErrorCount int `json:"fatalErrorCount"`
WarningCount int `json:"warningCount"`
FixableErrorCount int `json:"fixableErrorCount"`
FixableWarningCount int `json:"fixableWarningCount"`
UsedDeprecatedRules []interface{} `json:"usedDeprecatedRules"`
}
type EslintCheckResultMessage struct {
RuleId string `json:"ruleId"`
Severity int `json:"severity"`
Message string `json:"message"`
Line int `json:"line"`
Column int `json:"column"`
NodeType interface{} `json:"nodeType"`
MessageId string `json:"messageId"`
}