Skip to content

Commit

Permalink
added defensive environment variable checks where appropriate, remove…
Browse files Browse the repository at this point in the history
…d some debugging
  • Loading branch information
mattb-hippo committed May 2, 2024
1 parent e7b415e commit b649708
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
27 changes: 21 additions & 6 deletions Contentful-Schema/utils/create-initial-migration-version.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import contentful from 'contentful-management';
import chalk from 'chalk';
import core from '@actions/core';

console.log('space id: ', process.env.SPACE_ID);
const red = chalk.bold.red;
const managementToken = process.env.MANAGEMENT_TOKEN;
const spaceId = process.env.SPACE_ID;
const contentfulAlias = process.env.ENVIRONMENT;

try {
if (!managementToken) throw new Error("Environment variable MANAGEMENT_TOKEN not set");
if (!spaceId) throw new Error("Environment variable SPACE_ID not set");
if (!contentfulAlias) throw new Error("Environment variable ENVIRONMENT not set");
}
catch (e) {
core.setFailed(red(e));
process.exit();
}

var client = contentful.createClient(
{ accessToken: process.env.MANAGEMENT_TOKEN },
{ accessToken: managementToken },
{ type: 'plain' }
);

const newMigrationVersionEntry = await client.entry.create ({
environmentId: process.env.ENVIRONMENT,
spaceId: process.env.SPACE_ID,
environmentId: contentfulAlias,
spaceId: spaceId,
contentTypeId: 'migrationVersion'
}, {})

await client.entry.publish({
environmentId: process.env.ENVIRONMENT,
spaceId: process.env.SPACE_ID,
environmentId: contentfulAlias,
spaceId: spaceId,
entryId: newMigrationVersionEntry.sys.id
}, newMigrationVersionEntry);
19 changes: 16 additions & 3 deletions Contentful-Schema/utils/get-environment-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@ import chalk from 'chalk';
import core from '@actions/core';

const red = chalk.bold.red;
const spaceId = process.env.SPACE_ID;
const contentfulAlias = process.env.ENVIRONMENT;
const deliveryKey = process.env.DELIVERY_KEY;

try {
if (!deliveryKey) throw new Error("Environment variable DELIVERY_KEY not set");
if (!spaceId) throw new Error("Environment variable SPACE_ID not set");
if (!contentfulAlias) throw new Error("Environment variable ENVIRONMENT not set");
}
catch (e) {
core.setFailed(red(e));
process.exit();
}

var client = contentful.createClient({
accessToken: process.env.DELIVERY_KEY,
space: process.env.SPACE_ID,
environment: process.env.ENVIRONMENT
accessToken: deliveryKey,
space: spaceId,
environment: contentfulAlias
});

client.getEntries({ content_type: 'migrationVersion' }).then(entries => {
Expand Down
2 changes: 1 addition & 1 deletion Contentful-Schema/utils/get-required-migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const isMigration = filename => {
try {
await access(migrationsDir);
}
catch (error) {
catch (error) {
core.setFailed(chalk.red("Migrations directory doesn't exist"));
process.exit();
}
Expand Down
15 changes: 14 additions & 1 deletion Contentful-Schema/utils/set-environment-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ import contentful from 'contentful-management';
import core from '@actions/core';
import chalk from 'chalk';

const red = chalk.bold.red;
const managementToken = process.env.MANAGEMENT_TOKEN;
const stagingEnvironment = process.env.STAGING_ENVIRONMENT;
const spaceId = process.env.SPACE_ID;
const migrationFilename = process.env.MIGRATION_FILENAME

try {
if (!managementToken) throw new Error("Environment variable MANAGEMENT_TOKEN not set");
if (!stagingEnvironment) throw new Error("Environment variable STAGING_ENVIRONMENT not set");
if (!spaceId) throw new Error("Environment variable SPACE_ID not set");
if (!migrationFilename) throw new Error("Environment variable MIGRATION_FILENAME not set");
}
catch (e) {
core.setFailed(red(e));
process.exit();
}

const newVersion = parseInt(migrationFilename.split('-')[0]);

var client = contentful.createClient(
Expand All @@ -22,7 +35,7 @@ var entries = await client.entry.getMany({
});

if (entries.total !== 1) {
core.setFailed (chalk.red('Migration version record missing or not unique'));
core.setFailed (red('Migration version record missing or not unique'));
process.exit();
}

Expand Down
16 changes: 13 additions & 3 deletions Contentful-Schema/utils/verify-space-capacity.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ const managementToken = process.env.MANAGEMENT_TOKEN;
const spaceId = process.env.SPACE_ID;
const spaceCapacity = process.env.SPACE_CAPACITY;

const red = chalk.bold.red;

try {
if (!managementToken) throw new Error("Environment variable MANAGEMENT_TOKEN not set");
if (!spaceId) throw new Error("Environment variable SPACE_ID not set");
if (!spaceCapacity) throw new Error("Environment variable SPACE_CAPACITY not set");
}
catch (e) {
core.setFailed(red(e));
process.exit();
}

const client = contentful.createClient({
accessToken: managementToken
}, { type: 'plain' })
Expand All @@ -13,11 +25,9 @@ const environments = await client.environment.getMany({
spaceId: spaceId
});

console.log('total environments: ' + environments.items.length);
const actualEnvironments = environments.items.filter(obj => !("aliasedEnvironment" in obj.sys));
const environmentsInUse = actualEnvironments.length;
console.log('actual environments: ' + environmentsInUse);
console.log('space capacity: ' + spaceCapacity);

if (environmentsInUse >= spaceCapacity) {
core.setFailed('Contentful space has insufficient environment capacity. Configured max capacity is ' + spaceCapacity + ', current environment count is ' + environmentsInUse);
}

0 comments on commit b649708

Please sign in to comment.