Skip to content

[GitHub] Unused partials check #12

[GitHub] Unused partials check

[GitHub] Unused partials check #12

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
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 "s" flag allows the dot to match newlines, and the "e" flag executes the script.
# The -0777 option makes Perl read the entire file at once.
# The (?s) is a modifier to allow '.' to match newline characters
# This makes the regex a single-line regex, even if the text spans multiple lines.
if ! perl -ne "exit 0 if /\<Render.*?(?:\s|$SEARCH_PRODUCT).*?(?:\s|$SEARCH_FILE).*?\/?\>/s" -e "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