Skip to content

Commit

Permalink
Validate PR using a python script
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Apr 15, 2024
1 parent 3a16a14 commit 581c1ab
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 29 deletions.
32 changes: 3 additions & 29 deletions .github/workflows/ships-blueprint-json.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Ships Blueprint.json file?
name: Ships a valid Blueprint.json file?

on:
pull_request:
Expand All @@ -11,32 +11,6 @@ jobs:
- name: Checkout code
uses: actions/checkout@v2

- name: Install jsonlint
- name: Run validate_pr.py
run: |
sudo apt-get update
sudo apt-get install -y npm
npm install -g jsonlint
- name: Find updated directories
id: find_directories
run: |
git diff --name-only origin/main...$GITHUB_SHA | grep '^blueprints/' | cut -d/ -f2 | sort -u > updated_directories.txt
- name: Check blueprint.json files
id: check_blueprints
run: |
while IFS= read -r directory; do
if [ ! -f "blueprints/$directory/blueprint.json" ]; then
echo "Missing blueprint.json file in $directory"
exit 1
fi
done < updated_directories.txt
- name: Validate blueprint.json files
run: |
while IFS= read -r directory; do
if [ -f "blueprints/$directory/blueprint.json" ]; then
echo "Validating blueprint.json file in $directory"
jsonlint -q "blueprints/$directory/blueprint.json"
fi
done < updated_directories.txt
python validate_pr.py
41 changes: 41 additions & 0 deletions validate_pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import os
import re
import json
import sys

def validate_blueprints():
# Get the list of directories touched in the current branch
touched_dirs = get_touched_directories()

for dir in touched_dirs:
if not re.match(r'^blueprints/[^/]+$', dir):
continue
blueprint_json_path = os.path.join(dir, 'blueprint.json')

# Check if blueprint.json file exists
if not os.path.exists(blueprint_json_path):
print(f"Error: {dir}/{blueprint_json_path} file does not exist in this PR.")
sys.exit(1)

# Read and validate the JSON file
try:
with open(blueprint_json_path, 'r') as f:
blueprint_json = json.load(f)
except json.JSONDecodeError as e:
print(f"Error: Invalid JSON in {blueprint_json_path}: {str(e)}")
sys.exit(1)


def get_touched_directories():
# Run git diff command to get the list of directories touched in the current branch
diff_output = os.popen('git diff --name-only origin/trunk').read()
touched_dirs = set()

# Extract the directory paths from the diff output
for line in diff_output.splitlines():
dir_path = os.path.dirname(line)
touched_dirs.add(dir_path)

return touched_dirs
# Call the function to validate blueprints
validate_blueprints()

0 comments on commit 581c1ab

Please sign in to comment.