Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/auto-label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Auto-label Bot
on:
issues:
types: [opened, edited]
pull_request:
pull_request_target:
types: [opened, edited, synchronize]

permissions:
Expand Down Expand Up @@ -116,7 +116,7 @@ jobs:
}

label-pull-requests:
if: github.event_name == 'pull_request'
if: github.event_name == 'pull_request_target'
runs-on: ubuntu-latest
steps:
- name: Checkout code
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Coverage
on:
push:
branches: [ main ]
pull_request:
pull_request_target:
branches: [ main ]

jobs:
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
path: htmlcov/

- name: Comment coverage on PR
if: github.event_name == 'pull_request'
if: github.event_name == 'pull_request_target'
uses: py-cov-action/python-coverage-comment-action@v3
with:
GITHUB_TOKEN: ${{ github.token }}
Expand Down
65 changes: 65 additions & 0 deletions docs/api-reference/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,71 @@ print(f"AST cache hits: {stats['ast_cache']['hits']}")
refactron.clear_caches()
```

## Core Modules API

Refactron provides dedicated APIs for its core subsystems:

### Analyzers API
```python
from refactron.analyzers import CodeSmellAnalyzer, SecurityAnalyzer

# Run specific analyzers programmatically
code_analyzer = CodeSmellAnalyzer(config)
issues = code_analyzer.analyze(file_path="src/main.py")

security = SecurityAnalyzer(config)
vulns = security.detect_vulnerabilities(ast_tree)
```

### Autofix API
```python
from refactron.autofix import AutofixEngine

engine = AutofixEngine(config)
# Apply fixes for specified issues
result = engine.apply_fixes(issues, backup=True)
print(f"Fixed {result.fixed_count} issues. Backup at {result.backup_path}")
```

### LLM Integration API
```python
from refactron.llm import LLMProvider, PromptManager

llm = LLMProvider.get_default(config)
# Generate a documentation string
docstring = llm.generate("Write a comprehensive Python docstring for this function", context=func_code)

manager = PromptManager()
formatted_prompt = manager.create_prompt(task_type="refactor", code_snippet=snippet)
```

### RAG API
```python
from refactron.rag import RAGSearch

# Initialize search index
search = RAGSearch(config)
search.index_directory("myproject/")

# Query codebase
results = search.query("authentication logic", top_k=5)
for res in results:
print(f"Match in {res.file}: {res.score}")
```

### Refactorers API
```python
from refactron.refactorers import ExtractConstantRefactorer

refactorer = ExtractConstantRefactorer(config)
# Check applicability
can_refactor = refactorer.is_applicable(node)

if can_refactor:
changes = refactorer.compute_diff(node)
refactorer.apply(changes)
```

## Next Steps

<Card
Expand Down
10 changes: 9 additions & 1 deletion docs/cli/commands.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,15 @@ refactron auth <command>
```

**Commands:**
- `login` - Log in to account
- `status` - Check authentication status
- `logout` - Log out from account

**Examples:**
```bash
# Login
refactron auth login

# Check auth status
refactron auth status

Expand All @@ -288,16 +292,20 @@ refactron repo <command> [options]
```

**Commands:**
- `init` - Initialize repository
- `list` - List connected repositories
- `connect` - Connect a repository
- `disconnect` - Disconnect a repository

**Options:**
- `--path PATH` - Repository path (for connect)
- `--path PATH` - Repository path (for connect/init)
- `--delete-files` - Delete local files (for disconnect)

**Examples:**
```bash
# Initialize repo
refactron repo init

# List repos
refactron repo list

Expand Down
50 changes: 37 additions & 13 deletions docs/essentials/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,33 +109,57 @@ pattern_storage_dir: null # null = auto-detect
</Accordion>
</AccordionGroup>

## Threshold Configuration
## Advanced Threshold Configuration

Adjust thresholds to match your project's standards:
Refactron allows granular control over thresholds to match your team's specific coding standards. You can configure rules per-analyzer:

```yaml .refactron.yaml
# Complexity thresholds
max_function_complexity: 15 # Default: 10
max_function_length: 100 # Default: 50
max_parameters: 7 # Default: 5
max_nesting_depth: 4 # Default: 3

