From 4fa20a56eafcab9d8675baa2f0fe6a9c6ee5e184 Mon Sep 17 00:00:00 2001 From: Piotr Grzesik Date: Wed, 22 Dec 2021 10:06:20 +0100 Subject: [PATCH] feat(Telemetry): Report `didCreateService` property --- lib/cli/interactive-setup/deploy.js | 4 ++-- lib/cli/interactive-setup/index.js | 5 +---- lib/plugins/aws/deploy/lib/createStack.js | 1 + lib/plugins/aws/lib/updateStack.js | 1 + lib/utils/telemetry/generatePayload.js | 12 +++++------ scripts/serverless.js | 12 ++++++----- .../lib/cli/interactive-setup/deploy.test.js | 20 ------------------- .../utils/telemetry/generatePayload.test.js | 7 ++++--- 8 files changed, 22 insertions(+), 40 deletions(-) diff --git a/lib/cli/interactive-setup/deploy.js b/lib/cli/interactive-setup/deploy.js index 8f9c0112348..e1c281c1e19 100644 --- a/lib/cli/interactive-setup/deploy.js +++ b/lib/cli/interactive-setup/deploy.js @@ -184,7 +184,7 @@ module.exports = { await serverless.init(); delete serverless.isLocallyInstalled; await serverless.run(); - context.awsAccountId = serverless.getProvider('aws').accountId; + context.serverless = serverless; } else { try { await overrideStdoutWrite( @@ -199,7 +199,7 @@ module.exports = { // Remove previously set `isLocallyInstalled` as it was only needed to avoid local fallback in `init()` delete serverless.isLocallyInstalled; await serverless.run(); - context.awsAccountId = serverless.getProvider('aws').accountId; + context.serverless = serverless; } ); } catch (err) { diff --git a/lib/cli/interactive-setup/index.js b/lib/cli/interactive-setup/index.js index aeadd596b63..2c12edcda86 100644 --- a/lib/cli/interactive-setup/index.js +++ b/lib/cli/interactive-setup/index.js @@ -57,8 +57,5 @@ module.exports = async (context) => { } } - return { - configuration: context.configuration, - awsAccountId: context.awsAccountId, - }; + return context; }; diff --git a/lib/plugins/aws/deploy/lib/createStack.js b/lib/plugins/aws/deploy/lib/createStack.js index fd16257e487..0f9a4789ee8 100644 --- a/lib/plugins/aws/deploy/lib/createStack.js +++ b/lib/plugins/aws/deploy/lib/createStack.js @@ -58,6 +58,7 @@ module.exports = { params.DisableRollback = this.serverless.service.provider.disableRollback; } + this.provider.didCreateService = true; return this.provider .request('CloudFormation', 'createStack', params) .then((cfData) => this.monitorStack('create', cfData)); diff --git a/lib/plugins/aws/lib/updateStack.js b/lib/plugins/aws/lib/updateStack.js index 1313705e092..729bd7c108b 100644 --- a/lib/plugins/aws/lib/updateStack.js +++ b/lib/plugins/aws/lib/updateStack.js @@ -67,6 +67,7 @@ module.exports = { params.DisableRollback = this.serverless.service.provider.disableRollback; } + this.provider.didCreateService = true; return this.provider .request('CloudFormation', 'createStack', params) .then((cfData) => this.monitorStack('create', cfData)); diff --git a/lib/utils/telemetry/generatePayload.js b/lib/utils/telemetry/generatePayload.js index cfb6bb1c279..e0f16779709 100644 --- a/lib/utils/telemetry/generatePayload.js +++ b/lib/utils/telemetry/generatePayload.js @@ -138,7 +138,6 @@ module.exports = ({ serverless, commandUsage, variableSources, - awsAccountId, }) => { let commandDurationMs; @@ -301,20 +300,21 @@ module.exports = ({ payload.isConfigValid = getConfigurationValidationResult(configuration); payload.dashboard.orgUid = serverless && serverless.service.orgUid; - if ( - isAwsProvider && - ((serverless && command === 'deploy') || (command === '' && awsAccountId)) - ) { + if (isAwsProvider && serverless && (command === 'deploy' || command === '')) { const serviceName = isObject(configuration.service) ? configuration.service.name : configuration.service; - const accountId = awsAccountId || (serverless && serverless.getProvider('aws').accountId); + const accountId = serverless && serverless.getProvider('aws').accountId; if (serviceName && accountId) { payload.projectId = crypto .createHash('sha256') .update(`${serviceName}-${accountId}`) .digest('base64'); } + + payload.didCreateService = Boolean( + serverless && serverless.getProvider('aws').didCreateService + ); } } diff --git a/scripts/serverless.js b/scripts/serverless.js index ebae14697f2..e1bd5ddc8ae 100755 --- a/scripts/serverless.js +++ b/scripts/serverless.js @@ -514,7 +514,6 @@ const processSpanPromise = (async () => { const isStandaloneCommand = notIntegratedCommands.has(command); if (!isHelpRequest && (isInteractiveSetup || isStandaloneCommand)) { - let interactiveResult; if (configuration) require('../lib/cli/ensure-supported-command')(configuration); if (isInteractiveSetup) { if (!process.stdin.isTTY && !process.env.SLS_INTERACTIVE_SETUP_ENABLE) { @@ -524,15 +523,18 @@ const processSpanPromise = (async () => { 'INTERACTIVE_SETUP_IN_NON_TTY' ); } - interactiveResult = await require('../lib/cli/interactive-setup')({ + const interactiveContext = await require('../lib/cli/interactive-setup')({ configuration, serviceDir, configurationFilename, options, commandUsage, }); - if (interactiveResult.configuration) { - configuration = interactiveResult.configuration; + if (interactiveContext.configuration) { + configuration = interactiveContext.configuration; + } + if (interactiveContext.serverless) { + serverless = interactiveContext.serverless; } } else { await require(`../commands/${commands.join('-')}`)({ @@ -559,7 +561,7 @@ const processSpanPromise = (async () => { configuration, commandUsage, variableSources: variableSourcesInConfig, - awsAccountId: interactiveResult.awsAccountId, + serverless, }), outcome: 'success', }); diff --git a/test/unit/lib/cli/interactive-setup/deploy.test.js b/test/unit/lib/cli/interactive-setup/deploy.test.js index d6d59d1e1a9..271ea53d1ed 100644 --- a/test/unit/lib/cli/interactive-setup/deploy.test.js +++ b/test/unit/lib/cli/interactive-setup/deploy.test.js @@ -215,11 +215,6 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => { ], dashboardPlugin: {}, }; - this.getProvider = () => { - return { - accountId: '123', - }; - }; } } @@ -281,11 +276,6 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => { ], dashboardPlugin: {}, }; - this.getProvider = () => { - return { - accountId: '123', - }; - }; } } @@ -346,11 +336,6 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => { }, ], }; - this.getProvider = () => { - return { - accountId: '123', - }; - }; } } @@ -403,11 +388,6 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => { }, ], }; - this.getProvider = () => { - return { - accountId: '123', - }; - }; } } diff --git a/test/unit/lib/utils/telemetry/generatePayload.test.js b/test/unit/lib/utils/telemetry/generatePayload.test.js index 3e5c7dd9cc4..64e9590d3c8 100644 --- a/test/unit/lib/utils/telemetry/generatePayload.test.js +++ b/test/unit/lib/utils/telemetry/generatePayload.test.js @@ -681,7 +681,7 @@ describe('test/unit/lib/utils/telemetry/generatePayload.test.js', () => { expect(payload.projectId).to.deep.equal('35dsFwCaexwLHppAP4uDsjKW4ci54q1AKcN5JTNaDtw='); }); - it('Should correctly resolve projectId property when account passed externally', async () => { + it('Should correctly resolve `didCreateService` property', async () => { const { serverless } = await runServerless({ fixture: 'httpApi', command: 'print', @@ -689,15 +689,16 @@ describe('test/unit/lib/utils/telemetry/generatePayload.test.js', () => { service: 'to-ensure-unique-serivce-name', }, }); + serverless.getProvider('aws').didCreateService = true; const payload = generatePayload({ command: '', options: {}, commandSchema: commandsSchema.get('deploy'), serviceDir: serverless.serviceDir, configuration: serverless.configurationInput, - awsAccountId: '1234567890', + serverless, }); - expect(payload.projectId).to.deep.equal('35dsFwCaexwLHppAP4uDsjKW4ci54q1AKcN5JTNaDtw='); + expect(payload.didCreateService).to.be.true; }); });