Skip to content

Commit cc412b1

Browse files
authored
Port final trust fixes for release (#12965)
* Only allow Enter / NumpadEnter w/o ctrl/shift/alt (#12939) * Send telemetry for notebook trust prompt selections (#12964) * Fixes for persisting trust (#12950)
1 parent 0ef5ed6 commit cc412b1

File tree

6 files changed

+47
-24
lines changed

6 files changed

+47
-24
lines changed

src/client/datascience/constants.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,11 @@ export enum Telemetry {
368368
RunByLineStart = 'DATASCIENCE.RUN_BY_LINE',
369369
RunByLineStep = 'DATASCIENCE.RUN_BY_LINE_STEP',
370370
RunByLineStop = 'DATASCIENCE.RUN_BY_LINE_STOP',
371-
RunByLineVariableHover = 'DATASCIENCE.RUN_BY_LINE_VARIABLE_HOVER'
371+
RunByLineVariableHover = 'DATASCIENCE.RUN_BY_LINE_VARIABLE_HOVER',
372+
TrustAllNotebooks = 'DATASCIENCE.TRUST_ALL_NOTEBOOKS',
373+
TrustNotebook = 'DATASCIENCE.TRUST_NOTEBOOK',
374+
DoNotTrustNotebook = 'DATASCIENCE.DO_NOT_TRUST_NOTEBOOK',
375+
NotebookTrustPromptShown = 'DATASCIENCE.NOTEBOOK_TRUST_PROMPT_SHOWN'
372376
}
373377

374378
export enum NativeKeyboardCommandTelemetry {

src/client/datascience/interactive-ipynb/nativeEditorStorage.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,10 @@ export class NativeEditorStorage implements INotebookStorage {
282282
const dirtyContents = skipDirtyContents ? undefined : await this.getStoredContents(file, backupId);
283283
if (dirtyContents) {
284284
// This means we're dirty. Indicate dirty and load from this content
285-
return this.loadContents(file, dirtyContents, true, contents, forVSCodeNotebook);
285+
return this.loadContents(file, dirtyContents, true, forVSCodeNotebook);
286286
} else {
287287
// Load without setting dirty
288-
return this.loadContents(file, contents, undefined, undefined, forVSCodeNotebook);
288+
return this.loadContents(file, contents, undefined, forVSCodeNotebook);
289289
}
290290
} catch (ex) {
291291
// May not exist at this time. Should always have a single cell though
@@ -308,7 +308,6 @@ export class NativeEditorStorage implements INotebookStorage {
308308
file: Uri,
309309
contents: string | undefined,
310310
isInitiallyDirty = false,
311-
trueContents?: string,
312311
forVSCodeNotebook?: boolean
313312
) {
314313
// tslint:disable-next-line: no-any
@@ -348,18 +347,9 @@ export class NativeEditorStorage implements INotebookStorage {
348347
}
349348
const pythonNumber = json ? await this.extractPythonMainVersion(json) : 3;
350349

351-
/* As an optimization, we don't call trustNotebook for hot exit, since our hot exit backup code gets called by VS
352-
Code whenever the notebook model changes. This means it's called very often, perhaps even as often as autosave.
353-
Instead, when loading a file that is dirty, we check if the actual file contents on disk are trusted. If so, we treat
354-
the dirty contents as trusted as well. */
355-
const contentsToCheck = isInitiallyDirty && trueContents !== undefined ? trueContents : contents;
356-
const isTrusted =
357-
contents === undefined || isUntitledFile(file)
358-
? true // If no contents or untitled, this is a newly created file, so it should be trusted
359-
: await this.trustService.isNotebookTrusted(file, contentsToCheck!);
360-
return this.factory.createModel(
350+
const model = this.factory.createModel(
361351
{
362-
trusted: isTrusted,
352+
trusted: true,
363353
file,
364354
cells: remapped,
365355
notebookJson: json,
@@ -369,6 +359,23 @@ export class NativeEditorStorage implements INotebookStorage {
369359
},
370360
forVSCodeNotebook
371361
);
362+
363+
// If no contents or untitled, this is a newly created file
364+
// If dirty, that means it's been edited before in our extension
365+
if (contents !== undefined && !isUntitledFile(file) && !isInitiallyDirty) {
366+
const isNotebookTrusted = await this.trustService.isNotebookTrusted(file, model.getContent());
367+
if (isNotebookTrusted !== model.isTrusted) {
368+
model.update({
369+
source: 'user',
370+
kind: 'updateTrust',
371+
oldDirty: model.isDirty,
372+
newDirty: model.isDirty,
373+
isNotebookTrusted
374+
});
375+
}
376+
}
377+
378+
return model;
372379
}
373380

374381
private getStaticStorageKey(file: Uri): string {

src/client/datascience/interactive-ipynb/trustCommandHandler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import '../../common/extensions';
1313
import { IDisposableRegistry, IExperimentService } from '../../common/types';
1414
import { swallowExceptions } from '../../common/utils/decorators';
1515
import { DataScience } from '../../common/utils/localize';
16-
import { Commands } from '../constants';
16+
import { sendTelemetryEvent } from '../../telemetry';
17+
import { Commands, Telemetry } from '../constants';
1718
import { INotebookStorageProvider } from '../interactive-ipynb/notebookStorageProvider';
1819
import { INotebookEditorProvider, ITrustService } from '../types';
1920

@@ -57,10 +58,12 @@ export class TrustCommandHandler implements IExtensionSingleActivationService {
5758
DataScience.doNotTrustNotebook(),
5859
DataScience.trustAllNotebooks()
5960
);
61+
sendTelemetryEvent(Telemetry.NotebookTrustPromptShown);
6062

6163
switch (selection) {
6264
case DataScience.trustAllNotebooks():
6365
commands.executeCommand('workbench.action.openSettings', 'python.dataScience.alwaysTrustNotebooks');
66+
sendTelemetryEvent(Telemetry.TrustAllNotebooks);
6467
break;
6568
case DataScience.trustNotebook():
6669
// Update model trust
@@ -73,6 +76,10 @@ export class TrustCommandHandler implements IExtensionSingleActivationService {
7376
});
7477
const contents = model.getContent();
7578
await this.trustService.trustNotebook(model.file, contents);
79+
sendTelemetryEvent(Telemetry.TrustNotebook);
80+
break;
81+
case DataScience.doNotTrustNotebook():
82+
sendTelemetryEvent(Telemetry.DoNotTrustNotebook);
7683
break;
7784
default:
7885
break;

src/client/datascience/notebookStorage/baseModel.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,8 @@ export abstract class BaseNotebookModel implements INotebookModel {
196196
this.ensureNotebookJson();
197197

198198
// Reuse our original json except for the cells.
199-
const json = {
200-
cells: this.cells.map((c) => pruneCell(c.data)),
201-
metadata: this.notebookJson.metadata,
202-
nbformat: this.notebookJson.nbformat,
203-
nbformat_minor: this.notebookJson.nbformat_minor
204-
};
199+
const json = { ...this.notebookJson };
200+
json.cells = this.cells.map((c) => pruneCell(c.data));
205201
return JSON.stringify(json, null, this.indentAmount);
206202
}
207203
}

src/client/telemetry/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,10 +2192,20 @@ export interface IEventNamePropertyMapping {
21922192
[Telemetry.StartPageOpenFileBrowser]: never | undefined;
21932193
[Telemetry.StartPageOpenFolder]: never | undefined;
21942194
[Telemetry.StartPageOpenWorkspace]: never | undefined;
2195+
2196+
// Run by line events
21952197
[Telemetry.RunByLineStart]: never | undefined;
21962198
[Telemetry.RunByLineStep]: never | undefined;
21972199
[Telemetry.RunByLineStop]: never | undefined;
21982200
[Telemetry.RunByLineVariableHover]: never | undefined;
2201+
2202+
// Trusted notebooks events
2203+
[Telemetry.NotebookTrustPromptShown]: never | undefined;
2204+
[Telemetry.TrustNotebook]: never | undefined;
2205+
[Telemetry.TrustAllNotebooks]: never | undefined;
2206+
[Telemetry.DoNotTrustNotebook]: never | undefined;
2207+
2208+
// Native notebooks events
21992209
[VSCodeNativeTelemetry.AddCell]: never | undefined;
22002210
[VSCodeNativeTelemetry.DeleteCell]: never | undefined;
22012211
[VSCodeNativeTelemetry.MoveCell]: never | undefined;

src/datascience-ui/native-editor/nativeCell.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,7 @@ export function getConnectedNativeCell() {
892892

893893
function isCellNavigationKeyboardEvent(e: IKeyboardEvent) {
894894
return (
895-
e.code === 'Enter' ||
896-
e.code === 'NumpadEnter' ||
895+
((e.code === 'Enter' || e.code === 'NumpadEnter') && !e.shiftKey && !e.ctrlKey && !e.altKey) ||
897896
e.code === 'ArrowUp' ||
898897
e.code === 'k' ||
899898
e.code === 'ArrowDown' ||

0 commit comments

Comments
 (0)