From 4be572ae84bd692e73005743da823ad712a7b8a4 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Date: Fri, 1 Aug 2025 00:24:20 +0100 Subject: [PATCH 1/6] Add pre-commit to sort yml files --- .pre-commit-config.yaml | 31 +++++++++++++++ scripts/sort_yml.py | 88 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 .pre-commit-config.yaml create mode 100755 scripts/sort_yml.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..346aa0bb --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,31 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +--- +default_stages: [pre-commit] +default_language_version: + python: python3 +minimum_pre_commit_version: '3.2.0' +repos: + - repo: local + hooks: + - id: sort-actions-yml + name: Sort actions.yml + entry: ./scripts/sort_yml.py + language: python + additional_dependencies: ['rich>=12.4.4', 'ruamel.yaml'] + require_serial: true + files: ^actions\.yml$ \ No newline at end of file diff --git a/scripts/sort_yml.py b/scripts/sort_yml.py new file mode 100755 index 00000000..307b8b8f --- /dev/null +++ b/scripts/sort_yml.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# /// script +# requires-python = ">=3.11" +# dependencies = [ +# "rich", +# "ruamel.yaml", +# ] +# /// + +from ruamel.yaml import YAML +from ruamel.yaml.comments import CommentedMap +import sys +from pathlib import Path +from rich.console import Console + +console = Console(width=400, color_system="standard") + + +def sort_yaml_file(input_file: str): + """Sorts the keys of a YAML file in alphabetical order""" + + yaml = YAML() + yaml.preserve_quotes = True + + input_path = Path(input_file) + + if not input_path.exists(): + raise FileNotFoundError(f"File '{input_file}' not found.") + + with open(input_path, 'r', encoding='utf-8') as f: + data = yaml.load(f) + + sorted_data = CommentedMap() + + sorted_keys: list[str] = sorted(data.keys(), key=str.lower) + + # Copy data in sorted order + for key in sorted_keys: + sorted_data[key] = data[key] + + # Preserve any comment at the beginning of the file + if hasattr(data, 'ca') and hasattr(data.ca, 'comment'): + if not hasattr(sorted_data, 'ca'): + sorted_data.ca = data.ca.__class__() + sorted_data.ca.comment = data.ca.comment + + with open(input_path, 'w', encoding='utf-8') as f: + yaml.dump(sorted_data, f) + + +errors = [] +def main(): + files = sys.argv[1:] + for file in files: + console.print(f"[blue]Sorting YAML file {file}") + try: + sort_yaml_file(file) + console.print(f"[blue]✅ YAML file sorted successfully {file}!") + except FileNotFoundError as e: + errors.append((file, str(e))) + except Exception as e: + errors.append((file, str(e))) + + if errors: + console.print("[red]Errors occurred while sorting YAML files:") + for file, error in errors: + console.print(f"[red]File: {file} - Error: {error}") + sys.exit(1) + +if __name__ == "__main__": + main() \ No newline at end of file From b24ed88092f9029e76d2ff15b205074dd0581cfe Mon Sep 17 00:00:00 2001 From: Pavan Kumar Date: Sat, 2 Aug 2025 16:21:54 +0100 Subject: [PATCH 2/6] Add pre-commit checks workflow to CI --- .github/workflows/pre-commit.yml | 41 ++++++++++++++++++++++++++++++++ 1 file changed, 41 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 00000000..5e85f4d6 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,41 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +--- +name: Pre-commit Checks + +on: + pull_request: + branches: [main] + +permissions: + contents: read + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Run pre-commit checks + run: | + pipx install uv + uv pip install pre-commit + pre-commit install + pre-commit run --all-files --color always --show-diff-on-failure \ No newline at end of file From 1db664a48a6e44ee4ca1ddbd30734876dd0b4095 Mon Sep 17 00:00:00 2001 From: Pavan Kumar Date: Sat, 2 Aug 2025 16:38:30 +0100 Subject: [PATCH 3/6] Fix pre-commit job --- .github/workflows/pre-commit.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 5e85f4d6..844e7b82 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -29,13 +29,13 @@ jobs: pre-commit: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 with: persist-credentials: false - name: Run pre-commit checks run: | pipx install uv + uv venv uv pip install pre-commit - pre-commit install - pre-commit run --all-files --color always --show-diff-on-failure \ No newline at end of file + uv run pre-commit run --all-files --color always --show-diff-on-failure From c924d11d3a32f609d5b4f921dbe29a361ad63a08 Mon Sep 17 00:00:00 2001 From: GPK Date: Sat, 2 Aug 2025 18:18:29 +0100 Subject: [PATCH 4/6] Update .github/workflows/pre-commit.yml Co-authored-by: Jarek Potiuk Signed-off-by: GPK --- .github/workflows/pre-commit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 844e7b82..2a19c5ce 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -35,7 +35,7 @@ jobs: - name: Run pre-commit checks run: | - pipx install uv + curl -LsSf https://astral.sh/uv/install.sh | sh uv venv uv pip install pre-commit uv run pre-commit run --all-files --color always --show-diff-on-failure From e96f1a19d4c081654926ca16d33c48bdb66aca57 Mon Sep 17 00:00:00 2001 From: GPK Date: Sat, 2 Aug 2025 18:18:42 +0100 Subject: [PATCH 5/6] Update .github/workflows/pre-commit.yml Co-authored-by: Jarek Potiuk Signed-off-by: GPK --- .github/workflows/pre-commit.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index 2a19c5ce..cbf2045b 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -36,6 +36,7 @@ jobs: - name: Run pre-commit checks run: | curl -LsSf https://astral.sh/uv/install.sh | sh - uv venv - uv pip install pre-commit - uv run pre-commit run --all-files --color always --show-diff-on-failure + uv tool install pre-commit + uv tool ensure-path + # not sure if we need to source .bashrc here :) + pre-commit run --all-files --color always --show-diff-on-failure From 2f61110e32fa2caddc9392f86d55dff8eab7e01e Mon Sep 17 00:00:00 2001 From: Pavan Kumar Date: Sat, 2 Aug 2025 18:20:59 +0100 Subject: [PATCH 6/6] Fix path --- .github/workflows/pre-commit.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index cbf2045b..19106084 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -37,6 +37,5 @@ jobs: run: | curl -LsSf https://astral.sh/uv/install.sh | sh uv tool install pre-commit - uv tool ensure-path - # not sure if we need to source .bashrc here :) + uv tool ensurepath pre-commit run --all-files --color always --show-diff-on-failure