Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
13 changes: 13 additions & 0 deletions .github/CI_CD_SETUP_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,23 @@ The CI pipeline runs on every PR and push to `main`/`develop`:
### Workflows

- **ci.yml** - Main CI pipeline
- **self-optimize.yml** - Continuous self-optimization (NEW!)
- **deploy-preview.yml** - Vercel preview deployments
- **codeql-analysis.yml** - Security code scanning
- **auto-merge.yml** - Automated PR merging (existing)

### Self-Optimization Workflow (NEW!)

Automatically analyzes and optimizes PRs:
- Auto-fixes ESLint issues
- Detects and reports dead code
- Analyzes code complexity
- Identifies test coverage gaps
- Flags security issues
- Posts inline PR comments

See [Self-Optimization Guide](SELF_OPTIMIZATION_GUIDE.md) for details.

### Dependabot

Automatically creates PRs for dependency updates:
Expand Down
366 changes: 366 additions & 0 deletions .github/SELF_OPTIMIZATION_GUIDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,366 @@
# Continuous Self-Optimization Workflow

## Overview

The Continuous Self-Optimization Workflow is an automated system that analyzes, optimizes, and improves code quality on every pull request. It performs comprehensive analysis and applies safe, non-breaking fixes automatically while flagging issues that require manual review.

## Features

### 1. Automated Code Analysis

The workflow performs multi-faceted analysis on every PR:

- **ESLint Analysis**: Identifies style violations, potential bugs, and code quality issues
- **Type Safety Check**: Ensures TypeScript strict mode compliance
- **Complexity Analysis**: Detects overly complex functions that need refactoring
- **Test Coverage**: Identifies gaps in test coverage
- **Dead Code Detection**: Finds unused exports, imports, and unreachable code
- **Security Scanning**: Detects risky patterns and potential vulnerabilities

### 2. Automated Fixes

Safe, non-breaking fixes are automatically applied and committed:

- **ESLint Auto-fix**: Automatically fixes style violations and simple issues
- **Code Formatting**: Ensures consistent code style across the codebase
- **Import Organization**: Removes unused imports and organizes imports
- **Type Safety Improvements**: Adds missing type annotations where safe

### 3. Dead Code Removal

The workflow identifies and helps remove:

- **Unused Exports**: Functions, classes, and variables not used anywhere
- **Unreachable Code**: Code after return statements or in impossible branches
- **Commented Code**: Large blocks of commented-out code
- **Duplicate Code**: Identical or similar code blocks that should be refactored

### 4. Inline PR Comments

The workflow posts inline comments on specific lines of code for:

- **High Complexity Functions**: Functions exceeding complexity thresholds
- **Security Issues**: Use of dangerous patterns like `eval()`
- **TODO/FIXME Items**: Technical debt that should be addressed
- **Console.log Usage**: Production code using console.log instead of logger
- **Type Safety Issues**: Excessive use of `any` type

### 5. Comprehensive PR Reports

Each PR receives detailed reports covering:

- **ESLint Fix Summary**: What was automatically fixed
- **Unused Code Report**: Detected dead code with locations
- **Complexity Report**: High-complexity functions requiring refactoring
- **Coverage Analysis**: Test coverage gaps by file
- **Risky Pattern Detection**: Security and quality issues
- **Production Readiness**: Validation that code is production-safe

## Workflow Triggers

The workflow runs on:

- **Pull Request Events**: `opened`, `synchronize`, `reopened`
- **Target Branches**: `main`, `develop`, `dev`

## Workflow Jobs

### Job 1: Analyze & Optimize

**Duration**: ~10-15 minutes

**Steps**:

1. **Checkout Code**: Fetches the PR branch with full history
2. **Setup Environment**: Installs Node.js 20 and dependencies
3. **ESLint Auto-fix**: Runs ESLint with `--fix` flag on backend and webapp
4. **Dead Code Detection**: Uses `ts-prune` to find unused exports
5. **Complexity Analysis**: Identifies functions with high cyclomatic complexity
6. **Coverage Analysis**: Runs tests and identifies low-coverage files
7. **Risky Pattern Detection**: Searches for security issues and code smells
8. **Commit Fixes**: Automatically commits safe fixes back to the PR
9. **Generate Reports**: Creates comprehensive markdown reports
10. **Post PR Comment**: Updates or creates a summary comment on the PR
11. **Create Inline Comments**: Adds specific comments on problematic lines

### Job 2: Validate Production Readiness

**Duration**: ~5-10 minutes

**Steps**:

1. **Checkout Code**: Fetches the updated PR branch
2. **Verify No Mocks**: Ensures no mock/placeholder implementations in production code
3. **Run Test Suite**: Executes full test suite with coverage
4. **Verify Build**: Confirms both backend and webapp build successfully
5. **Post Validation Comment**: Reports production readiness status

## Configuration

### Required Permissions

The workflow requires these GitHub permissions:

```yaml
permissions:
contents: write # To commit automated fixes
pull-requests: write # To post comments
issues: write # To create review comments
checks: write # To report check status
```

### Environment Variables

No additional environment variables required - uses repository secrets automatically.

## What Gets Automatically Fixed

### ✅ Safe Auto-fixes

These are applied automatically:

- Semicolon consistency
- Quote style consistency
- Whitespace and indentation
- Import order
- Unused variable removal (when safe)
- Type inference improvements
- Simple ESLint rule violations

### ⚠️ Manual Review Required

These are flagged for human review:

- High complexity functions (cyclomatic complexity > 10)
- Security risks (eval, innerHTML, etc.)
- Type safety issues (excessive `any` usage)
- TODO/FIXME comments in production code
- Potential mock implementations
- Low test coverage areas

## Understanding the Reports

### ESLint Auto-Fix Report

