From c2c42774cc96537591e99548063f1afb8d1c862e Mon Sep 17 00:00:00 2001 From: barduinor Date: Thu, 21 Sep 2023 12:51:17 -0400 Subject: [PATCH] WIP: implemented deploymen of utilities folder and sub folders --- src/Collection.js | 5 ++- src/CollectionAdvanced.js | 27 ++++++++++++++- src/CollectionAdvancedUtils.js | 2 +- src/DeployIncremental.js | 60 ++++++++++++++++++++++++++++------ src/scripts/releaseAdvanced.js | 2 +- 5 files changed, 82 insertions(+), 14 deletions(-) diff --git a/src/Collection.js b/src/Collection.js index 66a2d41..81dde79 100644 --- a/src/Collection.js +++ b/src/Collection.js @@ -205,10 +205,13 @@ class Collection { } // create a folder object - createFolder (folderName) { + createFolder (folderName, folderParentId = null, description = null) { const folder = { name: folderName, + folder: folderParentId, + description: description, item: [] + } const folderId = Utils.GenID(JSON.stringify(folder)) folder.id = folderId diff --git a/src/CollectionAdvanced.js b/src/CollectionAdvanced.js index 92614ff..67f58b1 100644 --- a/src/CollectionAdvanced.js +++ b/src/CollectionAdvanced.js @@ -51,17 +51,42 @@ class CollectionAdvanced extends Collection { */ // RB: override process () { - return { + const localCollection = { info: this.getInfo(), item: this.getItems(), event: [this.collectionPreRequest()], variable: this.getVariables(), auth: this.defaultAuth() } + + // RB: inject utilities + this.injectUtilities(localCollection) + + return localCollection } // PRIVATE + injectUtilities (localCollection) { + // insert Utils folder at top level item + const folderUtilities = this.createFolder('(Utilities)', null, 'Utility scripts for Postman Collection') + localCollection.item.unshift(folderUtilities) + + // insert create environment into Utils folder + const folderCreateEnvironments = this.createFolder('Create Environments', folderUtilities.id, 'Utility scripts for Postman Collection') + localCollection.item.splice(1, 0, folderCreateEnvironments) + + // insert test environment into Utils folder + const folderTestEnvironments = this.createFolder('Test Environments', folderUtilities.id, 'Utility scripts for Postman Collection') + localCollection.item.splice(2, 0, folderTestEnvironments) + + // insert Authorixe OAuth Box App Helper into Utils folder + const folderAuthorizeOAuthBoxAppHelper = this.createFolder('Authorize OAuth Box App Helper', folderUtilities.id, 'Utility scripts for Postman Collection') + localCollection.item.splice(3, 0, folderAuthorizeOAuthBoxAppHelper) + + return localCollection + } + authForEndPoint (endpoint) { // RB: All endpoints inherit from the collection return null diff --git a/src/CollectionAdvancedUtils.js b/src/CollectionAdvancedUtils.js index e4a7df7..234e96f 100644 --- a/src/CollectionAdvancedUtils.js +++ b/src/CollectionAdvancedUtils.js @@ -8,7 +8,7 @@ const Utils = require('./Utils') const injectUtils = (localCollection) => { // insert Utils folder at top level item - localCollection.item.unshift(genFolder('(Utils)', 'Utility scripts for Postman Collection')) + localCollection.item.unshift(genFolder('(Utilities)', 'Utility scripts for Postman Collection')) return localCollection } diff --git a/src/DeployIncremental.js b/src/DeployIncremental.js index 4b27b76..f44b2b4 100644 --- a/src/DeployIncremental.js +++ b/src/DeployIncremental.js @@ -15,15 +15,9 @@ const pmConvert = require('./PostmanCovertions') const pmAPI = require('./postmanAPI') -const injectUtils = require('./CollectionAdvancedUtils').injectUtils const { GenID } = require('./Utils') -const deployIncremental = async (privateRemoteCollectionId, localCollection, publicRemoteCollectionId, withUtils = true) => { - // inject extra Postman objects - if (withUtils) { - localCollection = injectUtils(localCollection) - } - +const deployIncremental = async (privateRemoteCollectionId, localCollection, publicRemoteCollectionId) => { let remoteCollection = await refreshRemoteCollection(privateRemoteCollectionId) console.log('Incremental deployment of collection ', localCollection.info.name) @@ -273,10 +267,34 @@ async function mergeRequests (remoteCollection, localCollection) { // loop folders for (const localFolder of localFoldersRequest) { - const remoteRequests = remoteFoldersRequest.find(remoteFolder => remoteFolder.id === localFolder.id).item + // original code + // const remoteRequests = remoteFoldersRequest.find(remoteFolder => remoteFolder.id === localFolder.id).item + // const localRequests = localFolder.item + // + // need to handle the case where the folder is empty + // in that case the item property is undefined + const remoteRemoteFolder = remoteFoldersRequest.find(remoteFolder => ((remoteFolder.id === localFolder.id))) + + // subfolders exist in the local collection as root folders + // but in the remote collection they are in the parent folder + // so we need to handle the case where the subfolder + // exists as a root element in the local collection + // but not in the remote collection + if (!remoteRemoteFolder) { + continue + } + + // handle undifined items + remoteRemoteFolder.item = remoteRemoteFolder.item || [] + + // filter out anything that is not a request + remoteRemoteFolder.item = remoteRemoteFolder.item.filter(request => request.request) + + const remoteRequests = remoteRemoteFolder.item + const localRequests = localFolder.item - // create new requests + // Identify old and new requests const newRequests = localRequests.filter(localRequest => !remoteRequests.find(remoteRequest => remoteRequest.id === localRequest.id)) const oldRequests = remoteRequests.filter(remoteRequest => !localRequests.find(localRequest => localRequest.id === remoteRequest.id)) @@ -350,8 +368,30 @@ async function mergeResponses (remoteCollection, localCollection) { let anyResponseHasChanged = false // loop folders for (const localFolder of localFoldersRequest) { - const remoteRequests = remoteFoldersRequest.find(remoteFolder => remoteFolder.id === localFolder.id).item + // const remoteRequests = remoteFoldersRequest.find(remoteFolder => remoteFolder.id === localFolder.id).item + // need to handle the case where the folder is empty + // in that case the item property is undefined + const remoteRemoteFolder = remoteFoldersRequest.find(remoteFolder => ((remoteFolder.id === localFolder.id))) + + // subfolders exist in the local collection as root folders + // but in the remote collection they are in the parent folder + // so we need to handle the case where the subfolder + // exists as a root element in the local collection + // but not in the remote collection + if (!remoteRemoteFolder) { + continue + } + + // handle undifined items + remoteRemoteFolder.item = remoteRemoteFolder.item || [] + + // filter out anything that is not a request + remoteRemoteFolder.item = remoteRemoteFolder.item.filter(request => request.request) + + const remoteRequests = remoteRemoteFolder.item + const localRequests = localFolder.item + console.log(' In Folder: ', localFolder.name) // loop requests for (const localRequest of localRequests) { diff --git a/src/scripts/releaseAdvanced.js b/src/scripts/releaseAdvanced.js index b52873a..4eace5d 100644 --- a/src/scripts/releaseAdvanced.js +++ b/src/scripts/releaseAdvanced.js @@ -9,7 +9,7 @@ const release = async (locale = process.argv[1]) => { const localCollection = JSON.parse(fs.readFileSync(`${OUTPUT_FOLDER}/collection.advanced.${locale}.json`).toString()) const privateRemoteCollectionId = process.env[`PRIVATE_${locale.toUpperCase()}_POSTMAN_COLLECTION_ADVANCED_ID`] const publicRemoteCollectionId = process.env[`PUBLIC_${locale.toUpperCase()}_POSTMAN_COLLECTION_ADVANCED_ID`] - await deployIncremental(privateRemoteCollectionId, localCollection, publicRemoteCollectionId, false) + await deployIncremental(privateRemoteCollectionId, localCollection, publicRemoteCollectionId) } const releaseAll = async () => {