Skip to content

Add github action to verify if a redirect was added #3417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions .github/scripts/check_redirects.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

if [ "$CURRENT_BRANCH" == "$BASE_BRANCH" ]; then
echo "Running on $BASE_BRANCH branch. Skipping check."
exit 0
fi


# Get list of deleted or renamed files in this branch compared to base
DELETED_FILES=$(git diff --name-status $BASE_BRANCH $CURRENT_BRANCH --diff-filter=DR | awk '{print $2}' | grep -E '\.(rst|py|md)$' | grep -v 'redirects.py')
# Check if any deleted or renamed files were found
if [ -z "$DELETED_FILES" ]; then
echo "No deleted or renamed files found. Skipping check."
exit 0
fi

echo "Deleted or renamed files:"
echo "$DELETED_FILES"

# Check if redirects.py has been updated
REDIRECTS_UPDATED=$(git diff --name-status $BASE_BRANCH $CURRENT_BRANCH --diff-filter=AM | grep 'redirects.py' && echo "yes" || echo "no")

if [ "$REDIRECTS_UPDATED" == "no" ]; then
echo "ERROR: Files were deleted or renamed but redirects.py was not updated. Please update .github/scripts/redirects.py to redirect these files."
exit 1
fi

# Check if each deleted file has a redirect entry
MISSING_REDIRECTS=0
for FILE in $DELETED_FILES; do
# Convert file path to URL path format (remove extension and adjust path)
REDIRECT_PATH=$(echo $FILE | sed -E 's/(.+)_source\/(.+)\.(py|rst|md)$/\1\/\2.html/')

# Check if this path exists in redirects.py as a key. We don't check for values.
if ! grep -q "\"$REDIRECT_PATH\":" redirects.py; then
echo "ERROR: Missing redirect for deleted file: $FILE (should have entry for \"$REDIRECT_PATH\")"
MISSING_REDIRECTS=1
fi
done

if [ $MISSING_REDIRECTS -eq 1 ]; then
echo "ERROR: Please add redirects for all deleted/renamed files to redirects.py"
exit 1
fi

echo "All deleted/renamed files have proper redirects. Check passed!"
25 changes: 25 additions & 0 deletions .github/workflows/check-redirects.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Check Redirects for Deleted or Renamed Files

on:
pull_request:
paths:
- '*/**/*.rst'
- '*/**/*.py'
- '*/**/*.md'

jobs:
check-redirects:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Run redirect check script
run: |
chmod +x ./.github/scripts/check_redirects.sh
./.github/scripts/check_redirects.sh
env:
BASE_BRANCH: ${{ github.base_ref }}
CURRENT_BRANCH: ${{ github.head_ref }}