-
Notifications
You must be signed in to change notification settings - Fork 13
/
filters.go
293 lines (268 loc) · 8.53 KB
/
filters.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
package opslevel
import (
"fmt"
"slices"
"github.com/gosimple/slug"
)
type FilterId struct {
Id ID
Name string
}
type Filter struct {
FilterId
Connective ConnectiveEnum
HtmlUrl string
Predicates []FilterPredicate
}
type FilterPredicate struct {
Key PredicateKeyEnum `json:"key" yaml:"key" default:"repository_ids"`
KeyData string `json:"keyData,omitempty" yaml:"keyData,omitempty" default:"null"`
Type PredicateTypeEnum `json:"type" yaml:"type" default:"equals"`
Value string `json:"value,omitempty" yaml:"value,omitempty" default:"1"`
CaseSensitive *bool `json:"caseSensitive,omitempty" yaml:"caseSensitive,omitempty" default:"false"`
}
// Validate the FilterPredicate based on known expectations before sending to API
func (filterPredicate *FilterPredicate) Validate() error {
// validation common to Predicate and FilterPredicate types
basicPredicate := Predicate{Type: filterPredicate.Type, Value: filterPredicate.Value}
if err := basicPredicate.Validate(); err != nil {
return err
}
// validation specific to FilterPredicate types
if err := filterPredicate.validateKeyHasExpectedType(); err != nil {
return err
}
if err := filterPredicate.validateValue(); err != nil {
return err
}
if err := filterPredicate.validateCaseSensitivity(); err != nil {
return err
}
if err := filterPredicate.validateKeyData(); err != nil {
return err
}
return nil
}
func (filterPredicate *FilterPredicate) validateCaseSensitivity() error {
if filterPredicate.CaseSensitive == nil {
return nil
}
knownNotCaseSensitiveTypes := []PredicateTypeEnum{
PredicateTypeEnumDoesNotExist,
PredicateTypeEnumExists,
PredicateTypeEnumGreaterThanOrEqualTo,
PredicateTypeEnumLessThanOrEqualTo,
PredicateTypeEnumSatisfiesVersionConstraint,
PredicateTypeEnumMatchesRegex,
PredicateTypeEnumDoesNotMatchRegex,
PredicateTypeEnumBelongsTo,
PredicateTypeEnumMatches,
PredicateTypeEnumDoesNotMatch,
PredicateTypeEnumSatisfiesJqExpression,
}
if slices.Contains(knownNotCaseSensitiveTypes, filterPredicate.Type) && *filterPredicate.CaseSensitive {
return fmt.Errorf("FilterPredicate type '%s' cannot have CaseSensitive value set.", filterPredicate.Type)
}
return nil
}
func (filterPredicate *FilterPredicate) validateKeyData() error {
keyDataExpectedTypes := []PredicateKeyEnum{
PredicateKeyEnumProperties,
PredicateKeyEnumTags,
}
knownNoKeyDataSetTypes := []PredicateKeyEnum{
PredicateKeyEnumAliases,
PredicateKeyEnumDomainID,
PredicateKeyEnumFilterID,
PredicateKeyEnumFramework,
PredicateKeyEnumLifecycleIndex,
PredicateKeyEnumLanguage,
PredicateKeyEnumName,
PredicateKeyEnumOwnerID,
PredicateKeyEnumOwnerIDs,
PredicateKeyEnumProduct,
PredicateKeyEnumRepositoryIDs,
PredicateKeyEnumSystemID,
PredicateKeyEnumTierIndex,
}
if slices.Contains(keyDataExpectedTypes, filterPredicate.Key) && filterPredicate.KeyData == "" {
return fmt.Errorf("FilterPredicate key '%s' expects a value for 'key_data'", filterPredicate.Key)
}
if slices.Contains(knownNoKeyDataSetTypes, filterPredicate.Key) && filterPredicate.KeyData != "" {
return fmt.Errorf("FilterPredicate key '%s' cannot have a value set for 'key_data'", filterPredicate.Key)
}
return nil
}
func (filterPredicate *FilterPredicate) validateKeyHasExpectedType() error {
var expectedPredicateTypes []PredicateTypeEnum
containsTypes := []PredicateTypeEnum{
PredicateTypeEnumContains,
PredicateTypeEnumDoesNotContain,
}
equalsTypes := []PredicateTypeEnum{
PredicateTypeEnumDoesNotEqual,
PredicateTypeEnumEquals,
}
lessThanGreaterThanTypes := []PredicateTypeEnum{
PredicateTypeEnumGreaterThanOrEqualTo,
PredicateTypeEnumLessThanOrEqualTo,
}
filterMatchTypes := []PredicateTypeEnum{
PredicateTypeEnumDoesNotMatch,
PredicateTypeEnumMatches,
}
regexMatchesTypes := []PredicateTypeEnum{
PredicateTypeEnumDoesNotMatchRegex,
PredicateTypeEnumMatchesRegex,
}
startsOrEndsWithTypes := []PredicateTypeEnum{
PredicateTypeEnumEndsWith,
PredicateTypeEnumStartsWith,
}
switch filterPredicate.Key {
case PredicateKeyEnumAliases, PredicateKeyEnumFramework, PredicateKeyEnumLanguage, PredicateKeyEnumName, PredicateKeyEnumProduct:
expectedPredicateTypes = slices.Concat(
containsTypes,
equalsTypes,
existsTypes,
regexMatchesTypes,
startsOrEndsWithTypes,
)
case PredicateKeyEnumFilterID:
expectedPredicateTypes = filterMatchTypes
case PredicateKeyEnumLifecycleIndex, PredicateKeyEnumTierIndex:
expectedPredicateTypes = slices.Concat(equalsTypes, existsTypes, lessThanGreaterThanTypes)
case PredicateKeyEnumOwnerIDs:
expectedPredicateTypes = []PredicateTypeEnum{PredicateTypeEnumEquals}
case PredicateKeyEnumDomainID, PredicateKeyEnumOwnerID, PredicateKeyEnumSystemID:
expectedPredicateTypes = slices.Concat(equalsTypes, existsTypes)
case PredicateKeyEnumProperties:
expectedPredicateTypes = append(existsTypes, PredicateTypeEnumSatisfiesJqExpression)
case PredicateKeyEnumRepositoryIDs:
expectedPredicateTypes = existsTypes
case PredicateKeyEnumTags:
expectedPredicateTypes = append(slices.Concat(
containsTypes,
equalsTypes,
existsTypes,
regexMatchesTypes,
startsOrEndsWithTypes,
), PredicateTypeEnumSatisfiesVersionConstraint)
default:
return nil
}
if !slices.Contains(expectedPredicateTypes, filterPredicate.Type) {
return fmt.Errorf(
"FilterPredicate key '%s' expected to have one of the following types: %v",
filterPredicate.Key,
expectedPredicateTypes,
)
}
return nil
}
// Validates Value requirements expected by some Key types
func (filterPredicate *FilterPredicate) validateValue() error {
if slices.Contains(existsTypes, filterPredicate.Type) {
if filterPredicate.Value != "" {
return fmt.Errorf("FilterPredicate type '%s' cannot have value set.", filterPredicate.Type)
}
return nil
}
idPredicateKeyTypes := []PredicateKeyEnum{
PredicateKeyEnumDomainID,
PredicateKeyEnumFilterID,
PredicateKeyEnumOwnerID,
PredicateKeyEnumOwnerIDs,
PredicateKeyEnumSystemID,
}
if slices.Contains(idPredicateKeyTypes, filterPredicate.Key) && !IsID(filterPredicate.Value) {
return fmt.Errorf("FilterPredicate with key '%s' expects value to be an ID", filterPredicate.Key)
}
return nil
}
type FilterConnection struct {
Nodes []Filter
PageInfo PageInfo
TotalCount int
}
func (filter *Filter) Alias() string {
return slug.Make(filter.Name)
}
func (client *Client) CreateFilter(input FilterCreateInput) (*Filter, error) {
var m struct {
Payload struct {
Filter Filter
Errors []OpsLevelErrors
} `graphql:"filterCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("FilterCreate"))
return &m.Payload.Filter, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) GetFilter(id ID) (*Filter, error) {
var q struct {
Account struct {
Filter Filter `graphql:"filter(id: $id)"`
}
}
v := PayloadVariables{
"id": id,
}
err := client.Query(&q, v, WithName("FilterGet"))
if q.Account.Filter.Id == "" {
err = fmt.Errorf("filter with ID '%s' not found", id)
}
return &q.Account.Filter, HandleErrors(err, nil)
}
func (client *Client) ListFilters(variables *PayloadVariables) (*FilterConnection, error) {
var q struct {
Account struct {
Filters FilterConnection `graphql:"filters(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("FilterList")); err != nil {
return nil, err
}
for q.Account.Filters.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.Filters.PageInfo.End
resp, err := client.ListFilters(variables)
if err != nil {
return nil, err
}
q.Account.Filters.Nodes = append(q.Account.Filters.Nodes, resp.Nodes...)
q.Account.Filters.PageInfo = resp.PageInfo
q.Account.Filters.TotalCount += resp.TotalCount
}
return &q.Account.Filters, nil
}
func (client *Client) UpdateFilter(input FilterUpdateInput) (*Filter, error) {
var m struct {
Payload struct {
Filter Filter
Errors []OpsLevelErrors
} `graphql:"filterUpdate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("FilterUpdate"))
return &m.Payload.Filter, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) DeleteFilter(id ID) error {
var m struct {
Payload struct {
Id ID `graphql:"deletedId"`
Errors []OpsLevelErrors
} `graphql:"filterDelete(input: $input)"`
}
v := PayloadVariables{
"input": DeleteInput{Id: id},
}
err := client.Mutate(&m, v, WithName("FilterDelete"))
return HandleErrors(err, m.Payload.Errors)
}