Skip to content

Commit 376e5c1

Browse files
assignUserraboof
andauthored
ci: trigger workflow on changes to gateway files (#170)
* trigger workflow on changes to gateway files * use uv * Fix writing yaml to strings --------- Co-authored-by: Arnout Engelen <arnout@bzzt.net>
1 parent 36b3fb5 commit 376e5c1

4 files changed

Lines changed: 28 additions & 15 deletions

File tree

.github/workflows/pytest.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,9 @@ jobs:
3232
with:
3333
persist-credentials: false
3434

35-
- name: Set up Python environment
36-
uses: actions/setup-python@v5
37-
38-
- name: Install dependencies
39-
run: |
40-
pip install pytest ruyaml
41-
4235
- name: Run tests
4336
run: |
44-
pytest
37+
pipx install uv
38+
uvx --with ruyaml pytest
4539
env:
4640
GH_TOKEN: ${{ github.token }}

.github/workflows/update_actions.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
paths:
1111
- ".github/workflows/update_actions.yml"
1212
- ".github/workflows/dummy.yml"
13+
- gateway/*
1314

1415
permissions:
1516
contents: read
@@ -28,8 +29,10 @@ jobs:
2829
with:
2930
persist-credentials: true
3031

32+
- run: pip install ruyaml
33+
3134
- name: Update actions.yml
32-
shell: python
35+
shell: python
3336
run: |
3437
import sys
3538
sys.path.append("./gateway/")

.github/workflows/update_dummy.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ on:
1010
paths:
1111
- ".github/workflows/update_dummy.yml"
1212
- "actions.yml"
13+
- gateway/*
1314

1415
permissions: {}
1516

@@ -28,8 +29,10 @@ jobs:
2829
# We have to use a PAT to commit the workflow file
2930
token: ${{ secrets.ALLOWLIST_WORKFLOW_TOKEN || github.token }}
3031

32+
- run: pip install ruyaml
33+
3134
- name: Update Workflow
32-
shell: python
35+
shell: python
3336
run: |
3437
import sys
3538
sys.path.append("./gateway/")

gateway/gateway.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
# /// script
2+
# requires-python = ">=3.13"
3+
# dependencies = [
4+
# "ruyaml",
5+
# ]
6+
# ///
7+
18
import os
29
from datetime import date, timedelta
10+
from io import StringIO
311
from pathlib import Path
412
from typing import Dict, NotRequired, TypedDict
513

614
import ruyaml
715

16+
817
class RefDetails(TypedDict):
918
"""
1019
Type definition for reference details of GitHub Actions for actions.yml
@@ -66,6 +75,11 @@ def write_yaml(path: Path, yaml_dict: dict | list):
6675
yaml = ruyaml.YAML()
6776
yaml.dump(yaml_dict, file)
6877

78+
def to_yaml_string(yaml_dict: dict | list):
79+
yaml = ruyaml.YAML()
80+
stream = StringIO()
81+
yaml.dump(yaml_dict, stream)
82+
return stream.getvalue()
6983

7084
def write_str(path: Path, content: str):
7185
with open(path, "w") as file:
@@ -182,10 +196,9 @@ def update_actions(dummy_path: Path, actions_path: Path):
182196
actions: ActionsYAML = load_yaml(actions_path)
183197

184198
update_refs(steps, actions)
185-
gha_print(yaml.safe_dump(actions), "Generated List")
199+
gha_print(to_yaml_string(actions), "Generated List")
186200
write_yaml(actions_path, actions)
187201

188-
189202
def create_pattern(actions: ActionsYAML) -> list[str]:
190203
"""
191204
Create a pattern list of valid action references.
@@ -218,8 +231,8 @@ def update_patterns(pattern_path: Path, list_path: Path):
218231
"""
219232
actions: ActionsYAML = load_yaml(list_path)
220233
patterns = create_pattern(actions)
221-
comment = f"# This file was generated from {pattern_path} by gateway/gateway.py. DO NOT UPDATE MANUALLY.\n"
222-
patterns_str = comment + yaml.safe_dump(patterns)
234+
comment = f"# This file was generated from {list_path} by gateway/gateway.py. DO NOT UPDATE MANUALLY.\n"
235+
patterns_str = comment + to_yaml_string(patterns)
223236
gha_print(patterns_str, "Generated Patterns")
224237
write_str(pattern_path, patterns_str)
225238

@@ -273,5 +286,5 @@ def clean_actions(actions_path: Path):
273286
"""
274287
actions: ActionsYAML = load_yaml(actions_path)
275288
remove_expired_refs(actions)
276-
gha_print(yaml.safe_dump(actions), "Cleaned Actions")
289+
gha_print(to_yaml_string(actions), "Cleaned Actions")
277290
write_yaml(actions_path, actions)

0 commit comments

Comments
 (0)