Skip to content

Commit 181f801

Browse files
committed
feat(tree-explorer): allow running edit preset connections from the header
Running from header will automatically determine which scope to open and create a preset to edit if none are defined
1 parent bf9cd64 commit 181f801

File tree

6 files changed

+78
-21
lines changed

6 files changed

+78
-21
lines changed

.vscode/settings.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@
1717
},
1818
// Turn off tsc task auto detection since we have the necessary tasks as npm scripts
1919
"typescript.tsc.autoDetect": "off",
20-
"mdb.presetSavedConnections":[
20+
"mdb.presetSavedConnections": [
2121
{
22-
"name": "Local Test Connection",
22+
"name": "Preset Database",
2323
"connectionString": "mongodb://localhost:27017"
2424
}
25-
],
26-
"conventionalCommits.scopes": [
27-
"tree-explorer"
2825
]
2926
}

package.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,18 @@
493493
},
494494
{
495495
"command": "mdb.addConnection",
496-
"when": "view == mongoDBConnectionExplorer"
496+
"when": "view == mongoDBConnectionExplorer",
497+
"group": "1@1"
497498
},
498499
{
499500
"command": "mdb.addConnectionWithURI",
500-
"when": "view == mongoDBConnectionExplorer"
501+
"when": "view == mongoDBConnectionExplorer",
502+
"group": "1@2"
503+
},
504+
{
505+
"command": "mdb.editPresetConnections",
506+
"when": "view == mongoDBConnectionExplorer",
507+
"group": "2@1"
501508
}
502509
],
503510
"view/item/context": [
@@ -1189,11 +1196,11 @@
11891196
"mdb.presetSavedConnections": {
11901197
"scope": "window",
11911198
"type": "array",
1192-
"description": "Defines preset saved connections.",
1199+
"description": "Defines preset saved connections. These connections can be used to define and share connection configurations in a workspace or global scope. Do not store sensitive credentials here.",
11931200
"examples": [
11941201
[
11951202
{
1196-
"name": "Local Test Connection",
1203+
"name": "Preset Database",
11971204
"connectionString": "mongodb://localhost:27017"
11981205
}
11991206
]
@@ -1202,7 +1209,7 @@
12021209
"type": "object",
12031210
"examples": [
12041211
{
1205-
"name": "Local Test Connection",
1212+
"name": "Preset Database",
12061213
"connectionString": "mongodb://localhost:27017"
12071214
}
12081215
],

src/connectionController.ts

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import type { StorageController } from './storage';
2121
import type { StatusView } from './views';
2222
import type TelemetryService from './telemetry/telemetryService';
2323
import { openLink } from './utils/linkHelper';
24-
import type { LoadedConnection } from './storage/connectionStorage';
24+
import type {
25+
ConnectionSource,
26+
LoadedConnection,
27+
} from './storage/connectionStorage';
2528
import { ConnectionStorage } from './storage/connectionStorage';
2629
import LINKS from './utils/links';
2730
import { isAtlasStream } from 'mongodb-build-info';
31+
import { DocumentSource } from './documentSource';
32+
import type { ConnectionTreeItem } from './explorer';
2833

2934
// eslint-disable-next-line @typescript-eslint/no-var-requires
3035
const packageJSON = require('../package.json');
@@ -161,6 +166,46 @@ export default class ConnectionController {
161166
});
162167
}
163168

169+
async openPresetConnectionsSettings(
170+
originTreeItem: ConnectionTreeItem | undefined
171+
): Promise<void> {
172+
this._telemetryService.trackPresetConnectionEdited({
173+
source: DocumentSource.DOCUMENT_SOURCE_TREEVIEW,
174+
source_details: originTreeItem ? 'tree_item' : 'header',
175+
});
176+
let source: ConnectionSource | undefined = originTreeItem?.source;
177+
if (!source) {
178+
const mdbConfiguration = vscode.workspace.getConfiguration('mdb');
179+
180+
const presetConnections = mdbConfiguration?.inspect(
181+
'presetSavedConnections'
182+
);
183+
184+
if (presetConnections?.workspaceValue) {
185+
source = 'workspaceSettings';
186+
} else if (presetConnections?.globalValue) {
187+
source = 'globalSettings';
188+
} else {
189+
// If no preset settings exist in workspace and global scope,
190+
// set a default one inside the workspace and open it.
191+
source = 'workspaceSettings';
192+
await mdbConfiguration.update('presetSavedConnections', [
193+
{
194+
name: 'Preset Database',
195+
connectionString: 'mongodb://localhost:27017',
196+
},
197+
]);
198+
}
199+
}
200+
if (originTreeItem?.source === 'globalSettings') {
201+
await vscode.commands.executeCommand('workbench.action.openSettingsJson');
202+
} else {
203+
await vscode.commands.executeCommand(
204+
'workbench.action.openWorkspaceSettingsFile'
205+
);
206+
}
207+
}
208+
164209
async loadSavedConnections(): Promise<void> {
165210
this._connections = Object.create(null);
166211

src/mdbExtensionController.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -492,16 +492,8 @@ export default class MDBExtensionController implements vscode.Disposable {
492492
);
493493
this.registerCommand(
494494
EXTENSION_COMMANDS.MDB_EDIT_PRESET_CONNECTIONS,
495-
async (element: ConnectionTreeItem) => {
496-
if (element.source === 'workspaceSettings') {
497-
await vscode.commands.executeCommand(
498-
'workbench.action.openWorkspaceSettingsFile'
499-
);
500-
} else if (element.source === 'globalSettings') {
501-
await vscode.commands.executeCommand(
502-
'workbench.action.openSettingsJson'
503-
);
504-
}
495+
async (element: ConnectionTreeItem | undefined) => {
496+
await this._connectionController.openPresetConnectionsSettings(element);
505497
return true;
506498
}
507499
);

src/telemetry/telemetryService.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ type ConnectionEditedTelemetryEventProperties = {
9191
type SavedConnectionsLoadedProperties = {
9292
// Total number of connections saved on disk
9393
saved_connections: number;
94+
// Total number of connections from preset settings
95+
preset_connections: number;
9496
// Total number of connections that extension was able to load, it might
9597
// differ from saved_connections since there might be failures in loading
9698
// secrets for a connection in which case we don't list the connections in the
@@ -145,6 +147,11 @@ export type ParticipantChatOpenedFromActionProperties = {
145147
command?: ParticipantCommandType;
146148
};
147149

150+
export type PresetSavedConnectionEditedProperties = {
151+
source: DocumentSource;
152+
source_details: 'tree_item' | 'header';
153+
};
154+
148155
export type ParticipantInputBoxSubmitted = {
149156
source: DocumentSource;
150157
input_length: number | undefined;
@@ -216,6 +223,7 @@ export enum TelemetryEventTypes {
216223
/** Tracks after a participant interacts with the input box we open to let the user write the prompt for participant. */
217224
PARTICIPANT_INPUT_BOX_SUBMITTED = 'Participant Inbox Box Submitted',
218225
PARTICIPANT_RESPONSE_GENERATED = 'Participant Response Generated',
226+
PRESET_CONNECTION_EDITED = 'Preset Connection Edited',
219227
}
220228

221229
/**
@@ -446,6 +454,12 @@ export default class TelemetryService {
446454
);
447455
}
448456

457+
trackPresetConnectionEdited(
458+
props: PresetSavedConnectionEditedProperties
459+
): void {
460+
this.track(TelemetryEventTypes.PRESET_CONNECTION_EDITED, props);
461+
}
462+
449463
trackPlaygroundCreated(playgroundType: string): void {
450464
this.track(TelemetryEventTypes.PLAYGROUND_CREATED, {
451465
playground_type: playgroundType,

src/test/suite/telemetry/telemetryService.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,7 @@ suite('Telemetry Controller Test Suite', () => {
672672
testTelemetryService.trackSavedConnectionsLoaded({
673673
saved_connections: 3,
674674
loaded_connections: 3,
675+
preset_connections: 3,
675676
connections_with_secrets_in_keytar: 0,
676677
connections_with_secrets_in_secret_storage: 3,
677678
});
@@ -684,6 +685,7 @@ suite('Telemetry Controller Test Suite', () => {
684685
properties: {
685686
saved_connections: 3,
686687
loaded_connections: 3,
688+
preset_connections: 3,
687689
connections_with_secrets_in_keytar: 0,
688690
connections_with_secrets_in_secret_storage: 3,
689691
},

0 commit comments

Comments
 (0)