Skip to content

Remove definition wrapper to test performance #1043

Remove definition wrapper to test performance

Remove definition wrapper to test performance #1043

name: Check if a deleted file needs a redirect
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
are-redirects-needed:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get deleted .mdx files
id: deleted-files
run: |
git fetch origin ${{ github.base_ref }}
deleted_files=$(git diff --name-only --diff-filter=D origin/${{ github.base_ref }}...HEAD | grep '\.mdx$' | grep -v '^snippets/' | grep -v '^custom/' || true)
echo "deleted_files<<EOF" >> $GITHUB_OUTPUT
echo "$deleted_files" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Check for redirects
run: |
deleted_files="${{ steps.deleted-files.outputs.deleted_files }}"
if [ -z "$deleted_files" ]; then
echo "No .mdx files were deleted in this PR."
exit 0
fi
echo "Checking redirects for deleted .mdx files..."
missing_redirects=()
while IFS= read -r file; do
if [ -z "$file" ]; then
continue
fi
# Remove .mdx extension
path_without_ext="${file%.mdx}"
# Check if file is mentioned in docs.json navigation object
navigation_json=$(jq -c '.navigation' docs.json)
if echo "$navigation_json" | grep -q "$path_without_ext"; then
echo "File $file is in docs.json navigation, checking for redirect..."
# Convert file path to expected redirect source
# Remove .mdx extension and add leading /
# If the file is index.mdx, remove /index from the path
if [[ "$path_without_ext" == */index ]]; then
redirect_source="/${path_without_ext%/index}"
else
redirect_source="/$path_without_ext"
fi
echo "Checking for redirect from: $redirect_source"
# Check if redirect exists in docs.json
if ! jq -e --arg source "$redirect_source" '.redirects[] | select(.source == $source)' docs.json > /dev/null; then
echo "::error file=$file::No redirect found for deleted file: $file (expected redirect source: $redirect_source)"
missing_redirects+=("$file")
else
echo "✓ Redirect found for: $file"
fi
else
echo "File $file is not in docs.json navigation, checking if it was removed from docs.json..."
# Check if docs.json was changed in this PR
git fetch origin ${{ github.base_ref }}
if git diff --name-only origin/${{ github.base_ref }}...HEAD | grep -q '^docs.json$'; then
echo "docs.json was changed, checking diff for deletions..."
# Get the diff of docs.json and check if the file path was deleted
diff_output=$(git diff origin/${{ github.base_ref }}...HEAD -- docs.json)
if echo "$diff_output" | grep -E '^\-' | grep -q "$path_without_ext"; then
echo "File $file was removed from docs.json, checking for redirect..."
# Convert file path to expected redirect source
if [[ "$path_without_ext" == */index ]]; then
redirect_source="/${path_without_ext%/index}"
else
redirect_source="/$path_without_ext"
fi
echo "Checking for redirect from: $redirect_source"
# Check if redirect exists in docs.json
if ! jq -e --arg source "$redirect_source" '.redirects[] | select(.source == $source)' docs.json > /dev/null; then
echo "::error file=$file::No redirect found for deleted file: $file (expected redirect source: $redirect_source)"
missing_redirects+=("$file")
else
echo "✓ Redirect found for: $file"
fi
else
echo "File $file was not found in docs.json deletions, skipping redirect check"
fi
else
echo "docs.json was not changed, skipping redirect check for $file"
fi
fi
done <<< "$deleted_files"
if [ ${#missing_redirects[@]} -gt 0 ]; then
echo ""
echo "❌ The following deleted files are missing redirects in docs.json:"
printf ' - %s\n' "${missing_redirects[@]}"
echo ""
echo "Please add redirects to prevent broken links."
exit 1
else
echo ""
echo "✓ All deleted .mdx files have redirects configured."
fi