Skip to content

[GitHub] Unused partials check #13

[GitHub] Unused partials check

[GitHub] Unused partials check #13

Workflow file for this run

name: Partial audit
# **What it does**: Regularly audits our documentation for unused image files.
# **Why we have it**: Good hygiene + helps simplify future image audits.
# **Who does it impact**: PCX team
on:
pull_request:
branches:
- production
workflow_dispatch:
schedule:
- cron: "0 0 1 * *"
jobs:
partial-audit:
permissions:
issues: write
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Find Unused Files
id: find-files
run: |
# Find all .mdx files, but only in the ./src/content/partials directory
FILES=$(find ./src/content/partials -type f -name "*.mdx")
# Check if files are referenced in any Render component
UNUSED_FILES=""
for FILE in $FILES; do
# Extract product and file name from the path
TEMP_STRING=${FILE#*partials/}
PRODUCT=${TEMP_STRING%%/*}
TEMP_PATH=${FILE#*src/content/partials/*/}
FILENAME=${TEMP_PATH%.mdx}
# Construct the individual search strings for product and file
# We escape the quotes to ensure they are treated as part of the string.
SEARCH_PRODUCT="product=\"$PRODUCT\""
SEARCH_FILE="file=\"$FILENAME\""
# --- START LOGGING ---
echo "----------------------------------------"
echo "Checking file: $FILE"
echo " - SEARCH_PRODUCT: $SEARCH_PRODUCT"
echo " - SEARCH_FILE: $SEARCH_FILE"
# --- END LOGGING ---
# Use Perl to search for a multi-line <Render> component with both values.
# The entire logic is now in a single `-e` block, fixing the syntax error.
if ! perl -ne "exit 0 if /<Render.*?(?:\s|)$SEARCH_PRODUCT.*?(?:\s|)$SEARCH_FILE.*?\/?\>/s; exit 1" ./*/*.mdx; then
UNUSED_FILES+="$FILE\n"
echo " -> UNUSED! Adding to list."
else
echo " -> Used. Skipping."
fi
echo "" # Add a blank line for readability
done
# If there are unused files, output them
if [ -n "$UNUSED_FILES" ]; then
echo "unused_files=$UNUSED_FILES" >> "$GITHUB_OUTPUT"
fi