From 0ea76df9b11812663d64873d21a463221d239f3a Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Thu, 6 Nov 2025 12:12:52 +0100 Subject: [PATCH 1/7] Also add yaml into pattern --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From ce6306ac61d6dea7c7b1e6d05005e9b32d57fd5f Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Thu, 6 Nov 2025 12:12:59 +0100 Subject: [PATCH 2/7] Add pre-commit config --- .pre-commit-config.yaml | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 .pre-commit-config.yaml 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 From 114e4c222a293d421e360d46948781339d0c7b9b Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Thu, 6 Nov 2025 12:13:09 +0100 Subject: [PATCH 3/7] Add pre-commit workflow --- .github/workflows/pre-commit.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 .github/workflows/pre-commit.yml diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000000..4f4b4e5172 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,14 @@ +name: pre-commit + +on: + pull_request: + push: + branches: [main] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + - uses: actions/setup-python@v6 + - uses: pre-commit/action@v3.0.1 From 86b49f931d2980ecc68dcd6969b4b2af4d3abdf0 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Thu, 6 Nov 2025 12:17:05 +0100 Subject: [PATCH 4/7] Add bootstrap action --- .github/workflows/pre-commit.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 4f4b4e5172..81db891ca5 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -10,5 +10,6 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 + - uses: ./.github/actions/bootstrap - uses: actions/setup-python@v6 - uses: pre-commit/action@v3.0.1 From b73f92af2885dc1978c6cd8a788c20f3e0aef4d4 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Thu, 6 Nov 2025 12:24:59 +0100 Subject: [PATCH 5/7] Add pre-commit instructions --- CONTRIBUTING.MD | 62 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) 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. From d86881db63c2500150ce51739bb92666c9dd80e2 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Thu, 6 Nov 2025 12:31:21 +0100 Subject: [PATCH 6/7] Fix pre-commit on CI and only run on changed files --- .github/workflows/pre-commit.yml | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 81db891ca5..dc238da557 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -10,6 +10,17 @@ jobs: 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 - - uses: pre-commit/action@v3.0.1 + - 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 }} From 356a5a49df27eadf77fb22a4049128da2045ca84 Mon Sep 17 00:00:00 2001 From: Jan Calanog Date: Thu, 6 Nov 2025 12:35:17 +0100 Subject: [PATCH 7/7] Make git fetch command in boostrap work also if the repository is already a complete clone --- .github/actions/bootstrap/action.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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