From 5f448c0683692505ca73602f7b185b552f300a60 Mon Sep 17 00:00:00 2001 From: liwuv587 Date: Thu, 21 Aug 2025 15:37:53 +0800 Subject: [PATCH 1/3] 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 --- .github/scripts/validate_files.py | 24 +++++++++++++++++------- .github/workflows/code-review.yml | 16 +++++++++++++++- test.json | 4 ++++ test.yaml | 5 +++++ 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 test.json create mode 100644 test.yaml diff --git a/.github/scripts/validate_files.py b/.github/scripts/validate_files.py index 7feb2a2..f093269 100644 --- a/.github/scripts/validate_files.py +++ b/.github/scripts/validate_files.py @@ -54,6 +54,7 @@ def main(): parser.add_argument('--directory', '-d', default='.', help='Directory to scan for files') parser.add_argument('--check-json', action='store_true', help='Check JSON files') parser.add_argument('--check-yaml', action='store_true', help='Check YAML files') + parser.add_argument('--changed-files', nargs='*', help='List of changed files to validate') args = parser.parse_args() @@ -62,17 +63,26 @@ def main(): all_valid = True - if args.check_json: - json_files = find_files(args.directory, '.json') - print(f"Found {len(json_files)} JSON files") + # If changed files are provided, filter them by extension + if args.changed_files: + json_files = [f for f in args.changed_files if f.endswith('.json')] + yaml_files = [f for f in args.changed_files if f.endswith('.yaml') or f.endswith('.yml')] + else: + # Otherwise, find all files in directory + json_files = find_files(args.directory, '.json') if args.check_json else [] + yaml_files = [] + if args.check_yaml: + yaml_files = find_files(args.directory, '.yaml') + yaml_files += find_files(args.directory, '.yml') + + if args.check_json and json_files: + print(f"Found {len(json_files)} JSON files to validate") for file_path in json_files: if not validate_json(file_path): all_valid = False - if args.check_yaml: - yaml_files = find_files(args.directory, '.yaml') - yaml_files += find_files(args.directory, '.yml') - print(f"Found {len(yaml_files)} YAML files") + if args.check_yaml and yaml_files: + print(f"Found {len(yaml_files)} YAML files to validate") for file_path in yaml_files: if not validate_yaml(file_path): all_valid = False diff --git a/.github/workflows/code-review.yml b/.github/workflows/code-review.yml index c00eaee..70c682d 100644 --- a/.github/workflows/code-review.yml +++ b/.github/workflows/code-review.yml @@ -13,6 +13,15 @@ jobs: - name: Checkout code uses: actions/checkout@v4 + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v45 + with: + files: | + **/*.json + **/*.yaml + **/*.yml + - name: Set up Python uses: actions/setup-python@v4 with: @@ -25,4 +34,9 @@ jobs: - name: Validate JSON and YAML files run: | - python .github/scripts/validate_files.py --directory . --check-json --check-yaml \ No newline at end of file + if [ "${{ steps.changed-files.outputs.any_changed }}" = "true" ]; then + echo "Changed files: ${{ steps.changed-files.outputs.all_changed_files }}" + python .github/scripts/validate_files.py --changed-files ${{ steps.changed-files.outputs.all_changed_files }} --check-json --check-yaml + else + echo "No JSON or YAML files changed" + fi \ No newline at end of file diff --git a/test.json b/test.json new file mode 100644 index 0000000..28aaed5 --- /dev/null +++ b/test.json @@ -0,0 +1,4 @@ +{ + "name": "test", + "value": 123 +} \ No newline at end of file diff --git a/test.yaml b/test.yaml new file mode 100644 index 0000000..c745c19 --- /dev/null +++ b/test.yaml @@ -0,0 +1,5 @@ +name: test +value: 123 +nested: + key: value + number: 456 \ No newline at end of file From 596b594e6ab71d6f777c4ccbe895a40bc2c779bf Mon Sep 17 00:00:00 2001 From: liwuv587 Date: Thu, 21 Aug 2025 15:39:30 +0800 Subject: [PATCH 2/3] test: add invalid test files for validation testing --- invalid.json | 5 +++++ invalid.yaml | 3 +++ 2 files changed, 8 insertions(+) create mode 100644 invalid.json create mode 100644 invalid.yaml diff --git a/invalid.json b/invalid.json new file mode 100644 index 0000000..7a81f8b --- /dev/null +++ b/invalid.json @@ -0,0 +1,5 @@ +{ + "name": "test", + "value": 123, + "invalid": +} \ No newline at end of file diff --git a/invalid.yaml b/invalid.yaml new file mode 100644 index 0000000..ebf6fa7 --- /dev/null +++ b/invalid.yaml @@ -0,0 +1,3 @@ +name: test +value: 123 + invalid_indentation: value \ No newline at end of file From d9d79972a365fda5f52cb72ad205e2550378b573 Mon Sep 17 00:00:00 2001 From: liwuv587 Date: Thu, 21 Aug 2025 15:39:52 +0800 Subject: [PATCH 3/3] chore: remove test and invalid config files --- invalid.json | 5 ----- invalid.yaml | 3 --- test.json | 4 ---- test.yaml | 5 ----- 4 files changed, 17 deletions(-) delete mode 100644 invalid.json delete mode 100644 invalid.yaml delete mode 100644 test.json delete mode 100644 test.yaml diff --git a/invalid.json b/invalid.json deleted file mode 100644 index 7a81f8b..0000000 --- a/invalid.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "name": "test", - "value": 123, - "invalid": -} \ No newline at end of file diff --git a/invalid.yaml b/invalid.yaml deleted file mode 100644 index ebf6fa7..0000000 --- a/invalid.yaml +++ /dev/null @@ -1,3 +0,0 @@ -name: test -value: 123 - invalid_indentation: value \ No newline at end of file diff --git a/test.json b/test.json deleted file mode 100644 index 28aaed5..0000000 --- a/test.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "test", - "value": 123 -} \ No newline at end of file diff --git a/test.yaml b/test.yaml deleted file mode 100644 index c745c19..0000000 --- a/test.yaml +++ /dev/null @@ -1,5 +0,0 @@ -name: test -value: 123 -nested: - key: value - number: 456 \ No newline at end of file