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

Commit

Permalink
removing current_version from action result (#145)
Browse files Browse the repository at this point in the history
* removing current_version from action result

* 5.6.0
  • Loading branch information
luisbritos authored Apr 29, 2021
1 parent c29de35 commit 6fbb382
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 95 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.5.1",
"version": "5.6.0",
"description": "Supporting tools for the Source Control extensions",
"main": "lib/index.js",
"scripts": {
Expand Down
136 changes: 63 additions & 73 deletions src/auth0/handlers/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ 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 = {
type: 'array',
items: {
type: 'object',
required: [ 'name', 'supported_triggers', 'code', 'runtime' ],
required: [ 'name', 'supported_triggers', 'code' ],
additionalProperties: false,
properties: {
code: { type: 'string', default: '' },
Expand All @@ -29,7 +30,6 @@ export const schema = {
}
},
status: { type: 'string', default: '' },
runtime: { type: 'string', default: '' },
secrets: {
type: 'array',
items: {
Expand Down Expand Up @@ -83,36 +83,7 @@ export const schema = {
}
}
},
current_version: {
type: 'object',
properties: {
code: { type: 'string', default: '' },
dependencies: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
properties: {
name: { type: 'string' },
version: { type: 'string' },
registry_url: { type: 'string' }
}
}
},
runtime: { type: 'string', default: '' },
secrets: {
type: 'array',
items: {
type: 'object',
properties: {
name: { type: 'string' },
value: { type: 'string' },
updated_at: { type: 'string', format: 'date-time' }
}
}
}
}
}
deployed: { type: 'boolean' }
}
}
};
Expand All @@ -131,6 +102,18 @@ function mapCurrentVersion(currentVersion) {
}
}

function mapAction(action, version) {
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)
};
}

async function waitUntilVersionIsDeployed(client, actionId, versionId, retries) {
const version = await client.actions.getVersion({ action_id: actionId, version_id: versionId });
if (retries > 0 && !version.deployed) {
Expand Down Expand Up @@ -187,11 +170,11 @@ export default class ActionHandler extends DefaultHandler {
// 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 => ({ ...action, secrets: mapSecrets(action.secrets), current_version: mapCurrentVersion(currentVersion) }))));
.then(async currentVersion => mapAction(action, currentVersion))));
return this.existing;
} catch (err) {
if (err.statusCode === 404 || err.statusCode === 501) {
return [];
return null;
}
throw err;
}
Expand All @@ -203,35 +186,38 @@ export default class ActionHandler extends DefaultHandler {
const versionToCreate = {
code: version.code,
dependencies: version.dependencies,
secrets: version.secrets.filter(secret => secret.value !== HIDDEN_SECRET_VALUE),
runtime: version.runtime
runtime: DEFAULT_RUNTIME
};
const newVersion = await this.client.actions.createVersion({ action_id: actionId }, versionToCreate);

// wait WAIT_FOR_DEPLOY seconds for version deploy, if can't deploy an error will arise
await waitUntilVersionIsDeployed(this.client, actionId, newVersion.id, WAIT_FOR_DEPLOY);

// Update draft version
await this.client.actions.update({ action_id: actionId }, versionToCreate);

return newVersion;
}

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

