Skip to content

Commit

Permalink
refactor(Telemetry): Ensure to report projectId for interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
pgrzesik committed Dec 23, 2021
1 parent 0190d0d commit 08b5acb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/cli/interactive-setup/deploy.js
Expand Up @@ -142,7 +142,8 @@ module.exports = {
context.inapplicabilityReasonCode = 'NO_CREDENTIALS_CONFIGURED';
return false;
},
async run({ initial, configuration, configurationFilename, serviceDir, stepHistory }) {
async run(context) {
const { initial, configuration, configurationFilename, serviceDir, stepHistory } = context;
const serviceName = configuration.service;
const shouldDeploy = await promptWithHistory({
name: 'shouldDeploy',
Expand Down Expand Up @@ -183,6 +184,7 @@ module.exports = {
await serverless.init();
delete serverless.isLocallyInstalled;
await serverless.run();
context.awsAccountId = serverless.getProvider('aws').accountId;
} else {
try {
await overrideStdoutWrite(
Expand All @@ -197,6 +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;
}
);
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions lib/cli/interactive-setup/index.js
Expand Up @@ -59,5 +59,6 @@ module.exports = async (context) => {

return {
configuration: context.configuration,
awsAccountId: context.awsAccountId,
};
};
8 changes: 6 additions & 2 deletions lib/utils/telemetry/generatePayload.js
Expand Up @@ -138,6 +138,7 @@ module.exports = ({
serverless,
commandUsage,
variableSources,
awsAccountId,
}) => {
let commandDurationMs;

Expand Down Expand Up @@ -300,11 +301,14 @@ module.exports = ({
payload.isConfigValid = getConfigurationValidationResult(configuration);
payload.dashboard.orgUid = serverless && serverless.service.orgUid;

if (serverless && command === 'deploy' && isAwsProvider) {
if (
isAwsProvider &&
((serverless && command === 'deploy') || (command === '' && awsAccountId))
) {
const serviceName = isObject(configuration.service)
? configuration.service.name
: configuration.service;
const accountId = serverless.getProvider('aws').accountId;
const accountId = awsAccountId || (serverless && serverless.getProvider('aws').accountId);
if (serviceName && accountId) {
payload.projectId = crypto
.createHash('sha256')
Expand Down
8 changes: 5 additions & 3 deletions scripts/serverless.js
Expand Up @@ -514,6 +514,7 @@ 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 @@ -523,15 +524,15 @@ const processSpanPromise = (async () => {
'INTERACTIVE_SETUP_IN_NON_TTY'
);
}
const result = await require('../lib/cli/interactive-setup')({
interactiveResult = await require('../lib/cli/interactive-setup')({
configuration,
serviceDir,
configurationFilename,
options,
commandUsage,
});
if (result.configuration) {
configuration = result.configuration;
if (interactiveResult.configuration) {
configuration = interactiveResult.configuration;
}
} else {
await require(`../commands/${commands.join('-')}`)({
Expand All @@ -558,6 +559,7 @@ const processSpanPromise = (async () => {
configuration,
commandUsage,
variableSources: variableSourcesInConfig,
awsAccountId: interactiveResult.awsAccountId,
}),
outcome: 'success',
});
Expand Down
20 changes: 20 additions & 0 deletions test/unit/lib/cli/interactive-setup/deploy.test.js
Expand Up @@ -215,6 +215,11 @@ describe('test/unit/lib/cli/interactive-setup/deploy.test.js', () => {
],
dashboardPlugin: {},
};
this.getProvider = () => {
return {
accountId: '123',
};
};
}
}

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

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

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

Expand Down
20 changes: 20 additions & 0 deletions test/unit/lib/utils/telemetry/generatePayload.test.js
Expand Up @@ -680,4 +680,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 () => {
const { serverless } = await runServerless({
fixture: 'httpApi',
command: 'print',
configExt: {
service: 'to-ensure-unique-serivce-name',
},
});
const payload = generatePayload({
command: '',
options: {},
commandSchema: commandsSchema.get('deploy'),
serviceDir: serverless.serviceDir,
configuration: serverless.configurationInput,
awsAccountId: '1234567890',
});

expect(payload.projectId).to.deep.equal('35dsFwCaexwLHppAP4uDsjKW4ci54q1AKcN5JTNaDtw=');
});
});

0 comments on commit 08b5acb

Please sign in to comment.