Shows what was automatically fixed:

```markdown
## ESLint Auto-Fix Results

### Backend Fixes
Fixed 15 issues:
- 8 formatting issues
- 5 unused imports
- 2 quote style inconsistencies

### Webapp Fixes
Fixed 23 issues:
- 15 formatting issues
- 8 unused imports
```

### Unused Code Report

Lists potentially dead code:

```markdown
## Unused Code Detection

### Unused Exports
Found 12 unused exports

#### Details:
src/utils/helpers.ts:45 - unused export 'formatDate'
src/services/legacy.ts:100 - unused export 'oldFunction'
```

### Complexity Report

Identifies complex functions:

```markdown
## Code Complexity Analysis

### High Complexity Issues Found:
- arbitrage.ts:150 - Function 'executeArbitrage' has complexity of 25 (max 10)
- scanner.ts:75 - Function 'scanOpportunities' has complexity of 18 (max 10)
```

### Coverage Report

Shows test coverage gaps:

```markdown
## Test Coverage Analysis

### Coverage Summary:
- Statements: 78.5%
- Branches: 65.3%
- Functions: 82.1%
- Lines: 77.8%

### Files with Low Coverage (<80%):
- src/services/newService.ts: 45%
- src/integrations/api.ts: 62%
```

### Risky Code Report

Flags potential issues:

```markdown
## Risky Code Pattern Detection

### Potential Security Issues:

⚠️ **eval() usage detected (2 instances)** - High security risk
src/utils/dynamic.ts:45: eval(userInput)

⚠️ **Excessive 'any' type usage (156 instances)** - Type safety compromised

📝 **Found 34 TODO/FIXME comments** - Technical debt identified
```

## Best Practices

### For Developers

1. **Review Automated Changes**: Always review what the bot committed
2. **Address Flagged Issues**: Don't ignore warnings in the reports
3. **Reduce Complexity**: Refactor functions flagged for high complexity
4. **Add Tests**: Cover files with low test coverage
5. **Remove Dead Code**: Clean up unused exports and imports
6. **Fix Security Issues**: Address all security warnings immediately

### For Reviewers

1. **Check Bot Comments**: Review inline comments on the PR
2. **Verify Automated Fixes**: Ensure auto-fixes are appropriate
3. **Enforce Standards**: Don't approve PRs with critical issues
4. **Monitor Trends**: Watch for recurring patterns across PRs
5. **Validate Production Readiness**: Ensure no mocks in production code

## Troubleshooting

### Workflow Fails to Commit Fixes

**Cause**: Permission issues or branch protection rules

**Solution**: Ensure the GitHub Actions bot has write permissions and branch protection allows bot commits.

### Too Many Inline Comments

**Cause**: Large PR with many issues

**Solution**: The workflow limits to 50 inline comments. Fix issues in smaller batches.

### False Positives in Dead Code Detection

**Cause**: Dynamic imports or reflection usage

**Solution**: Add `// eslint-disable-next-line` comments or exclude from analysis.

### High Complexity Not Fixed Automatically

**Cause**: Complexity requires manual refactoring

**Solution**: This is intentional. Refactoring complex functions requires human judgment.

## Integration with Existing Workflows

The self-optimization workflow integrates with:

- **ci.yml**: Runs before main CI checks
- **codeql-analysis.yml**: Complements security scanning
- **auto-merge.yml**: Blocks auto-merge if critical issues found
- **deploy-preview.yml**: Ensures quality before deployment

## Metrics and Monitoring

The workflow tracks:

- Number of auto-fixes applied per PR
- Unused code detected and removed
- Complexity trends over time
- Test coverage improvements
- Security issues identified

Artifacts are saved for 30 days for historical analysis.

## Customization

### Adjusting Complexity Thresholds

Edit `.github/workflows/self-optimize.yml`:

```yaml
"complexity": ["warn", 15], # Change from 10 to 15
```

### Adding Custom Checks

Add steps to the workflow:

```yaml
- name: Custom check
run: |
# Your custom analysis
./scripts/custom-check.sh
```

### Excluding Files

Add exclusions to ESLint or analysis commands:

```bash
npx eslint 'src/**/*.ts' --ignore-pattern 'src/legacy/**'
```

## Safety Guarantees

The workflow ensures:

1. **Non-Breaking Changes**: Only safe, validated fixes are committed
2. **Rollback Capability**: All changes are in separate commits, easy to revert
3. **Human Oversight**: Critical issues require manual review
4. **Test Validation**: All automated changes are tested before commit
5. **Production Safety**: No mocks or placeholders allowed in production code

## Performance

- **Runtime**: 15-20 minutes on average PR
- **Concurrency**: Cancels previous runs when new commits pushed
- **Resource Usage**: Standard GitHub Actions runner
- **Artifact Storage**: Reports retained for 30 days

## Future Enhancements

Planned improvements:

1. **AI-Powered Refactoring**: Suggest specific refactoring patterns
2. **Automated Test Generation**: Create tests for uncovered code
3. **Dependency Updates**: Automatically update dependencies
4. **Performance Optimization**: Identify and fix performance bottlenecks
5. **Documentation Generation**: Auto-generate docs from code

## Support

For issues or questions:

1. Check workflow logs in Actions tab
2. Review artifact reports
3. Open an issue with workflow run URL
4. Contact the DevOps team

## Related Documentation

- [CI/CD Setup Guide](.github/CI_CD_SETUP_GUIDE.md)
- [Contributing Guidelines](CONTRIBUTING.md)
- [Security Guide](SECURITY_GUIDE.md)
- [Testing Guide](TESTING.md)

---

**Last Updated**: 2025-12-22
**Version**: 1.0.0
**Status**: ✅ Production Ready
Loading
Loading