Skip to content

Commit cde1b99

Browse files
committed
generate typescript types based on schema instead of CRD
1 parent 0ed581f commit cde1b99

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

Diff for: .github/actions/generate_types/generate.py

+33-22
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212

1313
import os
1414
import json
15-
import yaml
16-
import requests
17-
from collections import OrderedDict
1815

1916
def write_json(filename: str, object: dict) -> None:
2017
"""
@@ -29,28 +26,41 @@ def create_ref(path):
2926
"""
3027
return '#/definitions/' + path
3128

32-
def consolidate_crds() -> object:
29+
def consolidate_schemas() -> object:
3330
"""
34-
Consolidate all crds in /crds into one json object
31+
Consolidate all schemas into one json object
3532
"""
36-
crds_dir = os.path.join('crds')
37-
crds = os.listdir(crds_dir)
38-
consolidated_crds_json = {
33+
schemas_dir = os.path.join('schemas/latest')
34+
consolidated_schemas_json = {
3935
'definitions': {},
4036
}
41-
for file in crds:
42-
crd_file_path = os.path.join(crds_dir, file)
43-
with open(crd_file_path) as file:
44-
yamlData = yaml.load(file, Loader=yaml.FullLoader)
45-
crd_name = yamlData['spec']['names']['kind']
46-
47-
# Add all the available schema versions
48-
for version in yamlData['spec']['versions']:
49-
new_json_name = version['name'] + '.' + crd_name
50-
new_schema = version['schema']['openAPIV3Schema']
51-
consolidated_crds_json['definitions'][new_json_name] = new_schema
52-
53-
return consolidated_crds_json
37+
38+
with open(os.path.join(schemas_dir, 'k8sApiVersion.txt')) as f:
39+
k8sApiVersion = f.readline()
40+
41+
with open(os.path.join(schemas_dir, 'jsonSchemaVersion.txt')) as f:
42+
devfileVersion = f.readline()
43+
devfileVersion = devfileVersion.replace('-alpha', '')
44+
45+
definitionName = devfileVersion + '.Devfile'
46+
devfile_json_schema_path = os.path.join(schemas_dir, 'devfile.json')
47+
with open(devfile_json_schema_path, 'r') as devfileFile:
48+
jsonData = json.load(devfileFile)
49+
consolidated_schemas_json['definitions'][definitionName] = jsonData
50+
51+
definitionName = k8sApiVersion + '.DevWorkspace'
52+
dw_json_schema_path = os.path.join(schemas_dir, 'dev-workspace.json')
53+
with open(dw_json_schema_path, 'r') as devfileFile:
54+
jsonData = json.load(devfileFile)
55+
consolidated_schemas_json['definitions'][definitionName] = jsonData
56+
57+
definitionName = k8sApiVersion + '.DevWorkspaceTemplate'
58+
dwt_json_schema_path = os.path.join(schemas_dir, 'dev-workspace-template.json')
59+
with open(dwt_json_schema_path, 'r') as devfileFile:
60+
jsonData = json.load(devfileFile)
61+
consolidated_schemas_json['definitions'][definitionName] = jsonData
62+
63+
return consolidated_schemas_json
5464

5565
def add_property_definition(root_definitions_object: dict, current_path: str, curr_object: dict, queue: list) -> None:
5666
"""
@@ -251,5 +261,6 @@ def flatten(consolidated_crds_object: dict) -> None:
251261
write_json('swagger.json', flattened_swagger_object)
252262

253263
if __name__ == "__main__":
254-
swagger_crds_json = consolidate_crds()
264+
# Get the schema and flatten them
265+
swagger_crds_json = consolidate_schemas()
255266
flatten(swagger_crds_json)

Diff for: .github/actions/generate_types/requirements.txt

-2
This file was deleted.

Diff for: .github/workflows/release-typescript-models.yaml

-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ jobs:
2727
with:
2828
python-version: '3.9.2'
2929

30-
- name: Install Python dependencies
31-
uses: py-actions/py-dependency-install@v2
32-
with:
33-
path: "api/.github/actions/generate_types/requirements.txt"
34-
3530
- name: Generate openapi-generator compatible swagger.json
3631
run: |
3732
python .github/actions/generate_types/generate.py

0 commit comments

Comments
 (0)