From 23a1efe1fbfa3aa264baa818a26bbcb5f5d6f585 Mon Sep 17 00:00:00 2001 From: a20688392 Date: Sun, 25 Jun 2023 18:28:55 +0800 Subject: [PATCH 1/6] ci: generate swagger file --- package.json | 1 + src/swagger/generate-swagger-file.ts | 23 +++++++++++++++++++++++ src/swagger/swagger.module.ts | 14 ++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/swagger/generate-swagger-file.ts create mode 100644 src/swagger/swagger.module.ts diff --git a/package.json b/package.json index b4053a8..a48b886 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "scripts": { "prebuild": "rimraf dist", "build": "nest build", + "generate-swagger": "node ./dist/swagger/generate-swagger-file.js", "format": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix && prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "nest start", "start:dev": "nest start --watch", diff --git a/src/swagger/generate-swagger-file.ts b/src/swagger/generate-swagger-file.ts new file mode 100644 index 0000000..93f3ce6 --- /dev/null +++ b/src/swagger/generate-swagger-file.ts @@ -0,0 +1,23 @@ +import { NestFactory } from "@nestjs/core"; +import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger"; +import * as fs from "fs"; + +import { SwaggerGenerateModule } from "./swagger.module"; + +async function generateSwaggerJson() { + const app = await NestFactory.create(SwaggerGenerateModule); + + const config = new DocumentBuilder() + .setTitle(process.env.APP_SWAGGER_Title ?? "Cophr") + .setDescription(process.env.APP_SWAGGER_Description ?? "") + .setVersion(process.env.APP_SWAGGER_Version ?? "N/A") + .build(); + + const document = SwaggerModule.createDocument(app, config); + + fs.writeFileSync("swagger-docs.json", JSON.stringify(document)); + + await app.close(); +} + +void generateSwaggerJson(); diff --git a/src/swagger/swagger.module.ts b/src/swagger/swagger.module.ts new file mode 100644 index 0000000..9ce9d20 --- /dev/null +++ b/src/swagger/swagger.module.ts @@ -0,0 +1,14 @@ +import { Module } from "@nestjs/common"; +import { TypeOrmModule } from "@nestjs/typeorm"; +import { AppController } from "src/app.controller"; +import { AppService } from "src/app.service"; +import { AuthModule } from "src/auth/auth.module"; +import { dataSourceJest } from "src/config/data-source"; +import { UserModule } from "src/user/user.module"; + +@Module({ + controllers: [AppController], + imports: [TypeOrmModule.forRoot(dataSourceJest), UserModule, AuthModule], + providers: [AppService], +}) +export class SwaggerGenerateModule {} From 8eddff892c3553ee0a96194bab275cc8e006f5a5 Mon Sep 17 00:00:00 2001 From: a20688392 Date: Sun, 25 Jun 2023 18:30:07 +0800 Subject: [PATCH 2/6] ci: upload file on Swaggerhub --- .github/workflows/ci.yml | 54 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0691fb4..464fcb8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,3 +67,57 @@ jobs: - name: Unit Test run: yarn test + + generate-swagger: + runs-on: ubuntu-latest + env: + SWAGGERHUB_API_KEY: ${{ secrets.SWAGGERHUB_API_KEY }} + steps: + - name: Checkout Code + uses: actions/checkout@v3 + + - name: Get yarn cache directory path + id: yarn-cache-dir-path + run: echo "dir=$(yarn config get cacheFolder)" >> $GITHUB_OUTPUT + + - uses: actions/cache@v3 + id: yarn-cache + with: + path: ${{ steps.yarn-cache-dir-path.outputs.dir }} + key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} + restore-keys: | + ${{ runner.os }}-yarn- + + - name: Setup node and cache dependencies + uses: actions/setup-node@v3 + with: + node-version: 16 + cache: "yarn" + + - name: Install Dependencies + run: yarn install + + - name: Nest Build + run: yarn build + + - name: Install swaggerhub-cli + run: yarn global add swaggerhub-cli + + - name: Read package version + uses: tyankatsu0105/read-package-version-actions@v1 + id: package-version + + - name: Generate Swagger JSON + run: yarn generate-swagger + env: + APP_SWAGGER_Version: ${{ steps.package-version.outputs.version }} + + - name: Upload on Swaggerhub - Cosphr_develop + if: github.ref != 'refs/heads/main' + run: | + swaggerhub api:update "a20688392/Cosphr_test/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public + + - name: Upload on Swaggerhub - Cosphr_main + if: github.ref == 'refs/heads/main' + run: | + swaggerhub api:create "a20688392/Cosphr/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public From e7c99c3c7f866bbb6fd3df1815d316007fac8ea2 Mon Sep 17 00:00:00 2001 From: a20688392 Date: Sat, 1 Jul 2023 19:21:34 +0800 Subject: [PATCH 3/6] fix: organization name correction --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 464fcb8..3a7e803 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -112,12 +112,12 @@ jobs: env: APP_SWAGGER_Version: ${{ steps.package-version.outputs.version }} - - name: Upload on Swaggerhub - Cosphr_develop + - name: Upload on Swaggerhub - Cophr_develop if: github.ref != 'refs/heads/main' run: | - swaggerhub api:update "a20688392/Cosphr_test/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public + swaggerhub api:update "Cophr/test/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public - - name: Upload on Swaggerhub - Cosphr_main + - name: Upload on Swaggerhub - Cophr_main if: github.ref == 'refs/heads/main' run: | - swaggerhub api:create "a20688392/Cosphr/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public + swaggerhub api:create "Cophr/main/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public From 264c533aa3e1dd913b184803ec32a21da0b1e7c2 Mon Sep 17 00:00:00 2001 From: a20688392 Date: Sun, 2 Jul 2023 04:30:52 +0800 Subject: [PATCH 4/6] refactor: revise description --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3a7e803..6d6a0f3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -112,7 +112,7 @@ jobs: env: APP_SWAGGER_Version: ${{ steps.package-version.outputs.version }} - - name: Upload on Swaggerhub - Cophr_develop + - name: Upload on Swaggerhub - Cophr_test if: github.ref != 'refs/heads/main' run: | swaggerhub api:update "Cophr/test/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public From aa357cea5759e832e4863f47b6d57075f0fb0c0f Mon Sep 17 00:00:00 2001 From: a20688392 Date: Mon, 3 Jul 2023 18:01:35 +0800 Subject: [PATCH 5/6] refactor: swagger version by commit hash --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6d6a0f3..f416495 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -115,7 +115,7 @@ jobs: - name: Upload on Swaggerhub - Cophr_test if: github.ref != 'refs/heads/main' run: | - swaggerhub api:update "Cophr/test/${{ env.APP_SWAGGER_Version }}" --setdefault --file=swagger-docs.json --visibility=public + swaggerhub api:update "Cophr/test/${GITHUB_SHA::8}" --setdefault --file=swagger-docs.json --visibility=public - name: Upload on Swaggerhub - Cophr_main if: github.ref == 'refs/heads/main' From 0a56db0da82fed8b992289a344be5691a802b59b Mon Sep 17 00:00:00 2001 From: a20688392 Date: Mon, 3 Jul 2023 18:06:57 +0800 Subject: [PATCH 6/6] fix: use SwaggerHub API create instead of update why need? In order to achieve automatic, swagger docs change will follow the commit hash. create will generate new swagger docs. update will update specified version information. --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f416495..70db455 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -115,7 +115,7 @@ jobs: - name: Upload on Swaggerhub - Cophr_test if: github.ref != 'refs/heads/main' run: | - swaggerhub api:update "Cophr/test/${GITHUB_SHA::8}" --setdefault --file=swagger-docs.json --visibility=public + swaggerhub api:create "Cophr/test/${GITHUB_SHA::8}" --setdefault --file=swagger-docs.json --visibility=public - name: Upload on Swaggerhub - Cophr_main if: github.ref == 'refs/heads/main'