| 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 | package derive |
| 14 | |
| 15 | import ( |
| 16 | "fmt" |
| 17 | "sort" |
| 18 | |
| 19 | "the-valley/composed" |
| 20 | ) |
| 21 | |
| 22 | // Result is what a change requires. |
| 23 | type Result struct { |
| 24 | Paths []string // every side of the diff, sorted bytewise, each once |
| 25 | Classes []string // the classes at least one path matched, sorted |
| 26 | Unclassified []string // the paths no class covers, sorted |
| 27 | Carry string // the change's carry mode: exact, same-class or same-paths |
| 28 | Required []Required // the required checks, sorted by name |
| 29 | Follows []Follow // the follow checks it sets due and does not require, sorted by name |
| 30 | } |
| 31 | |
| 32 | // Follow is one check that follows a class the change touches. It runs on |
| 33 | // main after the change lands, and does not gate the change. |
| 34 | type Follow struct { |
| 35 | Name string |
| 36 | Classes []string // the matched classes it follows, sorted |
| 37 | composed.Check |
| 38 | } |
| 39 | |
| 40 | // Required is one check a change requires. |
| 41 | type Required struct { |
| 42 | Name string |
| 43 | |
| 44 | // Classes are the matched classes requiring it, sorted. |
| 45 | Classes []string |
| 46 | |
| 47 | // Unclassified is whether the unclassified set requires it, because a |
| 48 | // path no class covers is among the change's paths. |
| 49 | Unclassified bool |
| 50 | |
| 51 | // Pin is Mandatory when any requirement of it is mandatory, Unknown |
| 52 | // when none is and the model did not read one's pin, and Default when |
| 53 | // every one is a default a project may turn off. |
| 54 | Pin composed.Pin |
| 55 | |
| 56 | // How the check is run, how long an observation of it stays good, and |
| 57 | // what running it costs, as the policy defines it. |
| 58 | composed.Check |
| 59 | } |
| 60 | |
| 61 | // strictness orders the carry modes, loosest first. |
| 62 | var strictness = map[string]int{"same-paths": 0, "same-class": 1, "exact": 2} |
| 63 | |
| 64 | // Derive says what a change touching paths requires under m. paths need |
| 65 | // not be sorted or unique. |
| 66 | // |
| 67 | // The carry mode is the strictest among the matched classes. A path no |
| 68 | // class covers is same-paths, so it can make no change stricter. |
| 69 | func Derive(m *composed.Model, paths []string) (*Result, error) { |
| 70 | type compiled struct { |
| 71 | name string |
| 72 | globs []glob |
| 73 | } |
| 74 | names := make([]string, 0, len(m.Classes)) |
| 75 | for name := range m.Classes { |
| 76 | names = append(names, name) |
| 77 | } |
| 78 | sort.Strings(names) |
| 79 | classes := make([]compiled, len(names)) |
| 80 | for i, name := range names { |
| 81 | classes[i].name = name |
| 82 | for _, p := range m.Classes[name].Paths { |
| 83 | classes[i].globs = append(classes[i].globs, compile(p)) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | r := &Result{Paths: sortedSet(paths), Classes: []string{}, Unclassified: []string{}, Carry: "same-paths", Required: []Required{}, Follows: []Follow{}} |
| 88 | matched := map[string]bool{} |
| 89 | for _, path := range r.Paths { |
| 90 | hit := false |
| 91 | for _, c := range classes { |
| 92 | for _, g := range c.globs { |
| 93 | if g.match(path) { |
| 94 | matched[c.name] = true |
| 95 | hit = true |
| 96 | break |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | if !hit { |
| 101 | r.Unclassified = append(r.Unclassified, path) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | required := map[string]*Required{} |
| 106 | need := func(check string, pin composed.Pin) (*Required, error) { |
| 107 | q := required[check] |
| 108 | if q == nil { |
| 109 | def, ok := m.Checks[check] |
| 110 | if !ok { |
| 111 | return nil, fmt.Errorf("the policy requires %s and defines no check by that name", check) |
| 112 | } |
| 113 | q = &Required{Name: check, Classes: []string{}, Pin: composed.Default, Check: def} |
| 114 | required[check] = q |
| 115 | } |
| 116 | switch pin { |
| 117 | case composed.Mandatory: |
| 118 | q.Pin = composed.Mandatory |
| 119 | case composed.Unknown: |
| 120 | if q.Pin != composed.Mandatory { |
| 121 | q.Pin = composed.Unknown |
| 122 | } |
| 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. |
| 193 | func 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 | } |