Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Setting environment default locale fails due to 404 #17

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 58 additions & 31 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
36 changes: 28 additions & 8 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -83,15 +83,36 @@ export const runAction = async (space): Promise<void> => {
);

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(
Expand Down Expand Up @@ -122,9 +143,8 @@ export const runAction = async (space): Promise<void> => {
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
Expand Down