Initial commit
This commit is contained in:
191
skills/python-code-quality/patterns/ci-cd-quality-gates.md
Normal file
191
skills/python-code-quality/patterns/ci-cd-quality-gates.md
Normal file
@@ -0,0 +1,191 @@
|
||||
# CI/CD Quality Gates for Ruff and Pyright
|
||||
|
||||
Block merges when code quality fails. Run comprehensive checks in CI that catch
|
||||
issues missed locally.
|
||||
|
||||
## GitHub Actions
|
||||
|
||||
### Basic Quality Check
|
||||
|
||||
Create `.github/workflows/quality.yml`:
|
||||
|
||||
```yaml
|
||||
name: Code Quality
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
quality:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
pip install ruff pyright
|
||||
pip install -r requirements.txt
|
||||
|
||||
- name: Run ruff
|
||||
run: |
|
||||
ruff check .
|
||||
ruff format --check .
|
||||
|
||||
- name: Run pyright
|
||||
run: pyright
|
||||
```
|
||||
|
||||
### Comprehensive Check with Caching
|
||||
|
||||
```yaml
|
||||
name: Code Quality
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
quality:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.11'
|
||||
cache: 'pip'
|
||||
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
pip install ruff pyright
|
||||
pip install -r requirements.txt
|
||||
|
||||
- name: Lint with ruff
|
||||
run: ruff check . --output-format=github
|
||||
|
||||
- name: Check formatting
|
||||
run: ruff format --check . --diff
|
||||
|
||||
- name: Type check with pyright
|
||||
run: pyright --outputjson > pyright-report.json
|
||||
|
||||
- name: Upload pyright report
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: pyright-report
|
||||
path: pyright-report.json
|
||||
```
|
||||
|
||||
## GitLab CI
|
||||
|
||||
Create `.gitlab-ci.yml`:
|
||||
|
||||
```yaml
|
||||
code-quality:
|
||||
stage: test
|
||||
image: python:3.11
|
||||
before_script:
|
||||
- pip install ruff pyright
|
||||
- pip install -r requirements.txt
|
||||
script:
|
||||
- ruff check .
|
||||
- ruff format --check .
|
||||
- pyright
|
||||
rules:
|
||||
- if: $CI_MERGE_REQUEST_IID
|
||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
||||
```
|
||||
|
||||
## Quality Metrics
|
||||
|
||||
### Track Quality Over Time
|
||||
|
||||
```yaml
|
||||
- name: Generate quality report
|
||||
run: |
|
||||
ruff check . --output-format=json > ruff-report.json
|
||||
pyright --outputjson > pyright-report.json
|
||||
|
||||
- name: Comment PR with quality metrics
|
||||
uses: actions/github-script@v7
|
||||
with:
|
||||
script: |
|
||||
const fs = require('fs');
|
||||
const ruffReport = JSON.parse(fs.readFileSync('ruff-report.json'));
|
||||
const pyrightReport = JSON.parse(fs.readFileSync('pyright-report.json'));
|
||||
|
||||
const comment = `## Code Quality Report
|
||||
|
||||
**Ruff:** ${ruffReport.length} issues
|
||||
**Pyright:** ${pyrightReport.generalDiagnostics.length} issues
|
||||
`;
|
||||
|
||||
github.rest.issues.createComment({
|
||||
issue_number: context.issue.number,
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
body: comment
|
||||
});
|
||||
```
|
||||
|
||||
## Branch Protection Rules
|
||||
|
||||
### GitHub
|
||||
|
||||
Settings → Branches → Branch protection rules:
|
||||
|
||||
1. Require status checks to pass before merging
|
||||
2. Select "Code Quality" workflow
|
||||
3. Require branches to be up to date before merging
|
||||
|
||||
### GitLab
|
||||
|
||||
Settings → Repository → Protected branches:
|
||||
|
||||
1. Allowed to merge: Developers + Maintainers
|
||||
2. Require approval from code owners
|
||||
3. Pipelines must succeed
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Fail fast** - Run quality checks before tests
|
||||
2. **Cache dependencies** - Speed up CI with pip caching
|
||||
3. **Parallel jobs** - Run ruff and pyright in parallel
|
||||
4. **Quality trends** - Track violations over time
|
||||
5. **Auto-fix in CI** - Create PR with ruff fixes automatically
|
||||
|
||||
## Auto-fix Bot Example
|
||||
|
||||
```yaml
|
||||
- name: Auto-fix with ruff
|
||||
run: ruff check --fix .
|
||||
|
||||
- name: Commit fixes
|
||||
run: |
|
||||
git config user.name "ruff-bot"
|
||||
git config user.email "bot@example.com"
|
||||
git add .
|
||||
git diff --staged --quiet || git commit -m "style: auto-fix ruff violations"
|
||||
git push
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
**CI passes but pre-commit fails:**
|
||||
|
||||
- Ensure same ruff/pyright versions in CI and pre-commit
|
||||
- Check `.pre-commit-config.yaml` rev matches installed version
|
||||
|
||||
**CI too slow:**
|
||||
|
||||
- Use pip caching
|
||||
- Run quality checks in parallel with tests
|
||||
- Consider skipping pyright on non-Python file changes
|
||||
107
skills/python-code-quality/patterns/pre-commit-integration.md
Normal file
107
skills/python-code-quality/patterns/pre-commit-integration.md
Normal file
@@ -0,0 +1,107 @@
|
||||
# Pre-commit Integration for Ruff and Pyright
|
||||
|
||||
Run quality checks automatically before each commit to prevent bad code from
|
||||
entering the repository.
|
||||
|
||||
## Setup
|
||||
|
||||
### 1. Install pre-commit
|
||||
|
||||
```bash
|
||||
pip install pre-commit
|
||||
```
|
||||
|
||||
### 2. Create .pre-commit-config.yaml
|
||||
|
||||
```yaml
|
||||
repos:
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
rev: v0.6.0
|
||||
hooks:
|
||||
- id: ruff
|
||||
args: [--fix]
|
||||
- id: ruff-format
|
||||
|
||||
- repo: https://github.com/RobertCraigie/pyright-python
|
||||
rev: v1.1.380
|
||||
hooks:
|
||||
- id: pyright
|
||||
```
|
||||
|
||||
### 3. Install hooks
|
||||
|
||||
```bash
|
||||
pre-commit install
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Pre-commit hooks now run automatically:
|
||||
|
||||
```bash
|
||||
git add .
|
||||
git commit -m "feat: add feature"
|
||||
# Hooks run automatically before commit
|
||||
```
|
||||
|
||||
### Skip hooks (when needed)
|
||||
|
||||
```bash
|
||||
git commit --no-verify -m "wip: work in progress"
|
||||
```
|
||||
|
||||
## Manual Runs
|
||||
|
||||
Run hooks on all files:
|
||||
|
||||
```bash
|
||||
pre-commit run --all-files
|
||||
```
|
||||
|
||||
Run specific hook:
|
||||
|
||||
```bash
|
||||
pre-commit run ruff --all-files
|
||||
pre-commit run pyright --all-files
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
### Ruff with auto-fix
|
||||
|
||||
```yaml
|
||||
- id: ruff
|
||||
args: [--fix, --exit-non-zero-on-fix]
|
||||
```
|
||||
|
||||
### Pyright with specific directories
|
||||
|
||||
```yaml
|
||||
- id: pyright
|
||||
files: ^(src|tests)/
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
**Hook fails with "command not found":**
|
||||
|
||||
- Ensure ruff/pyright installed in environment
|
||||
- Try: `pre-commit clean` then `pre-commit install`
|
||||
|
||||
**Hooks too slow:**
|
||||
|
||||
- Run only on changed files (default behavior)
|
||||
- Skip pyright in pre-commit, run in CI instead
|
||||
|
||||
**Want to update hook versions:**
|
||||
|
||||
```bash
|
||||
pre-commit autoupdate
|
||||
```
|
||||
|
||||
## Best Practices
|
||||
|
||||
1. **Keep hooks fast** - Pre-commit should be < 10 seconds
|
||||
2. **Auto-fix when possible** - Use `--fix` for ruff
|
||||
3. **Document skip policy** - When is `--no-verify` acceptable?
|
||||
4. **Update regularly** - Run `pre-commit autoupdate` monthly
|
||||
Reference in New Issue
Block a user