Skip to content

Commit c149ceb

Browse files
committed
CLOUDP-298233: postman transformation in js
1 parent 558329f commit c149ceb

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

tools/postman/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ convert_to_collection:
1717
transform_collection:
1818
./scripts/transform-for-api.sh
1919

20+
.PHONY: transform_collection_js
21+
transform_collection_js:
22+
./scripts/transform-js
23+
2024
.PHONY: transform_collection_test
2125
transform_collection_test:
2226
./scripts/transform-for-api-test.sh
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
const _ = require('lodash');
4+
5+
// TODO CLI arguments instead of env variables.
6+
const COLLECTION_FILE_NAME = process.env.COLLECTION_FILE_NAME || 'collection.json';
7+
const COLLECTION_TRANSFORMED_FILE_NAME = process.env.COLLECTION_TRANSFORMED_FILE_NAME || 'collection-transformed.json';
8+
const OPENAPI_FILE_NAME = process.env.OPENAPI_FILE_NAME || 'atlas-api.json';
9+
const OPENAPI_FOLDER = process.env.OPENAPI_FOLDER || '../openapi';
10+
const TMP_FOLDER = process.env.TMP_FOLDER || '../tmp';
11+
const VERSION_FILE_NAME = process.env.VERSION_FILE_NAME || 'version.txt';
12+
const DESCRIPTION_FILE = process.env.DESCRIPTION_FILE || '../collection-description.md';
13+
const TOGGLE_INCLUDE_BODY = process.env.TOGGLE_INCLUDE_BODY !== 'false';
14+
const TOGGLE_ADD_DOCS_LINKS = process.env.TOGGLE_ADD_DOCS_LINKS === 'true';
15+
const TOKEN_URL_ENV = process.env.TOKEN_URL_ENV || '';
16+
const BASE_URL = process.env.BASE_URL || '';
17+
18+
// Dedicated transformation for postman collection.json file.
19+
const transform = () => {
20+
const currentApiRevision = fs.readFileSync(path.join(OPENAPI_FOLDER, VERSION_FILE_NAME), 'utf8').trim();
21+
let collection = loadJsonFile(path.join(TMP_FOLDER, COLLECTION_FILE_NAME));
22+
23+
console.log(`Wrapping Collection ${COLLECTION_FILE_NAME} in "collection" tag`);
24+
collection = { collection };
25+
26+
console.log('Disabling query params by default');
27+
_.forEach(collection.collection.item, (item) => {
28+
if (item.request && item.request.url && Array.isArray(item.request.url.query)) {
29+
item.request.url.query.forEach((query) => {
30+
query.disabled = true;
31+
});
32+
}
33+
});
34+
35+
console.log('Removing _postman_id');
36+
delete collection.collection.info._postman_id;
37+
38+
console.log('Removing circular references');
39+
// TODO
40+
41+
console.log(`Updating name with version ${currentApiRevision}`);
42+
collection.collection.info.name = `MongoDB Atlas Administration API ${currentApiRevision}`;
43+
44+
console.log('Adding Collection description');
45+
const description = fs.readFileSync(DESCRIPTION_FILE, 'utf8').trim();
46+
_.set(collection, 'collection.info.description.content', description);
47+
48+
console.log('Removing all variables. We use environment for variables instead');
49+
collection.collection.variable = [];
50+
51+
console.log(`Adding baseUrl property ${BASE_URL}`);
52+
collection.collection.variable.push({ key: 'baseUrl', value: BASE_URL });
53+
54+
if (TOGGLE_ADD_DOCS_LINKS) {
55+
console.log('Adding links to docs for each request');
56+
57+
const openapiContent = loadJsonFile(path.join(OPENAPI_FOLDER, OPENAPI_FILE_NAME));
58+
const paths = _.keys(openapiContent.paths);
59+
60+
paths.forEach((pathKey) => {
61+
const methods = _.keys(openapiContent.paths[pathKey]);
62+
methods.forEach((method) => {
63+
const requestInfo = openapiContent.paths[pathKey][method];
64+
const title = requestInfo.summary;
65+
const operationId = requestInfo.operationId;
66+
const tag = _.kebabCase(requestInfo.tags[0]);
67+
68+
const url = `https://mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/${tag}/operation/${operationId}`;
69+
70+
const requestItem = _.find(_.flatten(collection.collection.item.map((i) => i.item)), { name: title });
71+
if (requestItem && requestItem.description && requestItem.description.content) {
72+
requestItem.description.content += `\n\nFind out more at ${url}`;
73+
}
74+
});
75+
});
76+
}
77+
78+
if (!TOGGLE_INCLUDE_BODY) {
79+
console.log('Removing generated bodies');
80+
_.forEach(_.flatten(collection.collection.item.map((i) => i.item)), (item) => {
81+
if (item.response) {
82+
item.response.forEach((response) => {
83+
response.body = '';
84+
});
85+
item.request.body = {};
86+
}
87+
});
88+
}
89+
90+
if (TOKEN_URL_ENV) {
91+
console.log(`Adding client credentials auth url variable ${TOKEN_URL_ENV}`);
92+
collection.collection.variable.push({ key: 'clientCredentialsTokenUrl', value: TOKEN_URL_ENV });
93+
}
94+
95+
96+
saveJsonFile(path.join(TMP_FOLDER, COLLECTION_TRANSFORMED_FILE_NAME), collection);
97+
98+
console.log('Transformation complete');
99+
};
100+
101+
function loadJsonFile(filePath) {
102+
const data = fs.readFileSync(filePath, 'utf8');
103+
return JSON.parse(data);
104+
}
105+
106+
function saveJsonFile(filePath, json) {
107+
fs.writeFileSync(filePath, JSON.stringify(json, null, 2), 'utf8');
108+
}
109+
110+
transform();

0 commit comments

Comments
 (0)