// Figure out what needs to be deleted or created
if (!currentVersionAssets && !existing) {
return { create };
if (actionAsset.deployed) {
const versionToCreate = {
action_id: actionId,
code: actionAsset.code,
dependencies: actionAsset.dependencies
};
if (existingVersion) {
// name or secrets modifications are not supported yet
if (actionAsset.code !== existingVersion.code || !areArraysEquals(actionAsset.dependencies, existingVersion.dependencies)) {
create.push(versionToCreate);
}
} else {
create.push(versionToCreate);
}
}

if (currentVersionAssets && !existing) {
create.push({ ...currentVersionAssets, action_id: actionId });
return { create };
}
if (currentVersionAssets.code !== existing.code
|| currentVersionAssets.runtime !== existing.runtime
|| !areArraysEquals(currentVersionAssets.dependencies, existing.dependencies)
|| !areArraysEquals((currentVersionAssets.secrets || []).map(s => s.name), (existing.secrets || []).map(s => s.name))) {
create.push({ ...currentVersionAssets, action_id: actionId });
}
return {
create: create
};
Expand Down Expand Up @@ -267,44 +253,49 @@ export default class ActionHandler extends DefaultHandler {
async actionChanges(action, found) {
const actionChanges = {};

if (action.name !== found.name) {
actionChanges.name = action.name;
}
if (action.code !== found.code) {
actionChanges.code = action.code;
}
// if action is deployed, should compare against curren_version - calcCurrentVersionChanges method
if (!action.deployed) {
// name or secrets modifications are not supported yet
if (action.code !== found.code) {
actionChanges.code = action.code;
}

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

if (!areArraysEquals((action.secrets || []).map(s => s.name), (found.secrets || []).map(s => s.name))) {
actionChanges.secrets = action.secrets;
if (!areArraysEquals(action.required_configuration, found.required_configuration)) {
actionChanges.required_configuration = action.required_configuration;
}

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

return actionChanges;
}

async createAction(data) {
const action = { ...data };
const currentVersion = action.current_version;
// eslint-disable-next-line prefer-destructuring
const actionToCreate = {
name: action.name,
supported_triggers: action.supported_triggers,
code: action.code,
dependencies: action.dependencies,
secrets: action.secrets,
runtime: action.runtime
runtime: DEFAULT_RUNTIME
};

const created = await this.client.actions.create(actionToCreate);
if (currentVersion) {
await this.createVersions([ { ...currentVersion, action_id: created.id } ]);

// if action.deployed is true an actionVersion should be created
if (action.deployed) {
await this.createVersions([ {
code: action.code,
dependencies: action.dependencies,
action_id: created.id
} ]);
}
return created;
}
Expand Down Expand Up @@ -346,7 +337,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.current_version, found.current_version);
const currentVersionChanges = await this.calcCurrentVersionChanges(found.id, action, found.current_version);
if (currentVersionChanges.create.length > 0) {
await this.processVersionsChanges(currentVersionChanges);
}
Expand Down Expand Up @@ -380,12 +371,11 @@ 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.current_version, found.current_version);
if (action.name !== found.name
|| action.code !== found.code
const currentVersionChanges = await this.calcCurrentVersionChanges(found.id, action, found.current_version);
if (action.code !== found.code
|| !areArraysEquals(action.dependencies, found.dependencies)
|| !areArraysEquals((action.secrets || []).map(s => s.name), (found.secrets || []).map(s => s.name))
|| action.runtime !== found.runtime
|| !areArraysEquals(action.required_configuration, found.required_configuration)
|| !areArraysEquals(action.supported_triggers, found.supported_triggers)
|| currentVersionChanges.create.length > 0) {
update.push(action);
}
Expand Down
27 changes: 13 additions & 14 deletions src/auth0/handlers/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,26 @@ export default class TriggersHandler extends DefaultHandler {
return [];
}

const res = await this.client.actions.getAllTriggers();

const triggers = _(res.triggers).map('id').uniq().value();

const triggerBindings = {};

for (let i = 0; i < triggers.length; i++) {
const triggerId = triggers[i];
try {
const res = await this.client.actions.getAllTriggers();
const triggers = _(res.triggers).map('id').uniq().value();

try {
for (let i = 0; i < triggers.length; i++) {
const triggerId = triggers[i];
const { bindings } = await this.client.actions.getTriggerBindings({ trigger_id: triggerId });
triggerBindings[triggerId] = bindings.map(binding => ({ action_name: binding.action.name, display_name: binding.display_name }));
} catch (err) {
if (err.statusCode === 404 || err.statusCode === 501) {
return null;
}
throw err;
}
}

return triggerBindings;
this.existing = triggerBindings;
return this.existing;
} catch (err) {
if (err.statusCode === 404 || err.statusCode === 501) {
return null;
}
throw err;
}
}

async updateTrigger(updates) {
Expand Down
16 changes: 10 additions & 6 deletions tests/auth0/handlers/actions.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,18 @@ describe('#actions handler', () => {
dependencies: [],
id: 'version-id',
runtime: 'node12',
secrets: []
secrets: [],
status: 'build'
};

const actionsData = [
{
id: 'action-id-1',
name: 'action-test-1',
secrets: [],
dependencies: [],
code: code,
status: 'build',
supported_triggers: [
{
id: 'post-login',
Expand All @@ -196,10 +200,10 @@ describe('#actions handler', () => {

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

it('should return an empty array for 501 status code', async () => {
it('should return an null for 501 status code', async () => {
const auth0 = {
actions: {
getAll: () => {
Expand All @@ -213,10 +217,10 @@ describe('#actions handler', () => {

const handler = new actions.default({ client: auth0, config });
const data = await handler.getType();
expect(data).to.deep.equal([]);
expect(data).to.deep.equal(null);
});

it('should return an empty array for 404 status code', async () => {
it('should return an null for 404 status code', async () => {
const auth0 = {
actions: {
getAll: () => {
Expand All @@ -230,7 +234,7 @@ describe('#actions handler', () => {

const handler = new actions.default({ client: auth0, config });
const data = await handler.getType();
expect(data).to.deep.equal([]);
expect(data).to.deep.equal(null);
});

it('should throw an error for all other failed requests', async () => {
Expand Down

0 comments on commit 6fbb382

Please sign in to comment.