diff --git a/.github/workflows/pr-proof.yml b/.github/workflows/pr-proof.yml new file mode 100644 index 00000000..eaaeef10 --- /dev/null +++ b/.github/workflows/pr-proof.yml @@ -0,0 +1,47 @@ +name: PR proof + +# A pull request that changes what users see must show proof on screen. The rule and the list +# of UI paths live in scripts/pr-proof-check.sh; CONTRIBUTING.md explains it to contributors. +# +# Safe for fork pull requests: pull_request (never pull_request_target), read-only token, no +# secrets. The pull request body is never put on a command line by this file: the job passes +# only the PR number through env and reads the body with gh. +on: + pull_request: + branches: [develop] + types: [opened, edited, synchronize, reopened, ready_for_review] + +concurrency: + group: pr-proof-${{ github.event.pull_request.number }} + cancel-in-progress: true + +permissions: + contents: read + pull-requests: read + +jobs: + proof: + name: PR proof + # Public repo, so a hosted Linux runner is free. Explicit image, never *-latest. + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Check for UI proof + env: + GH_TOKEN: ${{ github.token }} + GH_REPO: ${{ github.repository }} + PR: ${{ github.event.pull_request.number }} + run: | + set -euo pipefail + work="$(mktemp -d)" + gh pr view "$PR" --json body,baseRefName,author > "$work/pr.json" + # All changed files, paged: gh pr view --json files stops at a fixed count. + gh api "repos/$GH_REPO/pulls/$PR/files" --paginate --jq '.[].filename' > "$work/files.txt" + jq -r '.body // ""' "$work/pr.json" > "$work/body.md" + args=(--body "$work/body.md" --files "$work/files.txt" --base "$(jq -r '.baseRefName' "$work/pr.json")" --author "$(jq -r '.author.login // ""' "$work/pr.json")") + if [ "$(jq -r '.author.is_bot // false' "$work/pr.json")" = true ]; then args+=(--bot); fi + bash scripts/pr-proof-check.sh "${args[@]}" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 4a5fe9ef..5c122435 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -19,7 +19,7 @@ A change that users can see (Settings, the arrow, a notice, a menu, any new or c Put the images in the pull request description, or commit them as PNG files under `docs/verification//`. If users see no change, tick "This PR changes nothing users see" in the description instead. -A maintainer checks this before a review starts. A new string goes into every locale in the same commit; `LocaleCoverageTests` enforces that. +The `PR proof` check enforces this: it fails when a change touches UI files and the description has no image and no ticked box, and the diff adds no proof. A maintainer also checks this before a review starts. A new string goes into every locale in the same commit; `LocaleCoverageTests` enforces that. ### Branch name convention diff --git a/scripts/pr-proof-check.sh b/scripts/pr-proof-check.sh new file mode 100755 index 00000000..0a59251f --- /dev/null +++ b/scripts/pr-proof-check.sh @@ -0,0 +1,122 @@ +#!/bin/bash +# scripts/pr-proof-check.sh : the "PR proof" check. A pull request that changes what users see +# must show proof on screen. No network: the caller (.github/workflows/pr-proof.yml) fetches the +# data with gh and passes it in. +# +# bash scripts/pr-proof-check.sh --body FILE --files FILE [--base BRANCH] [--author LOGIN] [--bot] +# +# --body FILE the pull request description, as text +# --files FILE the changed paths, one per line +# --base BRANCH the base branch; anything but develop passes (default: develop) +# --author LOGIN the author login; a bot login passes +# --bot the author is a bot (gh author.is_bot); passes +# +# A path "looks like UI" when it is one of: +# hidden/Features/**/*.swift whose path contains Settings, Preferences, StatusBar, Notice, +# Palette, Presentation, Arrivals or About +# *.storyboard, *.xib, anything under *.xcassets/, *.lproj/*.strings +# +# A UI change passes when ANY of these holds: +# 1. the body ticks "- [x] This PR changes nothing users see" (x in either case) +# 2. the body has an image or video: a markdown image, &2; exit 2; } ;; + esac + case "$1" in + --body) body_file="$2"; shift 2 ;; + --files) files_file="$2"; shift 2 ;; + --base) base="$2"; shift 2 ;; + --author) author="$2"; shift 2 ;; + --bot) is_bot=1; shift ;; + *) echo "pr-proof-check: unknown argument: $1" >&2; exit 2 ;; + esac +done +[ -r "$body_file" ] && [ -r "$files_file" ] || { echo "pr-proof-check: --body and --files must be readable files" >&2; exit 2; } + +if [ "$base" != "develop" ]; then + echo "PR proof: base is $base, not develop. Nothing to check." + exit 0 +fi +case "$author" in + *"[bot]"|app/*) is_bot=1 ;; +esac +if [ "$is_bot" -eq 1 ]; then + echo "PR proof: bot author. Nothing to check." + exit 0 +fi + +is_ui_path() { + case "$1" in + hidden/Features/*.swift) + case "$1" in + *Settings*|*Preferences*|*StatusBar*|*Notice*|*Palette*|*Presentation*|*Arrivals*|*About*) return 0 ;; + esac + return 1 ;; + *.storyboard|*.xib|*.xcassets/*|*.lproj/*.strings) return 0 ;; + esac + return 1 +} + +is_proof_path() { + case "$1" in + docs/verification/*) + case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in + *.png|*.jpg|*.jpeg|*.gif|*.webp|*.mp4|*.mov) return 0 ;; + esac ;; + esac + return 1 +} + +ui_files=""; proof_in_diff=0 +while IFS= read -r path || [ -n "$path" ]; do + path="${path%$'\r'}" + [ -n "$path" ] || continue + if is_ui_path "$path"; then ui_files="$ui_files$path"$'\n'; fi + if is_proof_path "$path"; then proof_in_diff=1; fi +done < "$files_file" + +if [ -z "$ui_files" ]; then + echo "PR proof: no changed path looks like UI. Pass." + exit 0 +fi + +body="$(tr -d '\r' < "$body_file")" + +if printf '%s\n' "$body" | grep -Eiq '^[[:space:]]*[-*][[:space:]]+\[x\][[:space:]]+This PR changes nothing users see'; then + echo "PR proof: the box 'changes nothing users see' is ticked. Pass." + exit 0 +fi +if printf '%s\n' "$body" | grep -Eiq '!\[[^]]*\]\([^)]+\)|]|]|user-attachments/assets/'; then + echo "PR proof: the description has an image or video. Pass." + exit 0 +fi +if [ "$proof_in_diff" -eq 1 ]; then + echo "PR proof: the diff adds proof under docs/verification/. Pass." + exit 0 +fi + +echo "PR proof: FAILED. This PR changes files that look like UI, and shows no proof." +echo +echo "Changed files that look like UI:" +while IFS= read -r ui_path; do + [ -n "$ui_path" ] && printf ' - %q\n' "$ui_path" +done </ in this PR." +echo " 3. If users see no change (a refactor, a rename), tick the box" +echo " '- [x] This PR changes nothing users see' in the description." +echo +echo "The check runs again when you edit the description or push. See the UI proof section of CONTRIBUTING.md." +exit 1 diff --git a/scripts/test-pr-proof-check.sh b/scripts/test-pr-proof-check.sh new file mode 100755 index 00000000..d0b1bb2d --- /dev/null +++ b/scripts/test-pr-proof-check.sh @@ -0,0 +1,170 @@ +#!/bin/bash +# scripts/test-pr-proof-check.sh : offline tests for scripts/pr-proof-check.sh. +# +# bash scripts/test-pr-proof-check.sh +# +# No network, no gh. Every case writes a body file and a changed-file list into a throwaway +# directory and checks the exit code and the message. The directory is left in $TMPDIR for the +# OS to clean. + +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +SCRIPT="$ROOT/scripts/pr-proof-check.sh" +WORK="$(mktemp -d "${TMPDIR:-/tmp}/hb-pr-proof-test.XXXXXX")" + +pass=0; fail=0; failed_names="" + +UI_SWIFT="hidden/Features/Settings/SettingsViews.swift" +STRINGS="hidden/en.lproj/Localizable.strings" +NON_UI="HiddenTests/NoHideNoticeTests.swift" +BODY_EMPTY="## What and why + +Adds a thing. + +## UI proof + +- [ ] This PR changes something users see, and the proof is above +- [ ] This PR changes nothing users see +" + +# body_with MARK_A MARK_B : the template with the two boxes marked (a space, x or X) +body_with() { + printf '## What and why\n\nAdds a thing.\n\n## UI proof\n\n- [%s] This PR changes something users see, and the proof is above\n- [%s] This PR changes nothing users see\n' "$1" "$2" +} + +# run_check BODY FILES... : write both inputs, run the script; sets RC and OUT +run_check() { + local body="$1"; shift + printf '%s\n' "$body" > "$WORK/body.md" + : > "$WORK/files.txt" + local f + for f in "$@"; do printf '%s\n' "$f" >> "$WORK/files.txt"; done + OUT="$(bash "$SCRIPT" --body "$WORK/body.md" --files "$WORK/files.txt" ${EXTRA[@]+"${EXTRA[@]}"} 2>&1)" + RC=$? +} + +check() { # description, then a command + local desc="$1"; shift + if "$@" >/dev/null 2>&1; then + pass=$((pass + 1)) + else + fail=$((fail + 1)); failed_names="$failed_names [$desc]" + echo " FAIL: $desc" + fi +} +rc_is() { [ "$RC" -eq "$1" ]; } +out_has() { printf '%s\n' "$OUT" | grep -Fq -- "$1"; } + +EXTRA=() + +echo "== no UI files" +run_check "$BODY_EMPTY" "$NON_UI" "docs/MANUAL.md" "scripts/test.sh" +check "tests and docs only pass" rc_is 0 +run_check "$BODY_EMPTY" "HiddenTests/LocaleCoverageTests.swift" "hidden/Common/LanguagePreferences.swift" +check "non-UI Swift passes" rc_is 0 + +echo "== UI file, no proof" +run_check "$BODY_EMPTY" "$UI_SWIFT" "$NON_UI" +check "fails" rc_is 1 +check "names the UI file" out_has "$UI_SWIFT" +check "does not name the non-UI file" test -z "$(printf '%s\n' "$OUT" | grep -F "$NON_UI")" +check "points at CONTRIBUTING.md" out_has "CONTRIBUTING.md" + +echo "== the three ways" +run_check "$(body_with ' ' x)" "$UI_SWIFT" +check "ticked 'changes nothing users see' passes" rc_is 0 +run_check "$(body_with ' ' X)" "$UI_SWIFT" +check "capital X passes" rc_is 0 +run_check "$BODY_EMPTY +![light](https://github.com/consolelabs/hidden/blob/abc/docs/verification/x/a.png?raw=true)" "$UI_SWIFT" +check "markdown image passes" rc_is 0 +run_check "$BODY_EMPTY +" "$UI_SWIFT" +check "img tag passes" rc_is 0 +run_check "$BODY_EMPTY +" "$UI_SWIFT" +check "video tag passes" rc_is 0 +run_check "$BODY_EMPTY +https://github.com/user-attachments/assets/1234abcd-0000-0000-0000-000000000000" "$UI_SWIFT" +check "user-attachments URL passes" rc_is 0 +run_check "$BODY_EMPTY" "$UI_SWIFT" "docs/verification/lang-picker/settings-dark.png" +check "PNG under docs/verification passes" rc_is 0 +run_check "$BODY_EMPTY" "$UI_SWIFT" "docs/verification/lang-picker/Settings-Dark.PNG" +check "upper-case .PNG under docs/verification passes" rc_is 0 +run_check "$(body_with ' ' x | sed $'s/$/\r/')" "$UI_SWIFT" +check "CRLF body with the ticked opt-out passes" rc_is 0 + +echo "== things that must not count" +run_check "$(body_with x ' ')" "$UI_SWIFT" +check "ticking 'changes something users see' alone fails" rc_is 1 +run_check "$BODY_EMPTY" "$UI_SWIFT" "docs/verification/memory-soak-heartbeat.md" +check "a text file under docs/verification fails" rc_is 1 +run_check "$BODY_EMPTY" "$UI_SWIFT" "img/screenshot.png" +check "a PNG outside docs/verification fails" rc_is 1 +run_check "See [the link](https://example.com/shot) for the screenshot." "$UI_SWIFT" +check "a plain link is not an image" rc_is 1 +run_check "" "$UI_SWIFT" +check "an empty body fails" rc_is 1 + +echo "== strings and assets count as UI" +run_check "$BODY_EMPTY" "$STRINGS" +check "strings-only change fails" rc_is 1 +check "strings-only names the file" out_has "$STRINGS" +run_check "$BODY_EMPTY" "hidden/Assets.xcassets/AppIcon.appiconset/Contents.json" +check "xcassets change fails" rc_is 1 +run_check "$BODY_EMPTY" "hidden/Base.lproj/Main.storyboard" +check "storyboard change fails" rc_is 1 +run_check "$BODY_EMPTY" "hidden/Features/Palette/PaletteContentView.swift" +check "Palette Swift file counts as UI" rc_is 1 +run_check "$BODY_EMPTY" "hidden/Features/Reveal/RevealSession.swift" +check "other Features Swift file is not UI" rc_is 0 +run_check "$BODY_EMPTY" "hidden/Features/Settings/My View.swift" +check "a UI path with a space is read whole" rc_is 1 +check "a UI path with a space is named escaped" out_has 'My\ View.swift' +run_check "$BODY_EMPTY" "Hidden Bar.xcodeproj/project.pbxproj" +check "a non-UI path with spaces passes" rc_is 0 +run_check "$BODY_EMPTY" "hidden/Base.lproj/Main.xib" +check "xib change fails" rc_is 1 +run_check "$BODY_EMPTY" "::error::fake.storyboard" +check "a path that looks like a workflow command is not printed at line start" test -z "$(printf '%s\n' "$OUT" | grep -E '^::')" + +echo "== bots and other bases" +EXTRA=(--author "foo[bot]") +run_check "$BODY_EMPTY" "$UI_SWIFT" +check "bot login passes" rc_is 0 +EXTRA=(--bot) +run_check "$BODY_EMPTY" "$UI_SWIFT" +check "--bot passes" rc_is 0 +EXTRA=(--base release/v1.13) +run_check "$BODY_EMPTY" "$UI_SWIFT" +check "base other than develop passes" rc_is 0 +EXTRA=(--base develop --author tieubao) +run_check "$BODY_EMPTY" "$UI_SWIFT" +check "human author on develop is checked" rc_is 1 +EXTRA=(--author dependabot-fan) +run_check "$BODY_EMPTY" "$UI_SWIFT" +check "a human login that starts with dependabot is checked" rc_is 1 +EXTRA=(--author renovate-fan) +run_check "$BODY_EMPTY" "$UI_SWIFT" +check "a human login that starts with renovate is checked" rc_is 1 +EXTRA=(--author app/dependabot) +run_check "$BODY_EMPTY" "$UI_SWIFT" +check "an app/ login passes" rc_is 0 +EXTRA=() + +echo "== usage" +EXTRA=() +bash "$SCRIPT" --body "$WORK/nope" --files "$WORK/nope" >/dev/null 2>&1; RC=$? +check "missing input files exit 2" rc_is 2 +bash "$SCRIPT" --body >/dev/null 2>&1 & +hang_pid=$!; waited=0 +while kill -0 "$hang_pid" 2>/dev/null && [ "$waited" -lt 20 ]; do sleep 0.25; waited=$((waited + 1)); done +if kill -0 "$hang_pid" 2>/dev/null; then kill "$hang_pid" 2>/dev/null; RC=124; else wait "$hang_pid"; RC=$?; fi +check "a flag with no value exits 2, not a hang" rc_is 2 + +echo +echo "passed checks: $pass failed checks: $fail" +if [ "$fail" -ne 0 ]; then + echo "FAILED:$failed_names" + exit 1 +fi +echo "ALL GREEN"