-
Notifications
You must be signed in to change notification settings - Fork 7
332 lines (293 loc) · 13 KB
/
Copy pathcode-metrix.yml
File metadata and controls
332 lines (293 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
name: Code Metrics & Quality Dashboard
on:
push:
branches: [ master, develop ]
pull_request:
branches: [ master, develop ]
schedule:
- cron: '0 2 * * 0'
permissions:
contents: write
pull-requests: write
jobs:
metrics:
name: Generate Code Metrics
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install radon coverage pytest pytest-cov pylint
# ── Cyclomatic Complexity ─────────────────────────────────
- name: Run Radon - Cyclomatic Complexity
continue-on-error: true
run: |
set -x
radon cc atdork.py core/ lib/ -a -s --json > radon-cc.json 2>&1 || true
if [ ! -s radon-cc.json ]; then echo '{}' > radon-cc.json; fi
echo "radon-cc.json contents:"
cat radon-cc.json
python << 'PYTHON_EOF'
import json
try:
with open('radon-cc.json') as f:
data = json.load(f)
all_cc = []
# Radon menghasilkan dict { "filepath": [ { "complexity": ... }, ... ] }
if isinstance(data, dict):
for file_path, file_data in data.items():
if isinstance(file_data, list):
for func in file_data:
if isinstance(func, dict) and 'complexity' in func:
all_cc.append(func['complexity'])
if all_cc:
avg_cc = sum(all_cc) / len(all_cc)
with open("complexity_score.txt", "w") as f:
f.write(f"{avg_cc:.2f}")
else:
with open("complexity_score.txt", "w") as f:
f.write("N/A")
except Exception:
with open("complexity_score.txt", "w") as f:
f.write("N/A")
PYTHON_EOF
echo "Complexity score: $(cat complexity_score.txt 2>/dev/null || echo 'missing')"
# ── Maintainability Index ─────────────────────────────────
- name: Run Radon - Maintainability Index
continue-on-error: true
run: |
radon mi atdork.py core/ lib/ -s --json > radon-mi.json 2>&1 || true
if [ ! -s radon-mi.json ]; then echo '{}' > radon-mi.json; fi
echo "radon-mi.json contents:"
cat radon-mi.json
python << 'PYTHON_EOF'
import json
try:
with open('radon-mi.json') as f:
data = json.load(f)
mi_values = []
# Radon MI menghasilkan dict { "filepath": { "mi": value } }
if isinstance(data, dict):
for file_path, file_data in data.items():
if isinstance(file_data, (int, float)):
mi_values.append(file_data)
elif isinstance(file_data, dict) and 'mi' in file_data:
mi_values.append(file_data['mi'])
if mi_values:
avg_mi = sum(mi_values) / len(mi_values)
with open("maintainability_score.txt", "w") as f:
f.write(f"{avg_mi:.1f}")
else:
with open("maintainability_score.txt", "w") as f:
f.write("N/A")
except Exception:
with open("maintainability_score.txt", "w") as f:
f.write("N/A")
PYTHON_EOF
echo "Maintainability score: $(cat maintainability_score.txt 2>/dev/null || echo 'missing')"
# ── Test Coverage ─────────────────────────────────────────
- name: Run pytest with Coverage
continue-on-error: true
run: |
pytest tests/ -v --cov=core --cov=lib --cov=atdork --cov-report=term-missing --cov-report=json 2>&1 || true
if [ ! -s coverage.json ]; then echo '{"totals":{"percent_covered":0}}' > coverage.json; fi
echo "coverage.json contents:"
cat coverage.json
python << 'PYTHON_EOF'
import json
try:
with open('coverage.json') as f:
data = json.load(f)
coverage = data.get('totals', {}).get('percent_covered', 0)
with open("coverage_score.txt", "w") as f:
f.write(f"{coverage:.1f}")
except Exception:
with open("coverage_score.txt", "w") as f:
f.write("N/A")
PYTHON_EOF
echo "Coverage score: $(cat coverage_score.txt 2>/dev/null || echo 'missing')"
# ── Pylint Score ──────────────────────────────────────────
- name: Run Pylint
continue-on-error: true
run: |
pylint atdork.py core/ lib/ --exit-zero > pylint-output.txt 2>&1 || true
python << 'PYTHON_EOF'
import re
try:
with open('pylint-output.txt') as f:
text = f.read()
match = re.search(r'Your code has been rated at ([\d.]+)', text)
if match:
score = match.group(1)
else:
score = "N/A"
with open("pylint_score.txt", "w") as f:
f.write(score)
except Exception:
with open("pylint_score.txt", "w") as f:
f.write("N/A")
PYTHON_EOF
echo "Pylint score: $(cat pylint_score.txt 2>/dev/null || echo 'missing')"
# ── Upload artifacts ──────────────────────────────────────
- name: Upload metrics artifacts
uses: actions/upload-artifact@v4
with:
name: code-metrics-reports
path: |
radon-cc.json
radon-mi.json
coverage.json
pylint-output.txt
*_score.txt
*_summary.txt
update-badges:
name: Update README Badges
needs: metrics
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download metrics
uses: actions/download-artifact@v4
with:
name: code-metrics-reports
- name: Update README
run: |
COMPLEXITY=$(cat complexity_score.txt 2>/dev/null || echo "N/A")
MAINTAINABILITY=$(cat maintainability_score.txt 2>/dev/null || echo "N/A")
COVERAGE=$(cat coverage_score.txt 2>/dev/null || echo "N/A")
PYLINT=$(cat pylint_score.txt 2>/dev/null || echo "N/A")
TIMESTAMP=$(date -u +'%Y-%m-%d %H:%M:%S UTC')
sed -i '/## Code Quality Metrics/,/Last updated:/d' README.md 2>/dev/null || true
cat >> README.md << 'READMEOF'
## Code Quality Metrics




| Metric | Score | Status |
|--------|-------|--------|
| Cyclomatic Complexity | COMPLEXITY_VAL avg | ✅ Good |
| Maintainability Index | MAINTAINABILITY_VAL | ✅ Good |
| Test Coverage | COVERAGE_VAL% | ⚠️ Fair |
| Pylint Score | PYLINT_VAL/100 | ✅ Good |
*Analysis: atdork.py, core/, lib/ • Last updated: TIMESTAMP_VAL*
READMEOF
sed -i "s|COMPLEXITY_VAL|$COMPLEXITY|g" README.md
sed -i "s|MAINTAINABILITY_VAL|$MAINTAINABILITY|g" README.md
sed -i "s|COVERAGE_VAL|$COVERAGE|g" README.md
sed -i "s|PYLINT_VAL|$PYLINT|g" README.md
sed -i "s|TIMESTAMP_VAL|$TIMESTAMP|g" README.md
- name: Commit & Push
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
if git diff --quiet README.md; then
echo "No changes to README"
exit 0
fi
git add README.md
git commit -m "docs: update code metrics"
for i in 1 2 3; do
echo "Push attempt $i"
git pull --rebase origin master && break || sleep $((RANDOM % 10 + 5))
done
git push origin master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
store-history:
name: Store Metrics History
needs: metrics
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/master'
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Download metrics
uses: actions/download-artifact@v4
with:
name: code-metrics-reports
- name: Save metrics history
run: |
mkdir -p .github/metrics-history
TIMESTAMP=$(date -u +'%Y-%m-%d')
COMPLEXITY=$(cat complexity_score.txt 2>/dev/null || echo "N/A")
MAINTAINABILITY=$(cat maintainability_score.txt 2>/dev/null || echo "N/A")
COVERAGE=$(cat coverage_score.txt 2>/dev/null || echo "N/A")
PYLINT=$(cat pylint_score.txt 2>/dev/null || echo "N/A")
cat > ".github/metrics-history/${TIMESTAMP}.json" << 'JSONEOF'
{
"timestamp": "TIMESTAMP_ISO",
"commit": "COMMIT_SHA",
"complexity": "COMPLEXITY_VAL",
"maintainability": "MAINTAINABILITY_VAL",
"coverage": "COVERAGE_VAL",
"pylint_score": "PYLINT_VAL"
}
JSONEOF
sed -i "s|TIMESTAMP_ISO|$(date -u +'%Y-%m-%dT%H:%M:%SZ')|g" ".github/metrics-history/${TIMESTAMP}.json"
sed -i "s|COMMIT_SHA|${{ github.sha }}|g" ".github/metrics-history/${TIMESTAMP}.json"
sed -i "s|COMPLEXITY_VAL|$COMPLEXITY|g" ".github/metrics-history/${TIMESTAMP}.json"
sed -i "s|MAINTAINABILITY_VAL|$MAINTAINABILITY|g" ".github/metrics-history/${TIMESTAMP}.json"
sed -i "s|COVERAGE_VAL|$COVERAGE|g" ".github/metrics-history/${TIMESTAMP}.json"
sed -i "s|PYLINT_VAL|$PYLINT|g" ".github/metrics-history/${TIMESTAMP}.json"
cat ".github/metrics-history/${TIMESTAMP}.json"
- name: Commit history
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add .github/metrics-history/
git commit -m "ci: record metrics for $(date -u +'%Y-%m-%d')" || echo "No changes to commit"
for i in 1 2 3; do
echo "Push attempt $i"
git pull --rebase origin master && break || sleep $((RANDOM % 10 + 5))
done
git push origin master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
pr-comment:
name: Comment PR with Metrics
needs: metrics
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- name: Download metrics
uses: actions/download-artifact@v4
with:
name: code-metrics-reports
- name: Post comment
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const complexity = fs.existsSync('complexity_score.txt') ? fs.readFileSync('complexity_score.txt', 'utf8').trim() : 'N/A';
const maintainability = fs.existsSync('maintainability_score.txt') ? fs.readFileSync('maintainability_score.txt', 'utf8').trim() : 'N/A';
const coverage = fs.existsSync('coverage_score.txt') ? fs.readFileSync('coverage_score.txt', 'utf8').trim() : 'N/A';
const pylint = fs.existsSync('pylint_score.txt') ? fs.readFileSync('pylint_score.txt', 'utf8').trim() : 'N/A';
const comment = `## 📊 Code Metrics Report
**Analysis Scope:** atdork.py, core/, lib/
| Metric | Value | Status |
|--------|-------|--------|
| Cyclomatic Complexity | ${complexity} avg | ✅ |
| Maintainability Index | ${maintainability} | ✅ |
| Test Coverage | ${coverage}% | ⚠️ |
| Pylint Score | ${pylint}/100 | ✅ |
*Generated by Code Metrics Workflow*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});