|
| 1 | +name: Check Broken Links |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'mintlify/**/*.mdx' |
| 7 | + types: [opened, synchronize, reopened] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + check-broken-links: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Setup Node.js |
| 21 | + uses: actions/setup-node@v4 |
| 22 | + with: |
| 23 | + node-version: '18' |
| 24 | + |
| 25 | + - name: Install Mintlify CLI |
| 26 | + run: npm install -g mintlify |
| 27 | + |
| 28 | + - name: Check for broken links |
| 29 | + id: broken-links |
| 30 | + working-directory: mintlify |
| 31 | + run: | |
| 32 | + # Run mint broken-links and capture output |
| 33 | + OUTPUT=$(mint broken-links 2>&1 || true) |
| 34 | +
|
| 35 | + # Filter out /api-reference/ false positives |
| 36 | + FILTERED_OUTPUT=$(echo "$OUTPUT" | grep -v "/api-reference/" || true) |
| 37 | +
|
| 38 | + # Count real broken links (excluding the summary line) |
| 39 | + BROKEN_COUNT=$(echo "$FILTERED_OUTPUT" | grep -E "^\s+/" | wc -l || echo "0") |
| 40 | +
|
| 41 | + # Save results to GitHub outputs |
| 42 | + echo "broken_count=$BROKEN_COUNT" >> $GITHUB_OUTPUT |
| 43 | +
|
| 44 | + # Save filtered output to file for PR comment |
| 45 | + if [ "$BROKEN_COUNT" -gt 0 ]; then |
| 46 | + echo "$FILTERED_OUTPUT" > broken_links_output.txt |
| 47 | + echo "has_broken_links=true" >> $GITHUB_OUTPUT |
| 48 | + else |
| 49 | + echo "✅ No broken links found" > broken_links_output.txt |
| 50 | + echo "has_broken_links=false" >> $GITHUB_OUTPUT |
| 51 | + fi |
| 52 | +
|
| 53 | + - name: Comment on PR |
| 54 | + uses: actions/github-script@v7 |
| 55 | + with: |
| 56 | + script: | |
| 57 | + const fs = require('fs'); |
| 58 | + const brokenCount = '${{ steps.broken-links.outputs.broken_count }}'; |
| 59 | + const hasBrokenLinks = '${{ steps.broken-links.outputs.has_broken_links }}' === 'true'; |
| 60 | +
|
| 61 | + let comment; |
| 62 | + if (hasBrokenLinks) { |
| 63 | + const output = fs.readFileSync('mintlify/broken_links_output.txt', 'utf8'); |
| 64 | + comment = `## 🔗 Broken Links Found\n\n${brokenCount} broken link(s) detected:\n\n\`\`\`\n${output}\`\`\`\n\n⚠️ Please fix these broken links before merging.`; |
| 65 | + } else { |
| 66 | + comment = `## ✅ Link Check Passed\n\nNo broken links found in the documentation files.`; |
| 67 | + } |
| 68 | +
|
| 69 | + // Find existing comment |
| 70 | + const { data: comments } = await github.rest.issues.listComments({ |
| 71 | + owner: context.repo.owner, |
| 72 | + repo: context.repo.repo, |
| 73 | + issue_number: context.issue.number, |
| 74 | + }); |
| 75 | +
|
| 76 | + const existingComment = comments.find(comment => |
| 77 | + comment.body.includes('🔗 Broken Links Found') || |
| 78 | + comment.body.includes('✅ Link Check Passed') |
| 79 | + ); |
| 80 | +
|
| 81 | + if (existingComment) { |
| 82 | + // Update existing comment |
| 83 | + await github.rest.issues.updateComment({ |
| 84 | + owner: context.repo.owner, |
| 85 | + repo: context.repo.repo, |
| 86 | + comment_id: existingComment.id, |
| 87 | + body: comment |
| 88 | + }); |
| 89 | + } else { |
| 90 | + // Create new comment |
| 91 | + await github.rest.issues.createComment({ |
| 92 | + owner: context.repo.owner, |
| 93 | + repo: context.repo.repo, |
| 94 | + issue_number: context.issue.number, |
| 95 | + body: comment |
| 96 | + }); |
| 97 | + } |
| 98 | +
|
| 99 | + - name: Fail if broken links found |
| 100 | + if: steps.broken-links.outputs.has_broken_links == 'true' |
| 101 | + run: | |
| 102 | + echo "❌ ${{ steps.broken-links.outputs.broken_count }} broken link(s) found" |
| 103 | + exit 1 |
0 commit comments