# Code quality thresholds
min_maintainability_index: 60 # Default: 65
max_cognitive_complexity: 20 # Default: 15
# Complexity analyzers
complexity:
max_function_complexity: 15 # Default: 10
max_function_length: 100 # Default: 50
max_cognitive_complexity: 20 # Default: 15
min_maintainability_index: 60 # Default: 65

# Code smell analyzers
code_smell:
max_parameters: 7 # Default: 5
max_nesting_depth: 4 # Default: 3
max_returns_count: 5 # Default: 3
max_boolean_arguments: 2 # Default: 2

# Security analyzers
security:
strict_mode: true # Fail on any security issue
check_subprocesses: true
check_sql_injection: true
```

## Exclude Patterns
## Advanced Exclude Patterns

Exclude files or directories from analysis:
You can define powerful exclusion rules using glob patterns, or exclude specific rules within specific directories:

```yaml .refactron.yaml
exclude_patterns:
# Standard ignore patterns
- "*/tests/*"
- "*/migrations/*"
- "*/venv/*"
- "*.pyc"
- "__pycache__"
# Ignore generated files
- "*/generated/*"
- "*_pb2.py"

# Rule-specific exclusions
per_file_ignores:
# Ignore magic numbers in test files
"*/tests/*":
- magic-number
# Ignore high complexity in specific legacy files
"legacy_module.py":
- complexity
- long-function
```

## Environment Variables
Expand Down
32 changes: 32 additions & 0 deletions docs/guides/code-analysis.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,38 @@ refactron analyze src/module1/
refactron analyze src/module2/
```

## Real-World Recipes

### Recipe: Scanning for SQL Injection in CI

A common requirement is to block pull requests that introduce critical security issues like SQL Injection. You can run Refactron in a strict mode during your CI pipeline:

```yaml .github/workflows/security-scan.yml
name: Security Scan
on: [push, pull_request]

jobs:
analyze:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Refactron
run: pip install refactron
- name: Run Security Analysis
run: |
# Use focused config for CI
refactron analyze . \
--config .refactron-ci.yaml \
--format json -o security-report.json
- name: Fail on Critical Issues
run: |
# Exit with error if any critical SQLi found
if grep -q '"level": "CRITICAL"' security-report.json; then
echo "Security check failed!"
exit 1
fi
```

## Next Steps

<CardGroup cols={2}>
Expand Down
31 changes: 31 additions & 0 deletions docs/guides/refactoring.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,37 @@ refactron feedback <operation-id> --action accepted
</Accordion>
</AccordionGroup>

## Real-World Recipes

### Recipe: Applying Batch Refactoring

When performing large-scale migrations, such as updating a legacy codebase or standardizing docstrings across hundreds of files, you can use Refactron's batch processing for automated refactoring:

```python
from refactron import Refactron
import glob

refactron = Refactron()

# Get all target files
python_files = glob.glob("legacy_app/**/*.py", recursive=True)

# Apply specific refactoring type in a batch
results = []
for file_path in python_files:
# Adding docstrings to all functions
result = refactron.refactor(
file_path,
preview=False,
operation_types=["add_docstring", "extract_constant"],
risk_level="safe" # Ensure only completely safe changes apply
)
if result.success:
results.append(result)

print(f"Successfully processed {len(results)} files automatically.")
```

## Next Steps

<CardGroup cols={2}>
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ profile = "black"
line_length = 100

[tool.mypy]
python_version = "3.8"
python_version = "3.9"
warn_return_any = true
warn_unused_configs = true
disallow_untyped_defs = true
ignore_missing_imports = true
follow_imports = "skip"

[[tool.mypy.overrides]]
module = "tests.*"
Expand Down
Loading
Loading