-
Notifications
You must be signed in to change notification settings - Fork 0
194 lines (164 loc) · 7.47 KB
/
update-api-docs.yml
File metadata and controls
194 lines (164 loc) · 7.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# =============================================================================
# Workflow: Actualizar documentación API (OpenAPI + Postman Collection)
# =============================================================================
# Este workflow se ejecuta después de que build-and-push complete exitosamente
# y actualiza los archivos de documentación en el repositorio:
# - openapi.json (spec OpenAPI 3.0 desde la API desplegada)
# - Invernaderos_API_Collection.postman_collection.json (generada desde OpenAPI)
#
# Documentación oficial:
# - openapi-to-postmanv2: https://github.com/postmanlabs/openapi-to-postman
# - SpringDoc OpenAPI: https://springdoc.org/
# =============================================================================
name: Update API Documentation
on:
# Se ejecuta cuando build-and-push completa exitosamente
workflow_run:
workflows: ["Build and Push Docker Image"]
types:
- completed
branches:
- main
- develop
# Permite ejecución manual
workflow_dispatch:
inputs:
environment:
description: 'Entorno desde el cual obtener la spec'
required: true
default: 'dev'
type: choice
options:
- dev
- prod
env:
# URLs de las APIs desplegadas (Traefik IngressRoute)
API_URL_DEV: https://inverapi-dev.apptolast.com
API_URL_PROD: https://inverapi-prod.apptolast.com
jobs:
update-docs:
name: Update OpenAPI & Postman Collection
runs-on: ubuntu-latest
# Solo ejecutar si el workflow anterior fue exitoso
if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# Necesario para poder hacer push
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.workflow_run.head_branch || github.ref }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Install openapi-to-postmanv2
run: npm install -g openapi-to-postmanv2
- name: Determine API URL
id: api-url
run: |
# Determinar qué entorno usar basado en la rama o input manual
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
ENV="${{ github.event.inputs.environment }}"
elif [ "${{ github.event.workflow_run.head_branch }}" = "main" ]; then
ENV="prod"
else
ENV="dev"
fi
if [ "$ENV" = "prod" ]; then
echo "url=${{ env.API_URL_PROD }}" >> $GITHUB_OUTPUT
else
echo "url=${{ env.API_URL_DEV }}" >> $GITHUB_OUTPUT
fi
echo "env=$ENV" >> $GITHUB_OUTPUT
echo "📍 Usando entorno: $ENV"
- name: Wait for API deployment
run: |
echo "⏳ Esperando 60 segundos para que el deployment complete..."
sleep 60
- name: Fetch OpenAPI spec
id: fetch-spec
run: |
API_URL="${{ steps.api-url.outputs.url }}"
echo "📥 Obteniendo OpenAPI spec desde: $API_URL/v3/api-docs"
# Intentar obtener el spec (máx 3 intentos)
for i in 1 2 3; do
HTTP_CODE=$(curl -s -w "%{http_code}" -o openapi.json "$API_URL/v3/api-docs")
if [ "$HTTP_CODE" = "200" ]; then
echo "✅ OpenAPI spec obtenido exitosamente"
# Formatear JSON para mejor legibilidad
cat openapi.json | jq '.' > openapi_formatted.json
mv openapi_formatted.json openapi.json
break
fi
echo "⚠️ Intento $i fallido (HTTP $HTTP_CODE), esperando 30 segundos..."
sleep 30
done
# Verificar que el archivo existe y tiene contenido
if [ ! -s openapi.json ]; then
echo "❌ Error: No se pudo obtener el OpenAPI spec"
exit 1
fi
echo "📊 Tamaño del spec: $(wc -c < openapi.json) bytes"
- name: Generate Postman Collection
run: |
echo "🔄 Generando Postman Collection desde OpenAPI spec..."
# Generar nueva colección con formato bonito y organización por tags
openapi2postmanv2 \
-s openapi.json \
-o Invernaderos_API_Collection_NEW.postman_collection.json \
-p \
-O folderStrategy=Tags,includeAuthInfoInExample=true
# Verificar que se generó correctamente
if [ ! -s Invernaderos_API_Collection_NEW.postman_collection.json ]; then
echo "❌ Error: No se pudo generar la colección Postman"
exit 1
fi
echo "✅ Colección Postman generada exitosamente"
echo "📊 Tamaño: $(wc -c < Invernaderos_API_Collection_NEW.postman_collection.json) bytes"
- name: Update collection metadata
run: |
# Actualizar metadatos de la colección (nombre, descripción, fecha)
BRANCH="${{ github.event.workflow_run.head_branch || github.ref_name }}"
DATE=$(date -u +"%Y-%m-%d %H:%M UTC")
ENV="${{ steps.api-url.outputs.env }}"
jq --arg date "$DATE" --arg branch "$BRANCH" --arg env "$ENV" '
.info.name = "Invernaderos API - Auto-generated" |
.info.description = "Colección generada automáticamente desde OpenAPI spec.\n\nFecha: \($date)\nRama: \($branch)\nEntorno: \($env)\n\nGenerado por GitHub Actions workflow."
' Invernaderos_API_Collection_NEW.postman_collection.json > temp.json
mv temp.json Invernaderos_API_Collection.postman_collection.json
rm -f Invernaderos_API_Collection_NEW.postman_collection.json
- name: Check for changes
id: changes
run: |
# Verificar si hay cambios en los archivos
git diff --quiet openapi.json Invernaderos_API_Collection.postman_collection.json || echo "changed=true" >> $GITHUB_OUTPUT
- name: Commit and push changes
if: steps.changes.outputs.changed == 'true'
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add openapi.json Invernaderos_API_Collection.postman_collection.json
BRANCH="${{ github.event.workflow_run.head_branch || github.ref_name }}"
ENV="${{ steps.api-url.outputs.env }}"
git commit -m "docs: auto-update OpenAPI spec and Postman collection
- Updated from $ENV environment API
- Branch: $BRANCH
- Generated by GitHub Actions
Co-Authored-By: github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
git push
- name: Summary
run: |
echo "## 📚 API Documentation Update" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Item | Status |" >> $GITHUB_STEP_SUMMARY
echo "|------|--------|" >> $GITHUB_STEP_SUMMARY
echo "| Environment | ${{ steps.api-url.outputs.env }} |" >> $GITHUB_STEP_SUMMARY
echo "| API URL | ${{ steps.api-url.outputs.url }} |" >> $GITHUB_STEP_SUMMARY
echo "| OpenAPI Spec | ✅ Updated |" >> $GITHUB_STEP_SUMMARY
echo "| Postman Collection | ✅ Updated |" >> $GITHUB_STEP_SUMMARY
if [ "${{ steps.changes.outputs.changed }}" = "true" ]; then
echo "| Commit | ✅ Pushed |" >> $GITHUB_STEP_SUMMARY
else
echo "| Commit | ⏭️ No changes |" >> $GITHUB_STEP_SUMMARY
fi