-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(orchestrator): verify if auto-generated openapi files are up-to-…
…date (#1323) * Verify if openapi files are up-to-date Signed-off-by: Gloria Ciavarrini <[email protected]> * fix: improve generation of definition.ts Signed-off-by: Gloria Ciavarrini <[email protected]> * fix: typo Signed-off-by: Gloria Ciavarrini <[email protected]> --------- Signed-off-by: Gloria Ciavarrini <[email protected]>
- Loading branch information
1 parent
f1f2ce1
commit 650b435
Showing
4 changed files
with
75 additions
and
846 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,72 @@ | ||
#!/bin/bash | ||
pwd | ||
set -ex | ||
set -e | ||
|
||
npx openapi-typescript ./src/openapi/openapi.yaml -o ./src/auto-generated/api/models/schema.ts | ||
npx openapi-generator-cli generate -g asciidoc -i ./src/openapi/openapi.yaml -o ./src/auto-generated/docs/index.adoc | ||
npx yaml2json -f ./src/openapi/openapi.yaml | ||
OPENAPI_SPEC_FILE="./src/openapi/openapi.yaml" | ||
SCHEMA_FILE="./src/auto-generated/api/models/schema.ts" | ||
DEFINITION_FILE="./src/auto-generated/api/definition.ts" | ||
METADATA_FILE="./src/auto-generated/.METADATA.sha1" | ||
|
||
export FILE=./src/auto-generated/api/definition.ts | ||
echo '// GENERATED FILE. DO NOT EDIT.' > ${FILE} | ||
echo 'const OPENAPI = `' >> ${FILE} | ||
cat ./src/openapi/openapi.json >> ${FILE} | ||
echo '`' >> ${FILE} | ||
echo "export const openApiDocument = JSON.parse(OPENAPI);" >> ${FILE} | ||
openapi_generate() { | ||
npx openapi-typescript ${OPENAPI_SPEC_FILE} -o ${SCHEMA_FILE} | ||
npx openapi-generator-cli generate -g asciidoc -i ./src/openapi/openapi.yaml -o ./src/auto-generated/docs/index.adoc | ||
npx yaml2json -f ${OPENAPI_SPEC_FILE} | ||
|
||
rm ./src/openapi/openapi.json | ||
yarn openapi:prettier:fix | ||
OPENAPI_SPEC_FILE_JSON=$(tr -d '[:space:]' < "$(dirname $OPENAPI_SPEC_FILE)"/openapi.json) | ||
cat << EOF > ${DEFINITION_FILE} | ||
/* eslint-disable */ | ||
/* prettier-ignore */ | ||
// GENERATED FILE DO NOT EDIT. | ||
const OPENAPI = \`${OPENAPI_SPEC_FILE_JSON}\`; | ||
export const openApiDocument = JSON.parse(OPENAPI); | ||
EOF | ||
|
||
rm ./src/openapi/openapi.json | ||
yarn openapi:prettier:fix | ||
NEW_SHA=$(openapi_checksum) | ||
openapi_update ${NEW_SHA} | ||
} | ||
|
||
openapi_checksum(){ | ||
cat ${DEFINITION_FILE} ${SCHEMA_FILE} ${OPENAPI_SPEC_FILE} | sha1sum | awk '{print $1}' | tr -d '[:space:]' | ||
} | ||
|
||
openapi_update() { | ||
echo "${1}" > "${METADATA_FILE}" | ||
} | ||
|
||
# Function to check if OpenAPI files are up-to-date | ||
openapi_check() { | ||
|
||
if [ ! -f "${METADATA_FILE}" ]; then | ||
echo "Error: Metadata file '${METADATA_FILE}' not found. Run 'yarn openapi:generate' first." | ||
exit 1 | ||
else | ||
STORED_SHA1=$(cat "${METADATA_FILE}") | ||
fi | ||
|
||
# Generate new SHA-1 checksum | ||
NEW_SHA1=$(openapi_checksum) | ||
|
||
# Check if the stored and current SHA-1 checksums differ | ||
if [ "${STORED_SHA1}" != "${NEW_SHA1}" ]; then | ||
echo "Changes detected in auto-generated files or openapi.yaml. Please run 'yarn openapi:generate' to update." | ||
exit 1 | ||
else | ||
echo "No changes detected in auto-generated files or openapi.yaml. Auto-generated files are up to date." | ||
fi | ||
} | ||
|
||
# Check the command passed as an argument | ||
case "$1" in | ||
"generate") | ||
openapi_generate | ||
;; | ||
"check") | ||
openapi_check | ||
;; | ||
*) | ||
echo "Error: Invalid command. Please use \"${0} generate\" to generate OpenAPI files or \"${0} check\" to verify their status." | ||
exit 1 | ||
;; | ||
esac |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
d82937d6ce2c454759c0aad81c0d8206daa8e425 |
Oops, something went wrong.