From 146322ea051ce21a6ecef9571416d5c6118d2338 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 Date: Tue, 22 Oct 2024 16:37:19 +0530 Subject: [PATCH 1/4] feat: import folder and collection level scripts --- .../src/utils/importers/postman-collection.js | 122 +++++++++++++++++- 1 file changed, 120 insertions(+), 2 deletions(-) diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index 053a557230..8e4f86d3f0 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -117,13 +117,78 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) = uid: uuid(), name: folderName, type: 'folder', - items: [] + items: [], + root: { + meta: { + name: folderName + }, + request: { + auth: { + mode: 'none', + basic: null, + bearer: null, + awsv4: null + }, + headers: [], + script: {}, + tests: '', + vars: {} + } + } }; brunoParent.items.push(brunoFolderItem); folderMap[folderName] = brunoFolderItem; if (i.item && i.item.length) { importPostmanV2CollectionItem(brunoFolderItem, i.item, i.auth ?? parentAuth, options); } + + if (i.event) { + console.log('yes, folder has i.event', i.event); + i.event.forEach((event) => { + if (!brunoFolderItem.root.request.script) { + brunoFolderItem.root.request.script = {}; + } + if (!brunoFolderItem.root.request.tests) { + brunoFolderItem.root.request.tests = {}; + } + + if (event.listen === 'prerequest' && event.script && event.script.exec) { + if (Array.isArray(event.script.exec)) { + brunoFolderItem.root.request.script.req = event.script.exec + .map((line, index) => + options.enablePostmanTranslations.enabled + ? postmanTranslation(line, () => pushTranslationLog('script', index)) + : `// ${line}` + ) + .join('\n'); + } else { + brunoFolderItem.root.request.script.req = options.enablePostmanTranslations.enabled + ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('script', 0)) + : `// ${event.script.exec[0]} `; + } + } + + if (event.listen === 'test' && event.script && event.script.exec) { + if (Array.isArray(event.script.exec)) { + brunoFolderItem.root.request.tests = event.script.exec + .map((line, index) => + options.enablePostmanTranslations.enabled + ? postmanTranslation(line, () => pushTranslationLog('test', index)) + : `// ${line}` + ) + .join('\n'); + } else { + brunoFolderItem.root.request.tests = options.enablePostmanTranslations.enabled + ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('test', 0)) + : `// ${event.script.exec[0]} `; + } + } + }); + } + + brunoParent.items.push(brunoFolderItem); + folderMap[folderName] = brunoFolderItem; + } else { if (i.request) { const baseRequestName = i.name; @@ -393,9 +458,62 @@ const importPostmanV2Collection = (collection, options) => { uid: uuid(), version: '1', items: [], - environments: [] + environments: [], + root: { + meta: { + name: collection.info.name + }, + request: { + auth: { + mode: 'none', + basic: null, + bearer: null, + awsv4: null + }, + headers: [], + script: {}, + tests: '', + vars: {} + } + } }; + if (collection.event) { + collection.event.forEach((event) => { + if (event.listen === 'prerequest' && event.script && event.script.exec) { + if (Array.isArray(event.script.exec)) { + brunoCollection.root.request.script.req = event.script.exec + .map((line, index) => + options.enablePostmanTranslations.enabled + ? postmanTranslation(line, () => pushTranslationLog('script', index)) + : `// ${line}` + ) + .join('\n'); + } else { + brunoCollection.root.request.script.req = options.enablePostmanTranslations.enabled + ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('script', 0)) + : `// ${event.script.exec[0]} `; + } + } + + if (event.listen === 'test' && event.script && event.script.exec) { + if (Array.isArray(event.script.exec)) { + brunoCollection.root.request.tests = event.script.exec + .map((line, index) => + options.enablePostmanTranslations.enabled + ? postmanTranslation(line, () => pushTranslationLog('test', index)) + : `// ${line}` + ) + .join('\n'); + } else { + brunoCollection.root.request.tests = options.enablePostmanTranslations.enabled + ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('test', 0)) + : `// ${event.script.exec[0]} `; + } + } + }); + } + importPostmanV2CollectionItem(brunoCollection, collection.item, collection.auth, options); return brunoCollection; From c1bfc75098c42e61d3a3805982a458fb644600b9 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 Date: Tue, 22 Oct 2024 16:51:17 +0530 Subject: [PATCH 2/4] refactor: importScriptsFromEvents function and remove duplicate code --- .../src/utils/importers/postman-collection.js | 154 ++++++++---------- 1 file changed, 68 insertions(+), 86 deletions(-) diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index 8e4f86d3f0..3ba2dd527f 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -97,6 +97,62 @@ const constructUrl = (url) => { let translationLog = {}; +const pushTranslationLog = (type, index) => { + if (!translationLog[i.name]) { + translationLog[i.name] = {}; + } + if (!translationLog[i.name][type]) { + translationLog[i.name][type] = []; + } + translationLog[i.name][type].push(index + 1); +}; + +const importScriptsFromEvents = (events, requestObject, options, pushTranslationLog) => { + events.forEach((event) => { + if (event.script && event.script.exec) { + if (event.listen === 'prerequest') { + if (!requestObject.script) { + requestObject.script = {}; + } + + if (Array.isArray(event.script.exec)) { + requestObject.script.req = event.script.exec + .map((line, index) => + options.enablePostmanTranslations.enabled + ? postmanTranslation(line, () => pushTranslationLog('script', index)) + : `// ${line}` + ) + .join('\n'); + } else { + requestObject.script.req = options.enablePostmanTranslations.enabled + ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('script', 0)) + : `// ${event.script.exec[0]} `; + } + } + + if (event.listen === 'test') { + if (!requestObject.tests) { + requestObject.tests = {}; + } + + if (Array.isArray(event.script.exec)) { + requestObject.tests = event.script.exec + .map((line, index) => + options.enablePostmanTranslations.enabled + ? postmanTranslation(line, () => pushTranslationLog('test', index)) + : `// ${line}` + ) + .join('\n'); + } else { + requestObject.tests = options.enablePostmanTranslations.enabled + ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('test', 0)) + : `// ${event.script.exec[0]} `; + } + } + } + }); +}; + const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) => { brunoParent.items = brunoParent.items || []; const folderMap = {}; @@ -136,59 +192,16 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) = } } }; - brunoParent.items.push(brunoFolderItem); - folderMap[folderName] = brunoFolderItem; if (i.item && i.item.length) { importPostmanV2CollectionItem(brunoFolderItem, i.item, i.auth ?? parentAuth, options); } if (i.event) { - console.log('yes, folder has i.event', i.event); - i.event.forEach((event) => { - if (!brunoFolderItem.root.request.script) { - brunoFolderItem.root.request.script = {}; - } - if (!brunoFolderItem.root.request.tests) { - brunoFolderItem.root.request.tests = {}; - } - - if (event.listen === 'prerequest' && event.script && event.script.exec) { - if (Array.isArray(event.script.exec)) { - brunoFolderItem.root.request.script.req = event.script.exec - .map((line, index) => - options.enablePostmanTranslations.enabled - ? postmanTranslation(line, () => pushTranslationLog('script', index)) - : `// ${line}` - ) - .join('\n'); - } else { - brunoFolderItem.root.request.script.req = options.enablePostmanTranslations.enabled - ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('script', 0)) - : `// ${event.script.exec[0]} `; - } - } - - if (event.listen === 'test' && event.script && event.script.exec) { - if (Array.isArray(event.script.exec)) { - brunoFolderItem.root.request.tests = event.script.exec - .map((line, index) => - options.enablePostmanTranslations.enabled - ? postmanTranslation(line, () => pushTranslationLog('test', index)) - : `// ${line}` - ) - .join('\n'); - } else { - brunoFolderItem.root.request.tests = options.enablePostmanTranslations.enabled - ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('test', 0)) - : `// ${event.script.exec[0]} `; - } - } - }); + importScriptsFromEvents(i.event, brunoFolderItem.root.request, options, pushTranslationLog); } brunoParent.items.push(brunoFolderItem); folderMap[folderName] = brunoFolderItem; - } else { if (i.request) { const baseRequestName = i.name; @@ -238,15 +251,16 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) = */ // type could be script or test - const pushTranslationLog = (type, index) => { - if (!translationLog[i.name]) { - translationLog[i.name] = {}; - } - if (!translationLog[i.name][type]) { - translationLog[i.name][type] = []; - } - translationLog[i.name][type].push(index + 1); - }; + // const pushTranslationLog = (type, index) => { + // if (!translationLog[i.name]) { + // translationLog[i.name] = {}; + // } + // if (!translationLog[i.name][type]) { + // translationLog[i.name][type] = []; + // } + // translationLog[i.name][type].push(index + 1); + // }; + if (i.event) { i.event.forEach((event) => { if (event.listen === 'prerequest' && event.script && event.script.exec) { @@ -479,39 +493,7 @@ const importPostmanV2Collection = (collection, options) => { }; if (collection.event) { - collection.event.forEach((event) => { - if (event.listen === 'prerequest' && event.script && event.script.exec) { - if (Array.isArray(event.script.exec)) { - brunoCollection.root.request.script.req = event.script.exec - .map((line, index) => - options.enablePostmanTranslations.enabled - ? postmanTranslation(line, () => pushTranslationLog('script', index)) - : `// ${line}` - ) - .join('\n'); - } else { - brunoCollection.root.request.script.req = options.enablePostmanTranslations.enabled - ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('script', 0)) - : `// ${event.script.exec[0]} `; - } - } - - if (event.listen === 'test' && event.script && event.script.exec) { - if (Array.isArray(event.script.exec)) { - brunoCollection.root.request.tests = event.script.exec - .map((line, index) => - options.enablePostmanTranslations.enabled - ? postmanTranslation(line, () => pushTranslationLog('test', index)) - : `// ${line}` - ) - .join('\n'); - } else { - brunoCollection.root.request.tests = options.enablePostmanTranslations.enabled - ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('test', 0)) - : `// ${event.script.exec[0]} `; - } - } - }); + importScriptsFromEvents(collection.event, brunoCollection.root.request, options, pushTranslationLog); } importPostmanV2CollectionItem(brunoCollection, collection.item, collection.auth, options); From e5c674e3c8e24cae1b5563d83931d8cac1857087 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 Date: Wed, 23 Oct 2024 13:46:38 +0530 Subject: [PATCH 3/4] refactor: Improve importScriptsFromEvents function and handle different types of event.script.exec --- .../src/utils/importers/postman-collection.js | 40 +++++++++++-------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index 3ba2dd527f..9fae28af28 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -115,7 +115,7 @@ const importScriptsFromEvents = (events, requestObject, options, pushTranslation requestObject.script = {}; } - if (Array.isArray(event.script.exec)) { + if (Array.isArray(event.script.exec) && event.script.exec.length > 0) { requestObject.script.req = event.script.exec .map((line, index) => options.enablePostmanTranslations.enabled @@ -123,10 +123,12 @@ const importScriptsFromEvents = (events, requestObject, options, pushTranslation : `// ${line}` ) .join('\n'); - } else { + } else if (typeof event.script.exec === 'string') { requestObject.script.req = options.enablePostmanTranslations.enabled - ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('script', 0)) - : `// ${event.script.exec[0]} `; + ? postmanTranslation(event.script.exec, () => pushTranslationLog('script', 0)) + : `// ${event.script.exec}`; + } else { + console.warn('Unexpected event.script.exec type', typeof event.script.exec); } } @@ -135,7 +137,7 @@ const importScriptsFromEvents = (events, requestObject, options, pushTranslation requestObject.tests = {}; } - if (Array.isArray(event.script.exec)) { + if (Array.isArray(event.script.exec) && event.script.exec.length > 0) { requestObject.tests = event.script.exec .map((line, index) => options.enablePostmanTranslations.enabled @@ -143,10 +145,12 @@ const importScriptsFromEvents = (events, requestObject, options, pushTranslation : `// ${line}` ) .join('\n'); - } else { + } else if (typeof event.script.exec === 'string') { requestObject.tests = options.enablePostmanTranslations.enabled - ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('test', 0)) - : `// ${event.script.exec[0]} `; + ? postmanTranslation(event.script.exec, () => pushTranslationLog('test', 0)) + : `// ${event.script.exec}`; + } else { + console.warn('Unexpected event.script.exec type', typeof event.script.exec); } } } @@ -267,7 +271,7 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) = if (!brunoRequestItem.request.script) { brunoRequestItem.request.script = {}; } - if (Array.isArray(event.script.exec)) { + if (Array.isArray(event.script.exec) && event.script.exec.length > 0) { brunoRequestItem.request.script.req = event.script.exec .map((line, index) => options.enablePostmanTranslations.enabled @@ -275,17 +279,19 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) = : `// ${line}` ) .join('\n'); - } else { + } else if (typeof event.script.exec === 'string') { brunoRequestItem.request.script.req = options.enablePostmanTranslations.enabled - ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('script', 0)) - : `// ${event.script.exec[0]} `; + ? postmanTranslation(event.script.exec, () => pushTranslationLog('script', 0)) + : `// ${event.script.exec}`; + } else { + console.warn('Unexpected event.script.exec type', typeof event.script.exec); } } if (event.listen === 'test' && event.script && event.script.exec) { if (!brunoRequestItem.request.tests) { brunoRequestItem.request.tests = {}; } - if (Array.isArray(event.script.exec)) { + if (Array.isArray(event.script.exec) && event.script.exec.length > 0) { brunoRequestItem.request.tests = event.script.exec .map((line, index) => options.enablePostmanTranslations.enabled @@ -293,10 +299,12 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) = : `// ${line}` ) .join('\n'); - } else { + } else if (typeof event.script.exec === 'string') { brunoRequestItem.request.tests = options.enablePostmanTranslations.enabled - ? postmanTranslation(event.script.exec[0], () => pushTranslationLog('test', 0)) - : `// ${event.script.exec[0]} `; + ? postmanTranslation(event.script.exec, () => pushTranslationLog('test', 0)) + : `// ${event.script.exec}`; + } else { + console.warn('Unexpected event.script.exec type', typeof event.script.exec); } } }); From 016ae9216ec1a673abbe773a23b510ead4a8ba18 Mon Sep 17 00:00:00 2001 From: Pragadesh-45 Date: Wed, 23 Oct 2024 13:56:43 +0530 Subject: [PATCH 4/4] refactor: add info about translation log near its definition --- .../src/utils/importers/postman-collection.js | 28 ++++++------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/packages/bruno-app/src/utils/importers/postman-collection.js b/packages/bruno-app/src/utils/importers/postman-collection.js index 9fae28af28..5ab2dc6c04 100644 --- a/packages/bruno-app/src/utils/importers/postman-collection.js +++ b/packages/bruno-app/src/utils/importers/postman-collection.js @@ -97,6 +97,15 @@ const constructUrl = (url) => { let translationLog = {}; +/* struct of translation log + { + [collectionName]: { + script: [index1, index2], + test: [index1, index2] + } + } + */ + const pushTranslationLog = (type, index) => { if (!translationLog[i.name]) { translationLog[i.name] = {}; @@ -245,25 +254,6 @@ const importPostmanV2CollectionItem = (brunoParent, item, parentAuth, options) = docs: i.request.description } }; - /* struct of translation log - { - [collectionName]: { - script: [index1, index2], - test: [index1, index2] - } - } - */ - - // type could be script or test - // const pushTranslationLog = (type, index) => { - // if (!translationLog[i.name]) { - // translationLog[i.name] = {}; - // } - // if (!translationLog[i.name][type]) { - // translationLog[i.name][type] = []; - // } - // translationLog[i.name][type].push(index + 1); - // }; if (i.event) { i.event.forEach((event) => {