From 64811eec9db9613347fec5f8162339807ca133d3 Mon Sep 17 00:00:00 2001 From: barduinor Date: Wed, 13 Sep 2023 17:12:16 -0400 Subject: [PATCH] sorting responses in requests --- src/DeployBulk.js | 58 +++++++++++++++++++--------------------- src/DeployIncremental.js | 20 ++++++++++++++ 2 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/DeployBulk.js b/src/DeployBulk.js index 0de3469..3907963 100644 --- a/src/DeployBulk.js +++ b/src/DeployBulk.js @@ -31,38 +31,36 @@ const deployBulk = async (collectionId, localCollection) => { 'Content-Type': 'application/json', 'X-Api-Key': process.env.POSTMAN_API_KEY } - } - ).then(function () { - console.log('EMPTY COLLECTION PUT OK: ', localCollection.info.name) - // then publish the new collection - axios.put( - `https://api.getpostman.com/collections/${collectionId}`, - // JSON.stringify({ collection }), - { collection: localCollection }, - { - headers: { - 'Content-Type': 'application/json', - 'X-Api-Key': process.env.POSTMAN_API_KEY - } - } - ).then(function () { - console.log('FULL COLLECTION PUT OK:', localCollection.info.name) - } - ).catch(function (error) { - // console.dir(error.response, { depth: 100 }) + }) + .then(function () { + console.log('EMPTY COLLECTION PUT OK: ', localCollection.info.name) + // then publish the new collection + axios.put( + `https://api.getpostman.com/collections/${collectionId}`, + // JSON.stringify({ collection }), + { collection: localCollection }, + { + headers: { + 'Content-Type': 'application/json', + 'X-Api-Key': process.env.POSTMAN_API_KEY + } + }) + .then(function () { + console.log('FULL COLLECTION PUT OK:', localCollection.info.name) + } + ) + .catch(function (error) { + // console.dir(error.response, { depth: 100 }) + logAxiosError(error) + // throw error + }) + }) + .catch(function (error) { + // console.dir(error.response, { depth: 100 }) logAxiosError(error) - // throw error - } - ) - } - ).catch(function (error) { - // console.dir(error.response, { depth: 100 }) - logAxiosError(error) - // throw error - } - ) + // throw error + }) } - function logAxiosError (error) { if (error.response) { // The request was made and the server responded with a status code diff --git a/src/DeployIncremental.js b/src/DeployIncremental.js index ddb8173..d7542d1 100644 --- a/src/DeployIncremental.js +++ b/src/DeployIncremental.js @@ -45,6 +45,8 @@ async function mergeFolders (remoteCollection, localCollection) { } console.log(' Deploying Folders:') + let hasChanges = false + // create new folders for (const folder of newFolders) { const msg = ` Creating new folder [${folder.name}]` @@ -55,6 +57,7 @@ async function mergeFolders (remoteCollection, localCollection) { console.log(msg, '-> FAIL') handlePostmanAPIError(error) }) + hasChanges = true } // delete old folders @@ -67,6 +70,23 @@ async function mergeFolders (remoteCollection, localCollection) { console.log(msg, '-> FAIL') handlePostmanAPIError(error) }) + hasChanges = true + } + + // sort folders + if (hasChanges) { + const tmpRootFolder = await new pmAPI.Folder(remoteCollection.collection.info.uid).create({ name: 'tmpRootFolder' }) + // refresh remote collection + remoteCollection = await new pmAPI.Collection(remoteCollection.collection.info.uid).get() + // for each remote folder transfer them to the tmpRootFolder + for (const folder of remoteCollection.collection.item) { + await new pmAPI.Folder(remoteCollection.collection.info.uid) + .update(folder.id, { folder: tmpRootFolder.data.id }) + .catch((error) => { + console.log(' Moving folder', folder.name, 'to tmpRootFolder -> FAIL') + handlePostmanAPIError(error) + }) + } } }