Skip to content

Commit

Permalink
merge to public collection
Browse files Browse the repository at this point in the history
  • Loading branch information
barduinor committed Sep 15, 2023
1 parent 1ced37e commit c10a1ce
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
55 changes: 35 additions & 20 deletions src/DeployIncremental.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,31 @@ const pmConvert = require('./PostmanCovertions')
const pmAPI = require('./postmanAPI')

const deployIncremental = async (remoteCollectionID, localCollection) => {
console.log('Incremental deployment of collection ', localCollection.info.name)

const report = {}

let remoteCollection = await new pmAPI.Collection(remoteCollectionID).get()
let remoteCollection = await refreshRemoteCollection(remoteCollectionID)
console.log('Incremental deployment of collection ', localCollection.info.name)
report.folders = await mergeFolders(remoteCollection, localCollection)

remoteCollection = await new pmAPI.Collection(remoteCollectionID).get()
report.Requests = await mergeRequests(remoteCollection, localCollection)
remoteCollection = await refreshRemoteCollection(remoteCollectionID)
report.requests = await mergeRequests(remoteCollection, localCollection)

remoteCollection = await new pmAPI.Collection(remoteCollectionID).get()
report.Responses = await mergeResponses(remoteCollection, localCollection)
remoteCollection = await refreshRemoteCollection(remoteCollectionID)
report.responses = await mergeResponses(remoteCollection, localCollection)

const publicCollectionID = '8119550-373aba62-5af5-459b-b9a4-e9db77f947a5XXXX'
const publicCollectionID = '8119550-373aba62-5af5-459b-b9a4-e9db77f947a5'

const msg = 'Merging to public collection'
await new pmAPI.Collection(remoteCollectionID).merge(publicCollectionID)
.then(() => { console.log(msg, '-> OK') })
.catch((error) => {
console.log(msg, '-> FAIL')
handlePostmanAPIError(error)
})
console.log('Incremental deployment of collection ', localCollection.info.name, ' completed')
if (report.folders.length > 0 || report.requests.length > 0 || report.responses.length > 0) {
const msg = 'Merging to public collection'
console.log(msg + '...')
await new pmAPI.Collection(remoteCollectionID).merge(publicCollectionID)
.then(() => { console.log(msg, '-> OK') })
.catch((error) => {
console.log(msg, '-> FAIL')
handlePostmanAPIError(error)
})
}
console.log('Incremental deployment of collection ', localCollection.info.name, ' completed\n\n')

// console.log('Report: \n', JSON.stringify(report, null, 2))
return report
Expand All @@ -56,13 +58,13 @@ async function mergeFolders (remoteCollection, localCollection) {

const hasChanges = newFolders.length > 0 || oldFolders.length > 0

const foldersReport = []

if (!hasChanges) {
console.log(' -> No changes')
return
return foldersReport
}

const foldersReport = []

// create new folders
for (const folder of newFolders) {
const msg = ` Creating new folder [${folder.name}]`
Expand Down Expand Up @@ -260,6 +262,16 @@ async function mergeResponses (remoteCollection, localCollection) {
return responsesReport
}

async function refreshRemoteCollection (remoteCollectionID) {
const msg = 'Refreshing remote collection'
console.log('\n' + msg + '...\n')
const remoteCollection = await new pmAPI.Collection(remoteCollectionID).get()
.catch((error) => {
console.log(msg, '-> FAIL')
handlePostmanAPIError(error)
})
return remoteCollection
}
// Handle axios error
const handlePostmanAPIError = (error) => {
if (error.response) {
Expand All @@ -270,7 +282,10 @@ const handlePostmanAPIError = (error) => {
// 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.cause)
console.log('NO RESPONSE:', error.message)
if (error.cause) {
console.log('CAUSE:', error.cause)
}
}
const { method, url, data } = error.config
console.log('REQUEST DETAILS', { method, url, data })
Expand Down
4 changes: 3 additions & 1 deletion src/postmanAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Collection {
async get () {
return await this.axios.get(
`https://api.getpostman.com/collections/${this.collectionId}`
, { timeout: 1000 * 30 } // 30 seconds
).then(function (response) {
if (response.status !== 200) {
throw new Error(`Error getting collection ${this.collectionId}: ${response.status} ${response.statusText}`)
Expand All @@ -26,7 +27,8 @@ class Collection {
async merge (destinationCollectionId, strategy = 'updateSourceWithDestination') {
return await this.axios.post(
'https://api.getpostman.com/collections/merge',
{ source: this.collectionId, destination: destinationCollectionId, strategy }
{ source: this.collectionId, destination: destinationCollectionId, strategy },
{ timeout: 1000 * 60 } // 60 seconds
).then(function (response) {
if (response.status !== 200) {
throw new Error(`Error merging collection from ${this.collectionId} to ${destinationCollectionId}: ${response.status} ${response.statusText}`)
Expand Down

0 comments on commit c10a1ce

Please sign in to comment.