Skip to content

Commit

Permalink
feat(orchestrator): verify if auto-generated openapi files are up-to-…
Browse files Browse the repository at this point in the history
…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
gciavarrini authored Mar 11, 2024
1 parent f1f2ce1 commit 650b435
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 846 deletions.
5 changes: 3 additions & 2 deletions plugins/orchestrator-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
],
"configSchema": "config.d.ts",
"scripts": {
"build": "backstage-cli package build",
"build": "yarn openapi:check && backstage-cli package build",
"tsc": "tsc",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test --passWithNoTests --coverage",
"clean": "backstage-cli package clean",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack",
"openapi": "./scripts/openapi.sh",
"openapi:generate": "./scripts/openapi.sh generate",
"openapi:check": "./scripts/openapi.sh check",
"openapi:prettier:fix": "prettier --ignore-unknown --write ./src/auto-generated"
},
"dependencies": {
Expand Down
79 changes: 67 additions & 12 deletions plugins/orchestrator-common/scripts/openapi.sh
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d82937d6ce2c454759c0aad81c0d8206daa8e425
Loading

0 comments on commit 650b435

Please sign in to comment.