From 99d89e36b00d906cf77efdd7ec1dcaa4af70863b Mon Sep 17 00:00:00 2001 From: barduinor Date: Fri, 15 Sep 2023 16:43:55 -0400 Subject: [PATCH] refactor release to accept private and public collections id's + moved deploybulk to oldDeployBulk --- .env.example | 18 ++++++- src/DeployIncremental.js | 12 ++--- src/OldDeployBulk.js | 86 ++++++++++++++++++++++++++++++++++ src/scripts/release.js | 8 ++-- src/scripts/releaseAdvanced.js | 5 +- 5 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 src/OldDeployBulk.js diff --git a/.env.example b/.env.example index 3526fd4..00048d2 100644 --- a/.env.example +++ b/.env.example @@ -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" diff --git a/src/DeployIncremental.js b/src/DeployIncremental.js index e4a9f56..495512d 100644 --- a/src/DeployIncremental.js +++ b/src/DeployIncremental.js @@ -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') diff --git a/src/OldDeployBulk.js b/src/OldDeployBulk.js new file mode 100644 index 0000000..eca0199 --- /dev/null +++ b/src/OldDeployBulk.js @@ -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 +} diff --git a/src/scripts/release.js b/src/scripts/release.js index daa5dc3..0870894 100644 --- a/src/scripts/release.js +++ b/src/scripts/release.js @@ -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 () => { diff --git a/src/scripts/releaseAdvanced.js b/src/scripts/releaseAdvanced.js index f724505..4eace5d 100644 --- a/src/scripts/releaseAdvanced.js +++ b/src/scripts/releaseAdvanced.js @@ -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 () => {