|
| 1 | +name: "Periodic Code Cleanliness Review" |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Run every 12 hours (at 00:00 and 12:00 UTC) |
| 6 | + - cron: '0 0,12 * * *' |
| 7 | + workflow_dispatch: # Allow manual trigger |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + issues: write |
| 13 | + |
| 14 | +jobs: |
| 15 | + code-cleanliness-review: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@main |
| 20 | + with: |
| 21 | + fetch-depth: 0 # Full history for better analysis |
| 22 | + |
| 23 | + - name: Analyze Large Files |
| 24 | + id: analyze |
| 25 | + run: | |
| 26 | + echo "## Large Files Analysis" > /tmp/analysis.md |
| 27 | + echo "" >> /tmp/analysis.md |
| 28 | + echo "Files larger than 500 lines that may benefit from splitting:" >> /tmp/analysis.md |
| 29 | + echo "" >> /tmp/analysis.md |
| 30 | + |
| 31 | + # Find files larger than 500 lines (excluding common large files) |
| 32 | + find . -type f \( -name "*.py" -o -name "*.js" -o -name "*.ts" -o -name "*.java" -o -name "*.go" -o -name "*.cs" -o -name "*.rb" \) \ |
| 33 | + ! -path "*/node_modules/*" \ |
| 34 | + ! -path "*/dist/*" \ |
| 35 | + ! -path "*/build/*" \ |
| 36 | + ! -path "*/.venv/*" \ |
| 37 | + ! -path "*/vendor/*" \ |
| 38 | + -exec wc -l {} \; | \ |
| 39 | + awk '$1 > 500 {print $1 " lines: " $2}' | \ |
| 40 | + sort -rn >> /tmp/analysis.md || echo "No large files found" >> /tmp/analysis.md |
| 41 | + |
| 42 | + echo "" >> /tmp/analysis.md |
| 43 | + echo "## Code Complexity Analysis" >> /tmp/analysis.md |
| 44 | + echo "" >> /tmp/analysis.md |
| 45 | + echo "Files with potential complexity issues:" >> /tmp/analysis.md |
| 46 | + |
| 47 | + # Find files with many functions/classes (basic heuristic) |
| 48 | + for ext in py js ts java go cs rb; do |
| 49 | + if [ "$ext" = "py" ]; then |
| 50 | + pattern="^def |^class " |
| 51 | + elif [ "$ext" = "js" ] || [ "$ext" = "ts" ]; then |
| 52 | + pattern="^function |^class |const.*=.*=>|function.*{$" |
| 53 | + else |
| 54 | + pattern="^class |^def |^func " |
| 55 | + fi |
| 56 | + |
| 57 | + find . -type f -name "*.$ext" \ |
| 58 | + ! -path "*/node_modules/*" \ |
| 59 | + ! -path "*/dist/*" \ |
| 60 | + ! -path "*/build/*" \ |
| 61 | + ! -path "*/.venv/*" \ |
| 62 | + ! -path "*/vendor/*" \ |
| 63 | + -exec sh -c 'count=$(grep -c "$1" "$2" 2>/dev/null || echo 0); if [ "$count" -gt 20 ]; then echo "$count definitions in $2"; fi' _ "$pattern" {} \; \ |
| 64 | + 2>/dev/null || true |
| 65 | + done | sort -rn >> /tmp/analysis.md |
| 66 | + |
| 67 | + cat /tmp/analysis.md |
| 68 | +
|
| 69 | + - name: GitHub Copilot Code Review |
| 70 | + uses: github/copilot-cli-action@main |
| 71 | + with: |
| 72 | + query: | |
| 73 | + Review the codebase for code cleanliness issues: |
| 74 | + 1. Identify files that are too large (>500 lines) and suggest how to split them into smaller, focused modules |
| 75 | + 2. Look for code duplication and suggest refactoring opportunities |
| 76 | + 3. Check for consistent code style and formatting |
| 77 | + 4. Identify complex functions that could be simplified |
| 78 | + 5. Suggest improvements for code organization and structure |
| 79 | + 6. Check for proper separation of concerns |
| 80 | + |
| 81 | + Provide actionable recommendations with specific file names and line numbers. |
| 82 | + env: |
| 83 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 84 | + continue-on-error: true |
| 85 | + |
| 86 | + - name: Create Issue for Code Cleanliness Review |
| 87 | + uses: actions/github-script@main |
| 88 | + with: |
| 89 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 90 | + script: | |
| 91 | + const fs = require('fs'); |
| 92 | + const analysis = fs.readFileSync('/tmp/analysis.md', 'utf8'); |
| 93 | + |
| 94 | + const date = new Date().toISOString().split('T')[0]; |
| 95 | + const title = `Code Cleanliness Review - ${date}`; |
| 96 | + |
| 97 | + const body = `# Periodic Code Cleanliness Review |
| 98 | + |
| 99 | + This is an automated review conducted every 12 hours to maintain code quality. |
| 100 | + |
| 101 | + ${analysis} |
| 102 | + |
| 103 | + ## Recommendations |
| 104 | + |
| 105 | + Please review the analysis above and: |
| 106 | + 1. Split large files (>500 lines) into smaller, focused modules |
| 107 | + 2. Refactor complex functions into smaller, testable units |
| 108 | + 3. Remove code duplication |
| 109 | + 4. Ensure consistent code style |
| 110 | + 5. Improve code organization and structure |
| 111 | + |
| 112 | + ## Next Steps |
| 113 | + |
| 114 | + - Assign this issue to relevant team members |
| 115 | + - Create follow-up PRs to address findings |
| 116 | + - Document any architectural decisions |
| 117 | + |
| 118 | + --- |
| 119 | + *This issue was automatically generated by the Code Cleanliness Review workflow.* |
| 120 | + `; |
| 121 | + |
| 122 | + // Check if similar issue exists (open, created in last 24 hours) |
| 123 | + const issues = await github.rest.issues.listForRepo({ |
| 124 | + owner: context.repo.owner, |
| 125 | + repo: context.repo.repo, |
| 126 | + state: 'open', |
| 127 | + labels: ['code-cleanliness', 'automated'], |
| 128 | + per_page: 10 |
| 129 | + }); |
| 130 | + |
| 131 | + const recentIssue = issues.data.find(issue => { |
| 132 | + const createdAt = new Date(issue.created_at); |
| 133 | + const hoursSinceCreation = (Date.now() - createdAt) / (1000 * 60 * 60); |
| 134 | + return hoursSinceCreation < 24; |
| 135 | + }); |
| 136 | + |
| 137 | + if (recentIssue) { |
| 138 | + console.log(`Recent issue found: #${recentIssue.number}, skipping creation`); |
| 139 | + // Update existing issue with new analysis |
| 140 | + await github.rest.issues.createComment({ |
| 141 | + owner: context.repo.owner, |
| 142 | + repo: context.repo.repo, |
| 143 | + issue_number: recentIssue.number, |
| 144 | + body: `## Updated Analysis (${date})\n\n${analysis}` |
| 145 | + }); |
| 146 | + } else { |
| 147 | + // Create new issue |
| 148 | + await github.rest.issues.create({ |
| 149 | + owner: context.repo.owner, |
| 150 | + repo: context.repo.repo, |
| 151 | + title: title, |
| 152 | + body: body, |
| 153 | + labels: ['code-cleanliness', 'automated', 'needs-review'] |
| 154 | + }); |
| 155 | + } |
0 commit comments