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