lobehub/.github/workflows/check-console-log.yml
René Wang c0542e80a3
🔨 chore: Add CI to Check console.log (#10333)
* lint: Clean breakpoints

* build: Add CI to check

* build: Add `next` branch

* build: Remove markdown files

* fix: CI hang out

* fix: Show warning on GitHub

* feat: Send comment

* fix: CI error

* fix: show file list
2025-11-21 14:18:10 +08:00

117 lines
4.2 KiB
YAML

name: Check Console Log (Warning)
on:
pull_request:
branches:
- main
- next
permissions:
contents: read
pull-requests: write
jobs:
check-console-log:
name: Check for console.log statements (non-blocking)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- name: Install bun
uses: oven-sh/setup-bun@v2
- name: Run console.log check
id: check
run: |
OUTPUT=$(bunx tsx scripts/checkConsoleLog.mts 2>&1)
echo "$OUTPUT"
# Save output to file for later use
echo "$OUTPUT" > /tmp/console-log-output.txt
# Check if violations were found
if echo "$OUTPUT" | grep -q "Total violations:"; then
echo "has_violations=true" >> $GITHUB_OUTPUT
TOTAL=$(echo "$OUTPUT" | grep -oP "Total violations: \K\d+")
echo "total=$TOTAL" >> $GITHUB_OUTPUT
else
echo "has_violations=false" >> $GITHUB_OUTPUT
fi
- name: Comment on PR
if: steps.check.outputs.has_violations == 'true'
uses: actions/github-script@v7
env:
VIOLATION_COUNT: ${{ steps.check.outputs.total }}
with:
script: |
const fs = require('fs');
const output = fs.readFileSync('/tmp/console-log-output.txt', 'utf8');
const total = process.env.VIOLATION_COUNT || '0';
// Parse violations from output (format: " file:line" followed by " content")
const lines = output.split('\n');
const violations = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
// Match lines like " packages/database/src/client/db.ts:258"
const fileMatch = line.match(/^\s{2}(\S+:\d+)\s*$/);
if (fileMatch) {
const file = fileMatch[1];
const content = lines[i + 1]?.trim() || '';
violations.push({ file, content });
i++;
}
}
// Build comment body
const maxDisplay = 30;
let body = `## ⚠️ Console.log Check Warning\n\n`;
body += `Found **${total}** \`console.log\` statement(s) in this PR.\n\n`;
if (violations.length > 0) {
body += `<details>\n<summary>📋 Click to see violations (${Math.min(violations.length, maxDisplay)} of ${total} shown)</summary>\n\n`;
body += `| File | Code |\n|------|------|\n`;
violations.slice(0, maxDisplay).forEach(v => {
const escapedContent = v.content
.substring(0, 60)
.replace(/\|/g, '\\|')
.replace(/`/g, "'");
body += `| \`${v.file}\` | \`${escapedContent}${v.content.length > 60 ? '...' : ''}\` |\n`;
});
if (parseInt(total) > maxDisplay) {
body += `\n*...and ${parseInt(total) - maxDisplay} more violations*\n`;
}
body += `\n</details>\n\n`;
}
body += `> 💡 **Tip:** Remove \`console.log\` or add files to \`.console-log-whitelist.json\`\n`;
body += `> ✅ This check is **non-blocking** and won't prevent merging.`;
// Find existing comment to update instead of creating duplicates
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(c =>
c.user.type === 'Bot' && c.body.includes('Console.log Check Warning')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}