From ce5b6cc9eb67333e42e584a83bcaa06875b8fe47 Mon Sep 17 00:00:00 2001 From: Hyejin Ahn Date: Wed, 1 Jun 2022 12:12:59 -0700 Subject: [PATCH] fix: MERC-8592 Check if a theme is deleted before pushing a new theme --- lib/stencil-push.js | 1 + lib/stencil-push.utils.js | 42 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/lib/stencil-push.js b/lib/stencil-push.js index 97df0c552..63071eb4e 100644 --- a/lib/stencil-push.js +++ b/lib/stencil-push.js @@ -13,6 +13,7 @@ function stencilPush(options = {}, callback) { utils.notifyUserOfThemeLimitReachedIfNecessary, utils.promptUserToDeleteThemesIfNecessary, utils.deleteThemesIfNecessary, + utils.checkIfDeletionIsComplete(), utils.uploadBundleAgainIfNecessary, utils.notifyUserOfThemeUploadCompletion, utils.pollForJobCompletion((data) => ({ themeId: data.theme_id })), diff --git a/lib/stencil-push.utils.js b/lib/stencil-push.utils.js index 68316bcb3..b5bbdc43d 100644 --- a/lib/stencil-push.utils.js +++ b/lib/stencil-push.utils.js @@ -190,6 +190,48 @@ utils.deleteThemesIfNecessary = async (options) => { return options; }; +utils.checkIfDeletionIsComplete = () => { + return async.retryable( + { + interval: 1000, + errorFilter: (err) => { + if (err.message === 'ThemeStillExists') { + console.log(`${'warning'.yellow} -- Theme still exists;Retrying ...`); + return true; + } + return false; + }, + times: 5, + }, + utils.checkIfThemeIsDeleted(), + ); +}; + +utils.checkIfThemeIsDeleted = () => async (options) => { + const { + themeLimitReached, + config: { accessToken }, + storeHash, + themeIdsToDelete, + } = options; + + if (!themeLimitReached) { + return options; + } + + const apiHost = options.apiHost || options.config.apiHost; + + const result = await themeApiClient.getThemes({ accessToken, apiHost, storeHash }); + + const themeStillExists = result.some((theme) => themeIdsToDelete.includes(theme.uuid)); + + if (themeStillExists) { + throw new Error('ThemeStillExists'); + } + + return { ...options, ...result }; +}; + utils.uploadBundleAgainIfNecessary = async (options) => { if (!options.themeLimitReached) { return options;