-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate-api-client-based-on-openapi.yml
More file actions
216 lines (189 loc) · 8.29 KB
/
create-api-client-based-on-openapi.yml
File metadata and controls
216 lines (189 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
name: Receive Documentation
on:
repository_dispatch:
types: [send-docs]
permissions:
contents: write
jobs:
process-docs:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Receive and Persist JSON OpenAPI specs
env:
AUTHOR: ${{ github.event.client_payload.author }}
OPENAPI_COMPRESSED: ${{ github.event.client_payload.file_content_base_64 }}
APP_NAME: ${{ github.event.client_payload.app_name }}
WEB_OPENAPI_PATH: ${{ github.workspace }}/web/openapi_specs
run: |
author=$GITHUB_AUTHOR
echo "::notice:: Process triggered by $AUTHOR"
# Replace - by _ in order to avoid some errors at generating packages.
APP_NAME=$(echo $APP_NAME | sed s/-/_/g)
if [[ $APP_NAME == "" ]]; then
echo "::error:: APP_NAME cannot be empty"
exit 1
fi
openapi_filename="$APP_NAME"_openapi_specification.json
openapi_filepath=$WEB_OPENAPI_PATH/$openapi_filename
echo "Persisting openapi specs in $openapi_filepath"
OPENAPI=$(echo $OPENAPI_COMPRESSED | base64 -d | unxz -c)
echo $OPENAPI > $openapi_filepath
echo "----"
echo "API_ROOT_DIR=${{ github.workspace }}/apis" >> $GITHUB_ENV
echo "CLIENT_ROOT_DIR=${{ github.workspace }}/agentverse-client" >> $GITHUB_ENV
echo "::notice::Update for $APP_NAME"
echo "APP_NAME=$APP_NAME" >> $GITHUB_ENV
echo "openapi_filepath=$openapi_filepath" >> $GITHUB_ENV
echo "openapi_filename=$openapi_filename" >> $GITHUB_ENV
- name: Check registered apps and get client path
run: |
app_key="${{ github.event.client_payload.app_name }}"
registered_apps="${{ vars.APP_MAPPING }}"
# decode
registered_apps=$(echo $registered_apps | xxd -r -p)
# get path from json
client_path=$(echo $registered_apps | jq -rc '.["'$app_key'"]')
if [ $client_path = "null" ]
then
exit 1
else
client_path=$(echo $client_path | sed s/-/_/g)
echo "CLIENT_PATH=$client_path" >> $GITHUB_ENV
fi
- name: Create scaffolding & register docs
run: |
echo "API_ROOT_DIR: $API_ROOT_DIR"
# TODO: What to do with conflict?
if [ ! -d $API_ROOT_DIR/$APP_NAME ]
then
echo "::notice:: Creating dir $API_ROOT_DIR/$APP_NAME"
mkdir $API_ROOT_DIR/$APP_NAME
else
echo "API $API_ROOT_DIR/$APP_NAME already exists."
fi
# Add file to register docs into fastapi
cat <<- EOF > $API_ROOT_DIR/$APP_NAME/register_api_docs.py
api_id: str = '$APP_NAME'
openapi_specs_name: str = '$openapi_filename'
docs_endpoint_name: str = '/$APP_NAME/'
EOF
# SYNC CLIENT GENERATION
# ---
- name: "SYNC CLIENT: Prepare openapi config file"
env:
CLIENT_ROOT_PROJECT_NAME: "agentverse_client"
LIBRARY: urllib3
PACKAGE_NAME: agentverse_client.${{env.CLIENT_PATH}}
run: |
config_file_path="./generator_config_file.yaml"
# CONFIG FILE DEFINITION
cat <<- EOF -> $config_file_path
templateDir: custom_templates
generatorName: python
inputSpec: $openapi_filepath
additionalProperties:
packageName: $PACKAGE_NAME
projectName: $APP_NAME
generateSourceCodeOnly: true
library: $LIBRARY
basePath: https://agentverse.ai # This and serverURL have to be the same value
serverURL: https://agentverse.ai # This and basePath have to be the same value
files:
configuration.mustache:
templateType: SupportingFiles
destinationFilename: "agentverse_client/$CLIENT_PATH/configuration.py"
configOptions:
packageVersion: "1.0.0"
EOF
cat $config_file_path
echo "config_file_path=$config_file_path" >> $GITHUB_ENV
echo $OPENAPI > ./openapi.json
- name: "SYNC CLIENT: Generate client"
uses: openapi-generators/openapitools-generator-action@v1
env:
API_TAG: ${{ github.event.client_payload.api_tag }}
with:
generator: python
generator-tag: 'v7.11.0'
config-file: ${{ env.config_file_path }}
openapi-file: ./web/openapi_specs/${{ env.openapi_filename }}
command-args: "--global-property models,supportingFiles,apis='$API_TAG' -o ./agentverse-client"
# ---
# ASYNC CLIENT GENERATION
# ---
- name: "ASYNC CLIENT: Prepare openapi config file"
env:
CLIENT_ROOT_PROJECT_NAME: "agentverse_client"
LIBRARY: asyncio
ROOT_PACKAGE_NAME: agentverse_client
ASYNC_CLIENT_DIR_NAME: aio
PACKAGE_NAME: ${{ env.CLIENT_PATH }}
run: |
echo "ROOT_PACKAGE_NAME=$ROOT_PACKAGE_NAME" >> $GITHUB_ENV
echo "ASYNC_CLIENT_DIR_NAME=$ASYNC_CLIENT_DIR_NAME" >> $GITHUB_ENV
ASYNC_PACKAGE_NAME="$ROOT_PACKAGE_NAME".$CLIENT_PATH."$ASYNC_CLIENT_DIR_NAME"
config_file_path="./generator_config_file.yaml"
# CONFIG FILE DEFINITION
cat <<- EOF -> $config_file_path
templateDir: custom_templates
generatorName: python
inputSpec: $openapi_filepath
additionalProperties:
packageName: $ASYNC_PACKAGE_NAME
projectName: $APP_NAME
generateSourceCodeOnly: true
library: $LIBRARY
basePath: https://agentverse.ai # This and serverURL have to be the same value
serverURL: https://agentverse.ai # This and basePath have to be the same value
files:
configuration.mustache:
templateType: SupportingFiles
destinationFilename: "agentverse_client/$CLIENT_PATH/$ASYNC_CLIENT_DIR_NAME/configuration.py"
configOptions:
packageVersion: "1.0.0"
EOF
cat $config_file_path
echo "config_file_path=$config_file_path" >> $GITHUB_ENV
echo $OPENAPI > ./openapi.json
- name: "ASYNC CLIENT: Generate client"
uses: openapi-generators/openapitools-generator-action@v1
env:
API_TAG: ${{ github.event.client_payload.api_tag }}
ASYNC_OUTPUT_LOCATION: "./tmp_async_agentverse-client"
with:
generator: python
generator-tag: 'v7.11.0'
config-file: ${{ env.config_file_path }}
openapi-file: ./web/openapi_specs/${{ env.openapi_filename }}
command-args: "--global-property models,supportingFiles,apis='$API_TAG' -o $ASYNC_OUTPUT_LOCATION"
# ---
# Moving async client to the root client dir.
# ---
- name: Move async client to the root client dir.
env:
ASYNC_OUTPUT_LOCATION: "./tmp_async_agentverse-client"
run: |
ASYNC_PACKAGE_NAME="$ROOT_PACKAGE_NAME".$CLIENT_PATH."$ASYNC_CLIENT_DIR_NAME"
rm -fR $CLIENT_ROOT_DIR/agentverse_client/$CLIENT_PATH/$ASYNC_CLIENT_DIR_NAME
mv "$ASYNC_OUTPUT_LOCATION"/"$ROOT_PACKAGE_NAME"/$CLIENT_PATH/"$ASYNC_CLIENT_DIR_NAME" $CLIENT_ROOT_DIR/agentverse_client/$CLIENT_PATH
# ---
- name: Persist changes with Git
run: |
echo "Commiting dir $API_ROOT_DIR/$APP_NAME, ${{ github.workspace }}/web/ and ${{ env.CLIENT_ROOT_DIR }}"
git config --local user.name "fetchbot"
git config --local user.email "fetchbot@fetch.ai"
# Pull before pushing changes to avoid conflicts
git stash
git pull
if git stash list | grep -q 'stash@'; then
echo "Stash entries found, popping the latest one"
git stash pop
echo "Stash popped successfully"
else
echo "No stash entries found, skipping stash pop"
fi
git add $API_ROOT_DIR/$APP_NAME ${{ github.workspace }}/web/ ${{ env.CLIENT_ROOT_DIR }}
git commit -m "Adding or updating docs and client for $APP_NAME" || echo "No changes to commit"
git push