Skip to content

Commit

Permalink
feat: implement workflow to push api specs to swaggerhub (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
junjie-w authored Dec 24, 2024
1 parent 5d1a9ca commit 4bb5110
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"commit": false,
"fixed": [],
"linked": [
["@api-client-sdk-workflow-demo/products-api", "@api-client-sdk-workflow-demo/users-api"]
["@api-client-sdk-workflow-sample/products-api", "@api-client-sdk-workflow-sample/users-api"]
],
"access": "public",
"updateInternalDependencies": "patch",
"ignore": ["@acme/docs"]
"ignore": []
}
89 changes: 89 additions & 0 deletions .github/workflows/push-spec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Push API Spec to SwaggerHub

on:
workflow_dispatch:
inputs:
api-name:
type: choice
description: API Name
options:
- products-api
- users-api
required: true
api-version:
description: API Version
required: true
default: 1.0.0

env:
SWAGGERHUB_API_KEY: ${{ secrets.SWAGGERHUB_API_KEY }}
USERNAME: ${{ secrets.SWAGGERHUB_USERNAME }}

jobs:
push-spec:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install Dependencies
run: |
cd apps/${{ github.event.inputs.api-name }}
npm install
- name: Generate Spec
run: |
cd apps/${{ github.event.inputs.api-name }}
# Create spec generator
cat > generate-spec.ts << 'EOL'
import { NestFactory } from '@nestjs/core';
import { SwaggerModule } from '@nestjs/swagger';
import { writeFileSync } from 'fs';
import { AppModule } from './src/app.module';
import { swaggerConfig } from './src/config/swagger.config';
async function generateSpec() {
let app;
try {
app = await NestFactory.create(AppModule);
const document = SwaggerModule.createDocument(app, swaggerConfig);
writeFileSync('openapi-spec.json', JSON.stringify(document, null, 2));
console.log('✨ OpenAPI spec generated successfully');
} catch (error) {
console.error('Failed to generate OpenAPI spec:',
error instanceof Error ? error.message : error);
process.exit(1);
} finally {
if (app) await app.close();
}
}
generateSpec();
EOL
npx ts-node generate-spec.ts
- name: Push to SwaggerHub
run: |
cd apps/${{ github.event.inputs.api-name }}
RESPONSE=$(curl -s -w "\n%{http_code}" -X POST \
"https://api.swaggerhub.com/apis/${{ env.USERNAME }}/sample-${{ github.event.inputs.api-name }}/${{ github.event.inputs.api-version }}" \
-H "Authorization: ${SWAGGERHUB_API_KEY}" \
-H "Content-Type: application/json" \
-d @openapi-spec.json)
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
BODY=$(echo "$RESPONSE" | sed '$d')
if [ "$HTTP_CODE" -ne 200 ]; then
echo "Failed to push to SwaggerHub:"
echo "$BODY"
exit 1
fi
echo "✨ Successfully pushed ${{ github.event.inputs.api-name }} v${{ github.event.inputs.api-version }} to SwaggerHub"
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Release

on:
push:
branches:
- main
# push:
# branches:
# - main

concurrency: ${{ github.workflow }}-${{ github.ref }}

Expand Down
2 changes: 1 addition & 1 deletion apps/products-api/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@api-client-sdk-workflow-demo/products-api",
"name": "@api-client-sdk-workflow-sample/products-api",
"version": "1.0.0",
"description": "",
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion apps/users-api/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@api-client-sdk-workflow-demo/users-api",
"name": "@api-client-sdk-workflow-sample/users-api",
"version": "1.0.0",
"description": "",
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"engines": {
"node": ">=18"
},
"name": "with-changesets",
"name": "api-client-sdk-workflow-sample",
"packageManager": "[email protected]",
"workspaces": [
"apps/*",
Expand Down

0 comments on commit 4bb5110

Please sign in to comment.