diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..6ddc1b2 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,30 @@ +name: publish +on: + push: + branches: + - main + workflow_dispatch: +permissions: + contents: write +jobs: + deploy: + name: Download and publish JSON schemas + if: github.event.repository.fork == false + runs-on: ubuntu-latest + steps: + - name: Checkout repo content + uses: actions/checkout@v4 + - name: Setup Python + uses: actions/setup-python@v5 + with: + python-version: 3.x + - name: Install dependencies + run: pip install requests + - name: Execute Python script + run: python sync.py ./src/catalog.json ./publish + - name: Deploy to GitHub Pages + uses: peaceiris/actions-gh-pages@v4 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + publish_dir: ./publish + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..d12dade --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# OpenPotato SchemaHub + +The aim of OpenPotato SchemaHub is to provide a central repository for the various JSON schema specifications at the OpenPotato Project. The schema files are stored under GitHub Pages and can be addressed via `https://schemahub.openpotato.org///`. + +Here is a list of the available schema files: + ++ https://schemahub.openpotato.org/csv-table-schema/v0.1/schema.json ++ https://schemahub.openpotato.org/opencoli/v0.1/schema.json diff --git a/publish/CNAME b/publish/CNAME new file mode 100644 index 0000000..93f3f38 --- /dev/null +++ b/publish/CNAME @@ -0,0 +1 @@ +schemahub.openpotato.org \ No newline at end of file diff --git a/publish/schema-catalog.json b/publish/schema-catalog.json new file mode 100644 index 0000000..4564af9 --- /dev/null +++ b/publish/schema-catalog.json @@ -0,0 +1,51 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "id": "https://schemahub.openpotato.org/schema-catalog.json", + "title": "Catalog Schema Specification for schemahub.openpotato.org", + "description": "JSON schema for the schemahub.openpotato.org catalog file", + "type": "object", + "properties": { + "schemas": { + "description": "A list of JSON schema references.", + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "description": "The name of the schema", + "type": "string" + }, + "description": { + "description": "A description of the schema", + "type": "string" + }, + "urls": { + "description": "A list of absolute schema location URLs", + "uniqueItems": true, + "type": "array", + "items": { + "type": "string", + "format": "uri", + "pattern": "^https://" + } + }, + "targetFolder": { + "description": "A relative target folder into which the schema files for the publication are to be copied.", + "type": "string" + } + }, + "required": [ + "name", + "description", + "urls", + "targetFolder" + ], + "additionalProperties": false + } + } + }, + "required": [ + "schemas" + ], + "additionalProperties": false +} \ No newline at end of file diff --git a/src/catalog.json b/src/catalog.json new file mode 100644 index 0000000..cb9d2b8 --- /dev/null +++ b/src/catalog.json @@ -0,0 +1,22 @@ +{ + "$schema": "https://schemahub.openpotato.org/schema-catalog.json", + "version": 1, + "schemas": [ + { + "name": "CSV Table Schema Specification v-0.1", + "description": "JSON schema for CSV Table Schema (version 0.1)", + "targetFolder": "csv-table-schema/v0.1", + "urls": [ + "https://raw.githubusercontent.com/openpotato/csv-table-schema/main/schemas/v0.1/schema.json" + ] + }, + { + "name": "OpenCoLi Schema v-0.1", + "description": "JSON schema for OpenCoLi format (version 0.1)", + "targetFolder": "opencoli/v0.1", + "urls": [ + "https://raw.githubusercontent.com/openpotato/opencoli/main/schemas/v0.1/schema.json" + ] + } + ] +} diff --git a/sync.py b/sync.py new file mode 100644 index 0000000..7a30173 --- /dev/null +++ b/sync.py @@ -0,0 +1,44 @@ +import json +import os +import requests +import argparse + +def download_json_files(catalog_file_path, base_target_folder): + # Read the JSON catalog file + with open(catalog_file_path, 'r') as file: + data = json.load(file) + + # Iterate through the schemas + for schema in data['schemas']: + relative_target_folder = schema['targetFolder'] + urls = schema['urls'] + + # Create the full path to the target folder under the base folder + full_target_folder = os.path.join(base_target_folder, relative_target_folder) + + # Create the target folder if it doesn't exist + if not os.path.exists(full_target_folder): + os.makedirs(full_target_folder) + + # Download each JSON file and save it to the target folder + for url in urls: + response = requests.get(url) + if response.status_code == 200: + file_name = os.path.join(full_target_folder, url.split('/')[-1]) + with open(file_name, 'w', encoding='utf8') as json_file: + json_file.write(response.text) + print(f"Downloaded and saved {url} to {file_name}") + else: + print(f"Failed to download {url}") + +if __name__ == "__main__": + # Setup argument parser + parser = argparse.ArgumentParser(description="Download JSON schemas and save them to specified folders.") + parser.add_argument('catalog_file_path', type=str, help='Path to the input JSON file.') + parser.add_argument('base_target_folder', type=str, help='Base folder where target folders will be created.') + + # Parse arguments + args = parser.parse_args() + + # Call the download function with the provided arguments + download_json_files(args.catalog_file_path, args.base_target_folder)