Thanks to visit codestin.com
Credit goes to github.com

Skip to content

peterscheffer/RefactorCrew

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LangGraph SonarQube Refactoring Agent

This is a LangGraph-based automated refactoring agent designed to process SonarQube issues and automatically fix them in a codebase.

Overview

The LangGraph SonarQube Refactoring Agent is an automated system that:

  1. Fetches open SonarQube issues assigned to a specific user
  2. Processes these issues in batches of up to 10
  3. Applies automated fixes using Qwen2.5-Coder model
  4. Creates GitHub pull requests with the changes

Key Features

Batch Processing

  • 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

Indentation Preservation

  • 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

GitHub Integration

  • Creates professional pull requests using the GitHub CLI (gh)
  • Includes detailed PR descriptions with issue lists
  • Automatically handles branch creation and pushing

SonarQube Integration

  • 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

Configuration

Environment Variables Required

  • GITHUB_TOKEN - GitHub personal access token for PR creation
  • OLLAMA_BASE_URL - URL to Ollama service (defaults to http://localhost:11434/v1)

Setup Instructions

  1. Clone and install dependencies:

    git clone https://github.com/peterscheffer/RefactorCrew.git
    cd RefactorCrew
    pip install -r requirements-core.txt
  2. Configure environment variables:

    cp .env.example .env

    Edit .env with your credentials:

    • SONARQUBE_TOKEN - Your SonarQube API token
    • SONARQUBE_URL - Your SonarQube instance URL
    • GITHUB_TOKEN - Your GitHub personal access token
    • GITHUB_REPO - Target repository (owner/repo)
    • PROJECT_ROOT - Path to your project directory
    • SLACK_TOKEN - (Optional) Slack webhook for notifications
    • OPENROUTER_API_KEY - (If using OpenRouter instead of Ollama)
  3. Run the agent:

    python aider_refactor_langgraph.py

Architecture

LangGraph State Machine

The agent uses a LangGraph state machine with nodes that handle:

  1. Sonar Analyst: Fetches issues from SonarQube
  2. Git State Verification: Ensures clean working directory
  3. Branch Creation: Creates fresh branches for changes
  4. Issue Extraction: Processes individual issues
  5. Rule Fetching: Retrieves fix guidance from SonarQube
  6. Fix Application: Applies fixes using the LLM
  7. Change Recording: Tracks whether actual changes were made
  8. Batch Management: Handles batch boundaries and PR creation

Processing Flow

  1. Fetch all open SonarQube issues assigned to the configured user
  2. Process issues in batches of 10 or fewer
  3. For each issue:
    • Retrieve the specific code location and rule details
    • Apply an automated fix using Qwen2.5-Coder
    • Preserve original indentation style
  4. Commit changes to a new branch
  5. Create a pull request with the fixes

Usage

To run this agent:

python aider_refactor_langgraph.py

The agent will:

  1. Connect to SonarQube and fetch issues
  2. Process them in batches of 10 or fewer
  3. Create branches and PRs for each batch
  4. Apply fixes using LLMs (via Aider)

Technical Details

Indentation Handling

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

Change Detection

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

Error Handling

The agent includes comprehensive error handling:

  • Graceful failure when SonarQube is unreachable
  • Safe handling of file access issues
  • Detailed error reporting for debugging

Security Considerations

⚠️ Important: This agent makes automated changes to files and creates GitHub PRs. Review all generated changes before merging.

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

Dependencies

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

Version History

v2.8 - Batching, Line Offset Tracking & Indentation Fix

  • 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

v2.7 - Initial Release

  • Core functionality for fetching SonarQube issues and applying fixes

License

This project is licensed under the MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! See CONTRIBUTING.md for guidelines.

Security

⚠️ Important Security Notes:

  • Never commit .env file containing real credentials to git
  • Always use placeholder values in .env.example
  • Regenerate API tokens if they are accidentally exposed
  • The config/.env.client file should also be kept private

For security issues, please email [email protected] instead of creating a public issue.

Support

For issues, questions, or suggestions, please create a GitHub issue or open a discussion.


Development

Running Tests

pytest tests/

Running with Docker

docker-compose up

For more details, see running_tests.md

About

LangGraph-based automated code refactoring agent for fixing SonarQube issues using Aider

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages