Skip to content

Commit e2f1076

Browse files
DOP-5448: Integrate Bump GH action to generate spec pages (#530)
Co-authored-by: Andrea Angiolillo <[email protected]>
1 parent b402dad commit e2f1076

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import { fileURLToPath } from 'node:url';
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = path.dirname(__filename);
7+
8+
// Standalone specs can be added directly to the mapping. Any spec that requires displaying a version dropdown
9+
// will need to map its different versions to a separate Bump "branch". For example, a new resource version
10+
// for Atlas Admin API v2 will lead to a new entry in the array with its own Bump branch.
11+
const SPEC_MAPPING = [
12+
{
13+
doc: process.env.ATLAS_ADMIN_V1_DOC_ID,
14+
file: 'openapi/v1-deprecated/v1.json',
15+
branch: 'main',
16+
},
17+
];
18+
19+
function handleAdminAPIv2() {
20+
const docId = process.env.ATLAS_ADMIN_V2_DOC_ID;
21+
const directory = 'openapi/v2';
22+
const filePath = path.join(__dirname, `../../${directory}/versions.json`);
23+
const versions = JSON.parse(fs.readFileSync(filePath, 'utf8'));
24+
25+
if (!versions || !Array.isArray(versions)) {
26+
console.error(`No versions found for Atlas Admin API v2 at ${filePath}`);
27+
return;
28+
}
29+
30+
for (const [index, version] of versions.entries()) {
31+
const openapiFilename = `openapi-${version}.json`;
32+
const openapiFilePath = path.join(path.dirname(filePath), openapiFilename);
33+
34+
if (!fs.existsSync(openapiFilePath)) {
35+
console.error(`Could not find resource version "${version}" at ${openapiFilePath}`);
36+
continue;
37+
}
38+
39+
const file = `${directory}/${openapiFilename}`;
40+
SPEC_MAPPING.push({
41+
doc: docId,
42+
file,
43+
branch: version,
44+
});
45+
46+
// We want the latest version to have its own version AND be the latest/default branch
47+
if (index === versions.length - 1) {
48+
SPEC_MAPPING.push({
49+
doc: docId,
50+
file,
51+
branch: 'latest',
52+
});
53+
}
54+
}
55+
}
56+
57+
handleAdminAPIv2();
58+
// Output to GH action
59+
console.log(JSON.stringify(SPEC_MAPPING));
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Check & deploy API documentation
2+
3+
on:
4+
workflow_dispatch: # Allow manual trigger in case of quick fix
5+
6+
permissions:
7+
contents: read
8+
9+
jobs:
10+
create-matrix:
11+
runs-on: ubuntu-latest
12+
outputs:
13+
matrix: ${{ steps.set-matrix.outputs.matrix }}
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v4
17+
- name: Setup Node
18+
uses: actions/setup-node@v4
19+
- name: Generate matrix
20+
id: set-matrix
21+
env:
22+
ATLAS_ADMIN_V1_DOC_ID: ${{ vars.ATLAS_ADMIN_V1_DOC_ID }}
23+
ATLAS_ADMIN_V2_DOC_ID: ${{ vars.ATLAS_ADMIN_V2_DOC_ID }}
24+
run: |
25+
spec_mapping=$(node .github/scripts/generateSpecMapping.js)
26+
echo "matrix=$spec_mapping" >> "$GITHUB_OUTPUT"
27+
28+
deploy-doc:
29+
needs: create-matrix
30+
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
31+
name: Deploy API documentation on Bump.sh
32+
strategy:
33+
matrix:
34+
spec-mapping: ${{ fromJson(needs.create-matrix.outputs.matrix) }}
35+
runs-on: ubuntu-latest
36+
steps:
37+
- name: Checkout
38+
uses: actions/checkout@v4
39+
- name: Deploy API documentation
40+
uses: bump-sh/github-action@690c81156715f37cb72d006e5cbb81fbd9b45365
41+
with:
42+
doc: ${{matrix.spec-mapping.doc}}
43+
token: ${{secrets.BUMP_TOKEN}}
44+
file: ${{matrix.spec-mapping.file}}
45+
branch: ${{matrix.spec-mapping.branch}}
46+
47+
api-preview:
48+
needs: create-matrix
49+
if: ${{ github.event_name == 'pull_request' }}
50+
name: Create API preview on Bump.sh
51+
strategy:
52+
matrix:
53+
spec-mapping: ${{ fromJSON(needs.create-matrix.outputs.matrix) }}
54+
runs-on: ubuntu-latest
55+
steps:
56+
- name: Checkout
57+
uses: actions/checkout@v4
58+
- name: Create API preview
59+
uses: bump-sh/github-action@690c81156715f37cb72d006e5cbb81fbd9b45365
60+
with:
61+
doc: ${{matrix.spec-mapping.doc}}
62+
token: ${{secrets.BUMP_TOKEN}}
63+
file: ${{matrix.spec-mapping.file}}
64+
branch: ${{matrix.spec-mapping.branch}}
65+
command: preview

0 commit comments

Comments
 (0)