| 1 | # shellcheck shell=bash |
| 2 | # Paths and fixtures come from the derivation environment. |
| 3 | # shellcheck disable=SC2154 |
| 4 | export HOME="$TMPDIR" |
| 5 | export GIT_CONFIG_NOSYSTEM=1 |
| 6 | git config --global user.name valley-check |
| 7 | git config --global user.email valley-check@localhost |
| 8 | git config --global init.defaultBranch main |
| 9 | |
| 10 | # The real floor is in the owning valley's repository, which a build cannot |
| 11 | # fetch. The example floor stands in for it: it has the same prose class |
| 12 | # and unclassified rule, and the template defines the #docs this layer |
| 13 | # embeds. |
| 14 | policy="$TMPDIR/policy" |
| 15 | mkdir -p "$policy/instance" "$policy/project" |
| 16 | cp "$floor" "$policy/instance/floor.cue" |
| 17 | cp "$template" "$policy/instance/template.cue" |
| 18 | cp "$project"/*.cue "$policy/project/" |
| 19 | |
| 20 | # The layer composes. |
| 21 | cue vet -c "$schema" "$policy"/instance/*.cue "$policy"/project/*.cue |
| 22 | |
| 23 | # Every check the composition names is a flake check this flake defines. |
| 24 | # A policy naming a check nobody can build would be found only when a |
| 25 | # change that requires it is approved. |
| 26 | cat > "$TMPDIR/attributes.cue" <<'EOF' |
| 27 | package verification |
| 28 | |
| 29 | import "strings" |
| 30 | |
| 31 | _attributes: strings.Join([ |
| 32 | for n, c in policy.checks if c.runner == "nix" {c.attribute}, |
| 33 | ], "\n") |
| 34 | EOF |
| 35 | cue eval --out text -e _attributes "$schema" "$policy"/instance/*.cue \ |
| 36 | "$policy"/project/*.cue "$TMPDIR/attributes.cue" > attributes.txt |
| 37 | [ -s attributes.txt ] || { echo "own-policy: no nix check named" >&2; exit 1; } |
| 38 | missing=0 |
| 39 | while IFS= read -r a; do |
| 40 | case " $checkNames " in |
| 41 | *" $a "*) ;; |
| 42 | *) |
| 43 | echo "own-policy: the policy names $a, which is not a flake check" >&2 |
| 44 | missing=1 |
| 45 | ;; |
| 46 | esac |
| 47 | done < attributes.txt |
| 48 | [ "$missing" -eq 0 ] || exit 1 |
| 49 | |
| 50 | # A change to the docs requires the floor's prose-format alone, and a |
| 51 | # change to the hook requires the hook's tests. Classes that matched too much or nothing |
| 52 | # at all would break one or the other. |
| 53 | repo="$TMPDIR/tree" |
| 54 | git init --quiet "$repo" |
| 55 | cd "$repo" || exit 1 |
| 56 | mkdir -p docs valleyhook nix/checks |
| 57 | echo seed > docs/architecture.md |
| 58 | echo seed > valleyhook/main.go |
| 59 | echo seed > nix/packages.nix |
| 60 | for f in $checkFiles; do |
| 61 | echo seed > "nix/checks/$f" |
| 62 | done |
| 63 | git add -A |
| 64 | git commit --quiet -m base |
| 65 | base="$(git rev-parse HEAD)" |
| 66 | |
| 67 | required() { |
| 68 | valley checks --schema "$schema" \ |
| 69 | --instance "$policy/instance" --project "$policy/project" \ |
| 70 | --names "$base" HEAD |
| 71 | } |
| 72 | |
| 73 | git checkout --quiet -B docs "$base" |
| 74 | echo edit > docs/architecture.md |
| 75 | git commit --quiet -am docs |
| 76 | got="$(required)" |
| 77 | [ "$got" = prose-format ] || { |
| 78 | echo "own-policy: a docs-only change must require prose-format alone, got:" >&2 |
| 79 | echo "$got" >&2 |
| 80 | exit 1 |
| 81 | } |
| 82 | |
| 83 | git checkout --quiet -B hook "$base" |
| 84 | echo edit > valleyhook/main.go |
| 85 | git commit --quiet -am hook |
| 86 | got="$(required)" |
| 87 | grep -qx valleyhook-unit <<<"$got" || { |
| 88 | echo "own-policy: a hook-only change must require valleyhook-unit, got:" >&2 |
| 89 | echo "$got" >&2 |
| 90 | exit 1 |
| 91 | } |
| 92 | if grep -qx prose-format <<<"$got"; then |
| 93 | echo "own-policy: a hook-only change fell to the unclassified rule" >&2 |
| 94 | exit 1 |
| 95 | fi |
| 96 | |
| 97 | # A check's own files require that check. Every file under nix/checks/ |
| 98 | # named after a flake check requires that check, and every other script |
| 99 | # there requires some check the nix class does not. A script that matched |
| 100 | # the nix class alone could be changed without its check being required. Other files that |
| 101 | # are not named after a check are definitions or data shared by several |
| 102 | # checks, which the policy may leave to the nix class. |
| 103 | git checkout --quiet -B shared "$base" |
| 104 | echo edit > nix/packages.nix |
| 105 | git commit --quiet -am shared |
| 106 | shared="$(required | LC_ALL=C sort)" |
| 107 | failed=0 |
| 108 | for f in $checkFiles; do |
| 109 | name="${f%.*}" |
| 110 | case " $checkNames " in |
| 111 | *" $name "*) named=1 ;; |
| 112 | *) named=0 ;; |
| 113 | esac |
| 114 | [ "$named" -eq 1 ] || [ "${f##*.}" = sh ] || continue |
| 115 | git checkout --quiet -B file "$base" |
| 116 | echo edit > "nix/checks/$f" |
| 117 | git commit --quiet -am "$f" |
| 118 | got="$(required | LC_ALL=C sort)" |
| 119 | if [ "$named" -eq 1 ]; then |
| 120 | grep -qx -- "$name" <<<"$got" && continue |
| 121 | echo "own-policy: a change to nix/checks/$f must require $name, got:" >&2 |
| 122 | else |
| 123 | [ -n "$(LC_ALL=C comm -23 <(echo "$got") <(echo "$shared"))" ] && continue |
| 124 | echo "own-policy: a change to nix/checks/$f requires no check of its own, got:" >&2 |
| 125 | fi |
| 126 | echo "$got" >&2 |
| 127 | failed=1 |
| 128 | done |
| 129 | [ "$failed" -eq 0 ] || exit 1 |
| 130 | |
| 131 | touch "$out" |