Skip to content

Commit 77dc580

Browse files
jamesbrinkclaude
andcommitted
feat(complexity): implement wily for code complexity analysis in local and CI environments
🦊 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f0f81c7 commit 77dc580

File tree

10 files changed

+368
-0
lines changed

10 files changed

+368
-0
lines changed

.cursorrules

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ MCP-NixOS provides MCP resources and tools for NixOS packages, system options, H
1414

1515
Official repository: [https://github.com/utensils/mcp-nixos](https://github.com/utensils/mcp-nixos)
1616

17+
## Branch Management
18+
19+
- Default development branch is `develop`
20+
- Main release branch is `main`
21+
- Branch protection rules are enforced:
22+
- `main`: Requires PR review (1 approval), admin enforcement, no deletion, no force push
23+
- `develop`: Protected from deletion but allows force push
24+
- PRs follow the pattern: commit to `develop` → open PR to `main` → merge once approved
25+
- Branch deletion on merge is disabled to preserve branch history
26+
1727
## Architecture
1828

1929
### Core Components
@@ -122,6 +132,30 @@ Official repository: [https://github.com/utensils/mcp-nixos](https://github.com/
122132
- All tests must check for both default OS cache paths and test-specific paths
123133
- The gitignore file excludes `mcp_nixos_test_cache/` and `*test_cache*/` patterns
124134

135+
### Code Complexity Analysis
136+
- Uses wily to track and report on code complexity metrics
137+
- Available locally via `nix develop -c complexity` command:
138+
- Build cache: `complexity build`
139+
- View report: `complexity report <file> <metric>`
140+
- Generate graph: `complexity graph <file> <metric>`
141+
- Rank files: `complexity rank [path] [metric]`
142+
- Compare changes: `complexity diff [git_ref]`
143+
- Pre-commit hook to check complexity on every commit
144+
- Continuous Integration:
145+
- Separate GitHub workflow for complexity analysis
146+
- Runs on all PRs targeting main branch
147+
- Posts complexity report as PR comment
148+
- Archives detailed reports as artifacts
149+
- Key metrics tracked:
150+
- Cyclomatic complexity
151+
- Maintainability index
152+
- Lines of code
153+
- Comments and documentation
154+
- Code complexity thresholds:
155+
- Functions should maintain cyclomatic complexity < 10
156+
- Files should maintain maintainability index > 65
157+
- Keep module sizes reasonable (< 500 lines preferred)
158+
125159
### Logging Tests
126160
- Prefer direct behavior verification over implementation details
127161
- When testing log level filtering:

.github/workflows/complexity.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Code Complexity Analysis
2+
3+
on:
4+
pull_request:
5+
branches: [main]
6+
workflow_dispatch: # Allow manual trigger
7+
8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.ref }}
10+
cancel-in-progress: true
11+
12+
jobs:
13+
analyze-complexity:
14+
name: Analyze Code Complexity
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
with:
20+
fetch-depth: 0 # Full git history for wily to analyze
21+
22+
- name: Install Nix
23+
uses: cachix/install-nix-action@v27
24+
with:
25+
nix_path: nixpkgs=channel:nixos-unstable
26+
extra_nix_config: |
27+
experimental-features = nix-command flakes
28+
accept-flake-config = true
29+
30+
- name: Cache Nix store
31+
uses: actions/cache@v4
32+
with:
33+
path: |
34+
~/.cache/nix
35+
key: ${{ runner.os }}-nix-${{ hashFiles('flake.lock') }}
36+
restore-keys: |
37+
${{ runner.os }}-nix-
38+
39+
- name: Set up Python environment
40+
run: |
41+
nix develop --command setup
42+
43+
- name: Install wily
44+
run: |
45+
nix develop --command bash -c 'if [ -z "$VIRTUAL_ENV" ]; then source .venv/bin/activate; fi && pip install wily'
46+
47+
- name: Build wily cache
48+
run: |
49+
nix develop --command bash -c 'if [ -z "$VIRTUAL_ENV" ]; then source .venv/bin/activate; fi && wily build mcp_nixos tests'
50+
51+
- name: Find base branch for PR or use default
52+
id: find-base-branch
53+
run: |
54+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
55+
echo "BASE_BRANCH=origin/${{ github.event.pull_request.base.ref }}" >> $GITHUB_OUTPUT
56+
else
57+
echo "BASE_BRANCH=HEAD^1" >> $GITHUB_OUTPUT
58+
fi
59+
60+
- name: Run wily diff
61+
id: wily-diff
62+
run: |
63+
if [ -z "$VIRTUAL_ENV" ]; then source .venv/bin/activate; fi
64+
65+
echo "Running complexity analysis comparing to ${{ steps.find-base-branch.outputs.BASE_BRANCH }}"
66+
DIFF_OUTPUT=$(wily diff mcp_nixos tests -r ${{ steps.find-base-branch.outputs.BASE_BRANCH }})
67+
68+
# Set multi-line output for GitHub Actions
69+
echo "DIFF_OUTPUT<<EOF" >> $GITHUB_ENV
70+
echo "$DIFF_OUTPUT" >> $GITHUB_ENV
71+
echo "EOF" >> $GITHUB_ENV
72+
73+
# Store output as artifact
74+
mkdir -p complexity-report
75+
echo "$DIFF_OUTPUT" > complexity-report/diff.txt
76+
77+
# Also create a more detailed report of top 10 most complex files
78+
wily rank mcp_nixos -n 10 mi > complexity-report/top10_maintainability.txt
79+
wily rank mcp_nixos -n 10 raw.loc > complexity-report/top10_loc.txt
80+
wily rank mcp_nixos -n 10 cyclomatic.complexity > complexity-report/top10_cyclomatic.txt
81+
82+
- name: Upload complexity report
83+
uses: actions/upload-artifact@v4
84+
with:
85+
name: complexity-report
86+
path: complexity-report/
87+
88+
- name: Add PR comment with complexity analysis
89+
if: github.event_name == 'pull_request'
90+
uses: thollander/actions-comment-pull-request@v2
91+
with:
92+
message: |
93+
## Code Complexity Analysis
94+
95+
```
96+
${{ env.DIFF_OUTPUT }}
97+
```
98+
99+
For more details, check the complexity-report artifact in the workflow run.
100+
comment_tag: complexity-analysis

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ uv-*.lock
7070
mcp-completion-docs.md
7171
TODO.md
7272

