|
| 1 | +# Cleanup site state: remove pr-preview directories for closed PRs. |
| 2 | +# Keeps only directories for currently open PRs so saved state matches reality. |
| 3 | +# This action keeps the existing pages only for the actually opened PRs to avoid restoring |
| 4 | +# the pages of closed PRs. |
| 5 | + |
| 6 | +name: 'Cleanup site state' |
| 7 | +description: 'Remove PR preview directories for closed PRs; keep only open PRs in site state' |
| 8 | + |
| 9 | +inputs: |
| 10 | + site_dir: |
| 11 | + description: 'Path to the site directory (e.g. _site) containing pr-preview subdirs' |
| 12 | + required: true |
| 13 | + |
| 14 | +runs: |
| 15 | + using: 'composite' |
| 16 | + steps: |
| 17 | + - name: Collect list of open PRs |
| 18 | + id: open-prs |
| 19 | + shell: bash |
| 20 | + env: |
| 21 | + GH_TOKEN: ${{ github.token }} |
| 22 | + run: | |
| 23 | + set -e |
| 24 | + |
| 25 | + # Build JSON array of open PR numbers for use in next step. |
| 26 | + if ! OPEN_PRS_JSON=$(gh pr list --state open --json number -q '.[].number' --limit 100 | jq -R -s -c '[split("\n")[] | select(length > 0) | tonumber]'); then |
| 27 | + echo "Error: Failed to list open PRs" >&2 |
| 28 | + exit 1 |
| 29 | + fi |
| 30 | + |
| 31 | + # Validate the output is valid JSON array |
| 32 | + if ! echo "$OPEN_PRS_JSON" | jq -e 'type == "array"' >/dev/null 2>&1; then |
| 33 | + echo "Error: Invalid JSON output from PR list" >&2 |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | + |
| 37 | + echo "open_prs_json=$OPEN_PRS_JSON" >> "$GITHUB_OUTPUT" |
| 38 | + echo "Found $(echo "$OPEN_PRS_JSON" | jq 'length') open PR(s)" |
| 39 | +
|
| 40 | + - name: Remove closed PR preview directories |
| 41 | + shell: bash |
| 42 | + env: |
| 43 | + SITE_DIR: ${{ inputs.site_dir }} |
| 44 | + OPEN_PRS_JSON: ${{ steps.open-prs.outputs.open_prs_json }} |
| 45 | + run: | |
| 46 | + set -e |
| 47 | + |
| 48 | + # Validate site_dir input |
| 49 | + if [ -z "$SITE_DIR" ]; then |
| 50 | + echo "Error: site_dir is empty" >&2 |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + |
| 54 | + # Reject paths with path traversal, absolute paths, or control characters |
| 55 | + if [[ "$SITE_DIR" == *".."* ]] || \ |
| 56 | + [[ "$SITE_DIR" == /* ]] || \ |
| 57 | + [[ "$SITE_DIR" == *$'\n'* ]] || \ |
| 58 | + [[ "$SITE_DIR" == *$'\r'* ]] || \ |
| 59 | + [[ "$SITE_DIR" == *$'\t'* ]]; then |
| 60 | + echo "Error: Invalid site_dir (path traversal or absolute path)" >&2 |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + |
| 64 | + # Get absolute path of workspace root for boundary checking |
| 65 | + WORKSPACE_ROOT=$(cd "$GITHUB_WORKSPACE" && pwd) |
| 66 | + |
| 67 | + # Resolve and validate site_dir path |
| 68 | + if [ ! -d "$SITE_DIR" ]; then |
| 69 | + echo "Info: site_dir does not exist: $SITE_DIR (first run or no previous state)" >&2 |
| 70 | + exit 0 |
| 71 | + fi |
| 72 | + |
| 73 | + RESOLVED_SITE_DIR=$(cd "$SITE_DIR" && pwd) |
| 74 | + |
| 75 | + # Ensure resolved path is within workspace |
| 76 | + if [[ "$RESOLVED_SITE_DIR" != "$WORKSPACE_ROOT"* ]]; then |
| 77 | + echo "Error: site_dir resolves outside workspace" >&2 |
| 78 | + exit 1 |
| 79 | + fi |
| 80 | + |
| 81 | + PR_PREVIEW="${RESOLVED_SITE_DIR}/pr-preview" |
| 82 | + |
| 83 | + # Validate PR_PREVIEW path is still within workspace |
| 84 | + if [[ "$PR_PREVIEW" != "$WORKSPACE_ROOT"* ]]; then |
| 85 | + echo "Error: PR_PREVIEW path escapes workspace" >&2 |
| 86 | + exit 1 |
| 87 | + fi |
| 88 | +
|
| 89 | + # If there is no previews, just exit |
| 90 | + if [ ! -d "$PR_PREVIEW" ]; then |
| 91 | + exit 0 |
| 92 | + fi |
| 93 | +
|
| 94 | + # Process each pr-* directory |
| 95 | + for dir in "$PR_PREVIEW"/pr-*; do |
| 96 | + RESOLVED_DIR=$(cd "$dir" && pwd) |
| 97 | +
|
| 98 | + # Skip if glob didn't match anything, files, symlinks, etc. |
| 99 | + if [ ! -e "$dir" ] || [ ! -d "$dir" ] || [[ "$RESOLVED_DIR" != "$WORKSPACE_ROOT"* ]] || [[ "$RESOLVED_DIR" != "$PR_PREVIEW"* ]]; then |
| 100 | + continue |
| 101 | + fi |
| 102 | + name=$(basename "$dir") |
| 103 | + |
| 104 | + # Validate directory name matches expected pattern (pr- followed by digits only) |
| 105 | + if [[ "$name" =~ ^pr-([0-9]+)$ ]]; then |
| 106 | + pr_num="${BASH_REMATCH[1]}" |
| 107 | + |
| 108 | + # Validate pr_num is numeric (extra safety) |
| 109 | + if ! [[ "$pr_num" =~ ^[0-9]+$ ]]; then |
| 110 | + echo "Warning: Invalid PR number extracted: $pr_num" >&2 |
| 111 | + continue |
| 112 | + fi |
| 113 | + |
| 114 | + # Validate OPEN_PRS_JSON is valid before using it |
| 115 | + if ! echo "$OPEN_PRS_JSON" | jq -e 'type == "array"' >/dev/null 2>&1; then |
| 116 | + echo "Error: Invalid OPEN_PRS_JSON format" >&2 |
| 117 | + exit 1 |
| 118 | + fi |
| 119 | + |
| 120 | + # Check if PR is still open |
| 121 | + if ! echo "$OPEN_PRS_JSON" | jq -e --argjson n "$pr_num" 'index($n) != null' >/dev/null 2>&1; then |
| 122 | + rm -rf "$dir" |
| 123 | + fi |
| 124 | + fi |
| 125 | + done |
| 126 | + |
| 127 | + # Clean up empty pr-preview directory |
| 128 | + if [ -d "$PR_PREVIEW" ] && [ -z "$(ls -A "$PR_PREVIEW" 2>/dev/null)" ]; then |
| 129 | + rm -rf "$PR_PREVIEW" |
| 130 | + fi |
0 commit comments