File size: 9,186 Bytes
b855ced
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env bash
# Local CI gate. Mirrors the upstream GitHub checks and adds server and
# security checks.
#
#   scripts/ci/gate.sh            full gate (run by the pre-push hook)
#   scripts/ci/gate.sh --staged   fast checks on staged changes (pre-commit hook)
#
# Environment:
#   TAPER_GATE_OFFLINE=1   skip steps that need the network
#   TAPER_GATE_BASE=<ref>  base for branch-wide checks (default: main)

set -uo pipefail

ROOT=$(git rev-parse --show-toplevel)
cd "$ROOT"

MODE=full
[[ ${1:-} == --staged ]] && MODE=staged
OFFLINE=${TAPER_GATE_OFFLINE:-0}
BASE=${TAPER_GATE_BASE:-main}
VENV="$ROOT/env/ci"

if [[ -t 1 ]]; then G=$'\e[32m'; R=$'\e[31m'; Y=$'\e[33m'; B=$'\e[1m'; N=$'\e[0m'; else G= R= Y= B= N=; fi

declare -a RESULTS=()
FAILED=0
LOG=$(mktemp)
trap 'rm -f "$LOG"' EXIT

# step NAME CMD...: run CMD, record PASS/FAIL, show output only on failure.
step() {
  local name=$1; shift
  local start=$SECONDS
  if "$@" >"$LOG" 2>&1; then
    RESULTS+=("${G}PASS${N}  $name ($((SECONDS - start))s)")
  else
    RESULTS+=("${R}FAIL${N}  $name")
    FAILED=1
    printf '%s\n' "${R}${B}── $name failed ──${N}"
    tail -n 40 "$LOG"
    echo
  fi
}

skip() { RESULTS+=("${Y}SKIP${N}  $1 — $2"); }

# Files to inspect: staged files (pre-commit) or everything changed on the branch.
changed_files() {
  if [[ $MODE == staged ]]; then
    git diff --cached --name-only --diff-filter=ACMR
  else
    git diff --name-only --diff-filter=ACMR "$(git merge-base "$BASE" HEAD)" HEAD
  fi
}

added_lines() {
  if [[ $MODE == staged ]]; then
    git diff --cached -U0 --diff-filter=ACMR
  else
    git diff -U0 --diff-filter=ACMR "$(git merge-base "$BASE" HEAD)" HEAD
  fi | grep -E '^\+[^+]' || true
}

touches() { changed_files | grep -qE "$1"; }

# ---------------------------------------------------------------------------
# Checks
# ---------------------------------------------------------------------------

check_whitespace() {
  if [[ $MODE == staged ]]; then git diff --cached --check
  else git diff --check "$(git merge-base "$BASE" HEAD)" HEAD; fi
}

# Secrets: credentials must never be committed. Patterns are deliberately
# specific to keep false positives near zero.
check_secrets() {
  local patterns=(
    '-----BEGIN ([A-Z]+ )?PRIVATE KEY-----'
    'AKIA[0-9A-Z]{16}'
    'gh[pousr]_[A-Za-z0-9]{36,}'
    'github_pat_[A-Za-z0-9_]{60,}'
    'xox[abprs]-[A-Za-z0-9-]{10,}'
    'sk-[A-Za-z0-9_-]{32,}'
    'tp_[A-Za-z0-9_-]{40,}'
  )
  local hits
  hits=$(added_lines | grep -nE "$(IFS='|'; echo "${patterns[*]}")") || true
  if [[ -n $hits ]]; then echo "Possible secrets in added lines:"; echo "$hits" | cut -c1-160; return 1; fi
  local bad
  bad=$(changed_files | grep -E '(^|/)(\.env(\..*)?|.*\.pem|.*\.key|id_(rsa|ed25519|ecdsa)|credentials\.json|settings\.local\.json)$' | grep -v '\.example$') || true
  if [[ -n $bad ]]; then echo "Refusing to commit credential-like files:"; echo "$bad"; return 1; fi
}

check_large_files() {
  local f size big=0
  while IFS= read -r f; do
    [[ -f $f ]] || continue
    size=$(stat -c %s "$f")
    if (( size > 2 * 1024 * 1024 )); then echo "$f is $((size / 1024)) KiB (limit 2048 KiB)"; big=1; fi
  done < <(changed_files)
  return $big
}

check_conflict_markers() {
  ! added_lines | grep -nE '^\+(<<<<<<<|>>>>>>>) '
}

ensure_venv() {
  local stamp="$VENV/.requirements.sha256"
  local want
  want=$(sha256sum scripts/requirements.lock | cut -d' ' -f1)
  if [[ ! -x $VENV/bin/python || ! -f $stamp || $(cat "$stamp") != "$want" ]]; then
    [[ $OFFLINE == 1 ]] && { echo "venv missing or stale and TAPER_GATE_OFFLINE=1"; return 1; }
    rm -rf "$VENV"
    python3 -m venv "$VENV" \
      && "$VENV/bin/pip" install -q --require-hashes --no-deps -r scripts/requirements.lock \
      && echo "$want" > "$stamp"
  fi
}

py_check() { "$VENV/bin/python" "scripts/$1"; }

ensure_server_deps() {
  [[ -d server/node_modules ]] && return 0
  [[ $OFFLINE == 1 ]] && { echo "server/node_modules missing and TAPER_GATE_OFFLINE=1"; return 1; }
  (cd server && npm ci --no-audit --no-fund)
}

ensure_client_deps() {
  [[ -d client/node_modules ]] && return 0
  [[ $OFFLINE == 1 ]] && { echo "client/node_modules missing and TAPER_GATE_OFFLINE=1"; return 1; }
  (cd client && npm ci --no-audit --no-fund)
}

