Skip to content

Commit

Permalink
Dont sent telemetry in forks (#21377)
Browse files Browse the repository at this point in the history
Closed #20941 

- Add enableTelemetry in package.json
- Add function that updates the 'enableTelemetry' value in package.json
- Update release pipelines
  • Loading branch information
paulacamargo25 authored Jun 8, 2023
1 parent 84e67af commit 3e5af44
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 31 deletions.
4 changes: 4 additions & 0 deletions build/azure-pipeline.pre-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ extends:
python ./build/update_ext_version.py --for-publishing
displayName: Update build number
- script: |
python ./build/update_package_file.py
displayName: Update telemetry in package.json
- script: npm run addExtensionPackDependencies
displayName: Update optional extension dependencies

Expand Down
4 changes: 4 additions & 0 deletions build/azure-pipeline.stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ extends:
- script: |
python ./build/update_ext_version.py --release --for-publishing
displayName: Update build number
- script: |
python ./build/update_package_file.py
displayName: Update telemetry in package.json
- script: npm run addExtensionPackDependencies
displayName: Update optional extension dependencies
Expand Down
21 changes: 21 additions & 0 deletions build/update_package_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import json
import pathlib
import sys

EXT_ROOT = pathlib.Path(__file__).parent.parent
PACKAGE_JSON_PATH = EXT_ROOT / "package.json"

def main(package_json: pathlib.Path) -> None:
package = json.loads(package_json.read_text(encoding="utf-8"))
package['enableTelemetry'] = True

# Overwrite package.json with new data add a new-line at the end of the file.
package_json.write_text(
json.dumps(package, indent=4, ensure_ascii=False) + "\n", encoding="utf-8"
)

if __name__ == "__main__":
main(PACKAGE_JSON_PATH, sys.argv[1:])
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"engines": {
"vscode": "^1.79.0-20230526"
},
"enableTelemetry": false,
"keywords": [
"python",
"django",
Expand Down
16 changes: 9 additions & 7 deletions src/client/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

import TelemetryReporter from '@vscode/extension-telemetry';

import * as path from 'path';
import * as fs from 'fs-extra';
import { DiagnosticCodes } from '../application/diagnostics/constants';
import { IWorkspaceService } from '../common/application/types';
import { AppinsightsKey, isTestExecution, isUnitTestExecution } from '../common/constants';
import { AppinsightsKey, EXTENSION_ROOT_DIR, isTestExecution, isUnitTestExecution } from '../common/constants';
import type { TerminalShellType } from '../common/terminal/types';
import { StopWatch } from '../common/utils/stopWatch';
import { isPromise } from '../common/utils/async';
Expand Down Expand Up @@ -41,12 +42,13 @@ function isTelemetrySupported(): boolean {
}

/**
* Checks if the telemetry is disabled in user settings
* Checks if the telemetry is disabled
* @returns {boolean}
*/
export function isTelemetryDisabled(workspaceService: IWorkspaceService): boolean {
const settings = workspaceService.getConfiguration('telemetry').inspect<boolean>('enableTelemetry')!;
return settings.globalValue === false;
export function isTelemetryDisabled(): boolean {
const packageJsonPath = path.join(EXTENSION_ROOT_DIR, 'package.json');
const packageJson = fs.readJSONSync(packageJsonPath);
return !packageJson.enableTelemetry;
}

const sharedProperties: Record<string, unknown> = {};
Expand Down Expand Up @@ -101,7 +103,7 @@ export function sendTelemetryEvent<P extends IEventNamePropertyMapping, E extend
properties?: P[E],
ex?: Error,
): void {
if (isTestExecution() || !isTelemetrySupported()) {
if (isTestExecution() || !isTelemetrySupported() || isTelemetryDisabled()) {
return;
}
const reporter = getTelemetryReporter();
Expand Down
4 changes: 4 additions & 0 deletions src/test/debugger/extension/adapter/factory.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import * as assert from 'assert';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as sinon from 'sinon';
import rewiremock from 'rewiremock';
Expand Down Expand Up @@ -36,6 +37,7 @@ suite('Debugging - Adapter Factory', () => {
let stateFactory: IPersistentStateFactory;
let state: PersistentState<boolean | undefined>;
let showErrorMessageStub: sinon.SinonStub;
let readJSONSyncStub: sinon.SinonStub;
let commandManager: ICommandManager;

const nodeExecutable = undefined;
Expand Down Expand Up @@ -66,6 +68,8 @@ suite('Debugging - Adapter Factory', () => {
setup(() => {
process.env.VSC_PYTHON_UNIT_TEST = undefined;
process.env.VSC_PYTHON_CI_TEST = undefined;
readJSONSyncStub = sinon.stub(fs, 'readJSONSync');
readJSONSyncStub.returns({ enableTelemetry: true });
rewiremock.enable();
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
stateFactory = mock(PersistentStateFactory);
Expand Down
39 changes: 15 additions & 24 deletions src/test/telemetry/index.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@

import { expect } from 'chai';
import rewiremock from 'rewiremock';
import * as TypeMoq from 'typemoq';
import * as sinon from 'sinon';
import * as fs from 'fs-extra';

import { instance, mock, verify, when } from 'ts-mockito';
import { WorkspaceConfiguration } from 'vscode';
import { IWorkspaceService } from '../../client/common/application/types';
import { WorkspaceService } from '../../client/common/application/workspace';
import {
_resetSharedProperties,
clearTelemetryReporter,
Expand All @@ -19,9 +16,9 @@ import {
} from '../../client/telemetry';

suite('Telemetry', () => {
let workspaceService: IWorkspaceService;
const oldValueOfVSC_PYTHON_UNIT_TEST = process.env.VSC_PYTHON_UNIT_TEST;
const oldValueOfVSC_PYTHON_CI_TEST = process.env.VSC_PYTHON_CI_TEST;
let readJSONSyncStub: sinon.SinonStub;

class Reporter {
public static eventName: string[] = [];
Expand All @@ -48,9 +45,10 @@ suite('Telemetry', () => {
}

setup(() => {
workspaceService = mock(WorkspaceService);
process.env.VSC_PYTHON_UNIT_TEST = undefined;
process.env.VSC_PYTHON_CI_TEST = undefined;
readJSONSyncStub = sinon.stub(fs, 'readJSONSync');
readJSONSyncStub.returns({ enableTelemetry: true });
clearTelemetryReporter();
Reporter.clear();
});
Expand All @@ -59,35 +57,28 @@ suite('Telemetry', () => {
process.env.VSC_PYTHON_CI_TEST = oldValueOfVSC_PYTHON_CI_TEST;
rewiremock.disable();
_resetSharedProperties();
sinon.restore();
});

const testsForisTelemetryDisabled = [
{
testName: 'Returns true when globalValue is set to false',
settings: { globalValue: false },
expectedResult: true,
testName: 'Returns true',
settings: { enableTelemetry: true },
expectedResult: false,
},
{
testName: 'Returns false otherwise',
settings: {},
expectedResult: false,
testName: 'Returns false ',
settings: { enableTelemetry: false },
expectedResult: true,
},
];

suite('Function isTelemetryDisabled()', () => {
testsForisTelemetryDisabled.forEach((testParams) => {
test(testParams.testName, async () => {
const workspaceConfig = TypeMoq.Mock.ofType<WorkspaceConfiguration>();
when(workspaceService.getConfiguration('telemetry')).thenReturn(workspaceConfig.object);
workspaceConfig
.setup((c) => c.inspect<string>('enableTelemetry'))
.returns(() => testParams.settings as any)
.verifiable(TypeMoq.Times.once());

expect(isTelemetryDisabled(instance(workspaceService))).to.equal(testParams.expectedResult);

verify(workspaceService.getConfiguration('telemetry')).once();
workspaceConfig.verifyAll();
readJSONSyncStub.returns(testParams.settings);
expect(isTelemetryDisabled()).to.equal(testParams.expectedResult);
sinon.assert.calledOnce(readJSONSyncStub);
});
});
});
Expand Down

0 comments on commit 3e5af44

Please sign in to comment.