Skip to content
This repository has been archived by the owner on Jul 31, 2021. It is now read-only.

Commit

Permalink
[DXEX-1467] update actions for the API2 changes (#146)
Browse files Browse the repository at this point in the history
* last api2 changes

* 5.7.0
  • Loading branch information
luisbritos authored May 7, 2021
1 parent 6fbb382 commit dc5248a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 67 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auth0-source-control-extension-tools",
"version": "5.6.0",
"version": "5.7.0",
"description": "Supporting tools for the Source Control extensions",
"main": "lib/index.js",
"scripts": {
Expand Down
82 changes: 22 additions & 60 deletions src/auth0/handlers/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { areArraysEquals } from '../../utils';

const WAIT_FOR_DEPLOY = 60; // seconds to wait for the version to deploy
const HIDDEN_SECRET_VALUE = '_VALUE_NOT_SHOWN_';
const DEFAULT_RUNTIME = 'node12';

// With this schema, we can only validate property types but not valid properties on per type basis
export const schema = {
Expand Down Expand Up @@ -53,36 +52,6 @@ export const schema = {
}
}
},
required_configuration: {
type: 'array',
items: {
type: 'object',
required: [ 'name', 'label', 'type' ],
properties: {
name: { type: 'string' },
label: { type: 'string' },
type: { type: 'string' },
placeholder: { type: 'string' },
description: { type: 'string' },
default_value: { type: 'string' }
}
}
},
required_secrets: {
type: 'array',
items: {
type: 'object',
required: [ 'name', 'label', 'type' ],
properties: {
name: { type: 'string' },
label: { type: 'string' },
type: { type: 'string' },
placeholder: { type: 'string' },
description: { type: 'string' },
default_value: { type: 'string' }
}
}
},
deployed: { type: 'boolean' }
}
}
Expand All @@ -96,21 +65,21 @@ function mapSecrets(secrets) {
}
}

