diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 000000000..1e56d7476 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,186 @@ +name: CI + +on: + push: + branches: [main, develop] + pull_request: + branches: [main, develop] + +jobs: + # Job 1: Lint and Typecheck (Fast feedback) + lint-typecheck: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" + cache: "npm" + + - name: Install dependencies + run: npm ci + + - name: Run ESLint + run: npm run lint + + - name: Run Typecheck + run: npm run typecheck + + # Job 2: Unit and Integration Tests + tests: + runs-on: ubuntu-latest + needs: lint-typecheck + + steps: + - uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" + cache: "npm" + + - name: Install dependencies + run: npm ci + + - name: Run Unit Tests + run: npm run test:unit + + - name: Run Integration Tests + run: npm run test:integration + + # Job 3: Contract Tests (API/Website compatibility) + contract-tests: + runs-on: ubuntu-latest + needs: lint-typecheck + + steps: + - uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" + cache: "npm" + + - name: Install dependencies + run: npm ci + + - name: Run API Contract Validation + run: npm run test:api:contracts + + - name: Generate OpenAPI spec + run: npm run api:generate-spec + + - name: Generate TypeScript types + run: npm run api:generate-types + + - name: Run Contract Compatibility Check + run: npm run test:contract:compatibility + + - name: Verify Website Type Checking + run: npm run website:type-check + + - name: Upload generated types as artifacts + uses: actions/upload-artifact@v3 + with: + name: generated-types + path: apps/website/lib/types/generated/ + retention-days: 7 + + # Job 4: E2E Tests (Only on main/develop push, not on PRs) + e2e-tests: + runs-on: ubuntu-latest + needs: [lint-typecheck, tests, contract-tests] + if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop') + + steps: + - uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" + cache: "npm" + + - name: Install dependencies + run: npm ci + + - name: Run E2E Tests + run: npm run test:e2e + + # Job 5: Comment PR with results (Only on PRs) + comment-pr: + runs-on: ubuntu-latest + needs: [lint-typecheck, tests, contract-tests] + if: github.event_name == 'pull_request' + + steps: + - name: Comment PR with results + uses: actions/github-script@v6 + with: + script: | + const fs = require('fs'); + const path = require('path'); + + // Read any contract change reports + const reportPath = path.join(process.cwd(), 'contract-report.json'); + if (fs.existsSync(reportPath)) { + const report = JSON.parse(fs.readFileSync(reportPath, 'utf8')); + + const comment = ` + ## 🔍 CI Results + + ✅ **All checks passed!** + + ### Changes Summary: + - Total changes: ${report.totalChanges} + - Breaking changes: ${report.breakingChanges} + - Added: ${report.added} + - Removed: ${report.removed} + - Modified: ${report.modified} + + Generated types are available as artifacts. + `; + + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: comment + }); + } + + # Job 6: Commit generated types (Only on main branch push) + commit-types: + runs-on: ubuntu-latest + needs: [lint-typecheck, tests, contract-tests] + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + + steps: + - uses: actions/checkout@v3 + + - name: Setup Node.js + uses: actions/setup-node@v3 + with: + node-version: "20" + cache: "npm" + + - name: Install dependencies + run: npm ci + + - name: Generate and snapshot types + run: | + npm run api:generate-spec + npm run api:generate-types + + - name: Commit generated types + run: | + git config --local user.email "github-actions[bot]@users.noreply.github.com" + git config --local user.name "github-actions[bot]" + git add apps/website/lib/types/generated/ + git diff --staged --quiet || git commit -m "chore: update generated API types [skip ci]" + git push diff --git a/.github/workflows/contract-testing.yml b/.github/workflows/contract-testing.yml deleted file mode 100644 index 219865a3c..000000000 --- a/.github/workflows/contract-testing.yml +++ /dev/null @@ -1,110 +0,0 @@ -name: Contract Testing - -on: - push: - branches: [main, develop] - pull_request: - branches: [main, develop] - -jobs: - contract-tests: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - node-version: '20' - cache: 'npm' - - - name: Install dependencies - run: npm ci - - - name: Run API contract validation - run: npm run test:api:contracts - - - name: Generate OpenAPI spec - run: npm run api:generate-spec - - - name: Generate TypeScript types - run: npm run api:generate-types - - - name: Run contract compatibility check - run: npm run test:contract:compatibility - - - name: Verify website type checking - run: npm run website:type-check - - - name: Upload generated types as artifacts - uses: actions/upload-artifact@v3 - with: - name: generated-types - path: apps/website/lib/types/generated/ - retention-days: 7 - - - name: Comment PR with results - if: github.event_name == 'pull_request' - uses: actions/github-script@v6 - with: - script: | - const fs = require('fs'); - const path = require('path'); - - // Read any contract change reports - const reportPath = path.join(process.cwd(), 'contract-report.json'); - if (fs.existsSync(reportPath)) { - const report = JSON.parse(fs.readFileSync(reportPath, 'utf8')); - - const comment = ` - ## 🔍 Contract Testing Results - - ✅ **All contract tests passed!** - - ### Changes Summary: - - Total changes: ${report.totalChanges} - - Breaking changes: ${report.breakingChanges} - - Added: ${report.added} - - Removed: ${report.removed} - - Modified: ${report.modified} - - Generated types are available as artifacts. - `; - - github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body: comment - }); - } - - contract-snapshot: - runs-on: ubuntu-latest - if: github.ref == 'refs/heads/main' - - steps: - - uses: actions/checkout@v3 - - - name: Setup Node.js - uses: actions/setup-node@v3 - with: - node-version: '20' - cache: 'npm' - - - name: Install dependencies - run: npm ci - - - name: Generate and snapshot types - run: | - npm run api:generate-spec - npm run api:generate-types - - - name: Commit generated types - run: | - git config --local user.email "github-actions[bot]@users.noreply.github.com" - git config --local user.name "github-actions[bot]" - git add apps/website/lib/types/generated/ - git diff --staged --quiet || git commit -m "chore: update generated API types [skip ci]" - git push \ No newline at end of file diff --git a/.husky/pre-commit b/.husky/pre-commit index 18de98416..d0a778429 100644 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1 +1 @@ -npm test \ No newline at end of file +npx lint-staged \ No newline at end of file diff --git a/README.md b/README.md index 485d04579..3c5276704 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ npm test Individual applications support hot reload and watch mode during development: - **web-api**: Backend REST API server -- **web-client**: Frontend React application +- **web-client**: Frontend React application - **companion**: Desktop companion application ## Testing Commands @@ -64,12 +64,28 @@ Individual applications support hot reload and watch mode during development: GridPilot follows strict BDD (Behavior-Driven Development) with comprehensive test coverage. ### Local Verification Pipeline -Run this sequence before pushing to ensure correctness: -```bash -npm run lint && npm run typecheck && npm run test:unit && npm run test:integration -``` + +GridPilot uses **lint-staged** to automatically validate only changed files on commit: + +- `eslint --fix` runs on changed JS/TS/TSX files +- `vitest related --run` runs tests related to changed files +- `prettier --write` formats JSON, MD, and YAML files + +This ensures fast commits without running the full test suite. + +### Pre-Push Hook + +A **pre-push hook** runs the full verification pipeline before pushing to remote: + +- `npm run lint` - Check for linting errors +- `npm run typecheck` - Verify TypeScript types +- `npm run test:unit` - Run unit tests +- `npm run test:integration` - Run integration tests + +You can skip this with `git push --no-verify` if needed. ### Individual Commands + ```bash # Run all tests npm test @@ -147,4 +163,4 @@ Comprehensive documentation is available in the [`/docs`](docs/) directory: ## License -MIT License - see [LICENSE](LICENSE) file for details. \ No newline at end of file +MIT License - see [LICENSE](LICENSE) file for details. diff --git a/package.json b/package.json index 40fabd7c3..3cde0da43 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "glob": "^13.0.0", "husky": "^9.1.7", "jsdom": "^22.1.0", + "lint-staged": "^15.2.10", "openapi-typescript": "^7.4.3", "prettier": "^3.0.0", "puppeteer": "^24.31.0", @@ -128,6 +129,7 @@ "test:unit": "vitest run tests/unit", "test:watch": "vitest watch", "test:website:types": "vitest run --config vitest.website.config.ts apps/website/lib/types/contractConsumption.test.ts", + "verify": "npm run lint && npm run typecheck && npm run test:unit && npm run test:integration", "typecheck": "npm run typecheck:targets", "typecheck:grep": "npm run typescript", "typecheck:root": "npx tsc --noEmit --project tsconfig.json", @@ -139,10 +141,19 @@ "website:start": "npm run start --workspace=@gridpilot/website", "website:type-check": "npm run type-check --workspace=@gridpilot/website" }, + "lint-staged": { + "*.{js,ts,tsx}": [ + "eslint --fix", + "vitest related --run" + ], + "*.{json,md,yml}": [ + "prettier --write" + ] + }, "version": "0.1.0", "workspaces": [ "core/*", "apps/*", "testing/*" ] -} \ No newline at end of file +} diff --git a/plans/ci-optimization.md b/plans/ci-optimization.md new file mode 100644 index 000000000..59c12b967 --- /dev/null +++ b/plans/ci-optimization.md @@ -0,0 +1,100 @@ +# CI/CD & Dev Experience Optimization Plan + +## Current Situation + +- **Husky `pre-commit`**: Runs `npm test` (Vitest) on every commit. This likely runs the entire test suite, which is slow and frustrating for developers. +- **Gitea Actions**: Currently only has `contract-testing.yml`. +- **Missing**: No automated linting or type-checking in CI, no tiered testing strategy. + +## Proposed Strategy: The "Fast Feedback Loop" + +We will implement a tiered approach to balance speed and safety. + +### 1. Local Development (Husky + lint-staged) + +**Goal**: Prevent obvious errors from entering the repo without slowing down the dev. + +- **Trigger**: `pre-commit` +- **Action**: Only run on **staged files**. +- **Tasks**: + - `eslint --fix` + - `prettier --write` + - `vitest related` (only run tests related to changed files) + +### 2. Pull Request (Gitea Actions) + +**Goal**: Ensure the branch is stable and doesn't break the build or other modules. + +- **Trigger**: PR creation and updates. +- **Tasks**: + - Full `lint` + - Full `typecheck` (crucial for monorepo integrity) + - Full `unit tests` + - `integration tests` + - `contract tests` + +### 3. Merge to Main / Release (Gitea Actions) + +**Goal**: Final verification before deployment. + +- **Trigger**: Push to `main` or `develop`. +- **Tasks**: + - Everything from PR stage. + - `e2e tests` (Playwright) - these are the slowest and most expensive. + +--- + +## Implementation Steps + +### Step 1: Install and Configure `lint-staged` + +We need to add `lint-staged` to [`package.json`](package.json) and update the Husky hook. + +### Step 2: Optimize Husky Hook + +Update [`.husky/pre-commit`](.husky/pre-commit) to run `npx lint-staged` instead of `npm test`. + +### Step 3: Create Comprehensive CI Workflow + +Create `.github/workflows/ci.yml` (Gitea Actions compatible) to handle the heavy lifting. + +--- + +## Workflow Diagram + +```mermaid +graph TD + A[Developer Commits] --> B{Husky pre-commit} + B -->|lint-staged| C[Lint/Format Changed Files] + C --> D[Run Related Tests] + D --> E[Commit Success] + + E --> F[Push to PR] + F --> G{Gitea CI PR Job} + G --> H[Full Lint & Typecheck] + G --> I[Full Unit & Integration Tests] + G --> J[Contract Tests] + + J --> K{Merge to Main} + K --> L{Gitea CI Main Job} + L --> M[All PR Checks] + L --> N[Full E2E Tests] + N --> O[Deploy/Release] +``` + +## Proposed `lint-staged` Configuration + +```json +{ + "*.{js,ts,tsx}": ["eslint --fix", "vitest related --run"], + "*.{json,md,yml}": ["prettier --write"] +} +``` + +--- + +## Questions for the User + +1. Do you want to include `typecheck` in the `pre-commit` hook? (Note: `tsc` doesn't support linting only changed files easily, so it usually checks the whole project, which might be slow). +2. Should we run `integration tests` on every PR, or only on merge to `main`? +3. Are there specific directories that should be excluded from this automated flow?