|
| 1 | +name: Check Broken Links in MDX Files |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'mintlify/**/*.mdx' |
| 7 | + - '.github/workflows/broken-links.yml' |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + check-broken-links: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Setup Node.js |
| 22 | + uses: actions/setup-node@v4 |
| 23 | + with: |
| 24 | + node-version: '18' |
| 25 | + |
| 26 | + - name: Install Mintlify CLI |
| 27 | + run: npm install -g mintlify |
| 28 | + |
| 29 | + - name: Run broken links check |
| 30 | + id: broken-links |
| 31 | + working-directory: mintlify |
| 32 | + run: | |
| 33 | + echo "Running mint broken-links in mintlify directory..." |
| 34 | + |
| 35 | + # Run broken links check and capture both stdout and stderr |
| 36 | + set +e # Don't exit on error |
| 37 | + mint broken-links &> broken-links-output.txt |
| 38 | + mint_exit_code=$? |
| 39 | + set -e # Re-enable exit on error |
| 40 | + |
| 41 | + echo "mint_exit_code=$mint_exit_code" >> $GITHUB_OUTPUT |
| 42 | + |
| 43 | + # Show original output for debugging |
| 44 | + echo "Original mint output:" |
| 45 | + cat broken-links-output.txt |
| 46 | + |
| 47 | + # Always report success and pass through the raw output |
| 48 | + echo "has_broken_links=false" >> $GITHUB_OUTPUT |
| 49 | + echo "RAW_OUTPUT<<EOF" >> $GITHUB_OUTPUT |
| 50 | + cat broken-links-output.txt >> $GITHUB_OUTPUT |
| 51 | + echo "EOF" >> $GITHUB_OUTPUT |
| 52 | + |
| 53 | + - name: Comment on PR |
| 54 | + uses: actions/github-script@v7 |
| 55 | + with: |
| 56 | + script: | |
| 57 | + const rawOutput = `${{ steps.broken-links.outputs.RAW_OUTPUT }}`; |
| 58 | + |
| 59 | + const body = `## 📝 Broken Links Check Report |
| 60 | + |
| 61 | + > **Note**: Entries under \`/api-reference/\` are likely false positives and can be ignored. |
| 62 | + |
| 63 | + The following is the output from the broken links check: |
| 64 | + |
| 65 | + \`\`\` |
| 66 | + ${rawOutput} |
| 67 | + \`\`\` |
| 68 | + `; |
| 69 | + |
| 70 | + // Find existing comment |
| 71 | + const comments = await github.rest.issues.listComments({ |
| 72 | + owner: context.repo.owner, |
| 73 | + repo: context.repo.repo, |
| 74 | + issue_number: context.issue.number, |
| 75 | + }); |
| 76 | + |
| 77 | + const existingComment = comments.data.find(comment => |
| 78 | + comment.user.login === 'github-actions[bot]' && |
| 79 | + comment.body.includes('Broken Links Check') |
| 80 | + ); |
| 81 | + |
| 82 | + if (existingComment) { |
| 83 | + // Update existing comment |
| 84 | + await github.rest.issues.updateComment({ |
| 85 | + owner: context.repo.owner, |
| 86 | + repo: context.repo.repo, |
| 87 | + comment_id: existingComment.id, |
| 88 | + body: body |
| 89 | + }); |
| 90 | + } else { |
| 91 | + // Create new comment |
| 92 | + await github.rest.issues.createComment({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + issue_number: context.issue.number, |
| 96 | + body: body |
| 97 | + }); |
| 98 | + } |
0 commit comments