-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: add CI linter to detect redirect chains and stale content links #18620
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
sfanahata
wants to merge
10
commits into
master
Choose a base branch
from
lint/redirect-chains
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7795942
feat: add CI linter to detect redirect chains and stale content links
d365c91
fix: address CI and review feedback on redirect chain linter
f496c58
[getsentry/action-github-commit] Auto commit
getsantry[bot] 523dff1
fix: use line-level PlatformLink check instead of regex lookbehind
1c380c5
fix: remove incorrect JSON parse fallback in workflow
0ff266d
fix: address all remaining review feedback
c56fcb2
fix: walk chain from entry's own destination, not unified map source
b4ee4f6
fix: paginate listComments to avoid duplicate bot comments
38a09ba
fix: apply same pagination fix to check-redirects-on-rename workflow
42da020
fix: add trailing slash normalization to Phase 1 chain detection
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| name: Lint Redirect Chains | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [master] | ||
| paths: | ||
| - 'middleware.ts' | ||
| - 'redirects.js' | ||
| - 'docs/**/*.mdx' | ||
| - 'docs/**/*.md' | ||
| - 'develop-docs/**/*.mdx' | ||
| - 'develop-docs/**/*.md' | ||
| - 'includes/**/*.mdx' | ||
| - 'includes/**/*.md' | ||
| - 'platform-includes/**/*.mdx' | ||
| - 'platform-includes/**/*.md' | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| lint-redirect-chains: | ||
| name: Check for redirect chains | ||
| runs-on: ubuntu-latest | ||
| continue-on-error: true # Fail the check but don't block merge | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
|
|
||
| steps: | ||
| - name: Checkout repository | ||
| uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6 | ||
|
|
||
| - name: Setup Node | ||
| uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v4 | ||
| with: | ||
| node-version-file: 'package.json' | ||
|
|
||
| - name: Run redirect chain lint | ||
| id: lint | ||
| continue-on-error: true | ||
| run: | | ||
| set +e | ||
| OUTPUT=$(npx tsx scripts/lint-redirect-chains.ts 2>&1) | ||
| EXIT_CODE=$? | ||
| set -e | ||
|
|
||
| echo "$OUTPUT" | ||
|
|
||
| # Extract JSON output if present | ||
| HAS_JSON=false | ||
| if echo "$OUTPUT" | grep -Fq -- "---JSON_OUTPUT---"; then | ||
| JSON_OUTPUT=$(echo "$OUTPUT" | sed -n '/---JSON_OUTPUT---/,/---JSON_OUTPUT---/p' | sed '1d;$d') | ||
| echo "lint_result<<EOF" >> $GITHUB_OUTPUT | ||
| echo "$JSON_OUTPUT" >> $GITHUB_OUTPUT | ||
| echo "EOF" >> $GITHUB_OUTPUT | ||
| HAS_JSON=true | ||
| fi | ||
|
|
||
| echo "has_results=$HAS_JSON" >> $GITHUB_OUTPUT | ||
| echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Post comment if redirect chains found | ||
| if: steps.lint.outputs.exit_code == '1' && steps.lint.outputs.has_results == 'true' | ||
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | ||
| env: | ||
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| with: | ||
| script: | | ||
| const lintResultJsonString = ${{ toJSON(steps.lint.outputs.lint_result) }}; | ||
| let lintResult; | ||
| try { | ||
| const jsonString = JSON.parse(lintResultJsonString); | ||
| lintResult = JSON.parse(jsonString); | ||
| } catch (e) { | ||
| console.error('Failed to parse lint result:', e); | ||
| core.setFailed('Failed to parse redirect chain lint output'); | ||
| return; | ||
| } | ||
|
|
||
| const redirectChains = lintResult.redirectChains || []; | ||
| const contentLinkIssues = lintResult.contentLinkIssues || []; | ||
|
|
||
| if (redirectChains.length === 0 && contentLinkIssues.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| let comment = '## \u26a0\ufe0f Redirect Chain Issues Detected\n\n'; | ||
| comment += 'This PR introduces or contains redirect chains that degrade SEO and add latency.\n\n'; | ||
|
|
||
| if (redirectChains.length > 0) { | ||
| comment += '### Redirect-to-Redirect Chains\n\n'; | ||
| comment += 'These redirect destinations point to another redirect, creating multi-hop chains. '; | ||
| comment += 'Update the destination to point directly to the final URL:\n\n'; | ||
| comment += '| Source | Current Destination | Should Be | File |\n'; | ||
| comment += '|--------|-------------------|-----------|------|\n'; | ||
| for (const chain of redirectChains) { | ||
| const docLabel = chain.isDeveloperDocs ? ' (dev)' : ''; | ||
| comment += `| \`${chain.source}\` | \`${chain.currentDest}\` | \`${chain.finalDest}\` | ${chain.file}${docLabel} |\n`; | ||
| } | ||
| comment += '\n'; | ||
| } | ||
|
|
||
| if (contentLinkIssues.length > 0) { | ||
| // Group by file | ||
| const byFile = {}; | ||
| for (const issue of contentLinkIssues) { | ||
| if (!byFile[issue.filePath]) byFile[issue.filePath] = []; | ||
|
sentry[bot] marked this conversation as resolved.
|
||
| byFile[issue.filePath].push(issue); | ||
| } | ||
|
|
||
| comment += `### Content Links Pointing to Redirects (${contentLinkIssues.length} found)\n\n`; | ||
| comment += 'These links point to URLs that redirect. Update them to point directly to the final destination:\n\n'; | ||
| comment += '| File | Line | Current Link | Should Be |\n'; | ||
| comment += '|------|------|--------------|-----------|\n'; | ||
| for (const [file, issues] of Object.entries(byFile)) { | ||
| for (const issue of issues) { | ||
| comment += `| \`${file}\` | ${issue.line} | \`${issue.linkPath}\` | \`${issue.finalDest}\` |\n`; | ||
| } | ||
| } | ||
| comment += '\n'; | ||
| } | ||
|
|
||
| comment += '---\n'; | ||
| comment += '*Each redirect hop loses ~15% of SEO link equity and adds latency for users.*\n'; | ||
|
|
||
| // Check for existing comments from this action (paginate to handle PRs with 30+ comments) | ||
| const comments = await github.paginate(github.rest.issues.listComments, { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| }); | ||
|
|
||
| const existingComment = comments.find(c => | ||
| c.user.type === 'Bot' && | ||
| c.user.login === 'github-actions[bot]' && | ||
| c.body.includes('Redirect Chain Issues Detected') | ||
| ); | ||
|
|
||
| if (existingComment) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: existingComment.id, | ||
| body: comment, | ||
| }); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: context.issue.number, | ||
| body: comment, | ||
| }); | ||
| } | ||
|
|
||
| - name: Report failure if redirect chains found | ||
| if: steps.lint.outputs.exit_code == '1' && steps.lint.outputs.has_results == 'true' | ||
| run: | | ||
| echo "::warning::Redirect chain issues detected. See PR comment for details." | ||
| exit 1 | ||
|
|
||
| - name: Success | ||
| if: steps.lint.outputs.exit_code == '0' | ||
| run: | | ||
| echo "No redirect chains or content link issues found." | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Workflow skips linter file changes
Medium Severity
The pull-request
pathsfilter listsmiddleware.ts,redirects.js, and content globs only. Changes confined toscripts/lint-redirect-chains.ts, its spec, or this workflow file do not match, so the new job will not run on PRs that touch only the linter or CI wiring—including the PR that introduces it.Reviewed by Cursor Bugbot for commit 523dff1. Configure here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The workflow intentionally only triggers on redirect/content file changes, because that's what it lints. Changes to the linter script itself are validated by the existing test suite (pnpm test / vitest). This matches the pattern of check-redirects-on-rename.yml. Adding the linter's own files to the path filter would just run the linter needlessly on PRs that only touch the linter code.