Skip to content

Commit 5f448c0

Browse files
committed
ci(workflows): optimize json and yaml validation workflow
Add changed-files detection step to only validate modified JSON/YAML files Update validate_files.py to accept changed files list and skip unchanged files
1 parent e7d52f5 commit 5f448c0

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

.github/scripts/validate_files.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def main():
5454
parser.add_argument('--directory', '-d', default='.', help='Directory to scan for files')
5555
parser.add_argument('--check-json', action='store_true', help='Check JSON files')
5656
parser.add_argument('--check-yaml', action='store_true', help='Check YAML files')
57+
parser.add_argument('--changed-files', nargs='*', help='List of changed files to validate')
5758

5859
args = parser.parse_args()
5960

@@ -62,17 +63,26 @@ def main():
6263

6364
all_valid = True
6465

65-
if args.check_json:
66-
json_files = find_files(args.directory, '.json')
67-
print(f"Found {len(json_files)} JSON files")
66+
# If changed files are provided, filter them by extension
67+
if args.changed_files:
68+
json_files = [f for f in args.changed_files if f.endswith('.json')]
69+
yaml_files = [f for f in args.changed_files if f.endswith('.yaml') or f.endswith('.yml')]
70+
else:
71+
# Otherwise, find all files in directory
72+
json_files = find_files(args.directory, '.json') if args.check_json else []
73+
yaml_files = []
74+
if args.check_yaml:
75+
yaml_files = find_files(args.directory, '.yaml')
76+
yaml_files += find_files(args.directory, '.yml')
77+
78+
if args.check_json and json_files:
79+
print(f"Found {len(json_files)} JSON files to validate")
6880
for file_path in json_files:
6981
if not validate_json(file_path):
7082
all_valid = False
7183

72-
if args.check_yaml:
73-
yaml_files = find_files(args.directory, '.yaml')
74-
yaml_files += find_files(args.directory, '.yml')
75-
print(f"Found {len(yaml_files)} YAML files")
84+
if args.check_yaml and yaml_files:
85+
print(f"Found {len(yaml_files)} YAML files to validate")
7686
for file_path in yaml_files:
7787
if not validate_yaml(file_path):
7888
all_valid = False

.github/workflows/code-review.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ jobs:
1313
- name: Checkout code
1414
uses: actions/checkout@v4
1515

16+
- name: Get changed files
17+
id: changed-files
18+
uses: tj-actions/changed-files@v45
19+
with:
20+
files: |
21+
**/*.json
22+
**/*.yaml
23+
**/*.yml
24+
1625
- name: Set up Python
1726
uses: actions/setup-python@v4
1827
with:
@@ -25,4 +34,9 @@ jobs:
2534
2635
- name: Validate JSON and YAML files
2736
run: |
28-
python .github/scripts/validate_files.py --directory . --check-json --check-yaml
37+
if [ "${{ steps.changed-files.outputs.any_changed }}" = "true" ]; then
38+
echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}"
39+
python .github/scripts/validate_files.py --changed-files ${{ steps.changed-files.outputs.all_changed_files }} --check-json --check-yaml
40+
else
41+
echo "No JSON or YAML files changed"
42+
fi

test.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "test",
3+
"value": 123
4+
}

test.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: test
2+
value: 123
3+
nested:
4+
key: value
5+
number: 456

0 commit comments

Comments
 (0)