|
| 1 | +name: Radon Code Metrics |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: # This allows manual triggering |
| 5 | + push: |
| 6 | + paths: |
| 7 | + - '**.py' # This will run the workflow only when Python files are changed |
| 8 | + pull_request: |
| 9 | + paths: |
| 10 | + - '**.py' # This will run the workflow only when Python files are changed |
| 11 | + |
| 12 | +jobs: |
| 13 | + radon: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - uses: actions/checkout@v3 |
| 17 | + with: |
| 18 | + fetch-depth: 0 # This is important to fetch all history for comparing changes |
| 19 | + |
| 20 | + - name: Set up Python |
| 21 | + uses: actions/setup-python@v4 |
| 22 | + with: |
| 23 | + python-version: '3.x' |
| 24 | + |
| 25 | + - name: Install radon |
| 26 | + run: pip install radon |
| 27 | + |
| 28 | + - name: Run radon |
| 29 | + run: | |
| 30 | + # Get the list of changed Python files |
| 31 | + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 32 | + CHANGED_FILES=$(git ls-files '*.py') |
| 33 | + else |
| 34 | + CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '\.py$' || echo "") |
| 35 | + fi |
| 36 | + |
| 37 | + if [ -n "$CHANGED_FILES" ]; then |
| 38 | + # Run Cyclomatic Complexity check |
| 39 | + echo "Running Cyclomatic Complexity check..." |
| 40 | + radon cc $CHANGED_FILES -a -s -n D |
| 41 | + |
| 42 | + # Run Maintainability Index check |
| 43 | + echo "Running Maintainability Index check..." |
| 44 | + radon mi $CHANGED_FILES -s -n D |
| 45 | + else |
| 46 | + echo "No Python files to analyze." |
| 47 | + fi |
| 48 | + continue-on-error: true |
| 49 | + |
| 50 | + - name: Check radon output |
| 51 | + run: | |
| 52 | + # Get the list of changed Python files |
| 53 | + if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then |
| 54 | + CHANGED_FILES=$(git ls-files '*.py') |
| 55 | + else |
| 56 | + CHANGED_FILES=$(git diff --name-only ${{ github.event.before }} ${{ github.sha }} | grep '\.py$' || echo "") |
| 57 | + fi |
| 58 | + |
| 59 | + if [ -n "$CHANGED_FILES" ]; then |
| 60 | + # Run checks and capture output |
| 61 | + CC_OUTPUT=$(radon cc $CHANGED_FILES -a -s -n D) |
| 62 | + MI_OUTPUT=$(radon mi $CHANGED_FILES -s -n D) |
| 63 | + |
| 64 | + # Check if there's any output (which indicates issues) |
| 65 | + if [ -n "$CC_OUTPUT" ] || [ -n "$MI_OUTPUT" ]; then |
| 66 | + echo "Radon detected code complexity or maintainability issues:" |
| 67 | + [ -n "$CC_OUTPUT" ] && echo "$CC_OUTPUT" |
| 68 | + [ -n "$MI_OUTPUT" ] && echo "$MI_OUTPUT" |
| 69 | + exit 1 |
| 70 | + else |
| 71 | + echo "No code complexity or maintainability issues detected." |
| 72 | + fi |
| 73 | + else |
| 74 | + echo "No Python files to analyze." |
| 75 | + fi |
0 commit comments