From 6fbb382a91731d8e9edba3145a8716f24fe9adfa Mon Sep 17 00:00:00 2001 From: luisbritos <60328073+luisbritos@users.noreply.github.com> Date: Thu, 29 Apr 2021 11:48:00 -0300 Subject: [PATCH] removing current_version from action result (#145) * removing current_version from action result * 5.6.0 --- package-lock.json | 2 +- package.json | 2 +- src/auth0/handlers/actions.js | 136 ++++++++++++-------------- src/auth0/handlers/triggers.js | 27 +++-- tests/auth0/handlers/actions.tests.js | 16 +-- 5 files changed, 88 insertions(+), 95 deletions(-) diff --git a/package-lock.json b/package-lock.json index c345752..a41649f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "auth0-source-control-extension-tools", - "version": "5.5.1", + "version": "5.6.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 13a9437..21453cd 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/auth0/handlers/actions.js b/src/auth0/handlers/actions.js index f0e999b..7d0e71d 100644 --- a/src/auth0/handlers/actions.js +++ b/src/auth0/handlers/actions.js @@ -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: '' }, @@ -29,7 +30,6 @@ export const schema = { } }, status: { type: 'string', default: '' }, - runtime: { type: 'string', default: '' }, secrets: { type: 'array', items: { @@ -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' } } } }; @@ -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) { @@ -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; } @@ -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 }; @@ -267,23 +253,24 @@ 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; @@ -291,20 +278,24 @@ export default class ActionHandler extends DefaultHandler { 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; } @@ -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); } @@ -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); } diff --git a/src/auth0/handlers/triggers.js b/src/auth0/handlers/triggers.js index 1e285bf..defb1a9 100644 --- a/src/auth0/handlers/triggers.js +++ b/src/auth0/handlers/triggers.js @@ -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) { diff --git a/tests/auth0/handlers/actions.tests.js b/tests/auth0/handlers/actions.tests.js index 97c059b..abff395 100644 --- a/tests/auth0/handlers/actions.tests.js +++ b/tests/auth0/handlers/actions.tests.js @@ -165,7 +165,8 @@ describe('#actions handler', () => { dependencies: [], id: 'version-id', runtime: 'node12', - secrets: [] + secrets: [], + status: 'build' }; const actionsData = [ @@ -173,6 +174,9 @@ describe('#actions handler', () => { id: 'action-id-1', name: 'action-test-1', secrets: [], + dependencies: [], + code: code, + status: 'build', supported_triggers: [ { id: 'post-login', @@ -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: () => { @@ -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: () => { @@ -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 () => {