Skip to content

Commit

Permalink
feat(Telemetry): Report didCreateService property
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Dec 23, 2021
1 parent 08b5acb commit 4fa20a5
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 40 deletions.
4 changes: 2 additions & 2 deletions lib/cli/interactive-setup/deploy.js
Expand Up @@ -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(
Expand All @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions lib/cli/interactive-setup/index.js
Expand Up @@ -57,8 +57,5 @@ module.exports = async (context) => {
}
}

return {
configuration: context.configuration,
awsAccountId: context.awsAccountId,
};
return context;
};
1 change: 1 addition & 0 deletions lib/plugins/aws/deploy/lib/createStack.js
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions lib/plugins/aws/lib/updateStack.js
Expand Up @@ -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));
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/telemetry/generatePayload.js
Expand Up @@ -138,7 +138,6 @@ module.exports = ({
serverless,
commandUsage,
variableSources,
awsAccountId,
}) => {
let commandDurationMs;

Expand Down Expand Up @@ -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
);
}
}

Expand Down
12 changes: 7 additions & 5 deletions scripts/serverless.js
Expand Up @@ -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) {
Expand All @@ -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('-')}`)({
Expand All @@ -559,7 +561,7 @@ const processSpanPromise = (async () => {
configuration,
commandUsage,
variableSources: variableSourcesInConfig,
awsAccountId: interactiveResult.awsAccountId,
serverless,
}),
outcome: 'success',
});
Expand Down
20 changes: 0 additions & 20 deletions test/unit/lib/cli/interactive-setup/deploy.test.js
Expand Up @@ -215,11 +215,6 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => {
],
dashboardPlugin: {},
};
this.getProvider = () => {
return {
accountId: '123',
};
};
}
}

Expand Down Expand Up @@ -281,11 +276,6 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => {
],
dashboardPlugin: {},
};
this.getProvider = () => {
return {
accountId: '123',
};
};
}
}

Expand Down Expand Up @@ -346,11 +336,6 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => {
},
],
};
this.getProvider = () => {
return {
accountId: '123',
};
};
}
}

Expand Down Expand Up @@ -403,11 +388,6 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => {
},
],
};
this.getProvider = () => {
return {
accountId: '123',
};
};
}
}

Expand Down
7 changes: 4 additions & 3 deletions test/unit/lib/utils/telemetry/generatePayload.test.js
Expand Up @@ -681,23 +681,24 @@ 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',
configExt: {
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;
});
});

0 comments on commit 4fa20a5

Please sign in to comment.