Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate JSON files. #8

Merged
merged 4 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Validate

on:
push:
pull_request:
workflow_dispatch:

jobs:
build:
name: Validate
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install jsonschema
run: pip install jsonschema==4.6.1

- name: Validate JSON files
run: python schemas/validate.py
61 changes: 61 additions & 0 deletions schemas/generic-ea-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://github.com/graalvm/oracle-graalvm-ea-builds/blob/main/schemas/generic-ea-schema.json",
"title": "Generic Schema for Oracle GraalVM Early Access Builds",
"type": "array",
"items": {
"title": "List of EA builds",
"type": "object",
"required": [
"version",
"latest",
"download_base_url",
"files"
],
"additionalProperties": false,
"properties": {
"version": {
"title": "The version of an EA build",
"type": "string"
},
"latest": {
"title": "Whether this is the latest EA build for this release train",
"type": "boolean"
},
"download_base_url": {
"title": "The base URL for downloading an EA build",
"type": "string"
},
"files": {
"title": "The files of an EA build",
"type": "array",
"items": {
"title": "A file of an EA build",
"type": "object",
"required": [
"filename",
"arch",
"platform"
],
"additionalProperties": false,
"properties": {
"filename": {
"title": "The name of an EA build file",
"type": "string"
},
"arch": {
"enum": ["aarch64", "x64"],
"title": "The architecture of an EA build file",
"type": "string"
},
"platform": {
"enum": ["darwin", "linux", "windows"],
"title": "The platform of an EA build file",
"type": "string"
}
}
}
}
}
}
}
52 changes: 52 additions & 0 deletions schemas/latest-ea-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"$id": "https://github.com/graalvm/oracle-graalvm-ea-builds/blob/main/schemas/latest-ea-schema.json",
"title": "Schema for Latest Oracle GraalVM Early Access Build",
"type": "object",
"required": [
"version",
"download_base_url",
"files"
],
"additionalProperties": false,
"properties": {
"version": {
"title": "The version of the latest EA build",
"type": "string"
},
"download_base_url": {
"title": "The base URL for downloading the latest EA build",
"type": "string"
},
"files": {
"title": "The files of the latest EA build",
"type": "array",
"items": {
"title": "A file of the latest EA build",
"type": "object",
"required": [
"filename",
"arch",
"platform"
],
"additionalProperties": false,
"properties": {
"filename": {
"title": "The name of a file of the latest EA build",
"type": "string"
},
"arch": {
"enum": ["aarch64", "x64"],
"title": "The architecture of a file of the latest EA build",
"type": "string"
},
"platform": {
"enum": ["darwin", "linux", "windows"],
"title": "The platform of a file of the latest EA build",
"type": "string"
}
}
}
}
}
}
62 changes: 62 additions & 0 deletions schemas/validate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from jsonschema import validate as json_validate
import json
import os
import urllib.request


GENERIC_EA_SCHEMA = 'generic-ea-schema.json'
LATEST_EA_JSON = 'latest-ea.json'
LATEST_EA_SCHEMA = 'latest-ea-schema.json'
ROOT_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
SCHEMAS_DIR = 'schemas'
VERSIONS_DIR = 'versions'


def validate(json_name, schema_name):
print(f'Validating {json_name}...')
with open(os.path.join(ROOT_PATH, VERSIONS_DIR, json_name)) as f:
json_contents = json.load(f)
with open(os.path.join(ROOT_PATH, SCHEMAS_DIR, schema_name)) as f:
schema_contents = json.load(f)
json_validate(json_contents, schema_contents)
print(f' {json_name} validates against {schema_name}')
if schema_name == LATEST_EA_SCHEMA:
builds = [json_contents]
else:
builds = json_contents
ensure_one_latest_build(json_name, builds)
validate_builds(builds)


def ensure_one_latest_build(json_name, builds):
num_latest = sum([1 if build['latest'] else 0 for build in builds])
assert num_latest == 1, f'Expected one latest build in {json_name}, got {num_latest}'


def validate_builds(builds):
for build in builds:
version = build['version']
download_base_url = build['download_base_url']
files = build['files']
assert version in download_base_url, f'version not found in download_base_url: {json.dumps(build)}'
assert all(version in file['filename'] for file in files), f'version not found in all filenames: {json.dumps(build)}'
is_pull_request = 'GITHUB_BASE_REF' in os.environ
if not is_pull_request:
check_urls_exist(download_base_url, files)


def check_urls_exist(download_base_url, files):
for file in files:
download_url = f'{download_base_url}{file["filename"]}'
print(f" Checking '{download_url}'...")
request = urllib.request.Request(download_url, method='HEAD')
response = urllib.request.urlopen(request)
assert response.status == 200, f"Expected status code of 200, got {response.status} for '{download_url}'"


if __name__ == '__main__':
for file_name in os.listdir('versions'):
assert file_name.endswith('.json'), f"Unexpected non-JSON file '{file_name}'"
schema_name = LATEST_EA_SCHEMA if file_name == LATEST_EA_JSON else GENERIC_EA_SCHEMA
validate(file_name, schema_name)
print('JSON validation successful')