Skip to content

Commit

Permalink
updated new DeployBulk to update collection head or full
Browse files Browse the repository at this point in the history
  • Loading branch information
barduinor committed Sep 15, 2023
1 parent 99d89e3 commit c25277c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 74 deletions.
113 changes: 39 additions & 74 deletions src/DeployBulk.js
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions src/postmanAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c25277c

Please sign in to comment.