From 7d859a317c269cbd8265caf102ed81051f9e6f6b Mon Sep 17 00:00:00 2001 From: pasindu Date: Wed, 2 Aug 2023 13:20:27 +0530 Subject: [PATCH] Improve check revision deployment list length logic --- .../publisher/APIPublisherServiceImpl.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java index 0c3b336f0bc..c13cadc8557 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -314,22 +314,27 @@ public class APIPublisherServiceImpl implements APIPublisherService { } } - // This will retrieve the deployed revision - JSONArray revisionDeploymentList = (JSONArray) publisherRESTAPIServices.getAPIRevisions(apiApplicationKey, - accessTokenInfo, existingAPI.getString("id"), true).get("list"); - // This will retrieve the un deployed revision list - JSONArray undeployedRevisionList = (JSONArray) publisherRESTAPIServices.getAPIRevisions(apiApplicationKey, - accessTokenInfo, existingAPI.getString("id"), false).get("list"); int apiRevisionCount = (int) publisherRESTAPIServices.getAPIRevisions(apiApplicationKey, accessTokenInfo, existingAPI.getString("id"), null).get("count"); - if (apiRevisionCount >= 5) { - JSONObject latestRevisionDeployment = revisionDeploymentList.getJSONObject(0); - JSONObject earliestUndeployRevision = undeployedRevisionList.getJSONObject(0); - publisherRESTAPIServices.undeployAPIRevisionDeployment(apiApplicationKey, - accessTokenInfo, latestRevisionDeployment, existingAPI.getString("id")); - publisherRESTAPIServices.deleteAPIRevision(apiApplicationKey, accessTokenInfo, - earliestUndeployRevision, existingAPI.getString("id")); + // This will retrieve the deployed revision + JSONArray revisionDeploymentList = (JSONArray) publisherRESTAPIServices.getAPIRevisions( + apiApplicationKey, accessTokenInfo, existingAPI.getString("id"), + true).get("list"); + if (revisionDeploymentList.length() > 0) { + JSONObject latestRevisionDeployment = revisionDeploymentList.getJSONObject(0); + publisherRESTAPIServices.undeployAPIRevisionDeployment(apiApplicationKey, + accessTokenInfo, latestRevisionDeployment, existingAPI.getString("id")); + } + // This will retrieve the un deployed revision list + JSONArray undeployedRevisionList = (JSONArray) publisherRESTAPIServices.getAPIRevisions( + apiApplicationKey, accessTokenInfo, existingAPI.getString("id"), + false).get("list"); + if (undeployedRevisionList.length() > 0) { + JSONObject earliestUndeployRevision = undeployedRevisionList.getJSONObject(0); + publisherRESTAPIServices.deleteAPIRevision(apiApplicationKey, accessTokenInfo, + earliestUndeployRevision, existingAPI.getString("id")); + } } // create new revision