Skip to content

Commit a1eeb19

Browse files
feat: add weekly dead code detection with proper exit codes (#996)
Adds automated dead code detection that runs weekly and properly reports failures. - Fix dead code script to return exit code 1 when dead code is detected - Add weekly GitHub action that runs every Saturday at 4 AM UTC - Action creates issues with detailed reports when dead code is found - Integrates with existing @code_is_used decorator system Co-authored-by: florath-ai-assistant[bot] <Andreas.Florath@telekom.de>
1 parent a4a3c68 commit a1eeb19

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
name: Dead Code Detection
2+
3+
on:
4+
# Run weekly on Saturdays at 4 AM UTC
5+
schedule:
6+
- cron: '0 4 * * 6'
7+
# Allow manual triggering
8+
workflow_dispatch:
9+
10+
jobs:
11+
dead-code-detection:
12+
name: Dead Code Detection
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Set up Python 3.11
19+
uses: actions/setup-python@v5
20+
with:
21+
python-version: "3.11"
22+
cache: 'pip'
23+
24+
- name: Install dependencies
25+
run: |
26+
python -m pip install --upgrade pip
27+
pip install -e .[dev]
28+
29+
- name: Run dead code detection
30+
id: dead-code-check
31+
run: |
32+
python scripts/detect-dead-code.py > dead-code-report.txt 2>&1
33+
continue-on-error: true
34+
35+
- name: Archive dead code detection results
36+
uses: actions/upload-artifact@v4
37+
if: always()
38+
with:
39+
name: dead-code-detection-results
40+
path: dead-code-report.txt
41+
retention-days: 30
42+
43+
- name: Create issue on dead code detection
44+
if: steps.dead-code-check.outcome == 'failure'
45+
uses: actions/github-script@v7
46+
with:
47+
script: |
48+
const fs = require('fs');
49+
let reportContent = '';
50+
try {
51+
reportContent = fs.readFileSync('dead-code-report.txt', 'utf8');
52+
} catch (error) {
53+
reportContent = 'Could not read dead code detection report.';
54+
}
55+
56+
const issue = await github.rest.issues.create({
57+
owner: context.repo.owner,
58+
repo: context.repo.repo,
59+
title: 'Dead Code Detected - ' + new Date().toISOString().split('T')[0],
60+
body: `The weekly dead code detection has identified potentially unused functions.
61+
62+
**Workflow Run:** ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
63+
64+
## Dead Code Report
65+
66+
\`\`\`
67+
${reportContent}
68+
\`\`\`
69+
70+
Please review the identified functions and either:
71+
1. Remove them if they are truly unused
72+
2. Mark them with \`@code_is_used\` decorator if they are used but not detected by runtime tracing (e.g., polymorphic methods, decorator-replaced methods, etc.)
73+
74+
See the script documentation in \`scripts/detect-dead-code.py\` for more details on when to use the \`@code_is_used\` decorator.
75+
76+
[AI-assisted]`,
77+
labels: ['maintenance', 'automated', 'dead-code']
78+
});
79+
console.log('Created issue:', issue.data.html_url);

scripts/detect-dead-code.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,11 @@ def main() -> None:
723723
else:
724724
print(" No functions discovered.")
725725

726+
# Exit with error code if dead code was found
727+
if dead_functions:
728+
import sys
729+
sys.exit(1)
730+
726731

727732
if __name__ == "__main__":
728733
main()

0 commit comments

Comments
 (0)