-
Notifications
You must be signed in to change notification settings - Fork 14
/
steps.go
182 lines (157 loc) · 4.42 KB
/
steps.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
package validate
import (
"fmt"
"strings"
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/ast"
"github.com/CircleCI-Public/circleci-yaml-language-server/pkg/utils"
"go.lsp.dev/protocol"
)
var WHEN_KEYWORDS = []string{
"on_success",
"always",
"on_fail",
}
func (val Validate) validateSteps(steps []ast.Step, name string, jobOrCommandParameters map[string]ast.Parameter) error {
for _, step := range steps {
switch step := step.(type) {
case ast.Run:
val.validateRunCommand(step, jobOrCommandParameters)
case ast.NamedStep:
val.validateNamedStep(step, jobOrCommandParameters)
case ast.Steps:
val.validateStepSteps(step, name)
}
}
return nil
}
func (val Validate) validateRunCommand(step ast.Run, jobOrCommandParameters map[string]ast.Parameter) {
if step.IsDeployStep {
val.addDiagnostic(protocol.Diagnostic{
Range: step.Range,
Message: "The `deploy` step is deprecated. Please use the `run` job instead.",
Severity: protocol.DiagnosticSeverityWarning,
Tags: []protocol.DiagnosticTag{
protocol.DiagnosticTagDeprecated,
},
})
}
var value string
// If the when field is a parameter, such as:
// when: << parameters.my_param >>
if step.When == "" && step.WhenRange.Start.Line == 0 {
return
}
if utils.CheckIfOnlyParamUsed(step.When) {
paramName, isPipelineParam := utils.GetParamNameUsedAtPos(val.Doc.Content, step.WhenRange.End)
var param ast.Parameter
var ok bool
if isPipelineParam {
param, ok = val.Doc.PipelineParameters[paramName]
} else {
param, ok = jobOrCommandParameters[paramName]
}
if !ok {
return
}
if param.IsOptional() {
switch param := param.(type) {
case ast.StringParameter:
value = param.Default
default:
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
step.WhenRange,
fmt.Sprintf("Parameter %s is not a string type parameter, and therefore cannot be used inside the `when` field", paramName),
))
return
}
}
} else {
value = step.When
}
if utils.FindInArray(WHEN_KEYWORDS, value) < 0 {
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
step.WhenRange,
fmt.Sprintf("Invalid when condition: expected `%s`; got `%s`", strings.Join(WHEN_KEYWORDS, "`, `"), value)))
}
}
func (val Validate) validateNamedStep(step ast.NamedStep, usableParams map[string]ast.Parameter) {
commandExists := val.Doc.DoesJobExist(step.Name) ||
val.Doc.DoesCommandExist(step.Name) ||
val.Doc.IsBuiltIn(step.Name) ||
val.Doc.IsOrbCommand(step.Name, val.Cache) ||
val.Doc.IsAlias(step.Name)
if val.Doc.IsFromUnfetchableOrb(step.Name) {
return
}
if !commandExists {
val.addDiagnostic(utils.CreateErrorDiagnosticFromRange(
step.Range,
fmt.Sprintf("Cannot find declaration for step %s", step.Name)))
}
if !val.Doc.IsBuiltIn(step.Name) {
targetEntityDefinedParams := val.Doc.GetDefinedParams(step.Name, val.Cache)
val.validateParametersValue(
step.Parameters,
step.Name,
step.Range,
targetEntityDefinedParams,
usableParams,
)
}
if step.Name == "store_test_results" {
val.addDiagnostic(
protocol.Diagnostic{
Message: "Path must be specified for `store_test_results` step",
Range: step.Range,
Severity: protocol.DiagnosticSeverityError,
})
}
}
func (val Validate) validateStepSteps(step ast.Steps, name string) {
if !val.Doc.DoesCommandExist(name) {
return
}
command := val.Doc.Commands[name]
parameter, ok := command.Parameters[step.Name]
if !ok {
return
}
parameterType := parameter.GetType()
if parameterType != "steps" {
val.addDiagnostic(protocol.Diagnostic{
Severity: protocol.DiagnosticSeverityError,
Range: step.Range,
Message: "Parameter type is not steps",
Source: "cci-language-server",
})
}
}
func (val Validate) checkIfStepsContainStep(steps []ast.Step, stepName string) bool {
for _, step := range steps {
if step.GetName() == stepName {
return true
}
}
return false
}
func (val Validate) checkIfStepsContainOrb(steps []ast.Step, orbName string) bool {
for _, step := range steps {
isOrb := val.Doc.IsOrbReference(step.GetName())
if isOrb && strings.Split(step.GetName(), "/")[0] == orbName {
return true
}
}
return false
}
func (val Validate) checkIfJobUseOrb(job ast.Job, orbName string) bool {
if val.checkIfStepsContainOrb(job.Steps, orbName) {
return true
}
if job.Executor != "" {
split := strings.Split(job.Executor, "/")
if split[0] == orbName {
return true
}
}
return false
}