This is a LangGraph-based automated refactoring agent designed to process SonarQube issues and automatically fix them in a codebase.
The LangGraph SonarQube Refactoring Agent is an automated system that:
- Fetches open SonarQube issues assigned to a specific user
- Processes these issues in batches of up to 10
- Applies automated fixes using Qwen2.5-Coder model
- Creates GitHub pull requests with the changes
- Processes issues in batches of maximum 10 issues per PR/branch
- Maintains line offset tracking to handle cumulative changes in files
- Creates fresh branches for each batch to isolate changes
- Detects and preserves the original file indentation style (tabs or spaces)
- Re-applies proper indentation to LLM-generated fixes
- Handles both tab and space-based indentation consistently
- Creates professional pull requests using the GitHub CLI (
gh) - Includes detailed PR descriptions with issue lists
- Automatically handles branch creation and pushing
- Fetches open issues assigned to a specific user from SonarQube
- Retrieves rule details and "how-to-fix" guidance
- Applies fixes while preserving existing code style
GITHUB_TOKEN- GitHub personal access token for PR creationOLLAMA_BASE_URL- URL to Ollama service (defaults tohttp://localhost:11434/v1)
-
Clone and install dependencies:
git clone https://github.com/peterscheffer/RefactorCrew.git cd RefactorCrew pip install -r requirements-core.txt -
Configure environment variables:
cp .env.example .env
Edit
.envwith your credentials:SONARQUBE_TOKEN- Your SonarQube API tokenSONARQUBE_URL- Your SonarQube instance URLGITHUB_TOKEN- Your GitHub personal access tokenGITHUB_REPO- Target repository (owner/repo)PROJECT_ROOT- Path to your project directorySLACK_TOKEN- (Optional) Slack webhook for notificationsOPENROUTER_API_KEY- (If using OpenRouter instead of Ollama)
-
Run the agent:
python aider_refactor_langgraph.py
The agent uses a LangGraph state machine with nodes that handle:
- Sonar Analyst: Fetches issues from SonarQube
- Git State Verification: Ensures clean working directory
- Branch Creation: Creates fresh branches for changes
- Issue Extraction: Processes individual issues
- Rule Fetching: Retrieves fix guidance from SonarQube
- Fix Application: Applies fixes using the LLM
- Change Recording: Tracks whether actual changes were made
- Batch Management: Handles batch boundaries and PR creation
- Fetch all open SonarQube issues assigned to the configured user
- Process issues in batches of 10 or fewer
- For each issue:
- Retrieve the specific code location and rule details
- Apply an automated fix using Qwen2.5-Coder
- Preserve original indentation style
- Commit changes to a new branch
- Create a pull request with the fixes
To run this agent:
python aider_refactor_langgraph.pyThe agent will:
- Connect to SonarQube and fetch issues
- Process them in batches of 10 or fewer
- Create branches and PRs for each batch
- Apply fixes using LLMs (via Aider)
The agent includes sophisticated indentation handling:
- Detects whether files use tabs or spaces for indentation
- Determines the appropriate indentation unit (2, 4, etc. spaces)
- Ensures fixed code maintains the original formatting style
The agent uses Git to detect whether actual changes were made:
- Checks for real file modifications (not just empty fixes)
- Tracks cumulative line offsets to maintain proper issue locations
The agent includes comprehensive error handling:
- Graceful failure when SonarQube is unreachable
- Safe handling of file access issues
- Detailed error reporting for debugging
The agent:
- Runs in a controlled environment with proper Git state validation
- Does not make changes to files outside the configured project root
- Requires explicit confirmation before creating PRs
This agent requires:
- Python 3.8+
- LangGraph and LangChain libraries
- Ollama service running locally
- GitHub CLI (
gh) installed and configured - SonarQube instance accessible at the configured URL
- Implements batch processing of up to 10 issues per PR
- Adds line offset tracking for proper issue location management
- Fixes indentation handling to preserve original file styles
- Core functionality for fetching SonarQube issues and applying fixes
This project is licensed under the MIT License - see LICENSE file for details.
Contributions are welcome! See CONTRIBUTING.md for guidelines.
- Never commit
.envfile containing real credentials to git - Always use placeholder values in
.env.example - Regenerate API tokens if they are accidentally exposed
- The
config/.env.clientfile should also be kept private
For security issues, please email [email protected] instead of creating a public issue.
For issues, questions, or suggestions, please create a GitHub issue or open a discussion.
pytest tests/docker-compose upFor more details, see running_tests.md