From c25277cb9c76a0ffcf66a678e37971f9e124f360 Mon Sep 17 00:00:00 2001 From: barduinor Date: Fri, 15 Sep 2023 17:07:45 -0400 Subject: [PATCH] updated new DeployBulk to update collection head or full --- src/DeployBulk.js | 113 ++++++++++++++++------------------------------ src/postmanAPI.js | 13 ++++++ 2 files changed, 52 insertions(+), 74 deletions(-) diff --git a/src/DeployBulk.js b/src/DeployBulk.js index 3907963..6208d87 100644 --- a/src/DeployBulk.js +++ b/src/DeployBulk.js @@ -1,86 +1,51 @@ -// Deploy collection in bulks -// -------------------------------------------------------------- -// Use the collection PUT to deploy the new collection in bulk -// using the constructed collection from the previous step. -// It first updates the collection with an empty collection -// to remove all folders and requests. -// Then it updates the collection with the new collection -// to add all folders and requests. -// ------------------------------------------------------------ -// We beleive this to be the source of the problem, where -// erractic behaviour is observed when deploying the collection -// in bulk, with an HTTP 500 server error. -// ------------------------------------------------------------ +// const pmConvert = require('./PostmanCovertions') +const pmAPI = require('./postmanAPI') -const axios = require('axios') - -const deployBulk = async (collectionId, localCollection) => { -// prevent old folders from remaining in place by first removing all items - const emptyCollection = { ...localCollection } - emptyCollection.item = [] - - // console.log('Empty Collection:',{ collection: emptyCollection }) - - // first publish an empty collection to ensure all folders are removed - await axios.put( - `https://api.getpostman.com/collections/${collectionId}`, - // JSON.stringify({ collection: emptyCollection}), - { collection: emptyCollection }, - { - headers: { - '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 }) - logAxiosError(error) - // throw error - }) +const deployColectionHead = async (localCollection, remoteCollectionID) => { + const collectionHead = { ...localCollection } + collectionHead.item = [] + const msg = `\nDeploying collection head ${collectionHead.info.name} to ${remoteCollectionID}` + await new pmAPI.Collection(remoteCollectionID) + .update(collectionHead) + .then(() => { console.log(msg, '-> OK\n') }) + .catch((error) => { + console.log(msg, '-> FAIL') + handlePostmanAPIError(error) }) - .catch(function (error) { - // console.dir(error.response, { depth: 100 }) - logAxiosError(error) - // throw error +} + +const deployColectionFull = async (localCollection, remoteCollectionID) => { + const msg = `\nDeploying full collection ${localCollection.info.name} to ${remoteCollectionID}` + await new pmAPI.Collection(remoteCollectionID) + .update(localCollection) + .then(() => { console.log(msg, '-> OK\n') }) + .catch((error) => { + console.log(msg, '-> FAIL') + handlePostmanAPIError(error) }) } -function logAxiosError (error) { + +// Handle axios error +const handlePostmanAPIError = (error) => { if (error.response) { - // The request was made and the server responded with a status code - // that falls out of the range of 2xx - console.log('ERROR DATA', error.response.data) - console.log('ERROR STATUS', error.response.status) - console.log('ERROR HEADERS', error.response.headers) - } else if (error.request) { - // The request was made but no response was received - // `error.request` is an instance of XMLHttpRequest in the browser and an instance of - // http.ClientRequest in node.js - console.log('ERROR REQUEST', error.request) + // The request was made and the server responded with a status code + // that falls out of the range of 2xx + console.log('API ERROR:', error.response.data) } else { - // Something happened in setting up the request that triggered an Error - console.log('ERROR MESSAGE', error.message) + // The request was made but no response was received + // `error.request` is an instance of XMLHttpRequest in the browser and an instance of + // http.ClientRequest in node.js + console.log('NO RESPONSE:', error.message) + if (error.cause) { + console.log('CAUSE:', error.cause) + } } - // console.log('ERROR CONFIG', error.config) + const { method, url, data } = error.config + console.log('REQUEST DETAILS', { method, url, data }) process.exit(1) } module.exports = { - deployBulk + deployColectionHead, + deployColectionFull } diff --git a/src/postmanAPI.js b/src/postmanAPI.js index fd66179..67c77e7 100644 --- a/src/postmanAPI.js +++ b/src/postmanAPI.js @@ -37,6 +37,19 @@ class Collection { } }) } + + async update (collection) { + return await this.axios.put( + `https://api.getpostman.com/collections/${this.collectionId}`, + collection + ).then(function (response) { + if (response.status !== 200) { + throw new Error(`Error updating collection ${collection.id}: ${response.status} ${response.statusText}`) + } else { + return response.data + } + }) + } } class Folder {