Skip to content

Commit

Permalink
chore(orchestrator): add openapi client generation support (janus-idp…
Browse files Browse the repository at this point in the history
…#1649)

* Generate Typescript OpenAPI client

Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>

* Add openapi check dependencies in turbo

Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>

* Lint ingore generated client

Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>

* Ignore generated client files

Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>

---------

Signed-off-by: Gloria Ciavarrini <gciavarrini@redhat.com>
  • Loading branch information
gciavarrini committed May 27, 2024
1 parent 7e0a3dd commit 3b14d9c
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 13 deletions.
4 changes: 3 additions & 1 deletion plugins/orchestrator-common/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -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/**'],
});
1 change: 1 addition & 0 deletions plugins/orchestrator-common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/generated/client
7 changes: 4 additions & 3 deletions plugins/orchestrator-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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"
}
}
21 changes: 15 additions & 6 deletions plugins/orchestrator-common/scripts/openapi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/orchestrator-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion plugins/orchestrator-common/src/openapi/types.ts
Original file line number Diff line number Diff line change
@@ -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 =
Expand Down
17 changes: 16 additions & 1 deletion plugins/orchestrator-common/turbo.json
Original file line number Diff line number Diff line change
@@ -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"]
}
}
}

0 comments on commit 3b14d9c

Please sign in to comment.