|
1 | 1 | name: "Periodic Code Cleanliness Review" |
2 | 2 |
|
3 | | -on: |
| 3 | +# REQUIREMENTS: |
| 4 | +# - A GitHub Personal Access Token with Copilot access must be created and stored as a repository secret named COPILOT_TOKEN |
| 5 | +# - See COPILOT_TOKEN_SETUP.md for detailed setup instructions |
4 | 6 |
|
| 7 | +on: |
5 | 8 | schedule: |
6 | | - |
7 | 9 | # Run every 12 hours (at 00:00 and 12:00 UTC) |
8 | | - |
9 | 10 | - cron: '0 0,12 * * *' |
10 | | - |
11 | 11 | workflow_dispatch: # Allow manual trigger |
12 | 12 |
|
13 | 13 | permissions: |
14 | | - |
15 | 14 | contents: write |
16 | | - |
17 | 15 | pull-requests: write |
18 | | - |
19 | 16 | issues: write |
20 | 17 |
|
21 | 18 | jobs: |
22 | | - |
23 | 19 | code-cleanliness-review: |
24 | | - |
25 | | - runs-on: self-hosted |
26 | | - |
| 20 | + runs-on: [self-hosted, linux, x64, big] |
27 | 21 | steps: |
28 | | - |
29 | 22 | - name: Checkout code |
30 | | - |
31 | 23 | uses: actions/checkout@main |
32 | | - |
33 | 24 | with: |
34 | | - |
35 | 25 | fetch-depth: 0 # Full history for better analysis |
36 | 26 |
|
37 | 27 | - name: Analyze Large Files |
38 | | - |
39 | 28 | id: analyze |
40 | | - |
41 | 29 | run: | |
42 | | -
|
43 | 30 | echo "## Large Files Analysis" > /tmp/analysis.md |
44 | | -
|
45 | 31 | echo "" >> /tmp/analysis.md |
46 | | -
|
47 | 32 | echo "Files larger than 500 lines that may benefit from splitting:" >> /tmp/analysis.md |
48 | | -
|
49 | 33 | echo "" >> /tmp/analysis.md |
50 | | -
|
51 | 34 | |
52 | | -
|
53 | 35 | # Find files larger than 500 lines (excluding common large files) |
54 | | -
|
55 | 36 | find . -type f \( -name "*.py" -o -name "*.js" -o -name "*.ts" -o -name "*.java" -o -name "*.go" -o -name "*.cs" -o -name "*.rb" \) \ |
56 | | -
|
57 | 37 | ! -path "*/node_modules/*" \ |
58 | | -
|
59 | 38 | ! -path "*/dist/*" \ |
60 | | -
|
61 | 39 | ! -path "*/build/*" \ |
62 | | -
|
63 | 40 | ! -path "*/.venv/*" \ |
64 | | -
|
65 | 41 | ! -path "*/vendor/*" \ |
66 | | -
|
67 | 42 | -exec wc -l {} \; | \ |
68 | | -
|
69 | 43 | awk '$1 > 500 {print $1 " lines: " $2}' | \ |
70 | | -
|
71 | 44 | sort -rn >> /tmp/analysis.md || echo "No large files found" >> /tmp/analysis.md |
72 | | -
|
73 | 45 | |
74 | | -
|
75 | 46 | echo "" >> /tmp/analysis.md |
76 | | -
|
77 | 47 | echo "## Code Complexity Analysis" >> /tmp/analysis.md |
78 | | -
|
79 | 48 | echo "" >> /tmp/analysis.md |
80 | | -
|
81 | 49 | echo "Files with potential complexity issues:" >> /tmp/analysis.md |
82 | | -
|
83 | 50 | |
84 | | -
|
85 | 51 | # Find files with many functions/classes (basic heuristic) |
86 | | -
|
87 | 52 | for ext in py js ts java go cs rb; do |
88 | | -
|
89 | 53 | if [ "$ext" = "py" ]; then |
90 | | -
|
91 | 54 | pattern="^def |^class " |
92 | | -
|
93 | 55 | elif [ "$ext" = "js" ] || [ "$ext" = "ts" ]; then |
94 | | -
|
95 | 56 | pattern="^function |^class |const.*=.*=>|function.*{$" |
96 | | -
|
97 | 57 | else |
98 | | -
|
99 | 58 | pattern="^class |^def |^func " |
100 | | -
|
101 | 59 | fi |
102 | | -
|
103 | 60 | |
104 | | -
|
105 | 61 | find . -type f -name "*.$ext" \ |
106 | | -
|
107 | 62 | ! -path "*/node_modules/*" \ |
108 | | -
|
109 | 63 | ! -path "*/dist/*" \ |
110 | | -
|
111 | 64 | ! -path "*/build/*" \ |
112 | | -
|
113 | 65 | ! -path "*/.venv/*" \ |
114 | | -
|
115 | 66 | ! -path "*/vendor/*" \ |
116 | | -
|
117 | 67 | -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" {} \; \ |
118 | | -
|
119 | 68 | 2>/dev/null || true |
120 | | -
|
121 | 69 | done | sort -rn >> /tmp/analysis.md |
122 | | -
|
123 | 70 | |
124 | | -
|
125 | 71 | cat /tmp/analysis.md |
126 | 72 |
|
127 | 73 | - name: GitHub Copilot Code Review |
128 | | - |
129 | | - uses: github/copilot-cli-action@main |
130 | | - |
| 74 | + uses: austenstone/copilot-cli-action@v2 |
131 | 75 | with: |
132 | | - |
133 | | - query: | |
134 | | -
|
| 76 | + copilot-token: ${{ secrets.COPILOT_TOKEN }} |
| 77 | + prompt: | |
135 | 78 | Review the codebase for code cleanliness issues: |
136 | | -
|
137 | 79 | 1. Identify files that are too large (>500 lines) and suggest how to split them into smaller, focused modules |
138 | | -
|
139 | 80 | 2. Look for code duplication and suggest refactoring opportunities |
140 | | -
|
141 | 81 | 3. Check for consistent code style and formatting |
142 | | -
|
143 | 82 | 4. Identify complex functions that could be simplified |
144 | | -
|
145 | 83 | 5. Suggest improvements for code organization and structure |
146 | | -
|
147 | 84 | 6. Check for proper separation of concerns |
148 | | -
|
149 | 85 | |
150 | | -
|
151 | 86 | Provide actionable recommendations with specific file names and line numbers. |
152 | | -
|
153 | | - env: |
154 | | - |
155 | | - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
156 | | - |
157 | 87 | continue-on-error: true |
158 | 88 |
|
159 | 89 | - name: Create Issue for Code Cleanliness Review |
160 | | - |
161 | 90 | uses: actions/github-script@main |
162 | | - |
163 | 91 | with: |
164 | | - |
165 | 92 | github-token: ${{ secrets.GITHUB_TOKEN }} |
166 | | - |
167 | 93 | script: | |
168 | | -
|
169 | 94 | const fs = require('fs'); |
170 | | -
|
171 | 95 | const analysis = fs.readFileSync('/tmp/analysis.md', 'utf8'); |
172 | | -
|
173 | 96 | |
174 | | -
|
175 | 97 | const date = new Date().toISOString().split('T')[0]; |
176 | | -
|
177 | 98 | const title = `Code Cleanliness Review - ${date}`; |
178 | | -
|
179 | 99 | |
180 | | -
|
181 | 100 | const body = `# Periodic Code Cleanliness Review |
182 | | -
|
183 | 101 | |
184 | | -
|
185 | 102 | This is an automated review conducted every 12 hours to maintain code quality. |
186 | | -
|
187 | 103 | |
188 | | -
|
189 | 104 | ${analysis} |
190 | | -
|
191 | 105 | |
192 | | -
|
193 | 106 | ## Recommendations |
194 | | -
|
195 | 107 | |
196 | | -
|
197 | 108 | Please review the analysis above and: |
198 | | -
|
199 | 109 | 1. Split large files (>500 lines) into smaller, focused modules |
200 | | -
|
201 | 110 | 2. Refactor complex functions into smaller, testable units |
202 | | -
|
203 | 111 | 3. Remove code duplication |
204 | | -
|
205 | 112 | 4. Ensure consistent code style |
206 | | -
|
207 | 113 | 5. Improve code organization and structure |
208 | | -
|
209 | 114 | |
210 | | -
|
211 | 115 | ## Next Steps |
212 | | -
|
213 | 116 | |
214 | | -
|
215 | 117 | - Assign this issue to relevant team members |
216 | | -
|
217 | 118 | - Create follow-up PRs to address findings |
218 | | -
|
219 | 119 | - Document any architectural decisions |
220 | | -
|
221 | 120 | |
222 | | -
|
223 | 121 | --- |
224 | | -
|
225 | 122 | *This issue was automatically generated by the Code Cleanliness Review workflow.* |
226 | | -
|
227 | 123 | `; |
228 | | -
|
229 | 124 | |
230 | | -
|
231 | 125 | // Check if similar issue exists (open, created in last 24 hours) |
232 | | -
|
233 | 126 | const issues = await github.rest.issues.listForRepo({ |
234 | | -
|
235 | 127 | owner: context.repo.owner, |
236 | | -
|
237 | 128 | repo: context.repo.repo, |
238 | | -
|
239 | 129 | state: 'open', |
240 | | -
|
241 | 130 | labels: ['code-cleanliness', 'automated'], |
242 | | -
|
243 | 131 | per_page: 10 |
244 | | -
|
245 | 132 | }); |
246 | | -
|
247 | 133 | |
248 | | -
|
249 | 134 | const recentIssue = issues.data.find(issue => { |
250 | | -
|
251 | 135 | const createdAt = new Date(issue.created_at); |
252 | | -
|
253 | 136 | const hoursSinceCreation = (Date.now() - createdAt) / (1000 * 60 * 60); |
254 | | -
|
255 | 137 | return hoursSinceCreation < 24; |
256 | | -
|
257 | 138 | }); |
258 | | -
|
259 | 139 | |
260 | | -
|
261 | 140 | if (recentIssue) { |
262 | | -
|
263 | 141 | console.log(`Recent issue found: #${recentIssue.number}, skipping creation`); |
264 | | -
|
265 | 142 | // Update existing issue with new analysis |
266 | | -
|
267 | 143 | await github.rest.issues.createComment({ |
268 | | -
|
269 | 144 | owner: context.repo.owner, |
270 | | -
|
271 | 145 | repo: context.repo.repo, |
272 | | -
|
273 | 146 | issue_number: recentIssue.number, |
274 | | -
|
275 | 147 | body: `## Updated Analysis (${date})\n\n${analysis}` |
276 | | -
|
277 | 148 | }); |
278 | | -
|
279 | 149 | } else { |
280 | | -
|
281 | 150 | // Create new issue |
282 | | -
|
283 | 151 | await github.rest.issues.create({ |
284 | | -
|
285 | 152 | owner: context.repo.owner, |
286 | | -
|
287 | 153 | repo: context.repo.repo, |
288 | | -
|
289 | 154 | title: title, |
290 | | -
|
291 | 155 | body: body, |
292 | | -
|
293 | 156 | labels: ['code-cleanliness', 'automated', 'needs-review'] |
294 | | -
|
295 | 157 | }); |
296 | | -
|
297 | 158 | } |
298 | | -
|
0 commit comments