diff --git a/.editorconfig b/.editorconfig index 578ce1883d..65d0ce8846 100644 --- a/.editorconfig +++ b/.editorconfig @@ -15,7 +15,7 @@ max_line_length = 160 indent_style = space indent_size = 4 -[*.{md,markdown,json,js,yml,csproj,fsproj,targets,targets,props}] +[*.{md,markdown,json,js,yml,yaml,csproj,fsproj,targets,targets,props}] indent_style = space indent_size = 2 diff --git a/.github/actions/bootstrap/action.yml b/.github/actions/bootstrap/action.yml index 0643fd9c99..e4ac144f71 100644 --- a/.github/actions/bootstrap/action.yml +++ b/.github/actions/bootstrap/action.yml @@ -16,7 +16,11 @@ runs: # Ensure we fetch all tags - shell: bash run: | - git fetch --prune --unshallow --tags + if [ -f .git/shallow ]; then + git fetch --prune --unshallow --tags + else + git fetch --prune --tags + fi git tag --list - name: Git config diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000000..dc238da557 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,26 @@ +name: pre-commit + +on: + pull_request: + push: + branches: [main] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + with: + fetch-depth: 0 + - uses: ./.github/actions/bootstrap + - run: npm ci + working-directory: src/Elastic.Documentation.Site + - uses: actions/setup-python@v6 + - name: Run pre-commit on PRs + uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 + if: ${{ github.event.pull_request }} + with: + extra_args: --from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }} + - name: Run pre-commit on branches + uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 + if: ${{ ! github.event.pull_request }} diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..ac647c9c6a --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,35 @@ +repos: + - repo: local + hooks: + - id: dotnet-format + name: dotnet format + language: system + entry: dotnet format --no-restore --verbosity quiet --include + files: \.(cs|fs)$ + pass_filenames: true + + # Run prettier only for src/Elastic.Documentation.Site + - id: prettier + name: Prettier Code Formatter + language: system + entry: bash -c 'cd src/Elastic.Documentation.Site && npx prettier --write "${@#src/Elastic.Documentation.Site/}"' -- + files: ^src/Elastic\.Documentation\.Site + verbose: true + pass_filenames: true + + # Run TypeScript type checking for src/Elastic.Documentation.Site + - id: typescript-check + name: TypeScript Type Check + language: system + entry: bash -c 'cd src/Elastic.Documentation.Site && npx tsc --noEmit' + files: ^src/Elastic\.Documentation\.Site/.*\.tsx?$ + pass_filenames: false + + # Run eslint only for src/Elastic.Documentation.Site + - id: eslint + name: ESLint + language: system + entry: bash -c 'cd src/Elastic.Documentation.Site && npx eslint --fix "${@#src/Elastic.Documentation.Site/}"' -- + files: ^src/Elastic\.Documentation\.Site/.*\.(js|jsx|ts|tsx|mjs)$ + verbose: true + pass_filenames: true diff --git a/CONTRIBUTING.MD b/CONTRIBUTING.MD index f07195b31e..79249dd71e 100644 --- a/CONTRIBUTING.MD +++ b/CONTRIBUTING.MD @@ -56,6 +56,68 @@ based on the labels of the pull requests merged into the `main` branch. See the [release-drafter configuration](./.github/release-drafter.yml) for more details. +## Pre-commit Hooks + +This repository uses [pre-commit](https://pre-commit.com/) to automatically format and validate code before commits. + +### What Gets Checked + +The following hooks run automatically on staged files: + +- **dotnet format** - Formats C# and F# files +- **prettier** - Formats TypeScript, JavaScript, CSS, and other files in `src/Elastic.Documentation.Site/` +- **TypeScript** - Type checks TypeScript files with `tsc --noEmit` +- **ESLint** - Lints and fixes JavaScript/TypeScript files + +### Installation + +#### Install pre-commit + +```bash +# macOS/Linux +pip install pre-commit + +# macOS with Homebrew +brew install pre-commit + +# Windows +pip install pre-commit +``` + +#### Install the hooks + +```bash +pre-commit install +``` + +### Usage + +Once installed, hooks run automatically when you commit: + +```bash +git add . +git commit -m "your message" +``` + +**Note:** If hooks modify files (prettier, eslint, dotnet format), the commit will fail so you can review the changes. Simply run the commit command again to commit the formatted code. + +#### Manual Execution + +Run hooks on all files: +```bash +pre-commit run --all-files +``` + +Run a specific hook: +```bash +pre-commit run prettier --all-files +pre-commit run dotnet-format --all-files +``` + +#### CI Integration + +Pre-commit hooks also run in GitHub Actions on all pull requests to ensure code quality and consistency. + ## Creating a New Release To create a new release trigger the [release](https://github.com/elastic/docs-builder/actions/workflows/release.yml) workflow on the `main` branch.