gunk-dev/the-valley

own-policy.sh

source73e9a20c2d42main's published tip history
2.8 KiB
1# shellcheck shell=bash
2# Paths and fixtures come from the derivation environment.
3# shellcheck disable=SC2154
4export HOME="$TMPDIR"
5export GIT_CONFIG_NOSYSTEM=1
6git config --global user.name valley-check
7git config --global user.email valley-check@localhost
8git 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.
14policy="$TMPDIR/policy"
15mkdir -p "$policy/instance" "$policy/project"
16cp "$floor" "$policy/instance/floor.cue"
17cp "$template" "$policy/instance/template.cue"
18cp "$project"/*.cue "$policy/project/"
19
20# The layer composes.
21cue 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 owing it is approved.
26cat > "$TMPDIR/attributes.cue" <<'EOF'
27package verification
28
29import "strings"
30
31_attributes: strings.Join([
32 for n, c in policy.checks if c.runner == "nix" {c.attribute},
33], "\n")
34EOF
35cue 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; }
38missing=0
39while 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
47done < attributes.txt
48[ "$missing" -eq 0 ] || exit 1
49
50# A change to the docs owes the floor's prose-format alone, and a change to
51# the hook owes the hook's tests. Classes that matched too much or nothing
52# at all would break one or the other.
53repo="$TMPDIR/tree"
54git init --quiet "$repo"
55cd "$repo" || exit 1
56mkdir -p docs valleyhook
57echo seed > docs/architecture.md
58echo seed > valleyhook/main.go
59git add -A
60git commit --quiet -m base
61base="$(git rev-parse HEAD)"
62
63owed() {
64 valley checks --schema "$schema" \
65 --instance "$policy/instance" --project "$policy/project" \
66 --names "$base" HEAD
67}
68
69git checkout --quiet -B docs "$base"
70echo edit > docs/architecture.md
71git commit --quiet -am docs
72got="$(owed)"
73[ "$got" = prose-format ] || {
74 echo "own-policy: a docs-only change must owe prose-format alone, got:" >&2
75 echo "$got" >&2
76 exit 1
77}
78
79git checkout --quiet -B hook "$base"
80echo edit > valleyhook/main.go
81git commit --quiet -am hook
82got="$(owed)"
83grep -qx valleyhook-unit <<<"$got" || {
84 echo "own-policy: a hook-only change must owe valleyhook-unit, got:" >&2
85 echo "$got" >&2
86 exit 1
87}
88if grep -qx prose-format <<<"$got"; then
89 echo "own-policy: a hook-only change fell to the unclassified rule" >&2
90 exit 1
91fi
92
93touch "$out"