forked from blockscout/blockscout
-
Notifications
You must be signed in to change notification settings - Fork 0
290 lines (257 loc) · 9.21 KB
/
generate-swagger.yml
File metadata and controls
290 lines (257 loc) · 9.21 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
name: Generate OpenAPI Specs
on:
push:
branches:
- master
- np-integrate-open-api-spex
paths-ignore:
- "CHANGELOG.md"
- "**/README.md"
- "docker/*"
- "docker-compose/*"
workflow_dispatch:
release:
types: [published]
env:
OTP_VERSION: '27.3.4.6'
ELIXIR_VERSION: '1.19.4'
RELEASE_VERSION: 10.0.6
jobs:
matrix-builder:
name: Build matrix
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- id: set-matrix
run: |
echo "matrix=$(node -e '
// Add/remove CI matrix chain types here
const chainTypes = [
"default",
"arbitrum",
"blackfort",
"ethereum",
"filecoin",
"optimism",
"optimism-celo",
"polygon_zkevm",
"rsk",
"scroll",
"shibarium",
"stability",
"zetachain",
"zilliqa",
"zksync",
"neon"
];
const matrix = { "chain-type": ${{ 'chainTypes' }} };
console.log(JSON.stringify(matrix));
')" >> $GITHUB_OUTPUT
build-and-cache:
name: Build and Cache deps
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ env.OTP_VERSION }}
elixir-version: ${{ env.ELIXIR_VERSION }}
hexpm-mirrors: |
https://builds.hex.pm
https://cdn.jsdelivr.net/hex
- name: "ELIXIR_VERSION.lock"
run: echo "${ELIXIR_VERSION}" > ELIXIR_VERSION.lock
- name: "OTP_VERSION.lock"
run: echo "${OTP_VERSION}" > OTP_VERSION.lock
- name: Restore Mix Deps Cache
uses: actions/cache@v4
id: deps-cache
with:
path: |
deps
_build
key: ${{ runner.os }}-${{ env.ELIXIR_VERSION }}-${{ env.OTP_VERSION }}-${{ env.MIX_ENV }}-deps-mixlockhash-${{ hashFiles('mix.lock') }}
restore-keys: |
${{ runner.os }}-${{ env.ELIXIR_VERSION }}-${{ env.OTP_VERSION }}-${{ env.MIX_ENV }}-deps-mixlockhash-
- name: Conditionally build Mix deps cache
if: steps.deps-cache.outputs.cache-hit != 'true'
run: |
mix local.hex --force
mix local.rebar --force
mix deps.get
mix deps.compile --skip-umbrella-children
generate-swagger:
strategy:
fail-fast: false
matrix: ${{ fromJson(needs.matrix-builder.outputs.matrix) }}
name: Generate Open API spec
runs-on: ubuntu-latest
needs:
- build-and-cache
- matrix-builder
steps:
- uses: actions/checkout@v5
- uses: erlef/setup-beam@v1
with:
otp-version: ${{ env.OTP_VERSION }}
elixir-version: ${{ env.ELIXIR_VERSION }}
hexpm-mirrors: |
https://builds.hex.pm
https://cdn.jsdelivr.net/hex
- name: Mix Deps Cache
uses: actions/cache/restore@v4
id: deps-cache
with:
path: |
deps
_build
key: ${{ runner.os }}-${{ env.ELIXIR_VERSION }}-${{ env.OTP_VERSION }}-${{ env.MIX_ENV }}-deps-mixlockhash-${{ hashFiles('mix.lock') }}
restore-keys: |
${{ runner.os }}-${{ env.ELIXIR_VERSION }}-${{ env.OTP_VERSION }}-${{ env.MIX_ENV }}-deps-mixlockhash-
- name: mix openapi.spec.yaml
run: |
mix openapi.spec.yaml --spec BlockScoutWeb.Specs.Public openapi.${{ matrix.chain-type }}.yaml --start-app=false
env:
CHAIN_TYPE: ${{ matrix.chain-type != 'default' && matrix.chain-type || '' }}
MUD_INDEXER_ENABLED: false
- name: Generate MUD-enabled spec for Optimism
if: matrix.chain-type == 'optimism'
run: |
mix openapi.spec.yaml --spec BlockScoutWeb.Specs.Public openapi.mud.yaml --start-app=false
env:
CHAIN_TYPE: optimism
MUD_INDEXER_ENABLED: true
- name: Upload OpenAPI spec
uses: actions/upload-artifact@v4
with:
name: openapi-spec-${{ matrix.chain-type }}
path: openapi.${{ matrix.chain-type }}.yaml
retention-days: 1
- name: Upload MUD-enabled spec
if: matrix.chain-type == 'optimism'
uses: actions/upload-artifact@v4
with:
name: openapi-spec-mud
path: openapi.mud.yaml
retention-days: 1
push-specs:
needs:
- generate-swagger
- matrix-builder
runs-on: ubuntu-latest
name: Push all OpenAPI specs
steps:
- name: Validate required secrets
run: |
if [ -z "${{ secrets.API_SPECS_PAT }}" ]; then
echo "Error: API_SPECS_PAT secret is not set"
exit 1
fi
- name: Checkout specs repository
uses: actions/checkout@v5
with:
repository: ${{ vars.API_SPECS_REPOSITORY }}
token: ${{ secrets.API_SPECS_PAT }}
path: api-specs
- name: Download all swagger specs
uses: actions/download-artifact@v5
with:
pattern: openapi-spec-*
merge-multiple: true
path: temp-specs
- name: Merge all OpenAPI specs into all-in-one spec
run: |
npm install js-yaml
cat > merge-specs.js << 'SCRIPT_EOF'
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');
const specDir = './temp-specs';
const outputFile = path.join(specDir, 'openapi.all.yaml');
const files = fs.readdirSync(specDir)
.filter(f => f.endsWith('.yaml'))
.sort();
let merged = null;
for (const file of files) {
const content = yaml.load(fs.readFileSync(path.join(specDir, file), 'utf8'));
if (!merged) {
merged = JSON.parse(JSON.stringify(content));
continue;
}
// Union merge paths (first definition wins for duplicates)
if (content.paths) {
merged.paths = merged.paths || {};
for (const [p, def] of Object.entries(content.paths)) {
if (!merged.paths[p]) {
merged.paths[p] = def;
}
}
}
// Union merge components (first definition wins for duplicates)
if (content.components) {
merged.components = merged.components || {};
for (const [section, defs] of Object.entries(content.components)) {
if (!merged.components[section]) {
merged.components[section] = defs;
} else {
for (const [name, def] of Object.entries(defs)) {
if (!merged.components[section][name]) {
merged.components[section][name] = def;
}
}
}
}
}
// Union merge tags (by name)
if (content.tags) {
merged.tags = merged.tags || [];
const existingTagNames = new Set(merged.tags.map(t => t.name));
for (const tag of content.tags) {
if (!existingTagNames.has(tag.name)) {
merged.tags.push(tag);
existingTagNames.add(tag.name);
}
}
}
}
fs.writeFileSync(outputFile, yaml.dump(merged, { lineWidth: -1 }));
console.log(`Merged ${files.length} specs into ${outputFile}`);
SCRIPT_EOF
node merge-specs.js
- name: Create specs directory structure
run: |
VERSION=${{ github.event_name == 'release' && env.RELEASE_VERSION || 'master' }}
for SPEC_FILE in temp-specs/*; do
if [ -f "$SPEC_FILE" ]; then
FILENAME=$(basename "$SPEC_FILE")
# Handle MUD spec specially
if [ "$FILENAME" = "openapi.mud.yaml" ]; then
mkdir -p "api-specs/blockscout/${VERSION}/mud"
cp "$SPEC_FILE" "api-specs/blockscout/${VERSION}/mud/swagger.yaml"
else
# Extract chain type from filename (openapi.CHAINTYPE.yaml)
CHAIN_TYPE=$(echo "$FILENAME" | sed 's/openapi\.\(.*\)\.yaml/\1/')
mkdir -p "api-specs/blockscout/${VERSION}/${CHAIN_TYPE}"
cp "$SPEC_FILE" "api-specs/blockscout/${VERSION}/${CHAIN_TYPE}/swagger.yaml"
fi
fi
done
- name: Commit and push changes
working-directory: api-specs
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add .
# Only commit if there are changes
if git diff --staged --quiet; then
echo "No changes to commit"
else
git commit -m "[SKIP-GH-PAGES] create OpenAPI specs for Blockscout ${{ github.event_name == 'release' && env.RELEASE_VERSION || github.sha }}"
git push
fi
- name: Clean up
if: always()
run: |
rm -rf temp-specs
rm -rf api-specs