From 3b14d9c9c594ad5b2896433deeead72693a3ef73 Mon Sep 17 00:00:00 2001 From: Gloria Ciavarrini Date: Mon, 27 May 2024 12:59:00 +0200 Subject: [PATCH] chore(orchestrator): add openapi client generation support (#1649) * Generate Typescript OpenAPI client Signed-off-by: Gloria Ciavarrini * Add openapi check dependencies in turbo Signed-off-by: Gloria Ciavarrini * Lint ingore generated client Signed-off-by: Gloria Ciavarrini * Ignore generated client files Signed-off-by: Gloria Ciavarrini --------- Signed-off-by: Gloria Ciavarrini --- plugins/orchestrator-common/.eslintrc.js | 4 +++- plugins/orchestrator-common/.gitignore | 1 + plugins/orchestrator-common/package.json | 7 ++++--- .../orchestrator-common/scripts/openapi.sh | 21 +++++++++++++------ .../.METADATA.sha1 | 0 .../api/definition.ts | 0 .../api/models/schema.ts | 0 .../docs/index.adoc/.openapi-generator-ignore | 0 .../docs/index.adoc/.openapi-generator/FILES | 0 .../index.adoc/.openapi-generator/VERSION | 0 .../docs/index.adoc/index.adoc | 0 plugins/orchestrator-common/src/index.ts | 2 +- .../orchestrator-common/src/openapi/types.ts | 2 +- plugins/orchestrator-common/turbo.json | 17 ++++++++++++++- 14 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 plugins/orchestrator-common/.gitignore rename plugins/orchestrator-common/src/{auto-generated => generated}/.METADATA.sha1 (100%) rename plugins/orchestrator-common/src/{auto-generated => generated}/api/definition.ts (100%) rename plugins/orchestrator-common/src/{auto-generated => generated}/api/models/schema.ts (100%) rename plugins/orchestrator-common/src/{auto-generated => generated}/docs/index.adoc/.openapi-generator-ignore (100%) rename plugins/orchestrator-common/src/{auto-generated => generated}/docs/index.adoc/.openapi-generator/FILES (100%) rename plugins/orchestrator-common/src/{auto-generated => generated}/docs/index.adoc/.openapi-generator/VERSION (100%) rename plugins/orchestrator-common/src/{auto-generated => generated}/docs/index.adoc/index.adoc (100%) diff --git a/plugins/orchestrator-common/.eslintrc.js b/plugins/orchestrator-common/.eslintrc.js index e2a53a6ad2..11ceb0612f 100644 --- a/plugins/orchestrator-common/.eslintrc.js +++ b/plugins/orchestrator-common/.eslintrc.js @@ -1 +1,3 @@ -module.exports = require('@backstage/cli/config/eslint-factory')(__dirname); +module.exports = require('@backstage/cli/config/eslint-factory')(__dirname, { + ignorePatterns: ['src/generated/client/**'], +}); diff --git a/plugins/orchestrator-common/.gitignore b/plugins/orchestrator-common/.gitignore new file mode 100644 index 0000000000..3dae30973a --- /dev/null +++ b/plugins/orchestrator-common/.gitignore @@ -0,0 +1 @@ +src/generated/client diff --git a/plugins/orchestrator-common/package.json b/plugins/orchestrator-common/package.json index f691ff0bec..b388b75a7d 100644 --- a/plugins/orchestrator-common/package.json +++ b/plugins/orchestrator-common/package.json @@ -28,7 +28,7 @@ ], "configSchema": "config.d.ts", "scripts": { - "build": "yarn openapi:check && backstage-cli package build", + "build": "backstage-cli package build", "tsc": "tsc", "lint": "backstage-cli package lint", "test": "backstage-cli package test --passWithNoTests --coverage", @@ -37,7 +37,7 @@ "postpack": "backstage-cli package postpack", "openapi:generate": "./scripts/openapi.sh generate", "openapi:check": "./scripts/openapi.sh check", - "openapi:prettier:fix": "prettier --ignore-unknown --write ./src/auto-generated" + "openapi:prettier:fix": "prettier --ignore-unknown --write ./src/generated/api" }, "dependencies": { "@backstage/types": "^1.1.1", @@ -47,6 +47,7 @@ "json-schema": "^0.4.0" }, "devDependencies": { - "@backstage/cli": "0.26.4" + "@backstage/cli": "0.26.4", + "@hey-api/openapi-ts": "0.34.5" } } diff --git a/plugins/orchestrator-common/scripts/openapi.sh b/plugins/orchestrator-common/scripts/openapi.sh index 3aa9c09d3b..205ce78b94 100755 --- a/plugins/orchestrator-common/scripts/openapi.sh +++ b/plugins/orchestrator-common/scripts/openapi.sh @@ -2,14 +2,23 @@ pwd set -e +GENERATED_FOLDER="./src/generated" 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" +API_FOLDER="${GENERATED_FOLDER}/api" +SCHEMA_FILE="${API_FOLDER}/models/schema.ts" +DEFINITION_FILE="${API_FOLDER}/definition.ts" +METADATA_FILE="${GENERATED_FOLDER}/.METADATA.sha1" +CLIENT_FOLDER="${GENERATED_FOLDER}/client" openapi_generate() { + # TypeScript Client generation + openapi-ts --input ${OPENAPI_SPEC_FILE} --output ${CLIENT_FOLDER} + + ## Schema generation npx --yes openapi-typescript@6.7.5 ${OPENAPI_SPEC_FILE} -o ${SCHEMA_FILE} - npx --yes @openapitools/openapi-generator-cli@v2.13.1 generate -g asciidoc -i ./src/openapi/openapi.yaml -o ./src/auto-generated/docs/index.adoc + # Docs generation + npx --yes @openapitools/openapi-generator-cli@v2.13.1 generate -g asciidoc -i ./src/openapi/openapi.yaml -o ./src/generated/docs/index.adoc + npx --yes --package=js-yaml-cli@0.6.0 -- yaml2json -f ${OPENAPI_SPEC_FILE} OPENAPI_SPEC_FILE_JSON=$(tr -d '[:space:]' < "$(dirname $OPENAPI_SPEC_FILE)"/openapi.json) @@ -51,10 +60,10 @@ openapi_check() { # 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." + echo "Changes detected in 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." + echo "No changes detected in generated files or openapi.yaml. generated files are up to date." fi } diff --git a/plugins/orchestrator-common/src/auto-generated/.METADATA.sha1 b/plugins/orchestrator-common/src/generated/.METADATA.sha1 similarity index 100% rename from plugins/orchestrator-common/src/auto-generated/.METADATA.sha1 rename to plugins/orchestrator-common/src/generated/.METADATA.sha1 diff --git a/plugins/orchestrator-common/src/auto-generated/api/definition.ts b/plugins/orchestrator-common/src/generated/api/definition.ts similarity index 100% rename from plugins/orchestrator-common/src/auto-generated/api/definition.ts rename to plugins/orchestrator-common/src/generated/api/definition.ts diff --git a/plugins/orchestrator-common/src/auto-generated/api/models/schema.ts b/plugins/orchestrator-common/src/generated/api/models/schema.ts similarity index 100% rename from plugins/orchestrator-common/src/auto-generated/api/models/schema.ts rename to plugins/orchestrator-common/src/generated/api/models/schema.ts diff --git a/plugins/orchestrator-common/src/auto-generated/docs/index.adoc/.openapi-generator-ignore b/plugins/orchestrator-common/src/generated/docs/index.adoc/.openapi-generator-ignore similarity index 100% rename from plugins/orchestrator-common/src/auto-generated/docs/index.adoc/.openapi-generator-ignore rename to plugins/orchestrator-common/src/generated/docs/index.adoc/.openapi-generator-ignore diff --git a/plugins/orchestrator-common/src/auto-generated/docs/index.adoc/.openapi-generator/FILES b/plugins/orchestrator-common/src/generated/docs/index.adoc/.openapi-generator/FILES similarity index 100% rename from plugins/orchestrator-common/src/auto-generated/docs/index.adoc/.openapi-generator/FILES rename to plugins/orchestrator-common/src/generated/docs/index.adoc/.openapi-generator/FILES diff --git a/plugins/orchestrator-common/src/auto-generated/docs/index.adoc/.openapi-generator/VERSION b/plugins/orchestrator-common/src/generated/docs/index.adoc/.openapi-generator/VERSION similarity index 100% rename from plugins/orchestrator-common/src/auto-generated/docs/index.adoc/.openapi-generator/VERSION rename to plugins/orchestrator-common/src/generated/docs/index.adoc/.openapi-generator/VERSION diff --git a/plugins/orchestrator-common/src/auto-generated/docs/index.adoc/index.adoc b/plugins/orchestrator-common/src/generated/docs/index.adoc/index.adoc similarity index 100% rename from plugins/orchestrator-common/src/auto-generated/docs/index.adoc/index.adoc rename to plugins/orchestrator-common/src/generated/docs/index.adoc/index.adoc diff --git a/plugins/orchestrator-common/src/index.ts b/plugins/orchestrator-common/src/index.ts index f3019876eb..eea896b685 100644 --- a/plugins/orchestrator-common/src/index.ts +++ b/plugins/orchestrator-common/src/index.ts @@ -1,6 +1,6 @@ export * from './types'; export * from './openapi/types'; -export * from './auto-generated/api/definition'; +export * from './generated/api/definition'; export * from './constants'; export * from './models'; export * from './workflow'; diff --git a/plugins/orchestrator-common/src/openapi/types.ts b/plugins/orchestrator-common/src/openapi/types.ts index eb6038094a..743ec47db8 100644 --- a/plugins/orchestrator-common/src/openapi/types.ts +++ b/plugins/orchestrator-common/src/openapi/types.ts @@ -1,4 +1,4 @@ -import { components } from '../auto-generated/api/models/schema'; +import { components } from '../generated/api/models/schema'; export type ErrorResponse = components['schemas']['ErrorResponse']; export type WorkflowOverviewListResultDTO = diff --git a/plugins/orchestrator-common/turbo.json b/plugins/orchestrator-common/turbo.json index 79100511bd..1c968f4d74 100644 --- a/plugins/orchestrator-common/turbo.json +++ b/plugins/orchestrator-common/turbo.json @@ -1,9 +1,24 @@ { "extends": ["//"], "pipeline": { + "start": { + "dependsOn": ["openapi:check"] + }, "tsc": { "outputs": ["../../dist-types/plugins/orchestrator-common/**"], - "dependsOn": ["^tsc"] + "dependsOn": ["^tsc", "openapi:check"] + }, + "openapi:check": { + "outputs": ["src/generated/**"] + }, + "openapi:generate": { + "outputs": ["src/generated/**"] + }, + "test": { + "dependsOn": ["openapi:check"] + }, + "export-dynamic": { + "dependsOn": ["openapi:check"] } } }