|
| 1 | +name: PR JSON Validation |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - "data/leetcode-problems.json" |
| 7 | + - "scripts/normalize_json.py" |
| 8 | + branches: |
| 9 | + - main |
| 10 | + |
| 11 | +jobs: |
| 12 | + validate-json: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + fetch-depth: 0 |
| 19 | + |
| 20 | + - name: Set up Python |
| 21 | + uses: actions/setup-python@v5 |
| 22 | + with: |
| 23 | + python-version: "3.11" |
| 24 | + |
| 25 | + - name: Validate JSON syntax |
| 26 | + run: | |
| 27 | + python3 -m json.tool data/leetcode-problems.json > /dev/null |
| 28 | + echo "✓ JSON syntax is valid" |
| 29 | +
|
| 30 | + - name: Test normalization script |
| 31 | + run: | |
| 32 | + # Make script executable |
| 33 | + chmod +x scripts/normalize_json.py |
| 34 | + |
| 35 | + # Normalize to a temporary file to compare |
| 36 | + python3 scripts/normalize_json.py data/leetcode-problems.json data/leetcode-problems.json.normalized |
| 37 | + |
| 38 | + # Check if the normalized version differs from the original |
| 39 | + if ! diff -q data/leetcode-problems.json data/leetcode-problems.json.normalized > /dev/null; then |
| 40 | + echo "❌ Error: JSON file is not normalized." |
| 41 | + echo "Please run './scripts/normalize_json.py data/leetcode-problems.json' and commit the changes." |
| 42 | + echo "" |
| 43 | + echo "Diff showing what needs to be normalized:" |
| 44 | + diff data/leetcode-problems.json data/leetcode-problems.json.normalized || true |
| 45 | + rm data/leetcode-problems.json.normalized |
| 46 | + exit 1 |
| 47 | + else |
| 48 | + echo "✓ JSON file is properly normalized" |
| 49 | + rm data/leetcode-problems.json.normalized |
| 50 | + fi |
| 51 | +
|
| 52 | + - name: Verify JSON structure |
| 53 | + run: | |
| 54 | + python3 << 'EOF' |
| 55 | + import json |
| 56 | + import sys |
| 57 | +
|
| 58 | + with open('data/leetcode-problems.json', 'r') as f: |
| 59 | + data = json.load(f) |
| 60 | +
|
| 61 | + # Check if root is a dict |
| 62 | + if not isinstance(data, dict): |
| 63 | + print("❌ Error: Root must be a dictionary") |
| 64 | + sys.exit(1) |
| 65 | +
|
| 66 | + # Check if all keys are numeric strings |
| 67 | + for key in data.keys(): |
| 68 | + try: |
| 69 | + int(key) |
| 70 | + except ValueError: |
| 71 | + print(f"❌ Error: Key '{key}' is not a numeric string") |
| 72 | + sys.exit(1) |
| 73 | +
|
| 74 | + # Check if keys are sorted |
| 75 | + keys = [int(k) for k in data.keys()] |
| 76 | + if keys != sorted(keys): |
| 77 | + print("❌ Error: Keys are not sorted numerically") |
| 78 | + sys.exit(1) |
| 79 | +
|
| 80 | + print(f"✓ JSON structure is valid ({len(data)} entries)") |
| 81 | + print(f"✓ All keys are numeric and sorted") |
| 82 | + EOF |
0 commit comments