Skip to content

PR Comment on Link Check #295

PR Comment on Link Check

PR Comment on Link Check #295

name: PR Comment on Link Check
on:
workflow_run:
workflows: ["Test Links"]
types: [completed]
# Write permissions for commenting
permissions:
pull-requests: write
jobs:
comment:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'failure'
steps:
- name: Download PR comment data
uses: actions/download-artifact@v4
with:
name: pr-comment-data
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Post comment on PR
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
// Read PR number from artifact
const prNumber = parseInt(fs.readFileSync('pr_number', 'utf8').trim());
if (!prNumber || isNaN(prNumber)) {
console.log('No valid PR number found, skipping comment.');
return;
}
const runUrl = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${{ github.event.workflow_run.id }}`;
const commentBody = `## ❌ Link Check Failed
**Broken links were detected in this PR.**
Please check the [workflow run logs](${runUrl}) for details on which links are broken.
### Common fixes:
1. **Typo in URL** - Check for spelling mistakes in the link
2. **Outdated link** - The page may have moved or been deleted
3. **Relative path issue** - Ensure relative links use the correct path
4. **External site down** - If the external site is temporarily down, you can add it to \`brev/.lycheeignore\`
### To test links locally:
\`\`\`bash
./brev/test-links.bash .
\`\`\`
📚 [Lychee documentation](https://github.com/lycheeverse/lychee)
`;
// Check if we already commented on this PR to avoid spam
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.includes('Link Check Failed')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: commentBody
});
console.log(`Updated existing comment on PR #${prNumber}`);
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: commentBody
});
console.log(`Created comment on PR #${prNumber}`);
}