Skip to content

Commit

Permalink
refactor release to accept private and public collections id's + move…
Browse files Browse the repository at this point in the history
…d deploybulk to oldDeployBulk
  • Loading branch information
barduinor committed Sep 15, 2023
1 parent bcafe58 commit 99d89e3
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 13 deletions.
18 changes: 17 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ JP_OAS3_REPO="https://github.com/box/box-openapi.git#jp"

# the Postman API key for publishing
POSTMAN_API_KEY=""

# PRIVATE collections id's
# These are the private collections
# that will be updated
# via the Box OpenAPI scripts
# On the Box CI/CD workspace
EN_POSTMAN_COLLECTION_ID="8119550-3e45693c-a4d7-4d77-b02c-f7035a152898"
JP_POSTMAN_COLLECTION_ID="8119550-8ab5448e-636f-44ee-ba16-97908b5bc7eb"
EN_POSTMAN_COLLECTION_ADVANCED_ID = "8119550-1ca6cb64-7560-4f24-a2b5-89ba095b1c17"
JP_POSTMAN_COLLECTION_ADVANCED_ID = "8119550-45cd808e-0844-4e46-ada0-5d0cb4b602d2"

# PUBLIC collection id's
# These are the public collections
# that will be updated
# via merge from the private collections
EN_POSTMAN_COLLECTION_ID="8119550-b5ea2aeb-c82a-425d-baff-ed5dfd1d7659"
JP_POSTMAN_COLLECTION_ID="8119550-73df4d75-420a-455b-97c2-d3d33103c1a4"
EN_POSTMAN_COLLECTION_ADVANCED_ID = ""
EN_POSTMAN_COLLECTION_ADVANCED_ID = "8119550-373aba62-5af5-459b-b9a4-e9db77f947a5"
JP_POSTMAN_COLLECTION_ADVANCED_ID = "8119550-9899c10b-11bb-4f53-9938-fced213a14d0"
12 changes: 5 additions & 7 deletions src/DeployIncremental.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,21 @@
const pmConvert = require('./PostmanCovertions')
const pmAPI = require('./postmanAPI')

const deployIncremental = async (remoteCollectionID, localCollection) => {
let remoteCollection = await refreshRemoteCollection(remoteCollectionID)
const deployIncremental = async (privateRemoteCollectionId, localCollection, publicRemoteCollectionId) => {
let remoteCollection = await refreshRemoteCollection(privateRemoteCollectionId)
console.log('Incremental deployment of collection ', localCollection.info.name)
const foldersHaveChanged = await mergeFolders(remoteCollection, localCollection)

remoteCollection = await refreshRemoteCollection(remoteCollectionID)
remoteCollection = await refreshRemoteCollection(privateRemoteCollectionId)
const requestsHaveChanged = await mergeRequests(remoteCollection, localCollection)

remoteCollection = await refreshRemoteCollection(remoteCollectionID)
remoteCollection = await refreshRemoteCollection(privateRemoteCollectionId)
const responsesHaveChanged = await mergeResponses(remoteCollection, localCollection)

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

if (foldersHaveChanged || requestsHaveChanged || responsesHaveChanged) {
const msg = 'Merging to public collection'
console.log('\n' + msg + '...')
await new pmAPI.Collection(remoteCollectionID).merge(publicCollectionID)
await new pmAPI.Collection(privateRemoteCollectionId).merge(publicRemoteCollectionId)
.then(() => { console.log(msg, '-> OK\n') })
.catch((error) => {
console.log(msg, '-> FAIL')
Expand Down
86 changes: 86 additions & 0 deletions src/OldDeployBulk.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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 axios = require('axios')

const oldDeployBulk = 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
})
})
.catch(function (error) {
// console.dir(error.response, { depth: 100 })
logAxiosError(error)
// throw error
})
}
function logAxiosError (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)
} else {
// Something happened in setting up the request that triggered an Error
console.log('ERROR MESSAGE', error.message)
}
// console.log('ERROR CONFIG', error.config)
process.exit(1)
}

module.exports = {
oldDeployBulk
}
8 changes: 5 additions & 3 deletions src/scripts/release.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
require('dotenv').config()

const fs = require('fs')
const deploy = require('../DeployBulk')
const oldDeploy = require('../OldDeployBulk')
const OUTPUT_FOLDER = './compiled'

const release = async (locale = process.argv[1]) => {
const collection = JSON.parse(fs.readFileSync(`${OUTPUT_FOLDER}/collection.${locale}.json`).toString())
const collectionId = process.env[`${locale.toUpperCase()}_POSTMAN_COLLECTION_ID`]
deploy.deployBulk(collectionId, collection)
const privateRemoteCollectionID = process.env[`PRIVATE_${locale.toUpperCase()}_POSTMAN_COLLECTION_ID`]
const publicRemoteCollectionID = process.env[`PUBLIC_${locale.toUpperCase()}_POSTMAN_COLLECTION_ID`]

oldDeploy.oldDeployBulk(privateRemoteCollectionID, collection, publicRemoteCollectionID)
}

const releaseAll = async () => {
Expand Down
5 changes: 3 additions & 2 deletions src/scripts/releaseAdvanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const OUTPUT_FOLDER = './compiled'

const release = async (locale = process.argv[1]) => {
const localCollection = JSON.parse(fs.readFileSync(`${OUTPUT_FOLDER}/collection.advanced.${locale}.json`).toString())
const remoteCollectionID = process.env[`${locale.toUpperCase()}_POSTMAN_COLLECTION_ADVANCED_ID`]
await deployIncremental(remoteCollectionID, localCollection)
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)
}

const releaseAll = async () => {
Expand Down

0 comments on commit 99d89e3

Please sign in to comment.