Skip to content

Commit

Permalink
Merge pull request #235 from CanDIG/sonchau/schema_action
Browse files Browse the repository at this point in the history
DIG-1669: Automate incrementing schema version when schemas get updated
  • Loading branch information
SonQBChau authored Jun 13, 2024
2 parents 8c0c95c + 88f6bd4 commit 8a0136d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 56 deletions.
46 changes: 22 additions & 24 deletions .github/workflows/update-schema.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,37 +36,35 @@ 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

- 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"
6 changes: 2 additions & 4 deletions chord_metadata_service/mohpackets/apis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions chord_metadata_service/mohpackets/docs/schema_version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
440
34 changes: 6 additions & 28 deletions chord_metadata_service/mohpackets/utils.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 8a0136d

Please sign in to comment.