73+
# Wily
74+
.wily/
75+
7376
# Logs
7477
*.log
7578
*.DS_Store

.goosehints

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ MCP-NixOS provides MCP resources and tools for NixOS packages, system options, H
1414

1515
Official repository: [https://github.com/utensils/mcp-nixos](https://github.com/utensils/mcp-nixos)
1616

17+
## Branch Management
18+
19+
- Default development branch is `develop`
20+
- Main release branch is `main`
21+
- Branch protection rules are enforced:
22+
- `main`: Requires PR review (1 approval), admin enforcement, no deletion, no force push
23+
- `develop`: Protected from deletion but allows force push
24+
- PRs follow the pattern: commit to `develop` → open PR to `main` → merge once approved
25+
- Branch deletion on merge is disabled to preserve branch history
26+
1727
## Architecture
1828

1929
### Core Components
@@ -122,6 +132,30 @@ Official repository: [https://github.com/utensils/mcp-nixos](https://github.com/
122132
- All tests must check for both default OS cache paths and test-specific paths
123133
- The gitignore file excludes `mcp_nixos_test_cache/` and `*test_cache*/` patterns
124134

135+
### Code Complexity Analysis
136+
- Uses wily to track and report on code complexity metrics
137+
- Available locally via `nix develop -c complexity` command:
138+
- Build cache: `complexity build`
139+
- View report: `complexity report <file> <metric>`
140+
- Generate graph: `complexity graph <file> <metric>`
141+
- Rank files: `complexity rank [path] [metric]`
142+
- Compare changes: `complexity diff [git_ref]`
143+
- Pre-commit hook to check complexity on every commit
144+
- Continuous Integration:
145+
- Separate GitHub workflow for complexity analysis
146+
- Runs on all PRs targeting main branch
147+
- Posts complexity report as PR comment
148+
- Archives detailed reports as artifacts
149+
- Key metrics tracked:
150+
- Cyclomatic complexity
151+
- Maintainability index
152+
- Lines of code
153+
- Comments and documentation
154+
- Code complexity thresholds:
155+
- Functions should maintain cyclomatic complexity < 10
156+
- Files should maintain maintainability index > 65
157+
- Keep module sizes reasonable (< 500 lines preferred)
158+
125159
### Logging Tests
126160
- Prefer direct behavior verification over implementation details
127161
- When testing log level filtering:

.pre-commit-config.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v4.5.0
4+
hooks:
5+
- id: trailing-whitespace
6+
- id: end-of-file-fixer
7+
- id: check-yaml
8+
- id: check-added-large-files
9+
10+
- repo: https://github.com/psf/black
11+
rev: 24.1.1
12+
hooks:
13+
- id: black
14+
args: ["--line-length", "120"]
15+
16+
- repo: https://github.com/pycqa/flake8
17+
rev: 7.0.0
18+
hooks:
19+
- id: flake8
20+
args: ["--max-line-length", "120", "--ignore", "E402,E203"]
21+
22+
- repo: local
23+
hooks:
24+
- id: wily
25+
name: wily
26+
entry: wily diff
27+
verbose: true
28+
language: python
29+
additional_dependencies: [wily]

.windsurfrules

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ MCP-NixOS provides MCP resources and tools for NixOS packages, system options, H
1414

1515
Official repository: [https://github.com/utensils/mcp-nixos](https://github.com/utensils/mcp-nixos)
1616

17+
## Branch Management
18+
19+
- Default development branch is `develop`
20+
- Main release branch is `main`
21+
- Branch protection rules are enforced:
22+
- `main`: Requires PR review (1 approval), admin enforcement, no deletion, no force push
23+
- `develop`: Protected from deletion but allows force push
24+
- PRs follow the pattern: commit to `develop` → open PR to `main` → merge once approved
25+
- Branch deletion on merge is disabled to preserve branch history
26+
1727
## Architecture
1828

1929
### Core Components
@@ -122,6 +132,30 @@ Official repository: [https://github.com/utensils/mcp-nixos](https://github.com/
122132
- All tests must check for both default OS cache paths and test-specific paths
123133
- The gitignore file excludes `mcp_nixos_test_cache/` and `*test_cache*/` patterns
124134

135+
### Code Complexity Analysis
136+
- Uses wily to track and report on code complexity metrics
137+
- Available locally via `nix develop -c complexity` command:
138+
- Build cache: `complexity build`
139+
- View report: `complexity report <file> <metric>`
140+
- Generate graph: `complexity graph <file> <metric>`
141+
- Rank files: `complexity rank [path] [metric]`
142+
- Compare changes: `complexity diff [git_ref]`
143+
- Pre-commit hook to check complexity on every commit
144+
- Continuous Integration:
145+
- Separate GitHub workflow for complexity analysis
146+
- Runs on all PRs targeting main branch
147+
- Posts complexity report as PR comment
148+
- Archives detailed reports as artifacts
149+
- Key metrics tracked:
150+
- Cyclomatic complexity
151+
- Maintainability index
152+
- Lines of code
153+
- Comments and documentation
154+
- Code complexity thresholds:
155+
- Functions should maintain cyclomatic complexity < 10
156+
- Files should maintain maintainability index > 65
157+
- Keep module sizes reasonable (< 500 lines preferred)
158+
125159
### Logging Tests
126160
- Prefer direct behavior verification over implementation details
127161
- When testing log level filtering:

CLAUDE.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,30 @@ Official repository: [https://github.com/utensils/mcp-nixos](https://github.com/
132132
- All tests must check for both default OS cache paths and test-specific paths
133133
- The gitignore file excludes `mcp_nixos_test_cache/` and `*test_cache*/` patterns
134134

135+
### Code Complexity Analysis
136+
- Uses wily to track and report on code complexity metrics
137+
- Available locally via `nix develop -c complexity` command:
138+
- Build cache: `complexity build`
139+
- View report: `complexity report <file> <metric>`
140+
- Generate graph: `complexity graph <file> <metric>`
141+
- Rank files: `complexity rank [path] [metric]`
142+
- Compare changes: `complexity diff [git_ref]`
143+
- Pre-commit hook to check complexity on every commit
144+
- Continuous Integration:
145+
- Separate GitHub workflow for complexity analysis
146+
- Runs on all PRs targeting main branch
147+
- Posts complexity report as PR comment
148+
- Archives detailed reports as artifacts
149+
- Key metrics tracked:
150+
- Cyclomatic complexity
151+
- Maintainability index
152+
- Lines of code
153+
- Comments and documentation
154+
- Code complexity thresholds:
155+
- Functions should maintain cyclomatic complexity < 10
156+
- Files should maintain maintainability index > 65
157+
- Keep module sizes reasonable (< 500 lines preferred)
158+
135159
### Logging Tests
136160
- Prefer direct behavior verification over implementation details
137161
- When testing log level filtering:

0 commit comments

Comments
 (0)