|
| 1 | +name: Check Redirects for Deleted Files |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - '**/*.rst' |
| 7 | + - '**/*.py' |
| 8 | + |
| 9 | +jobs: |
| 10 | + check-redirects: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v3 |
| 15 | + with: |
| 16 | + fetch-depth: 0 |
| 17 | + |
| 18 | + - name: Set up Python |
| 19 | + uses: actions/setup-python@v4 |
| 20 | + with: |
| 21 | + python-version: '3.11' |
| 22 | + |
| 23 | + - name: Check for deleted/renamed files and redirects |
| 24 | + run: | |
| 25 | + # Get list of deleted or renamed files in this PR |
| 26 | + DELETED_FILES=$(git diff --name-status ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^D\|^R' | awk '{print $2}' | grep -E '\.(rst|py|ipynb)$' | grep -v 'redirects.py') |
| 27 | + |
| 28 | + if [ -z "$DELETED_FILES" ]; then |
| 29 | + echo "No deleted or renamed files found. Skipping check." |
| 30 | + exit 0 |
| 31 | + fi |
| 32 | + |
| 33 | + echo "Deleted or renamed files:" |
| 34 | + echo "$DELETED_FILES" |
| 35 | + |
| 36 | + # Check if redirects.py has been updated |
| 37 | + REDIRECTS_UPDATED=$(git diff --name-status ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep -q '^M\|^A' | grep 'redirects.py' && echo "yes" || echo "no") |
| 38 | + |
| 39 | + if [ "$REDIRECTS_UPDATED" == "no" ]; then |
| 40 | + echo "::error::Files were deleted or renamed but redirects.py was not updated. Please add redirects for deleted/renamed files." |
| 41 | + exit 1 |
| 42 | + fi |
| 43 | + |
| 44 | + # Check if each deleted file has a redirect entry |
| 45 | + MISSING_REDIRECTS=0 |
| 46 | + for FILE in $DELETED_FILES; do |
| 47 | + # Convert file path to URL path format (remove extension and adjust path) |
| 48 | + URL_PATH=$(echo $FILE | sed 's/\.rst$//g' | sed 's/\.py$//g' | sed 's/\.ipynb$//g' | sed 's/^tutorials\///g') |
| 49 | + |
| 50 | + # Check if this path exists in redirects.py |
| 51 | + if ! grep -q "\"$URL_PATH\"" tutorials/redirects.py; then |
| 52 | + echo "::error::Missing redirect for deleted file: $FILE (URL path: $URL_PATH)" |
| 53 | + MISSING_REDIRECTS=1 |
| 54 | + fi |
| 55 | + done |
| 56 | + |
| 57 | + if [ $MISSING_REDIRECTS -eq 1 ]; then |
| 58 | + echo "::error::Please add redirects for all deleted/renamed files to redirects.py" |
| 59 | + exit 1 |
| 60 | + fi |
| 61 | + |
| 62 | + echo "All deleted/renamed files have proper redirects. Check passed!" |
| 63 | +
|
0 commit comments