-
Notifications
You must be signed in to change notification settings - Fork 7
Add redirect checker to block PRs with deleted pages missing redirects #666
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
base: main
Are you sure you want to change the base?
Changes from all commits
35b2215
bd52808
f707d4d
a03941d
4142993
ea2aa5c
cc59b74
16b8857
ad175d0
abf1d32
5026119
c538c65
3a84d1e
0ceb073
3da3841
605f962
6c4fa7a
eb2474b
7f47622
dc8b336
5af81ee
180a4cd
3fb1cc1
b293622
7aefcda
1389802
2ff5a42
59b36c8
f552b84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| name: Check Redirects for Deleted Pages | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: [opened, synchronize, reopened] | ||
| paths: | ||
| - "app/**/*.md" | ||
| - "app/**/*.mdx" | ||
| - "next.config.ts" | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| check-redirects: | ||
| name: Verify Deleted Pages Have Redirects | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 # Full history needed for branch comparison | ||
|
|
||
| - name: Fetch base branch | ||
| run: git fetch origin ${{ github.base_ref }}:${{ github.base_ref }} | ||
|
|
||
| - name: Setup Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: "20" | ||
|
|
||
| - name: Install dependencies | ||
| run: npm install -g pnpm && pnpm install | ||
|
|
||
| - name: Check for missing redirects | ||
| id: check | ||
| run: | | ||
| set -o pipefail | ||
| pnpm check-redirects ${{ github.base_ref }} 2>&1 | tee redirect-check-output.txt | ||
| continue-on-error: true | ||
|
|
||
| - name: Comment on PR if redirects are missing | ||
| if: steps.check.outcome == 'failure' | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const fs = require('fs'); | ||
| const output = fs.readFileSync('redirect-check-output.txt', 'utf8'); | ||
|
|
||
| // Extract the missing redirects and suggestions from output | ||
| const body = `## 🔗 Missing Redirects Detected | ||
|
|
||
| This PR deletes markdown files that don't have corresponding redirects in \`next.config.ts\`. | ||
|
|
||
| When you delete a page, you must add a redirect to prevent broken links for users who have bookmarked the old URL. | ||
|
|
||
| <details> | ||
| <summary>📋 View Details</summary> | ||
|
|
||
| \`\`\` | ||
| ${output} | ||
| \`\`\` | ||
|
|
||
| </details> | ||
|
|
||
| ### How to fix | ||
|
|
||
| 1. Open \`next.config.ts\` | ||
| 2. Find the \`redirects()\` function | ||
| 3. Add redirect entries for each deleted file (see suggestions above) | ||
| 4. Push the changes | ||
|
|
||
| --- | ||
| *This check ensures we maintain URL stability for our documentation.*`; | ||
|
|
||
| // Check if we already commented | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| }); | ||
|
|
||
| const botComment = comments.find(comment => | ||
| comment.user.type === 'Bot' && | ||
| comment.body.includes('Missing Redirects Detected') | ||
| ); | ||
|
|
||
| if (botComment) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: botComment.id, | ||
| body: body | ||
| }); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body: body | ||
| }); | ||
| } | ||
|
|
||
| - name: Remove outdated comment if check passes | ||
| if: steps.check.outcome == 'success' | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| }); | ||
|
|
||
| const botComment = comments.find(comment => | ||
| comment.user.type === 'Bot' && | ||
| comment.body.includes('Missing Redirects Detected') | ||
| ); | ||
|
|
||
| if (botComment) { | ||
| await github.rest.issues.deleteComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: botComment.id, | ||
| }); | ||
| } | ||
|
|
||
| - name: Fail if redirects are missing | ||
| if: steps.check.outcome == 'failure' | ||
| run: | | ||
| echo "❌ Missing redirects for deleted pages. See PR comment for details." | ||
| exit 1 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,51 @@ if [ -n "$STAGED_DOCS" ]; then | |
| fi | ||
| fi | ||
|
|
||
| # --- Check Redirects (when markdown pages are deleted or renamed) --- | ||
| # Detect deleted pages (D status) and renamed pages (R status - old path needs redirect) | ||
| DELETED_PAGES=$(git diff --cached --name-status | grep -E "^D.*page\.(md|mdx)$" | cut -f2 || true) | ||
| RENAMED_PAGES=$(git diff --cached --name-status | grep -E "^R.*page\.(md|mdx)$" | cut -f2 || true) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-commit hook regex checks wrong path for renamesMedium Severity The regex pattern |
||
|
|
||
| if [ -n "$DELETED_PAGES" ] || [ -n "$RENAMED_PAGES" ]; then | ||
| echo "🔗 Detected deleted/renamed page(s), checking for redirects..." | ||
|
|
||
| # Run the TypeScript redirect checker with auto-fix (only checks staged changes) | ||
| # This will add redirect entries to next.config.ts if missing | ||
| if ! pnpm check-redirects --auto-fix --staged-only 2>&1; then | ||
| # Stage next.config.ts if it was modified | ||
| if git diff --name-only next.config.ts 2>/dev/null | grep -q "next.config.ts"; then | ||
| git add next.config.ts | ||
| echo "" | ||
| echo "📝 Redirect entries added to next.config.ts and staged." | ||
| fi | ||
| echo "" | ||
| # Check if there are placeholders vs other errors | ||
| if grep -q "REPLACE_WITH_NEW_PATH" next.config.ts 2>/dev/null; then | ||
| echo "❌ Commit blocked: Please update the placeholder destinations in next.config.ts" | ||
| echo " Search for 'REPLACE_WITH_NEW_PATH' and provide actual redirect paths." | ||
| else | ||
| echo "❌ Commit blocked: Please fix the redirect issues shown above." | ||
| fi | ||
| exit 1 | ||
| fi | ||
| fi | ||
|
|
||
| # --- Update Internal Links (when redirects are added) --- | ||
| # If next.config.ts is staged, update any internal links pointing to redirected paths | ||
| if git diff --cached --name-only | grep -q "next.config.ts"; then | ||
| echo "🔗 Updating internal links for new redirects..." | ||
|
|
||
| # Run the TypeScript update script | ||
| if pnpm update-links 2>/dev/null; then | ||
| # Stage any files that were modified | ||
| UPDATED_FILES=$(git diff --name-only -- 'app/**/*.mdx' 'app/**/*.tsx' 'app/**/*.md' 2>/dev/null || true) | ||
| if [ -n "$UPDATED_FILES" ]; then | ||
| echo "$UPDATED_FILES" | xargs git add | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-commit hook stages unintended working directory changesMedium Severity After running |
||
| echo "✅ Internal links updated and staged" | ||
| fi | ||
| fi | ||
| fi | ||
|
|
||
| # --- Lint Staged (formatting) --- | ||
| pnpm dlx lint-staged | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.