| 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. |
| 18 | package derive |
| 19 | |
| 20 | import ( |
| 21 | "fmt" |
| 22 | "sort" |
| 23 | |
| 24 | "the-valley/composed" |
| 25 | ) |
| 26 | |
| 27 | // Result is what a change requires. |
| 28 | type 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. |
| 39 | type 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. |
| 46 | type 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, Unknown |
| 57 | // when none is and the model did not read one's pin, and Default when |
| 58 | // every one is a default a project may turn off. |
| 59 | Pin composed.Pin |
| 60 | |
| 61 | // How the check is run, how long an observation of it stays good, and |
| 62 | // what running it costs, as the policy defines it. |
| 63 | composed.Check |
| 64 | } |
| 65 | |
| 66 | // strictness orders the carry modes, loosest first. |
| 67 | var strictness = map[string]int{"same-paths": 0, "same-class": 1, "exact": 2} |
| 68 | |
| 69 | // Derive says what a change touching paths requires under m. paths need |
| 70 | // not be sorted or unique. |
| 71 | // |
| 72 | // The carry mode is the strictest among the matched classes. A path no |
| 73 | // class covers is same-paths, so it can make no change stricter. |
| 74 | func Derive(m *composed.Model, paths []string) (*Result, error) { |
| 75 | type compiled struct { |
| 76 | name string |
| 77 | globs []glob |
| 78 | } |
| 79 | names := make([]string, 0, len(m.Classes)) |
| 80 | for name := range m.Classes { |
| 81 | names = append(names, name) |
| 82 | } |
| 83 | sort.Strings(names) |
| 84 | classes := make([]compiled, len(names)) |
| 85 | for i, name := range names { |
| 86 | classes[i].name = name |
| 87 | for _, p := range m.Classes[name].Paths { |
| 88 | classes[i].globs = append(classes[i].globs, compile(p)) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | r := &Result{Paths: sortedSet(paths), Classes: []string{}, Unclassified: []string{}, Carry: "same-paths", Required: []Required{}, Follows: []Follow{}} |
| 93 | matched := map[string]bool{} |
| 94 | for _, path := range r.Paths { |
| 95 | hit := false |
| 96 | for _, c := range classes { |
| 97 | for _, g := range c.globs { |
| 98 | if g.match(path) { |
| 99 | matched[c.name] = true |
| 100 | hit = true |
| 101 | break |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | if !hit { |
| 106 | r.Unclassified = append(r.Unclassified, path) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | required := map[string]*Required{} |
| 111 | need := func(check string, pin composed.Pin) (*Required, error) { |
| 112 | q := required[check] |
| 113 | if q == nil { |
| 114 | def, ok := m.Checks[check] |
| 115 | if !ok { |
| 116 | return nil, fmt.Errorf("the policy requires %s and defines no check by that name", check) |
| 117 | } |
| 118 | q = &Required{Name: check, Classes: []string{}, Pin: composed.Default, Check: def} |
| 119 | required[check] = q |
| 120 | } |
| 121 | switch pin { |
| 122 | case composed.Mandatory: |
| 123 | q.Pin = composed.Mandatory |
| 124 | case composed.Unknown: |
| 125 | if q.Pin != composed.Mandatory { |
| 126 | q.Pin = composed.Unknown |
| 127 | } |
| 128 | case composed.Default: |
| 129 | default: |
| 130 | return nil, fmt.Errorf("the policy's requirement of %s is neither mandatory nor a default (%q)", check, pin) |
| 131 | } |
| 132 | return q, nil |
| 133 | } |
| 134 | for _, c := range classes { |
| 135 | if !matched[c.name] { |
| 136 | continue |
| 137 | } |
| 138 | r.Classes = append(r.Classes, c.name) |
| 139 | class := m.Classes[c.name] |
| 140 | mode, ok := strictness[class.Carry] |
| 141 | if !ok { |
| 142 | return nil, fmt.Errorf("class %s has no carry mode this deriver knows (%q)", c.name, class.Carry) |
| 143 | } |
| 144 | if mode > strictness[r.Carry] { |
| 145 | r.Carry = class.Carry |
| 146 | } |
| 147 | for check, pin := range class.Requires { |
| 148 | q, err := need(check, pin) |
| 149 | if err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | q.Classes = append(q.Classes, c.name) |
| 153 | } |
| 154 | } |
| 155 | if len(r.Unclassified) > 0 { |
| 156 | for check, pin := range m.Unclassified { |
| 157 | q, err := need(check, pin) |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | q.Unclassified = true |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | for _, q := range required { |
| 166 | r.Required = append(r.Required, *q) |
| 167 | } |
| 168 | sort.Slice(r.Required, func(i, j int) bool { return r.Required[i].Name < r.Required[j].Name }) |
| 169 | |
| 170 | // A check the change requires already runs at the gate, so following |
| 171 | // it as well sets nothing due. |
| 172 | follows := map[string]*Follow{} |
| 173 | for _, name := range r.Classes { |
| 174 | for _, check := range m.Classes[name].Follows { |
| 175 | if required[check] != nil { |
| 176 | continue |
| 177 | } |
| 178 | f := follows[check] |
| 179 | if f == nil { |
| 180 | def, ok := m.Checks[check] |
| 181 | if !ok { |
| 182 | return nil, fmt.Errorf("class %s follows %s, and the policy defines no check by that name", name, check) |
| 183 | } |
| 184 | f = &Follow{Name: check, Check: def} |
| 185 | follows[check] = f |
| 186 | } |
| 187 | f.Classes = append(f.Classes, name) |
| 188 | } |
| 189 | } |
| 190 | for _, f := range follows { |
| 191 | r.Follows = append(r.Follows, *f) |
| 192 | } |
| 193 | sort.Slice(r.Follows, func(i, j int) bool { return r.Follows[i].Name < r.Follows[j].Name }) |
| 194 | return r, nil |
| 195 | } |
| 196 | |
| 197 | // sortedSet is paths sorted bytewise, each once. |
| 198 | func sortedSet(paths []string) []string { |
| 199 | out := append([]string{}, paths...) |
| 200 | sort.Strings(out) |
| 201 | n := 0 |
| 202 | for i, p := range out { |
| 203 | if i == 0 || p != out[n-1] { |
| 204 | out[n] = p |
| 205 | n++ |
| 206 | } |
| 207 | } |
| 208 | return out[:n] |
| 209 | } |