mirror of
https://github.com/LukeHagar/api-specs.git
synced 2025-12-09 12:27:48 +00:00
added checks for changes
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
// ------------------------------------------------------------
|
// ------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
const requestFromLocal = (localRequest) => {
|
const requestFromLocal = (localRequest, responses) => {
|
||||||
// console.log('localRequest', localRequest)
|
// console.log('localRequest', localRequest)
|
||||||
let url = localRequest.request.url.host + '/' + localRequest.request.url.path
|
let url = localRequest.request.url.host + '/' + localRequest.request.url.path
|
||||||
|
|
||||||
@@ -48,6 +48,10 @@ const requestFromLocal = (localRequest) => {
|
|||||||
.map((header) => ({ key: header.key, value: header.value, enabled: header.enabled, description: header.description }))
|
.map((header) => ({ key: header.key, value: header.value, enabled: header.enabled, description: header.description }))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (JSON.stringify(localRequest.request.description) === '{}') {
|
||||||
|
localRequest.request.description = ''
|
||||||
|
}
|
||||||
|
|
||||||
const request = {
|
const request = {
|
||||||
// owner: '8119550',
|
// owner: '8119550',
|
||||||
// lastUpdatedBy: '8119550',
|
// lastUpdatedBy: '8119550',
|
||||||
@@ -87,7 +91,8 @@ const requestFromLocal = (localRequest) => {
|
|||||||
|
|
||||||
protocolProfileBehavior: localRequest.protocolProfileBehavior,
|
protocolProfileBehavior: localRequest.protocolProfileBehavior,
|
||||||
dataDisabled: localRequest.dataDisabled,
|
dataDisabled: localRequest.dataDisabled,
|
||||||
responses_order: localRequest.responses_order
|
responses_order: localRequest.responses_order,
|
||||||
|
responses: responses,
|
||||||
|
|
||||||
// createdAt: '2023-09-12T16:25:20.000Z',
|
// createdAt: '2023-09-12T16:25:20.000Z',
|
||||||
// updatedAt: '2023-09-12T16:25:23.000Z',
|
// updatedAt: '2023-09-12T16:25:23.000Z',
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const pmConvert = require('./PostmanCovertions')
|
const pmConvert = require('./PostmanCovertions')
|
||||||
const pmAPI = require('./postmanAPI')
|
const pmAPI = require('./postmanAPI')
|
||||||
let privateRemoteCollectionId = '23836355-c5640083-7523-4ad7-9f92-b23e079cbb7b'
|
|
||||||
let publicRemoteCollectionId = '23836355-6224d51a-d924-4c39-a58f-6970735aac8e'
|
let publicRemoteCollectionId = '23836355-6224d51a-d924-4c39-a58f-6970735aac8e'
|
||||||
let localCollection = JSON.parse(fs.readFileSync(`C:\\git\\api-specs\\postman\\collections\\sailpoint-api-v3.json`).toString())
|
let localCollection = JSON.parse(fs.readFileSync(`C:\\git\\api-specs\\postman\\collections\\sailpoint-api-v3.json`).toString())
|
||||||
|
|
||||||
@@ -10,38 +9,125 @@ let localCollection = JSON.parse(fs.readFileSync(`C:\\git\\api-specs\\postman\\c
|
|||||||
|
|
||||||
const release = async () => {
|
const release = async () => {
|
||||||
|
|
||||||
|
|
||||||
let remoteCollection = await refreshRemoteCollection(publicRemoteCollectionId)
|
let remoteCollection = await refreshRemoteCollection(publicRemoteCollectionId)
|
||||||
for (let item of remoteCollection.collection.item) {
|
|
||||||
new pmAPI.Folder(publicRemoteCollectionId).delete(item.id)
|
|
||||||
|
|
||||||
|
|
||||||
|
// This function just cleans up the variables so they match what is returned in postman
|
||||||
|
for (let variable of localCollection.variable) {
|
||||||
|
if (variable.type) {
|
||||||
|
delete variable.type
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the top level collection has changed, If so, update it... This will delete all folders unfortunately.
|
||||||
|
if (checkIfDifferent(remoteCollection.collection.event, localCollection.event)
|
||||||
|
|| checkIfDifferent(remoteCollection.collection.info.description, localCollection.info.description.content)
|
||||||
|
|| checkIfDifferent(remoteCollection.collection.info.name, localCollection.info.name)
|
||||||
|
|| checkIfDifferent(remoteCollection.collection.variable, localCollection.variable)
|
||||||
|
|| checkIfDifferent(remoteCollection.collection.auth, localCollection.auth)) {
|
||||||
|
const localEmptyCollection = { ...localCollection }
|
||||||
|
localEmptyCollection.item = []
|
||||||
|
//delete all folders. Do this one at a time so it doesn't timeout
|
||||||
|
for (let item of remoteCollection.collection.item) {
|
||||||
|
await new pmAPI.Folder(publicRemoteCollectionId).delete(item.id)
|
||||||
|
}
|
||||||
|
// now update the collection with the changes. All folders will be later in code
|
||||||
|
let updatedCollection = await new pmAPI.Collection(publicRemoteCollectionId).update({collection : localEmptyCollection})
|
||||||
|
remoteCollection = await refreshRemoteCollection(publicRemoteCollectionId)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add any missing folders
|
||||||
for (let item of localCollection.item) {
|
for (let item of localCollection.item) {
|
||||||
await updateItem(item)
|
let folder = getMatchingFolder(item, remoteCollection.collection.item)
|
||||||
|
if (folder == null) {
|
||||||
|
await updateEntireFolder(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// update any requests that have changed
|
||||||
|
for (let item of localCollection.item) {
|
||||||
|
let folder = getMatchingFolder(item, remoteCollection.collection.item)
|
||||||
|
if (folder !== null) {
|
||||||
|
await updateRequestsInFolder(item.item, folder.id, folder)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(remoteCollection)
|
console.log(remoteCollection)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getMatchingFolder(localFolder, remoteFolders) {
|
||||||
|
for (let folder of remoteFolders) {
|
||||||
|
if (folder.name === localFolder.name) {
|
||||||
|
return folder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
async function updateItem(item, folderId) {
|
function getMatchingRequest(localRequest, remoteRequests) {
|
||||||
|
for (let request of remoteRequests) {
|
||||||
|
if (request.name === localRequest.name) {
|
||||||
|
return request
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
function buildRequestBody(items) {
|
||||||
|
|
||||||
|
let responses = []
|
||||||
|
for (let response of items.response) {
|
||||||
|
responses.push(pmConvert.responseFromLocal(response, {}))
|
||||||
|
}
|
||||||
|
let postmanRequestBody = pmConvert.requestFromLocal(items, responses)
|
||||||
|
return postmanRequestBody
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkIfDifferent(source, dest) {
|
||||||
|
if (JSON.stringify(source) === JSON.stringify(dest)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function updateEntireFolder(item, folderId) {
|
||||||
if (item.item && !folderId) {
|
if (item.item && !folderId) {
|
||||||
let newFolder = await new pmAPI.Folder(publicRemoteCollectionId).create(item)
|
let newFolder = await new pmAPI.Folder(publicRemoteCollectionId).create(item)
|
||||||
await updateItem(item.item, newFolder.data.id)
|
await updateEntireFolder(item.item, newFolder.data.id)
|
||||||
} else {
|
} else {
|
||||||
for (let items of item) {
|
for (let items of item) {
|
||||||
let postmanRequestBody = pmConvert.requestFromLocal(items)
|
let responses = []
|
||||||
let newRequest = await new pmAPI.Request(publicRemoteCollectionId).create(postmanRequestBody, folderId)
|
|
||||||
for (let response of items.response) {
|
for (let response of items.response) {
|
||||||
let postmanResponseBody = pmConvert.responseFromLocal(response, {})
|
responses.push(pmConvert.responseFromLocal(response, {}))
|
||||||
let newResponse = await new pmAPI.Response(publicRemoteCollectionId).create(postmanResponseBody, newRequest.data.id)
|
}
|
||||||
console.log(newResponse)
|
let postmanRequestBody = pmConvert.requestFromLocal(items, responses)
|
||||||
|
let newRequest = await new pmAPI.Request(publicRemoteCollectionId).create(postmanRequestBody, folderId)
|
||||||
|
console.log(newRequest.data.name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function updateRequestsInFolder(item, folderId, remoteItem) {
|
||||||
|
for (let items of item) {
|
||||||
|
let remoteRequest = getMatchingRequest(items, remoteItem.item)
|
||||||
|
let postmanRequestBody = buildRequestBody(items)
|
||||||
|
let remotePostmanBody = buildRequestBody(remoteRequest)
|
||||||
|
if (checkIfDifferent(postmanRequestBody, remotePostmanBody)) {
|
||||||
|
let newRequestDelete = await new pmAPI.Request(publicRemoteCollectionId).delete(remoteRequest.id)
|
||||||
|
console.log(`deleting request ${newRequestDelete.data.id}`)
|
||||||
|
let newRequest = await new pmAPI.Request(publicRemoteCollectionId).create(postmanRequestBody, folderId)
|
||||||
|
console.log(`creating request ${newRequest.data.name}`)
|
||||||
|
} else {
|
||||||
|
console.log(`no changes to request ${remoteRequest.name}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async function refreshRemoteCollection (remoteCollectionID) {
|
async function refreshRemoteCollection (remoteCollectionID) {
|
||||||
const msg = 'Refreshing remote collection'
|
const msg = 'Refreshing remote collection'
|
||||||
|
|||||||
@@ -2,6 +2,25 @@ require('dotenv').config()
|
|||||||
const axiosRetry = require('axios-retry').default
|
const axiosRetry = require('axios-retry').default
|
||||||
const axios = require('axios')
|
const axios = require('axios')
|
||||||
|
|
||||||
|
const handleError = (error) => {
|
||||||
|
// Log only essential error details
|
||||||
|
if (error.code === 'ECONNABORTED') {
|
||||||
|
console.error(`A timeout occurred: ${error.message}`);
|
||||||
|
} else if (error.response) {
|
||||||
|
// Server responded with a status other than 200 range
|
||||||
|
console.error(`Request failed with status: ${error.response.status}`);
|
||||||
|
} else if (error.request) {
|
||||||
|
// Request was made but no response was received
|
||||||
|
console.error('No response received');
|
||||||
|
} else {
|
||||||
|
// Something else happened in setting up the request
|
||||||
|
console.error('Error:', error.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a rejected promise to maintain promise chain
|
||||||
|
return Promise.reject(error);
|
||||||
|
}
|
||||||
|
|
||||||
class Collection {
|
class Collection {
|
||||||
constructor (collectionId) {
|
constructor (collectionId) {
|
||||||
this.collectionId = collectionId
|
this.collectionId = collectionId
|
||||||
@@ -10,8 +29,18 @@ class Collection {
|
|||||||
timeout: 1000 * 5, // 60 seconds
|
timeout: 1000 * 5, // 60 seconds
|
||||||
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
|
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
|
||||||
})
|
})
|
||||||
axiosRetry(this.axios, {retries: 3})
|
axiosRetry(this.axios, {
|
||||||
|
retries: 10,
|
||||||
|
retryDelay: axiosRetry.exponentialDelay,
|
||||||
|
retryCondition: (error) => {
|
||||||
|
console.log('error, retrying')
|
||||||
|
return error.code === 'ECONNRESET' || error.code === 'ECONNABORTED' || axiosRetry.isNetworkOrIdempotentRequestError(error) || error.response.status === 429
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
this.axios.interceptors.response.use(response => response, handleError);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async get () {
|
async get () {
|
||||||
return await this.axios.get(
|
return await this.axios.get(
|
||||||
@@ -63,7 +92,15 @@ class Folder {
|
|||||||
timeout: 1000 * 5, // 60 seconds
|
timeout: 1000 * 5, // 60 seconds
|
||||||
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
|
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
|
||||||
})
|
})
|
||||||
axiosRetry(this.axios, {retries: 3})
|
axiosRetry(this.axios, {
|
||||||
|
retries: 10,
|
||||||
|
retryDelay: axiosRetry.exponentialDelay,
|
||||||
|
retryCondition: (error) => {
|
||||||
|
console.log('error, retrying')
|
||||||
|
return error.code === 'ECONNRESET' || error.code === 'ECONNABORTED' || axiosRetry.isNetworkOrIdempotentRequestError(error) || error.response.status === 429
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.axios.interceptors.response.use(response => response, handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
async get (folderId) {
|
async get (folderId) {
|
||||||
@@ -126,7 +163,15 @@ class Request {
|
|||||||
timeout: 1000 * 5, // 60 seconds
|
timeout: 1000 * 5, // 60 seconds
|
||||||
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
|
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
|
||||||
})
|
})
|
||||||
axiosRetry(this.axios, {retries: 3})
|
axiosRetry(this.axios, {
|
||||||
|
retries: 10,
|
||||||
|
retryDelay: axiosRetry.exponentialDelay,
|
||||||
|
retryCondition: (error) => {
|
||||||
|
console.log('error, retrying')
|
||||||
|
return error.code === 'ECONNRESET' || error.code === 'ECONNABORTED' || axiosRetry.isNetworkOrIdempotentRequestError(error) || error.response.status === 429
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.axios.interceptors.response.use(response => response, handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
async get (requestId) {
|
async get (requestId) {
|
||||||
@@ -190,7 +235,15 @@ class Response {
|
|||||||
timeout: 1000 * 5, // 60 seconds
|
timeout: 1000 * 5, // 60 seconds
|
||||||
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
|
headers: { 'Content-Type': 'application/json', 'X-Api-Key': this.apiKey }
|
||||||
})
|
})
|
||||||
axiosRetry(this.axios, {retries: 3})
|
axiosRetry(this.axios, {
|
||||||
|
retries: 10,
|
||||||
|
retryDelay: axiosRetry.exponentialDelay,
|
||||||
|
retryCondition: (error) => {
|
||||||
|
console.log('error, retrying')
|
||||||
|
return error.code === 'ECONNRESET' || error.code === 'ECONNABORTED' || axiosRetry.isNetworkOrIdempotentRequestError(error) || error.response.status === 429
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.axios.interceptors.response.use(response => response, handleError);
|
||||||
}
|
}
|
||||||
|
|
||||||
async get (responseId) {
|
async get (responseId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user