Skip to content

Commit cfb2f70

Browse files
Creating a github action for pydantic-to-typescript (#23)
1 parent 92d028d commit cfb2f70

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

.coveragerc

Whitespace-only changes.

action.yml

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: Pydantic to Typescript
2+
description: |
3+
Convert pydantic models into typescript definitions and ensure that your type definitions are in sync.
4+
If a mismatch is detected, a pull request will automatically be opened.
5+
author: Phillip Dupuis
6+
7+
inputs:
8+
python-module:
9+
required: true
10+
description: >
11+
name or filepath of the root python module.
12+
Typescript interfaces will be generated for all of the pydantic models
13+
contained within this module and its discoverable submodules.
14+
# TODO: maybe add ts-repo option? In case the typescript stuff is in a different repository. (But default to current repo if unspecified)
15+
ts-file:
16+
required: true
17+
description: >
18+
path to the file where the resulting typescript definitions will be saved.
19+
Example: './frontend/apiTypes.ts'.
20+
exclude-models:
21+
required: false
22+
default: ""
23+
description: >
24+
comma-separated list of the names of any pydantic models which should
25+
be omitted from the resulting typescript definitions.
26+
fail-on-mismatch:
27+
required: false
28+
type: boolean
29+
default: true
30+
description: >
31+
Boolean flag indicating if type definition mismatches should cause this action to fail.
32+
GITHUB_TOKEN:
33+
required: false
34+
description: >
35+
Value for secrets.GITHUB_TOKEN, necessary for pull requests to be automatically opened.
36+
37+
outputs:
38+
mismatch:
39+
value: ${{ steps.check-ts-defs.outputs.files_changed || 'false' }}
40+
description: >
41+
If true, the current typescript definitions in 'inputs.ts-file' are different
42+
from the ones which were automatically generated from the pydantic models.
43+
44+
runs:
45+
using: composite
46+
steps:
47+
- name: Set up Python
48+
uses: actions/setup-python@v4
49+
with:
50+
python-version: ">=3.6 <=3.10"
51+
- name: Install pydantic-to-typescript
52+
shell: bash
53+
run: |
54+
python -m pip install -U pip wheel pydantic-to-typescript
55+
- name: Set up Node.js 16
56+
uses: actions/setup-node@v3
57+
with:
58+
node-version: 16
59+
- name: Install json-schema-to-typescript
60+
shell: bash
61+
run: |
62+
npm i json-schema-to-typescript --location=global
63+
- name: Run pydantic2ts
64+
shell: bash
65+
env:
66+
INPUT_PY_MODULE: ${{ inputs.python-module }}
67+
INPUT_TS_FILE: ${{ inputs.ts-file }}
68+
INPUT_EXCLUDE_MODELS: ${{ inputs.exclude-models }}
69+
run: |
70+
CMD="pydantic2ts --module $INPUT_PY_MODULE --output $INPUT_TS_FILE"
71+
for model in ${INPUT_EXCLUDE_MODELS//,/ }
72+
do
73+
CMD+=" --exclude $model"
74+
done
75+
eval "$CMD"
76+
- name: Check if typescript definitions changed
77+
uses: tj-actions/[email protected]
78+
id: check-ts-defs
79+
with:
80+
files: ${{ inputs.ts-file }}
81+
- name: create pull request for typescript definition updates
82+
if: steps.check-ts-defs.outputs.files_changed == 'true'
83+
uses: peter-evans/create-pull-request@v4
84+
with:
85+
token: ${{ inputs.GITHUB_TOKEN }}
86+
add-paths: ${{ inputs.ts-file }}
87+
branch: update-ts-definitions
88+
- name: halt execution if type definition mismatch detected
89+
if: inputs.fail-on-mismatch == 'true' && steps.check-ts-defs.outputs.files_changed == 'true'
90+
shell: bash
91+
run: |
92+
echo "Typescript definitions and pydantic models are out of sync"
93+
exit 1

0 commit comments

Comments
 (0)