|
| 1 | +#!/usr/bin/env bash |
| 2 | +# tests/test-doc-links.sh |
| 3 | +# Automated link-check script that scans README.md and the current INSTALL.md |
| 4 | +# (currently plugins/dso/docs/INSTALL.md; task 24a0-4c55 will migrate to root INSTALL.md |
| 5 | +# once task 93d2-cd41 creates it and task fe56-8919 removes the old path). |
| 6 | +# for broken hyperlinks (markdown links and bare URLs). |
| 7 | +# |
| 8 | +# Checks: |
| 9 | +# - External URLs (http/https): HEAD request, accept HTTP < 400; fail on 5xx or connection error |
| 10 | +# - Internal relative paths: file must exist in repo |
| 11 | +# - Anchor fragments (#section): target file must contain a matching heading |
| 12 | +# |
| 13 | +# OPT-OUT LIST — add volatile/rate-limited URLs here (one per line, no trailing slash variation): |
| 14 | +# Format: OPT_OUT_URLS+=("https://example.com/volatile-url") |
| 15 | +# ---- BEGIN OPT-OUT ---- |
| 16 | +OPT_OUT_URLS=( |
| 17 | + # Returns 403 on HEAD requests (CDN/WAF blocks non-browser requests); URL is valid |
| 18 | + "https://acli.atlassian.com" |
| 19 | +) |
| 20 | +# ---- END OPT-OUT ---- |
| 21 | + |
| 22 | +set -euo pipefail |
| 23 | + |
| 24 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 25 | +REPO_ROOT="$(git -C "$SCRIPT_DIR" rev-parse --show-toplevel)" |
| 26 | + |
| 27 | +FAILURES=0 |
| 28 | +TESTS=0 |
| 29 | + |
| 30 | +pass() { TESTS=$((TESTS + 1)); echo " PASS: $1"; } |
| 31 | +fail() { TESTS=$((TESTS + 1)); FAILURES=$((FAILURES + 1)); echo " FAIL: $1"; } |
| 32 | + |
| 33 | +# Files to scan (relative to REPO_ROOT) |
| 34 | +SCAN_FILES=( |
| 35 | + "README.md" |
| 36 | + "plugins/dso/docs/INSTALL.md" |
| 37 | +) |
| 38 | + |
| 39 | +# Check if a URL is in the opt-out list |
| 40 | +_is_opted_out() { |
| 41 | + local url="$1" |
| 42 | + for opt_url in "${OPT_OUT_URLS[@]:-}"; do |
| 43 | + if [[ "$url" == "$opt_url" ]]; then |
| 44 | + return 0 |
| 45 | + fi |
| 46 | + done |
| 47 | + return 1 |
| 48 | +} |
| 49 | + |
| 50 | +# Extract markdown links [text](url) and bare http/https URLs from a file |
| 51 | +_extract_links() { |
| 52 | + local file="$1" |
| 53 | + # Markdown links: [text](url) — capture the URL part |
| 54 | + grep -oE '\[([^]]*)\]\(([^)]+)\)' "$file" | sed 's/\[.*\](\(.*\))/\1/' || true |
| 55 | + # Bare URLs not already in markdown link syntax |
| 56 | + # Exclude chars that typically end a URL: ) space > " ` | , ; backtick |
| 57 | + grep -oE '(^|[^(])(https?://[^)[:space:]>"`|,;]+)' "$file" | grep -oE 'https?://[^)[:space:]>"`|,;]+' || true |
| 58 | +} |
| 59 | + |
| 60 | +# Check an external URL via HEAD request |
| 61 | +_check_external_url() { |
| 62 | + local url="$1" |
| 63 | + # Strip trailing punctuation that may have been captured |
| 64 | + url="${url%[.,;:\"\'!]}" |
| 65 | + |
| 66 | + if _is_opted_out "$url"; then |
| 67 | + echo " SKIP (opt-out): $url" |
| 68 | + return 0 |
| 69 | + fi |
| 70 | + |
| 71 | + local http_code |
| 72 | + http_code=$(curl -s -o /dev/null -w "%{http_code}" -I --max-time 10 --location "$url" 2>/dev/null || echo "000") |
| 73 | + # Normalize: strip non-numeric chars (handles edge cases where curl appends extra output) |
| 74 | + http_code=$(echo "$http_code" | grep -oE '^[0-9]+' | head -1 || echo "0") |
| 75 | + http_code="${http_code:-0}" |
| 76 | + |
| 77 | + if [[ "$http_code" -le 0 ]]; then |
| 78 | + fail "Connection error for external URL: $url" |
| 79 | + return 1 |
| 80 | + elif [[ "$http_code" -ge 500 ]]; then |
| 81 | + fail "Server error ($http_code) for external URL: $url" |
| 82 | + return 1 |
| 83 | + elif [[ "$http_code" -ge 400 ]]; then |
| 84 | + fail "Client error ($http_code) for external URL: $url" |
| 85 | + return 1 |
| 86 | + else |
| 87 | + pass "External URL OK ($http_code): $url" |
| 88 | + return 0 |
| 89 | + fi |
| 90 | +} |
| 91 | + |
| 92 | +# Check an internal path (optionally with #anchor) |
| 93 | +_check_internal_path() { |
| 94 | + local raw_path="$1" |
| 95 | + local base_file="$2" |
| 96 | + |
| 97 | + # Split on '#' for anchor |
| 98 | + local path_part anchor_part |
| 99 | + path_part="${raw_path%%#*}" |
| 100 | + if [[ "$raw_path" == *#* ]]; then |
| 101 | + anchor_part="${raw_path#*#}" |
| 102 | + else |
| 103 | + anchor_part="" |
| 104 | + fi |
| 105 | + |
| 106 | + # Resolve path relative to the file's directory, then to repo root |
| 107 | + local resolved |
| 108 | + if [[ -z "$path_part" ]]; then |
| 109 | + # Pure anchor — check within the same file |
| 110 | + resolved="$base_file" |
| 111 | + elif [[ "$path_part" == /* ]]; then |
| 112 | + # Absolute path from repo root |
| 113 | + resolved="$REPO_ROOT$path_part" |
| 114 | + else |
| 115 | + # Relative to the file's directory |
| 116 | + resolved="$(dirname "$base_file")/$path_part" |
| 117 | + fi |
| 118 | + |
| 119 | + # Normalize (remove /./ and /../ sequences) |
| 120 | + if command -v realpath >/dev/null 2>&1; then |
| 121 | + resolved="$(realpath -m "$resolved" 2>/dev/null || echo "$resolved")" |
| 122 | + fi |
| 123 | + |
| 124 | + # Check file existence (skip if path_part is empty — pure anchor on same file) |
| 125 | + if [[ -n "$path_part" ]]; then |
| 126 | + if [[ ! -e "$resolved" ]]; then |
| 127 | + fail "Internal path not found: $raw_path (resolved: $resolved) [in $base_file]" |
| 128 | + return 1 |
| 129 | + fi |
| 130 | + fi |
| 131 | + |
| 132 | + # Check anchor if present |
| 133 | + if [[ -n "$anchor_part" ]]; then |
| 134 | + if [[ ! -f "$resolved" ]]; then |
| 135 | + fail "Anchor target file not found for #$anchor_part: $resolved [in $base_file]" |
| 136 | + return 1 |
| 137 | + fi |
| 138 | + # GitHub-style anchor: lowercase, spaces→dashes, strip punctuation |
| 139 | + local normalized_anchor |
| 140 | + normalized_anchor=$(echo "$anchor_part" | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed 's/[^a-z0-9-]//g' | sed 's/--*/-/g' | sed 's/^-//; s/-$//' || true) |
| 141 | + |
| 142 | + # Search for heading in the target file (# Heading, ## Heading, etc.) |
| 143 | + local found=false |
| 144 | + while IFS= read -r heading_line; do |
| 145 | + # Strip leading # characters and spaces |
| 146 | + local heading_text |
| 147 | + heading_text=$(echo "$heading_line" | sed 's/^#* *//' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed 's/[^a-z0-9-]//g' | sed 's/--*/-/g' | sed 's/^-//; s/-$//' || true) |
| 148 | + if [[ "$heading_text" == "$normalized_anchor" ]]; then |
| 149 | + found=true |
| 150 | + break |
| 151 | + fi |
| 152 | + done < <(grep -E '^#{1,6} ' "$resolved" || true) |
| 153 | + |
| 154 | + if ! $found; then |
| 155 | + fail "Anchor #$anchor_part not found in $resolved [in $base_file]" |
| 156 | + return 1 |
| 157 | + fi |
| 158 | + fi |
| 159 | + |
| 160 | + pass "Internal path OK: $raw_path [in $base_file]" |
| 161 | + return 0 |
| 162 | +} |
| 163 | + |
| 164 | +echo "=== Doc Link Checker ===" |
| 165 | +echo "Scanning: ${SCAN_FILES[*]}" |
| 166 | +echo "" |
| 167 | + |
| 168 | +# Deduplicate URLs to avoid redundant external checks |
| 169 | +declare -A seen_external=() |
| 170 | + |
| 171 | +for rel_file in "${SCAN_FILES[@]}"; do |
| 172 | + full_path="$REPO_ROOT/$rel_file" |
| 173 | + |
| 174 | + if [[ ! -f "$full_path" ]]; then |
| 175 | + fail "Source file not found: $rel_file" |
| 176 | + continue |
| 177 | + fi |
| 178 | + |
| 179 | + echo "--- Checking links in: $rel_file ---" |
| 180 | + |
| 181 | + # Collect all links from the file |
| 182 | + links=$(_extract_links "$full_path") |
| 183 | + |
| 184 | + if [[ -z "$links" ]]; then |
| 185 | + echo " (no links found)" |
| 186 | + continue |
| 187 | + fi |
| 188 | + |
| 189 | + while IFS= read -r link; do |
| 190 | + [[ -z "$link" ]] && continue |
| 191 | + |
| 192 | + # Strip trailing punctuation |
| 193 | + link="${link%[.,;:\"\'!]}" |
| 194 | + [[ -z "$link" ]] && continue |
| 195 | + |
| 196 | + if [[ "$link" == http://* ]] || [[ "$link" == https://* ]]; then |
| 197 | + # External URL |
| 198 | + if [[ -z "${seen_external[$link]+_}" ]]; then |
| 199 | + seen_external[$link]=1 |
| 200 | + _check_external_url "$link" || true |
| 201 | + sleep 1 |
| 202 | + else |
| 203 | + echo " DEDUP (already checked): $link" |
| 204 | + fi |
| 205 | + elif [[ "$link" == mailto:* ]]; then |
| 206 | + # Skip mailto links |
| 207 | + echo " SKIP (mailto): $link" |
| 208 | + elif [[ "$link" == "#"* ]]; then |
| 209 | + # Pure anchor on the same file |
| 210 | + _check_internal_path "$link" "$full_path" || true |
| 211 | + else |
| 212 | + # Internal relative path (with or without anchor) |
| 213 | + _check_internal_path "$link" "$full_path" || true |
| 214 | + fi |
| 215 | + done <<< "$links" |
| 216 | + |
| 217 | + echo "" |
| 218 | +done |
| 219 | + |
| 220 | +echo "=== Results: $((TESTS - FAILURES))/$TESTS passed ===" |
| 221 | +if (( FAILURES > 0 )); then |
| 222 | + echo "FAILED: $FAILURES link(s) broken" |
| 223 | + exit 1 |
| 224 | +else |
| 225 | + echo "All links OK." |
| 226 | + exit 0 |
| 227 | +fi |
0 commit comments