From ff6bcbb86a87659fc5ae1050df4508d6d9975421 Mon Sep 17 00:00:00 2001 From: antony Date: Fri, 24 Mar 2023 12:37:34 +0000 Subject: [PATCH] Adding retries to setting default locale --- dist/index.js | 89 +++++++++++++++++++++++++++++++++------------------ src/action.ts | 36 ++++++++++++++++----- 2 files changed, 86 insertions(+), 39 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7635e684..39069ea3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30467,7 +30467,7 @@ var readdirAsync = Object(external_util_.promisify)(external_fs_.readdir); * @param space */ var runAction = function (space) { return Object(tslib.__awaiter)(void 0, void 0, void 0, function () { - var branchNames, _a, environmentId, environment, environmentType, count, status_1, newEnv, keys, defaultLocale, availableMigrations, _b, versions, storedVersionEntry, currentVersionString, currentMigrationIndex, migrationsToRun, migrationOptions, migrationToRun, mutableStoredVersionEntry, filePath, environmentIdToDelete, environment_1, error_1; + var branchNames, _a, environmentId, environment, environmentType, count, status_1, newEnv, keys, defaultLocale, locales, error_1, availableMigrations, _b, versions, storedVersionEntry, currentVersionString, currentMigrationIndex, migrationsToRun, migrationOptions, migrationToRun, mutableStoredVersionEntry, filePath, environmentIdToDelete, environment_1, error_2; var _c; return Object(tslib.__generator)(this, function (_d) { switch (_d.label) { @@ -30518,20 +30518,47 @@ var runAction = function (space) { return Object(tslib.__awaiter)(void 0, void 0 case 7: _d.sent(); Logger.verbose("Set default locale to new environment"); - return [4 /*yield*/, environment.getLocales()]; + count = 0; + _d.label = 8; case 8: - defaultLocale = (_d.sent()).items.find(function (locale) { return locale.default; }).code; + if (!(count < MAX_NUMBER_OF_TRIES)) return [3 /*break*/, 14]; + _d.label = 9; + case 9: + _d.trys.push([9, 11, , 13]); + return [4 /*yield*/, environment.getLocales()]; + case 10: + locales = _d.sent(); + Logger.success("Successfully retrieved locales: ".concat(locales.items.map(function (_a) { + var code = _a.code; + return code; + }).join(', '))); + defaultLocale = locales.items.find(function (locale) { return locale.default; }).code; + return [3 /*break*/, 14]; + case 11: + error_1 = _d.sent(); + Logger.warn("Retrying retrieval of locales"); + return [4 /*yield*/, delay()]; + case 12: + _d.sent(); + count++; + return [3 /*break*/, 13]; + case 13: return [3 /*break*/, 8]; + case 14: + if (!defaultLocale) { + throw new Error("Unable to set default locale to new environment"); + } Logger.verbose("Read all the available migrations from the file system"); _b = to_semver_default.a; return [4 /*yield*/, readdirAsync(MIGRATIONS_DIR)]; - case 9: - availableMigrations = _b.apply(void 0, [(_d.sent()).map(function (file) { return filenameToVersion(file); }), { clean: false }]).reverse(); + case 15: + availableMigrations = _b.apply(void 0, [(_d.sent()).map(function (file) { return filenameToVersion(file); }), + { clean: false }]).reverse(); Logger.verbose("versionOrder: ".concat(JSON.stringify(availableMigrations, null, 4))); Logger.verbose("Find current version of the contentful space"); return [4 /*yield*/, environment.getEntries({ content_type: VERSION_CONTENT_TYPE, })]; - case 10: + case 16: versions = (_d.sent()).items; // If there is no entry or more than one of CONTENTFUL_VERSION_TRACKING // Then throw an Error and abort @@ -30559,29 +30586,29 @@ var runAction = function (space) { return Object(tslib.__awaiter)(void 0, void 0 }; Logger.verbose("Run migrations and update version entry"); mutableStoredVersionEntry = storedVersionEntry; - _d.label = 11; - case 11: - if (!(migrationToRun = migrationsToRun.shift())) return [3 /*break*/, 15]; + _d.label = 17; + case 17: + if (!(migrationToRun = migrationsToRun.shift())) return [3 /*break*/, 21]; filePath = external_path_default().join(MIGRATIONS_DIR, versionToFilename(migrationToRun)); Logger.verbose("Running ".concat(filePath)); return [4 /*yield*/, Object(cli.runMigration)(Object.assign(migrationOptions, { filePath: filePath, }))]; - case 12: + case 18: _d.sent(); Logger.success("Migration script ".concat(migrationToRun, ".js succeeded")); mutableStoredVersionEntry.fields.version[defaultLocale] = migrationToRun; return [4 /*yield*/, mutableStoredVersionEntry.update()]; - case 13: + case 19: mutableStoredVersionEntry = _d.sent(); return [4 /*yield*/, mutableStoredVersionEntry.publish()]; - case 14: + case 20: mutableStoredVersionEntry = _d.sent(); Logger.success("Updated field ".concat(VERSION_FIELD, " in ").concat(VERSION_CONTENT_TYPE, " entry to ").concat(migrationToRun)); - return [3 /*break*/, 11]; - case 15: + return [3 /*break*/, 17]; + case 21: Logger.log("Checking if we need to update ".concat(CONTENTFUL_ALIAS, " alias")); - if (!(environmentType === CONTENTFUL_ALIAS && SET_ALIAS)) return [3 /*break*/, 17]; + if (!(environmentType === CONTENTFUL_ALIAS && SET_ALIAS)) return [3 /*break*/, 23]; Logger.log("Running on ".concat(CONTENTFUL_ALIAS, ".")); Logger.log("Updating ".concat(CONTENTFUL_ALIAS, " alias.")); return [4 /*yield*/, space @@ -30592,37 +30619,37 @@ var runAction = function (space) { return Object(tslib.__awaiter)(void 0, void 0 }) .then(function (alias) { return Logger.success("alias ".concat(alias.sys.id, " updated.")); }) .catch(Logger.error)]; - case 16: + case 22: _d.sent(); - return [3 /*break*/, 18]; - case 17: + return [3 /*break*/, 24]; + case 23: Logger.verbose("Running on feature branch"); Logger.verbose("No alias changes required"); - _d.label = 18; - case 18: + _d.label = 24; + case 24: if (!(DELETE_FEATURE && branchNames.baseRef === branchNames.defaultBranch && - ((_c = github.context.payload.pull_request) === null || _c === void 0 ? void 0 : _c.merged))) return [3 /*break*/, 23]; - _d.label = 19; - case 19: - _d.trys.push([19, 22, , 23]); + ((_c = github.context.payload.pull_request) === null || _c === void 0 ? void 0 : _c.merged))) return [3 /*break*/, 29]; + _d.label = 25; + case 25: + _d.trys.push([25, 28, , 29]); environmentIdToDelete = getNameFromPattern(FEATURE_PATTERN, { branchName: branchNames.headRef, }); Logger.log("Delete the environment: ".concat(environmentIdToDelete)); return [4 /*yield*/, space.getEnvironment(environmentIdToDelete)]; - case 20: + case 26: environment_1 = _d.sent(); return [4 /*yield*/, (environment_1 === null || environment_1 === void 0 ? void 0 : environment_1.delete())]; - case 21: + case 27: _d.sent(); Logger.success("Deleted the environment: ".concat(environmentIdToDelete)); - return [3 /*break*/, 23]; - case 22: - error_1 = _d.sent(); + return [3 /*break*/, 29]; + case 28: + error_2 = _d.sent(); Logger.error("Cannot delete the environment"); - return [3 /*break*/, 23]; - case 23: + return [3 /*break*/, 29]; + case 29: // Set the outputs for further actions Object(core.setOutput)("environment_url", "https://app.contentful.com/spaces/".concat(space.sys.id, "/environments/").concat(environmentId)); Object(core.setOutput)("environment_name", environmentId); diff --git a/src/action.ts b/src/action.ts index 4ffbadfb..cc3099e0 100644 --- a/src/action.ts +++ b/src/action.ts @@ -4,7 +4,7 @@ import { runMigration } from "contentful-migration/built/bin/cli"; import { readdir } from "fs"; import path from "path"; import { promisify } from "util"; -import toSemver from 'to-semver' +import toSemver from "to-semver"; import { CONTENTFUL_ALIAS, @@ -83,15 +83,36 @@ export const runAction = async (space): Promise => { ); Logger.verbose("Set default locale to new environment"); - const defaultLocale = (await environment.getLocales()).items.find( - (locale) => locale.default - ).code; + + let defaultLocale: string; + count = 0; + while (count < MAX_NUMBER_OF_TRIES) { + try { + const locales = await environment.getLocales(); + + Logger.success( + `Successfully retrieved locales: ${locales.items.map(({ code }) => code).join(', ')}` + ); + + defaultLocale = locales.items.find((locale) => locale.default).code; + break; + } catch (error) { + Logger.warn("Retrying retrieval of locales"); + await delay(); + count++; + } + } + + if (!defaultLocale) { + throw new Error("Unable to set default locale to new environment"); + } Logger.verbose("Read all the available migrations from the file system"); // Check for available migrations // Migration scripts need to be sorted in order to run without conflicts const availableMigrations = toSemver( - (await readdirAsync(MIGRATIONS_DIR)).map((file) => filenameToVersion(file)), {clean: false} + (await readdirAsync(MIGRATIONS_DIR)).map((file) => filenameToVersion(file)), + { clean: false } ).reverse(); Logger.verbose( @@ -122,9 +143,8 @@ export const runAction = async (space): Promise => { storedVersionEntry.fields[VERSION_FIELD][defaultLocale]; Logger.verbose("Evaluate which migrations to run"); - const currentMigrationIndex = availableMigrations.indexOf( - currentVersionString - ); + const currentMigrationIndex = + availableMigrations.indexOf(currentVersionString); // If the migration can't be found // Then abort