diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml index c56da0f7..da85f2b0 100644 --- a/.github/workflows/pytest.yml +++ b/.github/workflows/pytest.yml @@ -32,15 +32,9 @@ jobs: with: persist-credentials: false - - name: Set up Python environment - uses: actions/setup-python@v5 - - - name: Install dependencies - run: | - pip install pytest ruyaml - - name: Run tests run: | - pytest + pipx install uv + uvx --with ruyaml pytest env: GH_TOKEN: ${{ github.token }} diff --git a/.github/workflows/update_actions.yml b/.github/workflows/update_actions.yml index d4bb193b..76e2a6a1 100644 --- a/.github/workflows/update_actions.yml +++ b/.github/workflows/update_actions.yml @@ -10,6 +10,7 @@ on: paths: - ".github/workflows/update_actions.yml" - ".github/workflows/dummy.yml" + - gateway/* permissions: contents: read @@ -28,8 +29,10 @@ jobs: with: persist-credentials: true + - run: pip install ruyaml + - name: Update actions.yml - shell: python + shell: python run: | import sys sys.path.append("./gateway/") diff --git a/.github/workflows/update_dummy.yml b/.github/workflows/update_dummy.yml index 4cd2b7c6..e8c5cca0 100644 --- a/.github/workflows/update_dummy.yml +++ b/.github/workflows/update_dummy.yml @@ -10,6 +10,7 @@ on: paths: - ".github/workflows/update_dummy.yml" - "actions.yml" + - gateway/* permissions: {} @@ -28,8 +29,10 @@ jobs: # We have to use a PAT to commit the workflow file token: ${{ secrets.ALLOWLIST_WORKFLOW_TOKEN || github.token }} + - run: pip install ruyaml + - name: Update Workflow - shell: python + shell: python run: | import sys sys.path.append("./gateway/") diff --git a/gateway/gateway.py b/gateway/gateway.py index 2a0fa296..febfb876 100644 --- a/gateway/gateway.py +++ b/gateway/gateway.py @@ -1,10 +1,19 @@ +# /// script +# requires-python = ">=3.13" +# dependencies = [ +# "ruyaml", +# ] +# /// + import os from datetime import date, timedelta +from io import StringIO from pathlib import Path from typing import Dict, NotRequired, TypedDict import ruyaml + class RefDetails(TypedDict): """ Type definition for reference details of GitHub Actions for actions.yml @@ -66,6 +75,11 @@ def write_yaml(path: Path, yaml_dict: dict | list): yaml = ruyaml.YAML() yaml.dump(yaml_dict, file) +def to_yaml_string(yaml_dict: dict | list): + yaml = ruyaml.YAML() + stream = StringIO() + yaml.dump(yaml_dict, stream) + return stream.getvalue() def write_str(path: Path, content: str): with open(path, "w") as file: @@ -182,10 +196,9 @@ def update_actions(dummy_path: Path, actions_path: Path): actions: ActionsYAML = load_yaml(actions_path) update_refs(steps, actions) - gha_print(yaml.safe_dump(actions), "Generated List") + gha_print(to_yaml_string(actions), "Generated List") write_yaml(actions_path, actions) - def create_pattern(actions: ActionsYAML) -> list[str]: """ Create a pattern list of valid action references. @@ -218,8 +231,8 @@ def update_patterns(pattern_path: Path, list_path: Path): """ actions: ActionsYAML = load_yaml(list_path) patterns = create_pattern(actions) - comment = f"# This file was generated from {pattern_path} by gateway/gateway.py. DO NOT UPDATE MANUALLY.\n" - patterns_str = comment + yaml.safe_dump(patterns) + comment = f"# This file was generated from {list_path} by gateway/gateway.py. DO NOT UPDATE MANUALLY.\n" + patterns_str = comment + to_yaml_string(patterns) gha_print(patterns_str, "Generated Patterns") write_str(pattern_path, patterns_str) @@ -273,5 +286,5 @@ def clean_actions(actions_path: Path): """ actions: ActionsYAML = load_yaml(actions_path) remove_expired_refs(actions) - gha_print(yaml.safe_dump(actions), "Cleaned Actions") + gha_print(to_yaml_string(actions), "Cleaned Actions") write_yaml(actions_path, actions)