cube-ms
Version:
Production-ready microservice framework with health monitoring, validation, error handling, and Docker Swarm support
337 lines (263 loc) โข 7.82 kB
Markdown
# ๐ Git Hooks & Code Quality Enforcement
Cube-MS projects come with **mandatory git hooks** that enforce code quality and testing standards. This ensures that **all code pushed to the repository has been tested and validated**.
## ๐ฏ What Git Hooks Do
### โ
**Enforced Standards**
- **Unit tests must pass** before code can be pushed
- **Environment validation** before every commit/push
- **Code linting** must pass before commits
- **Commit message format** must follow conventions
- **No sensitive data** allowed in commits
### ๐ซ **What Gets Blocked**
- Commits with linting errors
- Commits with failing tests
- Pushes without passing full test suite
- Commits with invalid message format
- Commits containing potential secrets
## ๐ Hook Types
### 1. **Pre-Commit Hook**
Runs before each commit:
```bash
โ
Environment validation
โ
ESLint code formatting
โ
Unit tests on staged files only
โ
Sensitive data detection
```
### 2. **Pre-Push Hook**
Runs before each push:
```bash
โ
Environment validation
โ
Full linting check
โ
Complete unit test suite
โ
Test coverage analysis
โ
Docker build validation
โ
Security scan
โ
Branch-specific validations
```
### 3. **Commit Message Hook**
Validates commit message format:
```bash
โ
Conventional commits format
โ
Minimum message length
โ
Descriptive content
```
## ๐ Automatic Setup
### During Project Creation
```bash
# Git hooks are automatically installed when you create a project
cube-ms create my-app
cd my-app
# Hooks are already active! โ
```
### Manual Setup (if needed)
```bash
# Install hooks manually
npm run postinstall
# Or run full setup
npm run setup
```
## ๐ Commit Message Format
### โ
**Accepted Formats**
**Conventional Commits (Recommended):**
```bash
feat(auth): implement JWT authentication system
fix(api): resolve validation error in user endpoint
docs(readme): update installation instructions
test(auth): add unit tests for auth service
chore(deps): update dependencies to latest versions
refactor(user): simplify user service logic
style(format): fix code formatting issues
perf(db): optimize database query performance
```
**Basic Format (Minimum):**
```bash
Add user registration functionality
Fix database connection timeout issue
Update API documentation with examples
Implement rate limiting for API endpoints
```
### โ **Rejected Formats**
```bash
fix # Too short
update # Not descriptive
changes # Vague
wip # Work in progress
minor fixes # Not specific
test # What kind of test?
```
## ๐งช Testing Commands
### Run Tests Manually
```bash
# Run unit tests only
npm run test:unit
# Run with coverage
npm run test:coverage
# Run CI test suite (what pre-push runs)
npm run test:ci
# Test pre-commit hook
npm run pre-commit
# Test pre-push hook
npm run pre-push
```
### Validate Environment
```bash
# Check environment configuration
npm run validate-env
# Fix linting issues
npm run lint:fix
```
## ๐จ Bypassing Hooks (Emergency Only)
### โ ๏ธ **Bypass Options**
```bash
# Skip commit hooks (emergency only)
git commit --no-verify -m "emergency fix"
# Skip push hooks (emergency only)
git push --no-verify
# Disable all hooks temporarily
HUSKY=0 git commit -m "bypass all hooks"
HUSKY=0 git push
```
### ๐ฅ **When to Bypass**
- **Production outages** requiring immediate fixes
- **Critical security vulnerabilities**
- **Infrastructure failures**
### ๐ฏ **Best Practice**
1. Fix the issue with bypass
2. **Immediately** create a follow-up commit that passes all tests
3. Document why bypass was needed
## ๐ ๏ธ Troubleshooting
### Common Issues & Solutions
#### 1. **"Tests failing" Error**
```bash
โ Unit tests failed!
# Solutions:
npm run test:unit # See which tests fail
npm run test:coverage # Check test coverage
npm run validate-env # Fix environment issues
```
#### 2. **"Linting errors" Error**
```bash
โ Linting failed!
# Solutions:
npm run lint:fix # Auto-fix most issues
npm run lint # See specific errors
```
#### 3. **"Environment validation failed" Error**
```bash
โ Environment validation failed!
# Solutions:
cp .env.example .env # Copy environment template
npm run validate-env # See specific issues
```
#### 4. **"Invalid commit message" Error**
```bash
โ Invalid commit message format!
# Solutions:
git commit --amend -m "feat(api): your descriptive message"
```
#### 5. **"Docker build failed" Error**
```bash
โ Docker build failed!
# Solutions:
docker build -t test . # Test Docker build
docker-compose up --build # Rebuild containers
```
### Hooks Not Working?
```bash
# Re-install hooks
npm run postinstall
# Check if hooks exist
ls -la .husky/
# Make hooks executable
chmod +x .husky/*
# Test hook manually
./.husky/pre-commit
```
## ๐ฏ Branch-Specific Rules
### **Main/Master Branch**
- โ
Extra production readiness checks
- โ
Commit message format validation
- โ
Docker build validation
- โ
Full security scan
### **Development Branch**
- โ
Standard checks
- โ
All tests must pass
- โ
Environment validation
### **Feature Branches**
- โ
Standard checks
- โ
Unit tests for changed files
- โ
Basic validations
## ๐ CI/CD Integration
### GitHub Actions
Git hooks work seamlessly with CI/CD:
```yaml
# .github/workflows/ci.yml
- name: ๐งช Run tests (same as pre-push hook)
run: npm run test:ci
- name: ๐งน Lint code (same as pre-commit hook)
run: npm run lint
- name: ๐ Validate environment
run: npm run validate-env
```
### Docker Integration
```bash
# Hooks validate Docker builds
docker build -t my-app . # Tested in pre-push
docker-compose up --build # Tested in CI
```
## ๐ Benefits
### โ
**Code Quality**
- No failing tests reach the repository
- Consistent code formatting
- Proper commit history
### โ
**Team Collaboration**
- Everyone follows same standards
- Reduced code review time
- Less bugs in production
### โ
**DevOps Ready**
- CI/CD pipelines run faster
- Docker builds are validated
- Environment issues caught early
## ๐ก Tips & Best Practices
### ๐ **Developer Workflow**
```bash
# 1. Make changes
vim src/my-feature.js
# 2. Test locally (optional but recommended)
npm run test:unit
# 3. Commit (hooks run automatically)
git add .
git commit -m "feat(api): add new endpoint"
# โ
Pre-commit hook runs: lint + tests + validation
# 4. Push (hooks run automatically)
git push
# โ
Pre-push hook runs: full test suite + coverage + security
```
### ๐ฏ **Writing Commit Messages**
```bash
# Good examples:
feat(auth): add JWT token refresh endpoint
fix(db): resolve connection pool timeout issue
docs(api): update authentication examples
test(user): add comprehensive user service tests
chore(deps): upgrade express to v4.21.2
# Include context when needed:
fix(auth): resolve JWT expiration causing 401 errors
feat(api): add user profile update with avatar upload
test(integration): add end-to-end API workflow tests
```
### ๐ **Security Best Practices**
- Never bypass hooks to commit secrets
- Use environment variables for sensitive data
- Regularly update dependencies
- Review security warnings in pre-push
---
## ๐ฏ Quick Reference
| Command | Purpose |
|---------|---------|
| `npm run test:ci` | Full test suite (same as pre-push) |
| `npm run lint:fix` | Fix code formatting |
| `npm run validate-env` | Check environment config |
| `git commit --no-verify` | Bypass commit hooks (emergency) |
| `git push --no-verify` | Bypass push hooks (emergency) |
| `HUSKY=0 git push` | Disable all hooks |
**Remember**: Git hooks are there to help you write better code and catch issues early! ๐