Skip to content

Commit

Permalink
chore: refactor initsobjectsdefinitions and send logname in telemetry (
Browse files Browse the repository at this point in the history
…#5865)

* chore: refactor initsobjectsdefinitions and send logname in telemetry

* chore: pass sobjectRefreshStartup value i exception message

* chore: send error name and error message as message
  • Loading branch information
CristiCanizales authored Sep 20, 2024
1 parent 581b63c commit df53f10
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { Properties, Measurements, TelemetryData } from '@salesforce/vscode-service-provider';
import {
Properties,
Measurements,
TelemetryData
} from '@salesforce/vscode-service-provider';
import * as vscode from 'vscode';
import { CliCommandExecutor, Command, CommandExecution } from '../cli';
import {
TelemetryBuilder,
TelemetryService
} from '../index';
import { TelemetryBuilder, TelemetryService } from '../index';
import { nls } from '../messages';
import { SettingsService } from '../settings';
import { CommandletExecutor, ContinueResponse } from '../types';
Expand Down Expand Up @@ -118,7 +119,8 @@ export abstract class SfCommandletExecutor<T> implements CommandletExecutor<T> {
}

export abstract class LibraryCommandletExecutor<T>
implements CommandletExecutor<T> {
implements CommandletExecutor<T>
{
protected cancellable: boolean = false;
private cancelled: boolean = false;
private readonly executionName: string;
Expand Down Expand Up @@ -219,7 +221,10 @@ export abstract class LibraryCommandletExecutor<T>
);
} catch (e) {
if (e instanceof Error) {
telemetryService.sendException(e.name, e.message);
telemetryService.sendException(
`LibraryCommandletExecutor - ${this.logName}`,
`Error: name = ${e.name} message = ${e.message}`
);
notificationService.showFailedExecution(this.executionName);
channelService.appendLine(e.message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ export class OrgLogoutDefault extends LibraryCommandletExecutor<string> {
try {
await removeUsername(response.data);
} catch (e) {
telemetryService.sendException(e.name, e.message);
telemetryService.sendException(
'org_logout_default',
`Error: name = ${e.name} message = ${e.message}`
);
return false;
}
return true;
Expand All @@ -92,7 +95,7 @@ export class OrgLogoutDefault extends LibraryCommandletExecutor<string> {
export const orgLogoutDefault = async () => {
const { username, isScratch, alias, error } = await resolveTargetOrg();
if (error) {
telemetryService.sendException(error.name, error.message);
telemetryService.sendException('org_logout_default', error.message);
void notificationService.showErrorMessage('Logout failed to run');
} else if (username) {
// confirm logout for scratch orgs due to special considerations:
Expand Down
1 change: 0 additions & 1 deletion packages/salesforcedx-vscode-core/src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export {
} from './packageInstall';
export {
RefreshSObjectsExecutor,
checkSObjectsAndRefresh,
refreshSObjects,
initSObjectDefinitions
} from './refreshSObjects';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ export class ProjectDeployStartExecutor extends SfCommandletExecutor<{}> {
e.message =
'Error while creating diagnostics for vscode problem view.';
}
telemetryService.sendException(e.name, e.message);
telemetryService.sendException(
execution.command.logName,
`Error: name = ${e.name} message = ${e.message}`
);
console.error(e.message);
}
telemetry.addProperty('success', String(success));
Expand Down
52 changes: 17 additions & 35 deletions packages/salesforcedx-vscode-core/src/commands/refreshSObjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
import { channelService } from '../channels';
import { WorkspaceContext } from '../context';
import { nls } from '../messages';
import { telemetryService } from '../telemetry';

Expand Down Expand Up @@ -178,7 +177,10 @@ export class RefreshSObjectsExecutor extends SfCommandletExecutor<{}> {
);
} catch (error) {
console.log('Generate error ' + error.error);
telemetryService.sendException(error.name, error.error);
telemetryService.sendException(
'generate_faux_classes_create',
`Error: name = ${error.name} message = ${error.error}`
);
RefreshSObjectsExecutor.isActive = false;

throw error;
Expand All @@ -201,32 +203,28 @@ export const refreshSObjects = async (source?: SObjectRefreshSource) => {
await commandlet.run();
};

export const verifyUsernameAndInitSObjectDefinitions = async (
projectPath: string
export const initSObjectDefinitions = async (
projectPath: string,
isSettingEnabled: boolean
) => {
const hasTargetOrgSet =
(await WorkspaceContext.getInstance().getConnection()).getUsername() !==
undefined;
if (hasTargetOrgSet) {
initSObjectDefinitions(projectPath).catch(e =>
telemetryService.sendException(e.name, e.message)
);
}
};

export const initSObjectDefinitions = async (projectPath: string) => {
if (projectPath) {
const sobjectFolder = getSObjectsDirectory();
const sobjectFolder = isSettingEnabled
? getSObjectsDirectory()
: getStandardSObjectsDirectory();
const refreshSource = isSettingEnabled
? SObjectRefreshSource.Startup
: SObjectRefreshSource.StartupMin;

if (!fs.existsSync(sobjectFolder)) {
telemetryService.sendEventData(
'sObjectRefreshNotification',
{ type: SObjectRefreshSource.Startup },
{ type: refreshSource },
undefined
);
try {
await refreshSObjects(SObjectRefreshSource.Startup);
await refreshSObjects(refreshSource);
} catch (e) {
telemetryService.sendException(e.name, e.message);
telemetryService.sendException('initSObjectDefinitions', e.message);
throw e;
}
}
Expand All @@ -244,19 +242,3 @@ const getStandardSObjectsDirectory = () => {
STANDARDOBJECTS_DIR
);
};

export const checkSObjectsAndRefresh = async (projectPath: string) => {
if (projectPath && !fs.existsSync(getStandardSObjectsDirectory())) {
telemetryService.sendEventData(
'sObjectRefreshNotification',
{ type: SObjectRefreshSource.StartupMin },
undefined
);
try {
await refreshSObjects(SObjectRefreshSource.StartupMin);
} catch (e) {
telemetryService.sendException(e.name, e.message);
throw e;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ export const diffOneFile = async (
await notificationService.showErrorMessage(err.message);
channelService.appendLine(err.message);
channelService.showChannelOutput();
telemetryService.sendException(err.name, err.message);
telemetryService.sendException(
'source_diff_file',
`Error: name = ${err.name} message = ${err.message}`
);
}
return;
}
Expand Down
30 changes: 18 additions & 12 deletions packages/salesforcedx-vscode-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
apexGenerateClass,
apexGenerateTrigger,
apexGenerateUnitTestClass,
checkSObjectsAndRefresh,
configList,
configSet,
dataQuery,
Expand Down Expand Up @@ -118,7 +117,10 @@ import { orgBrowser } from './orgBrowser';
import { OrgList } from './orgPicker';
import { isSalesforceProjectOpened } from './predicates';
import { SalesforceProjectConfig } from './salesforceProject';
import { getCoreLoggerService, registerGetTelemetryServiceCommand } from './services';
import {
getCoreLoggerService,
registerGetTelemetryServiceCommand
} from './services';
import { registerPushOrDeployOnSave, salesforceCoreSettings } from './settings';
import { taskViewService } from './statuses';
import { showTelemetryMessage, telemetryService } from './telemetry';
Expand Down Expand Up @@ -582,7 +584,11 @@ export const activate = async (extensionContext: vscode.ExtensionContext) => {
// Set internal dev context
const internalDev = salesforceCoreSettings.getInternalDev();

void vscode.commands.executeCommand('setContext', 'sf:internal_dev', internalDev);
void vscode.commands.executeCommand(
'setContext',
'sf:internal_dev',
internalDev
);

if (internalDev) {
// Internal Dev commands
Expand Down Expand Up @@ -685,15 +691,15 @@ export const activate = async (extensionContext: vscode.ExtensionContext) => {
.getConfiguration(SFDX_CORE_CONFIGURATION_NAME)
.get<boolean>(ENABLE_SOBJECT_REFRESH_ON_STARTUP, false);

if (sobjectRefreshStartup) {
initSObjectDefinitions(
vscode.workspace.workspaceFolders[0].uri.fsPath
).catch(e => telemetryService.sendException(e.name, e.message));
} else {
checkSObjectsAndRefresh(
vscode.workspace.workspaceFolders[0].uri.fsPath
).catch(e => telemetryService.sendException(e.name, e.message));
}
initSObjectDefinitions(
vscode.workspace.workspaceFolders[0].uri.fsPath,
sobjectRefreshStartup
).catch(e =>
telemetryService.sendException(
'initSObjectDefinitionsError',
`Error: name = ${e.name} message = ${e.message} with sobjectRefreshStartup = ${sobjectRefreshStartup}`
)
);
}

void activateTracker.markActivationStop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,31 @@ import * as path from 'path';
import { createSandbox, SinonSandbox, SinonStub } from 'sinon';
import { ProgressLocation, window } from 'vscode';
import {
checkSObjectsAndRefresh,
RefreshSObjectsExecutor,
RefreshSelection,
SObjectRefreshGatherer,
verifyUsernameAndInitSObjectDefinitions
initSObjectDefinitions
} from '../../../src/commands/refreshSObjects';
import { WorkspaceContext } from '../../../src/context';
import { nls } from '../../../src/messages';
import { telemetryService } from '../../../src/telemetry';

describe('GenerateFauxClasses', () => {
const sobjectsPath = path.join(projectPaths.toolsFolder(), SOBJECTS_DIR);
const standardSobjectsPath = path.join(sobjectsPath, STANDARDOBJECTS_DIR);
describe('initSObjectDefinitions', () => {
let sandboxStub: SinonSandbox;
let existsSyncStub: SinonStub;
let getUsernameStub: SinonStub;
let commandletSpy: SinonStub;
let notificationStub: SinonStub;
let telemetryEventStub: SinonStub;

const projectPath = path.join('sample', 'path');

beforeEach(() => {
sandboxStub = createSandbox();
existsSyncStub = sandboxStub.stub(fs, 'existsSync');
getUsernameStub = sandboxStub.stub();
sandboxStub
.stub(WorkspaceContext.prototype, 'getConnection')
.resolves({ getUsername: getUsernameStub });
commandletSpy = sandboxStub.stub(SfCommandlet.prototype, 'run');
telemetryEventStub = sandboxStub.stub(telemetryService, 'sendEventData');
notificationStub = sandboxStub.stub(
notificationService,
'showInformationMessage'
Expand All @@ -71,9 +67,8 @@ describe('GenerateFauxClasses', () => {

it('Should execute sobject refresh if no sobjects folder is present', async () => {
existsSyncStub.returns(false);
getUsernameStub.returns(new Map([['target-org', 'Sample']]));

await verifyUsernameAndInitSObjectDefinitions(projectPath);
await initSObjectDefinitions(projectPath, true);

expect(existsSyncStub.calledWith(sobjectsPath)).to.be.true;
expect(commandletSpy.calledOnce).to.be.true;
Expand All @@ -86,46 +81,16 @@ describe('GenerateFauxClasses', () => {

it('Should not execute sobject refresh if sobjects folder is present', async () => {
existsSyncStub.returns(true);
getUsernameStub.returns('Sample');

await verifyUsernameAndInitSObjectDefinitions(projectPath);
await initSObjectDefinitions(projectPath, true);

expect(existsSyncStub.calledWith(sobjectsPath)).to.be.true;
expect(commandletSpy.notCalled).to.be.true;
});

it('Should not execute sobject refresh if no target org set', async () => {
existsSyncStub.returns(false);
getUsernameStub.returns(undefined);

await verifyUsernameAndInitSObjectDefinitions(projectPath);

expect(commandletSpy.notCalled).to.be.true;
});
});

describe('checkSObjectsAndRefresh', () => {
let sandboxStub: SinonSandbox;
let existsSyncStub: SinonStub;
let telemetryEventStub: SinonStub;

const projectPath = path.join('sample', 'path');
const standardSobjectsPath = path.join(sobjectsPath, STANDARDOBJECTS_DIR);

beforeEach(() => {
sandboxStub = createSandbox();
existsSyncStub = sandboxStub.stub(fs, 'existsSync');
telemetryEventStub = sandboxStub.stub(telemetryService, 'sendEventData');
});

afterEach(() => {
sandboxStub.restore();
});

it('Should call refreshSObjects service when sobjects do not exist', async () => {
existsSyncStub.returns(false);

await checkSObjectsAndRefresh(projectPath);
await initSObjectDefinitions(projectPath, false);

expect(existsSyncStub.calledWith(standardSobjectsPath)).to.be.true;
expect(telemetryEventStub.callCount).to.equal(1);
Expand All @@ -136,11 +101,10 @@ describe('GenerateFauxClasses', () => {
});
expect(telemetryCallArgs[2]).to.equal(undefined);
});

it('Should not call refreshSObjects service when sobjects already exist', async () => {
existsSyncStub.returns(true);

await checkSObjectsAndRefresh(projectPath);
await initSObjectDefinitions(projectPath, false);

expect(existsSyncStub.calledWith(standardSobjectsPath)).to.be.true;
expect(telemetryEventStub.notCalled).to.be.true;
Expand Down

0 comments on commit df53f10

Please sign in to comment.