From 887a2f806578f96e07f0bbf2c36551eaf50f7ce2 Mon Sep 17 00:00:00 2001 From: Hyejin Ahn Date: Thu, 19 May 2022 15:26:24 -0700 Subject: [PATCH 1/2] fix: raise an error when 409 happen while trying to upload a theme for the second time --- lib/stencil-push.utils.js | 4 +++- lib/theme-api-client.js | 10 ++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/stencil-push.utils.js b/lib/stencil-push.utils.js index 2229c51b..68316bcb 100644 --- a/lib/stencil-push.utils.js +++ b/lib/stencil-push.utils.js @@ -87,6 +87,7 @@ utils.uploadBundle = async (options) => { config: { accessToken }, bundleZipPath, storeHash, + uploadBundleAgain, } = options; const apiHost = options.apiHost || options.config.apiHost; @@ -97,6 +98,7 @@ utils.uploadBundle = async (options) => { apiHost, bundleZipPath, storeHash, + uploadBundleAgain, }); return { ...options, @@ -193,7 +195,7 @@ utils.uploadBundleAgainIfNecessary = async (options) => { return options; } - return utils.uploadBundle(options); + return utils.uploadBundle({ ...options, uploadThemeAgain: true }); }; utils.notifyUserOfThemeUploadCompletion = async (options) => { diff --git a/lib/theme-api-client.js b/lib/theme-api-client.js index b844ff53..64d45f70 100644 --- a/lib/theme-api-client.js +++ b/lib/theme-api-client.js @@ -278,9 +278,10 @@ async function getVariationsByThemeId({ accessToken, apiHost, storeHash, themeId * @param {string} options.accessToken * @param {string} options.apiHost * @param {string} options.storeHash + * @param {boolean} options.uploadBundleAgain * @returns {Promise} */ -async function postTheme({ bundleZipPath, accessToken, apiHost, storeHash }) { +async function postTheme({ bundleZipPath, accessToken, apiHost, storeHash, uploadBundleAgain }) { const formData = new FormData(); formData.append('file', fs.createReadStream(bundleZipPath), { filename: path.basename(bundleZipPath), @@ -303,7 +304,12 @@ async function postTheme({ bundleZipPath, accessToken, apiHost, storeHash }) { // This status code is overloaded, so we need to check the message to determine // if we want to trigger the theme deletion flow. const uploadLimitPattern = /You have reached your upload limit/; - if (payload && payload.title && uploadLimitPattern.exec(payload.title)) { + if ( + payload && + payload.title && + uploadLimitPattern.exec(payload.title) && + !uploadBundleAgain + ) { return { themeLimitReached: true }; } From afdab92953784ddc08eaaa3820efbc6f730bfe2e Mon Sep 17 00:00:00 2001 From: Hyejin Ahn Date: Wed, 1 Jun 2022 12:12:59 -0700 Subject: [PATCH 2/2] 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 97df0c55..63071eb4 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 68316bcb..8733b560 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; +}; + utils.uploadBundleAgainIfNecessary = async (options) => { if (!options.themeLimitReached) { return options;