server_lint() { (cd server && npx --no-install eslint .); }
client_lint() { server/node_modules/.bin/eslint -c client/eslint.config.mjs client/; }
server_test() { (cd server && npm test --silent); }
redocly_lint() { npx -y @redocly/cli@2.31.5 lint --format=summary data/api/*/*.yaml; }
hugo_build() { hugo --quiet --destination "$(mktemp -d)"; }

# ---------------------------------------------------------------------------
# Plan
# ---------------------------------------------------------------------------

echo "${B}Taper local CI gate (${MODE})${N}"

step "No whitespace errors" check_whitespace
step "No secrets or credential files" check_secrets
step "No files over 2 MiB" check_large_files
step "No conflict markers" check_conflict_markers
# Docs as code (docs/docs-as-code.md): links, anchors, settings, index. Fast, so always.
step "Documentation (links, settings, index)" python3 scripts/ci/check-docs.py

spec_changed=1
server_changed=1
if [[ $MODE == staged ]]; then
  touches '^(data|scripts)/' || spec_changed=0
  touches '^server/' || server_changed=0
fi

if (( spec_changed )); then
  step "Python environment for spec checks" ensure_venv
  step "Event schema examples" py_check check-event-schema-examples.py
  step "OpenAPI definition examples" py_check check-openapi-sources.py
  step "JSON Schema inline examples" py_check check-json-schemas.py
  if [[ $MODE == full ]]; then
    if [[ $OFFLINE == 1 ]]; then skip "OpenAPI lint (redocly)" "offline"; else step "OpenAPI lint (redocly)" redocly_lint; fi
  fi
else
  skip "Spec checks" "no spec files staged"
fi

if (( server_changed )); then
  step "Server dependencies" ensure_server_deps
  step "Client dependencies (crypto, used by E2EE tests)" ensure_client_deps
  step "Server lint (eslint)" server_lint
  step "Server end-to-end tests" server_test
else
  skip "Server checks" "no server files staged"
fi

if [[ $MODE == full ]] || touches '^client/'; then
  step "Server dependencies (for client lint)" ensure_server_deps
  step "Client dependencies" ensure_client_deps
  step "Client lint (eslint, no innerHTML)" client_lint
  if [[ $MODE == full ]]; then
    if [[ -n ${TAPER_CHROMIUM:-} ]] || command -v chromium >/dev/null || command -v google-chrome >/dev/null; then
      step "Client browser test (headless Chromium, real E2EE)" node client/test-browser.mjs
    else
      skip "Client browser test" "no Chromium found (set TAPER_CHROMIUM)"
    fi
  fi
else
  skip "Client checks" "no client files staged"
fi

# Desktop app (desktop/): Python, GTK 4, libadwaita, WebKitGTK.
desktop_lint() { (cd desktop && "$VENV/bin/ruff" check --quiet . && "$VENV/bin/ruff" format --check --quiet .); }
desktop_deps() { python3 -c "import gi; gi.require_version('Gtk', '4.0'); gi.require_version('Adw', '1'); gi.require_version('WebKit', '6.0')" 2>/dev/null; }
if [[ $MODE == full ]] || touches '^desktop/'; then
  step "Python environment (ruff)" ensure_venv
  step "Desktop lint (ruff, with security checks)" desktop_lint
  if [[ $MODE == full ]]; then
    if command -v xvfb-run >/dev/null && desktop_deps; then
      step "Desktop app test (headless WebKitGTK, through a fake Tor)" python3 desktop/test_desktop.py
    else
      skip "Desktop app test" "needs xvfb-run, python3-gi, gir1.2-gtk-4.0, gir1.2-adw-1 and gir1.2-webkit-6.0"
    fi
  fi
else
  skip "Desktop checks" "no desktop files staged"
fi

# Live Tor test: real onion services on the Tor network. Opt-in, because it
# needs Internet access and takes minutes.
if [[ $MODE == full ]]; then
  if [[ ${TAPER_LIVE_TOR:-} == 1 ]] && command -v tor >/dev/null; then
    step "Live Tor test (onion services, federation, E2EE)" bash -c 'cd server && node test-tor-live.js'
  else
    skip "Live Tor test" "opt-in: TAPER_LIVE_TOR=1 (needs tor and Internet access)"
  fi
fi

# Reproducible release build (docs/reproducible-builds.md): builds twice, compares.
if [[ $MODE == full && ${TAPER_GATE_REPRO:-0} == 1 ]]; then
  step "Reproducible release build (bit for bit)" scripts/release/verify-reproducible.sh
elif [[ $MODE == full ]]; then
  skip "Reproducible release build" "opt-in: TAPER_GATE_REPRO=1 (needs npm network access)"
fi

if [[ $MODE == full ]]; then
  if command -v typos >/dev/null; then step "Spell check (typos)" typos --config _typos.toml
  else skip "Spell check (typos)" "typos not installed"; fi
  if command -v hugo >/dev/null; then step "Hugo site build" hugo_build
  else skip "Hugo site build" "hugo not installed (upstream uses 0.155.3 extended)"; fi
fi

# Extra steps registered by other scripts (e.g. SBOM checks).
for extra in scripts/ci/gate.d/*.sh; do
  [[ -f $extra ]] || continue
  # shellcheck source=/dev/null
  source "$extra"
done

echo
printf '%s\n' "${RESULTS[@]}"
echo
if (( FAILED )); then
  echo "${R}${B}Gate failed.${N} Fix the failures above (bypassing with --no-verify is for emergencies only)."
  exit 1
fi
echo "${G}${B}Gate passed.${N}"