gunk-dev/the-valley

derive.go

source818bd30431eamain's published tip history
6.2 KiB
1// Package derive says which checks a change requires. It takes the whole
2// composed policy, as composed.Read reads it, and every path the change's
3// diff touches (Diff), and returns each required check with the classes
4// requiring it, whether it is mandatory, and how it is run; the checks the
5// change sets due on main, which follow a matched class and gate nothing;
6// the change's carry mode; and the paths no class covers.
7//
8// Matching is a set operation, never first-match. A change requires the
9// union of the checks of every class one of its paths matches, and a path
10// no class matches requires the checks of the policy's `unclassified` set.
11// A class's paths are globs: `*` matches within one path segment, and a
12// `**` segment matches any number of segments, none included (Match).
13//
14// bin/valley's deriver (derive_required) answers the same question, and
15// the tests beside this file hold the two to the same answers. Where they
16// differ, the difference is a known fault of bin/valley's, and those tests
17// name each one.
18package derive
19
20import (
21 "fmt"
22 "sort"
23
24 "the-valley/composed"
25)
26
27// Result is what a change requires.
28type Result struct {
29 Paths []string // every side of the diff, sorted bytewise, each once
30 Classes []string // the classes at least one path matched, sorted
31 Unclassified []string // the paths no class covers, sorted
32 Carry string // the change's carry mode: exact, same-class or same-paths
33 Required []Required // the required checks, sorted by name
34 Follows []Follow // the follow checks it sets due and does not require, sorted by name
35}
36
37// Follow is one check that follows a class the change touches. It runs on
38// main after the change lands, and does not gate the change.
39type Follow struct {
40 Name string
41 Classes []string // the matched classes it follows, sorted
42 composed.Check
43}
44
45// Required is one check a change requires.
46type Required struct {
47 Name string
48
49 // Classes are the matched classes requiring it, sorted.
50 Classes []string
51
52 // Unclassified is whether the unclassified set requires it, because a
53 // path no class covers is among the change's paths.
54 Unclassified bool
55
56 // Pin is Mandatory when any requirement of it is mandatory, and
57 // Default when every one is a default a project may turn off.
58 Pin composed.Pin
59
60 // How the check is run, how long an observation of it stays good, and
61 // what running it costs, as the policy defines it.
62 composed.Check
63}
64
65// strictness orders the carry modes, loosest first.
66var strictness = map[string]int{"same-paths": 0, "same-class": 1, "exact": 2}
67
68// Derive says what a change touching paths requires under m. paths need
69// not be sorted or unique.
70//
71// The carry mode is the strictest among the matched classes. A path no
72// class covers is same-paths, so it can make no change stricter.
73func Derive(m *composed.Model, paths []string) (*Result, error) {
74 type compiled struct {
75 name string
76 globs []glob
77 }
78 names := make([]string, 0, len(m.Classes))
79 for name := range m.Classes {
80 names = append(names, name)
81 }
82 sort.Strings(names)
83 classes := make([]compiled, len(names))
84 for i, name := range names {
85 classes[i].name = name
86 for _, p := range m.Classes[name].Paths {
87 classes[i].globs = append(classes[i].globs, compile(p))
88 }
89 }
90
91 r := &Result{Paths: sortedSet(paths), Classes: []string{}, Unclassified: []string{}, Carry: "same-paths", Required: []Required{}, Follows: []Follow{}}
92 matched := map[string]bool{}
93 for _, path := range r.Paths {
94 hit := false
95 for _, c := range classes {
96 for _, g := range c.globs {
97 if g.match(path) {
98 matched[c.name] = true
99 hit = true
100 break
101 }
102 }
103 }
104 if !hit {
105 r.Unclassified = append(r.Unclassified, path)
106 }
107 }
108
109 required := map[string]*Required{}
110 need := func(check string, pin composed.Pin) (*Required, error) {
111 q := required[check]
112 if q == nil {
113 def, ok := m.Checks[check]
114 if !ok {
115 return nil, fmt.Errorf("the policy requires %s and defines no check by that name", check)
116 }
117 q = &Required{Name: check, Classes: []string{}, Pin: composed.Default, Check: def}
118 required[check] = q
119 }
120 switch pin {
121 case composed.Mandatory:
122 q.Pin = composed.Mandatory
123 case composed.Default:
124 default:
125 return nil, fmt.Errorf("the policy's requirement of %s is neither mandatory nor a default (%q)", check, pin)
126 }
127 return q, nil
128 }
129 for _, c := range classes {
130 if !matched[c.name] {
131 continue
132 }
133 r.Classes = append(r.Classes, c.name)
134 class := m.Classes[c.name]
135 mode, ok := strictness[class.Carry]
136 if !ok {
137 return nil, fmt.Errorf("class %s has no carry mode this deriver knows (%q)", c.name, class.Carry)
138 }
139 if mode > strictness[r.Carry] {
140 r.Carry = class.Carry
141 }
142 for check, pin := range class.Requires {
143 q, err := need(check, pin)
144 if err != nil {
145 return nil, err
146 }
147 q.Classes = append(q.Classes, c.name)
148 }
149 }
150 if len(r.Unclassified) > 0 {
151 for check, pin := range m.Unclassified {
152 q, err := need(check, pin)
153 if err != nil {
154 return nil, err
155 }
156 q.Unclassified = true
157 }
158 }
159
160 for _, q := range required {
161 r.Required = append(r.Required, *q)
162 }
163 sort.Slice(r.Required, func(i, j int) bool { return r.Required[i].Name < r.Required[j].Name })
164
165 // A check the change requires already runs at the gate, so following
166 // it as well sets nothing due.
167 follows := map[string]*Follow{}
168 for _, name := range r.Classes {
169 for _, check := range m.Classes[name].Follows {
170 if required[check] != nil {
171 continue
172 }
173 f := follows[check]
174 if f == nil {
175 def, ok := m.Checks[check]
176 if !ok {
177 return nil, fmt.Errorf("class %s follows %s, and the policy defines no check by that name", name, check)
178 }
179 f = &Follow{Name: check, Check: def}
180 follows[check] = f
181 }
182 f.Classes = append(f.Classes, name)
183 }
184 }
185 for _, f := range follows {
186 r.Follows = append(r.Follows, *f)
187 }
188 sort.Slice(r.Follows, func(i, j int) bool { return r.Follows[i].Name < r.Follows[j].Name })
189 return r, nil
190}
191
192// sortedSet is paths sorted bytewise, each once.
193func sortedSet(paths []string) []string {
194 out := append([]string{}, paths...)
195 sort.Strings(out)
196 n := 0
197 for i, p := range out {
198 if i == 0 || p != out[n-1] {
199 out[n] = p
200 n++
201 }
202 }
203 return out[:n]
204}