function mapCurrentVersion(currentVersion) {
if (currentVersion) {
return ({ ...currentVersion, secrets: mapSecrets(currentVersion.secrets) });
function mapDeployedVersion(deployedVersion) {
if (deployedVersion) {
return ({ ...deployedVersion, secrets: mapSecrets(deployedVersion.secrets) });
}
}

function mapAction(action, version) {
function mapAction(action, deployedVersion) {
return {
...action,
code: version ? version.code : action.code,
deployed: !!version,
secrets: version ? mapSecrets(version.secrets) : mapSecrets(action.secrets),
dependencies: version ? version.dependencies : action.dependencies,
status: version ? version.status : action.status,
current_version: mapCurrentVersion(version)
code: deployedVersion ? deployedVersion.code : action.code,
deployed: !!deployedVersion,
secrets: deployedVersion ? mapSecrets(deployedVersion.secrets) : mapSecrets(action.secrets),
dependencies: deployedVersion ? deployedVersion.dependencies : action.dependencies,
status: deployedVersion ? deployedVersion.status : action.status,
deployed_version: mapDeployedVersion(deployedVersion)
};
}

Expand All @@ -135,18 +104,18 @@ export default class ActionHandler extends DefaultHandler {
});
}

async getVersionById(actionId, currentVersion) {
async getVersionById(actionId, deployedVersion) {
// in case client version does not support actionVersions
if (typeof this.client.actions.getVersions !== 'function') {
return null;
}
// in case action doesn't have a current version yet
if (!currentVersion) {
if (!deployedVersion) {
return null;
}

try {
return await this.client.actions.getVersions({ action_id: actionId, version_id: currentVersion.id });
return await this.client.actions.getVersions({ action_id: actionId, version_id: deployedVersion.id });
} catch (err) {
if (err.statusCode === 404 || err.statusCode === 501) {
return null;
Expand All @@ -168,9 +137,9 @@ export default class ActionHandler extends DefaultHandler {
try {
const actions = await this.client.actions.getAll();
// need to get complete current version for each action
// the current_version inside the action doesn't have all the necessary information
this.existing = await Promise.all(actions.actions.map(action => this.getVersionById(action.id, action.current_version)
.then(async currentVersion => mapAction(action, currentVersion))));
// the deployed_version inside the action doesn't have all the necessary information
this.existing = await Promise.all(actions.actions.map(action => this.getVersionById(action.id, action.deployed_version)
.then(async deployedVersion => mapAction(action, deployedVersion))));
return this.existing;
} catch (err) {
if (err.statusCode === 404 || err.statusCode === 501) {
Expand All @@ -185,8 +154,7 @@ export default class ActionHandler extends DefaultHandler {
const actionId = version.action_id;
const versionToCreate = {
code: version.code,
dependencies: version.dependencies,
runtime: DEFAULT_RUNTIME
dependencies: version.dependencies
};
const newVersion = await this.client.actions.createVersion({ action_id: actionId }, versionToCreate);

Expand All @@ -199,7 +167,7 @@ export default class ActionHandler extends DefaultHandler {
return newVersion;
}

async calcCurrentVersionChanges(actionId, actionAsset, existingVersion) {
async calcDeployedVersionChanges(actionId, actionAsset, existingVersion) {
const create = [];

if (actionAsset.deployed) {
Expand Down Expand Up @@ -253,7 +221,7 @@ export default class ActionHandler extends DefaultHandler {
async actionChanges(action, found) {
const actionChanges = {};

// if action is deployed, should compare against curren_version - calcCurrentVersionChanges method
// if action is deployed, should compare against curren_version - calcDeployedVersionChanges method
if (!action.deployed) {
// name or secrets modifications are not supported yet
if (action.code !== found.code) {
Expand All @@ -265,10 +233,6 @@ export default class ActionHandler extends DefaultHandler {
}
}

if (!areArraysEquals(action.required_configuration, found.required_configuration)) {
actionChanges.required_configuration = action.required_configuration;
}

if (!areArraysEquals(action.supported_triggers, found.supported_triggers)) {
actionChanges.supported_triggers = action.supported_triggers;
}
Expand All @@ -283,8 +247,7 @@ export default class ActionHandler extends DefaultHandler {
name: action.name,
supported_triggers: action.supported_triggers,
code: action.code,
dependencies: action.dependencies,
runtime: DEFAULT_RUNTIME
dependencies: action.dependencies
};

const created = await this.client.actions.create(actionToCreate);
Expand Down Expand Up @@ -337,7 +300,7 @@ export default class ActionHandler extends DefaultHandler {
async updateAction(action, existing) {
const found = existing.find(existingAction => existingAction.name === action.name);
// update current version
const currentVersionChanges = await this.calcCurrentVersionChanges(found.id, action, found.current_version);
const currentVersionChanges = await this.calcDeployedVersionChanges(found.id, action, found.deployed_version);
if (currentVersionChanges.create.length > 0) {
await this.processVersionsChanges(currentVersionChanges);
}
Expand Down Expand Up @@ -371,10 +334,9 @@ export default class ActionHandler extends DefaultHandler {
if (found) {
del = del.filter(e => e.id !== found.id);
// current version changes
const currentVersionChanges = await this.calcCurrentVersionChanges(found.id, action, found.current_version);
const currentVersionChanges = await this.calcDeployedVersionChanges(found.id, action, found.deployed_version);
if (action.code !== found.code
|| !areArraysEquals(action.dependencies, found.dependencies)
|| !areArraysEquals(action.required_configuration, found.required_configuration)
|| !areArraysEquals(action.supported_triggers, found.supported_triggers)
|| currentVersionChanges.create.length > 0) {
update.push(action);
Expand Down
10 changes: 5 additions & 5 deletions tests/auth0/handlers/actions.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ describe('#actions handler', () => {
id: 'post-login',
version: 'v1'
} ],
current_version: {
deployed_version: {
code: 'some code',
dependencies: [],
secrets: [],
Expand All @@ -84,7 +84,7 @@ describe('#actions handler', () => {
id: 'post-login',
version: 'v1'
} ],
current_version: {
deployed_version: {
code: '/** @type {PostLoginAction} */\nmodule.exports = async (event, context) => {\n console.log(\'new version\');\n return {};\n };\n ',
dependencies: [],
secrets: [],
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('#actions handler', () => {
id: 'post-login',
version: 'v1'
} ],
current_version: {
deployed_version: {
code: 'some code',
dependencies: [],
secrets: [],
Expand Down Expand Up @@ -183,7 +183,7 @@ describe('#actions handler', () => {
version: 'v1'
}
],
current_version: { id: version.id }
deployed_version: { id: version.id }
}
];

Expand All @@ -200,7 +200,7 @@ describe('#actions handler', () => {

const handler = new actions.default({ client: auth0, config });
const data = await handler.getType();
expect(data).to.deep.equal([ { ...actionsData[0], deployed: true, current_version: version } ]);
expect(data).to.deep.equal([ { ...actionsData[0], deployed: true, deployed_version: version } ]);
});

it('should return an null for 501 status code', async () => {
Expand Down

0 comments on commit dc5248a

Please sign in to comment.