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

DIG-1669: Automate incrementing schema version when schemas get updated #235

Merged
merged 7 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
39 changes: 15 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,28 @@ 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: Increment schema version
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we can't use the SHA of the updated schema.yml (or schema.json, whichever) document as the schema version?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is the current branch SHA will get deleted once it merges. Sure I can put the SHA in, but if someone were to go back and find it, they won't be able to.

Copy link
Author

@SonQBChau SonQBChau Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless we want to keep all the branchs PR around

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the branch SHA, the file sha. That stays around.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, in this deleted branch https://github.com/CanDIG/htsget_app/blob/9d00ade5811bf48727b7b73516c30d06796d5f81/tests/test_htsget_server.py and in the stable branch it was merged into https://github.com/CanDIG/htsget_app/blob/stable/tests/test_htsget_server.py, the sha for test_htsget_server.py is 6429207.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I can try that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe that's not the file sha, I mean the commit sha.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

6429207 is the sha for the commit, whether it is in the deleted merged PR branch or in the stable branch it was merged into. So if a change was made to schema.json, which is triggering this schema version update, there should be a corresponding commit sha for that.

run: |
current_version=$(cat ./chord_metadata_service/mohpackets/docs/schema_version.txt)
new_version=$((current_version + 1))
echo $new_version > ./chord_metadata_service/mohpackets/docs/schema_version.txt

- name: Update schemas
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 schemas
file_pattern: "chord_metadata_service/mohpackets/docs/schema.json chord_metadata_service/mohpackets/docs/schema.md chord_metadata_service/mohpackets/docs/schema.yml 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 int(version)

SonQBChau marked this conversation as resolved.
Show resolved Hide resolved
SonQBChau marked this conversation as resolved.
Show resolved Hide resolved

def list_to_enum(enum_name, value_list):
Expand Down
Loading