Skip to content

Commit 11c3435

Browse files
committed
Validate PR using a python script
1 parent 67b7759 commit 11c3435

File tree

2 files changed

+44
-29
lines changed

2 files changed

+44
-29
lines changed
+3-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Ships Blueprint.json file?
1+
name: Ships a valid Blueprint.json file?
22

33
on:
44
pull_request:
@@ -11,32 +11,6 @@ jobs:
1111
- name: Checkout code
1212
uses: actions/checkout@v2
1313

14-
- name: Install jsonlint
14+
- name: Run validate_pr.py
1515
run: |
16-
sudo apt-get update
17-
sudo apt-get install -y npm
18-
npm install -g jsonlint
19-
20-
- name: Find updated directories
21-
id: find_directories
22-
run: |
23-
git diff --name-only origin/main...$GITHUB_SHA | grep '^blueprints/' | cut -d/ -f2 | sort -u > updated_directories.txt
24-
25-
- name: Check blueprint.json files
26-
id: check_blueprints
27-
run: |
28-
while IFS= read -r directory; do
29-
if [ ! -f "blueprints/$directory/blueprint.json" ]; then
30-
echo "Missing blueprint.json file in $directory"
31-
exit 1
32-
fi
33-
done < updated_directories.txt
34-
35-
- name: Validate blueprint.json files
36-
run: |
37-
while IFS= read -r directory; do
38-
if [ -f "blueprints/$directory/blueprint.json" ]; then
39-
echo "Validating blueprint.json file in $directory"
40-
jsonlint -q "blueprints/$directory/blueprint.json"
41-
fi
42-
done < updated_directories.txt
16+
python validate_pr.py

validate_pr.py

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import os
2+
import re
3+
import json
4+
import sys
5+
6+
def validate_blueprints():
7+
# Get the list of directories touched in the current branch
8+
touched_dirs = get_touched_directories()
9+
10+
for dir in touched_dirs:
11+
if not re.match(r'^blueprints/[^/]+$', dir):
12+
continue
13+
blueprint_json_path = os.path.join(dir, 'blueprint.json')
14+
15+
# Check if blueprint.json file exists
16+
if not os.path.exists(blueprint_json_path):
17+
print(f"Error: {dir}/{blueprint_json_path} file does not exist in this PR.")
18+
sys.exit(1)
19+
20+
# Read and validate the JSON file
21+
try:
22+
with open(blueprint_json_path, 'r') as f:
23+
blueprint_json = json.load(f)
24+
except json.JSONDecodeError as e:
25+
print(f"Error: Invalid JSON in {blueprint_json_path}: {str(e)}")
26+
sys.exit(1)
27+
28+
29+
def get_touched_directories():
30+
# Run git diff command to get the list of directories touched in the current branch
31+
diff_output = os.popen('git diff --name-only origin/trunk').read()
32+
touched_dirs = set()
33+
34+
# Extract the directory paths from the diff output
35+
for line in diff_output.splitlines():
36+
dir_path = os.path.dirname(line)
37+
touched_dirs.add(dir_path)
38+
39+
return touched_dirs
40+
# Call the function to validate blueprints
41+
validate_blueprints()

0 commit comments

Comments
 (0)