diff --git a/.github/workflows/update-schema.yaml b/.github/workflows/update-schema.yaml index 780dfd167..d258d9ff7 100644 --- a/.github/workflows/update-schema.yaml +++ b/.github/workflows/update-schema.yaml @@ -1,13 +1,13 @@ name: Docs on: - push: + pull_request: + branches: + - develop paths: - - 'chord_metadata_service/mohpackets/schemas/**' - - 'chord_metadata_service/mohpackets/models.py' - - 'chord_metadata_service/mohpackets/docs/schema.json' + - "chord_metadata_service/mohpackets/schemas/**" + - "chord_metadata_service/mohpackets/models.py" - jobs: generate-moh-schema: name: Update MoH schema @@ -36,21 +36,6 @@ jobs: echo "CACHE_DURATION = 0" >> config/settings/base.py echo "AGGREGATE_COUNT_THRESHOLD = 5" >> config/settings/base.py python manage.py export_openapi_schema --api chord_metadata_service.mohpackets.apis.core.api | python -m json.tool > chord_metadata_service/mohpackets/docs/schema.json - - - name: Commit new schema.json - uses: stefanzweifel/git-auto-commit-action@v5 - id: auto-commit-action - with: - commit_message: Update schema.json - file_pattern: 'chord_metadata_service/mohpackets/docs/schema.json' - - - name: Update schema.json with new SHA - if: steps.auto-commit-action.outputs.changes_detected == 'true' - run: | - REPO_NAME=${{ github.repository }} - SHA=${{ steps.auto-commit-action.outputs.commit_hash }} - SCHEMA_PATH=chord_metadata_service/mohpackets/docs/schema.json - sed -i 's|"description": "This is the RESTful API for the MoH Service."|"description": "This is the RESTful API for the MoH Service. Based on https://raw.githubusercontent.com/'"$REPO_NAME"'/'"$SHA"'/'"$SCHEMA_PATH"'"|' $SCHEMA_PATH - name: Install widdershins run: npm install -g widdershins @@ -58,15 +43,28 @@ jobs: - name: Convert schema to OpenAPI documentation run: | npx widdershins ./chord_metadata_service/mohpackets/docs/schema.json -o ./chord_metadata_service/mohpackets/docs/schema.md -u ./chord_metadata_service/mohpackets/docs/widdershins/templates/openapi3 -c true --omitHeader true - + - name: Install PyYAML run: pip install PyYAML - name: Convert schema.json to schema.yml run: python -c 'import json, yaml; json.load(open("chord_metadata_service/mohpackets/docs/schema.json")); print(yaml.dump(json.load(open("chord_metadata_service/mohpackets/docs/schema.json"))))' > chord_metadata_service/mohpackets/docs/schema.yml - - name: Update schemas with SHA + - name: Update schemas + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: Update schemas + file_pattern: "chord_metadata_service/mohpackets/docs/schema.json chord_metadata_service/mohpackets/docs/schema.md chord_metadata_service/mohpackets/docs/schema.yml" + + - name: Get SHA commit of schema.json + id: get-sha + run: echo "SHA=$(git log -1 --format=%H chord_metadata_service/mohpackets/docs/schema.json)" >> $GITHUB_ENV + + - name: Write SHA to schema_version.txt + run: echo ${{ env.SHA }} > chord_metadata_service/mohpackets/docs/schema_version.txt + + - name: Commit updated schema_version.txt uses: stefanzweifel/git-auto-commit-action@v5 with: - commit_message: Update schemas with SHA - file_pattern: 'chord_metadata_service/mohpackets/docs/schema.json chord_metadata_service/mohpackets/docs/schema.md chord_metadata_service/mohpackets/docs/schema.yml ' + commit_message: Update schema version + file_pattern: "chord_metadata_service/mohpackets/docs/schema_version.txt" diff --git a/chord_metadata_service/mohpackets/apis/core.py b/chord_metadata_service/mohpackets/apis/core.py index 1dfebf957..55f677d12 100644 --- a/chord_metadata_service/mohpackets/apis/core.py +++ b/chord_metadata_service/mohpackets/apis/core.py @@ -21,7 +21,7 @@ explorer_router as explorer_router, ) from chord_metadata_service.mohpackets.apis.ingestion import router as ingest_router -from chord_metadata_service.mohpackets.utils import get_schema_url +from chord_metadata_service.mohpackets.utils import get_schema_version from django.views.decorators.cache import cache_page from ninja.decorators import decorate_view @@ -192,14 +192,12 @@ def authenticate(self, request, service_token): @api.get("/service-info") @decorate_view(cache_page(settings.CACHE_DURATION)) def service_info(request): - schema_url = get_schema_url() - return JsonResponse( { "name": "katsu", "description": "A CanDIG clinical data service", "version": settings.KATSU_VERSION, - "schema_url": schema_url, + "schema_version": get_schema_version(), }, status=200, safe=False, diff --git a/chord_metadata_service/mohpackets/docs/schema_version.txt b/chord_metadata_service/mohpackets/docs/schema_version.txt new file mode 100644 index 000000000..534a21edd --- /dev/null +++ b/chord_metadata_service/mohpackets/docs/schema_version.txt @@ -0,0 +1 @@ +440 diff --git a/chord_metadata_service/mohpackets/utils.py b/chord_metadata_service/mohpackets/utils.py index f07cf6513..f788a32db 100644 --- a/chord_metadata_service/mohpackets/utils.py +++ b/chord_metadata_service/mohpackets/utils.py @@ -1,39 +1,17 @@ -import json import logging -import re from enum import Enum logger = logging.getLogger(__name__) -def get_schema_url(): +def get_schema_version(): """ - Retrieve the SHA URL from schema.json. - It reads the first 6 lines of the JSON file and extracts the URL from the "description". + Reads the version number from the schema file. """ - url_pattern = r"https://[^\s]+" # get everything after https - - schema_url = "None" - - try: - with open( - "chord_metadata_service/mohpackets/docs/schema.json", "r" - ) as json_file: - # Read the first 6 lines of the JSON file only - # The line we are looking for is on the 6th - first_6_lines = [next(json_file) for _ in range(6)] - first_6_lines.extend(["}", "}"]) # make valid JSON - # Concatenate the lines to form a JSON string - json_str = "".join(first_6_lines) - data = json.loads(json_str) - desc_str = data["info"]["description"] - schema_url = re.search(url_pattern, desc_str).group() - except Exception as e: - logger.debug( - f"An error occurred while fetching the schema URL. Details: {str(e)}" - ) - - return schema_url + file_path = "chord_metadata_service/mohpackets/docs/schema_version.txt" + with open(file_path, "r") as file: + version = file.read().strip() + return version def list_to_enum(enum_name, value_list):