-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
201 lines (183 loc) · 5.85 KB
/
rules.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
package main
import (
"encoding/json"
"log"
"strings"
)
// Rule contains all information needed to determine which Release an update request should receive. Specifically:
// * All of the gothmogFields, which are used to determine which Rules are relevant to an update request
// * A priority, which is used to tie-break when multiple matching Rules exist
// * A release_mapping, which is the Release that is returned when the most matching Rule is found
type Rule struct {
properties gothmogFields
release_mapping string
priority int
}
// TODO: is this even useful to define?
type Rules []Rule
// balrogRules is an intermediate structure that contains all fields that Balrog's
// Rules have.
type balrogRule struct {
BuildID string `json:"buildID"`
BuildTarget string `json:"buildTarget"`
Channel string `json:"channel"`
DistVersion string `json:"distVersion"`
Distribution string `json:"distribution"`
InstructionSet string `json:"instructionSet"`
Locale string `json:"locale"`
Mapping string `json:"mapping"`
Memory string `json:"memory"`
OsVersion string `json:"osVersion"`
Priority int `json:"priority"`
Product string `json:"product"`
Version string `json:"version"`
}
// parseRules transforms Balrog Rules into Gothmog Rules
// by plucking out the parts of the Balrog Rules we care about.
func parseRules(data []byte) (Rules, error) {
var importedRules []balrogRule
var parsedRules Rules
err := json.Unmarshal(data, &importedRules)
if err != nil {
return parsedRules, err
}
for _, rule := range importedRules {
parsedRules = append(parsedRules, Rule{
properties: gothmogFields{
product: rule.Product,
version: rule.Version,
buildid: rule.BuildID,
buildTarget: rule.BuildTarget,
locale: rule.Locale,
channel: rule.Channel,
osVersion: rule.OsVersion,
instructionSet: rule.InstructionSet,
memory: rule.Memory,
distribution: rule.Distribution,
distVersion: rule.DistVersion,
},
release_mapping: rule.Mapping,
priority: rule.Priority,
})
}
return parsedRules, nil
}
// matchCsv determines whether or not any of the comma separated
// values of `field` match `value`. `substring` controls whether
// a full or partial string match is performed.
func matchCsv(field string, value string, substring bool) bool {
for _, f := range strings.Split(field, ",") {
if substring {
if strings.Contains(value, f) {
return true
}
} else {
if f == value {
return true
}
}
}
return false
}
// matchComparison tests whether `value` matches the
// test in `field`. `field` may begin be a plain string
// or begin with <, <=, >, or >=. If the latter, the operator
// given is used to compare `value` against the non-operator
// portion of `field`
func matchComparison(field string, value string) bool {
// TODO: letting this anonymous function update `prefix`
// is a little nasty. It would be better if we could split
// by the list of all prefixes instead.
var prefix string
f := func(c rune) bool {
is_prefix := c == '<' || c == '>' || c == '='
if is_prefix {
prefix = prefix + string(c)
}
return is_prefix
}
field_value := strings.FieldsFunc(field, f)[0]
if prefix == "" {
if prefix == value {
return true
}
return false
}
// TODO: there must be a better way to do this.
// something like python's `operator` library maybe?
if prefix == "<" && value < field_value {
return true
}
if prefix == "<=" && value <= field_value {
return true
}
if prefix == ">" && value > field_value {
return true
}
if prefix == ">=" && value >= field_value {
return true
}
return false
}
func matchGlob(field string, value string) bool {
length := len(field)
if field[length-1:] == "*" && strings.HasPrefix(value, field[0:length-1]) {
return true
}
if field == value {
return true
}
return false
}
// findMatchingRule compares an incoming request against a set of
// Rules and returns the best matching Rule.
// TODO: surely there must be some kind of error case possible here?
// Or perhaps the sentinel value is enough? Or maybe get rid of that
// and return an ok/not ok bool instead?
func findMatchingRule(rules *Rules, req gothmogFields) Rule {
// TODO: this should be define outside of the function as a general
// sentinel value
var matchingRule Rule
matchingRule.priority = -1
for _, rule := range *rules {
if rule.properties.product != "" && rule.properties.product != req.product {
continue
}
if rule.properties.version != "" && !matchComparison(rule.properties.version, req.version) && !matchCsv(rule.properties.version, req.version, false) {
continue
}
if rule.properties.buildid != "" && !matchComparison(rule.properties.buildid, req.buildid) {
continue
}
if rule.properties.buildTarget != "" && !matchCsv(rule.properties.buildTarget, req.buildTarget, false) {
continue
}
if rule.properties.locale != "" && !matchCsv(rule.properties.locale, req.locale, false) {
continue
}
// matchGlob handles exact matching and glob matching
if !matchGlob(rule.properties.channel, req.channel) {
continue
}
if rule.properties.osVersion != "" && !matchCsv(rule.properties.osVersion, req.osVersion, true) {
continue
}
if rule.properties.instructionSet != "" && rule.properties.instructionSet != req.instructionSet {
continue
}
if rule.properties.memory != "" && !matchComparison(rule.properties.memory, req.memory) {
continue
}
if rule.properties.distribution != "" && rule.properties.distribution != req.distribution {
continue
}
if rule.properties.distVersion != "" && rule.properties.distVersion != req.distVersion {
continue
}
if rule.priority > matchingRule.priority {
log.Printf("Replacing matchingRule %v with %v", matchingRule, rule)
matchingRule = rule
}
}
return matchingRule
}