Skip to content

Commit e2fb4a3

Browse files
authored
Try to get saved profile from the legacy config (#1387)
## Changes V2 extension currently requires manual action from users if we detect multiple profiles that match the selected target. But in the V1 users might have already disambiguated the profiles, so here we use this inromation to get the saved profile. ## Tests Manually
1 parent b7d6651 commit e2fb4a3

File tree

6 files changed

+57
-13
lines changed

6 files changed

+57
-13
lines changed

packages/databricks-vscode/src/bundle/BundleFileSet.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ describe(__filename, async function () {
113113
path.join(tmpdirUri.fsPath, "includes", "included.yaml")
114114
),
115115
].map((v) => v.fsPath);
116-
expect(Array.from(new Set(actual).values())).to.deep.equal(
117-
Array.from(new Set(expected).values())
116+
expect(Array.from(new Set(actual).values()).sort()).to.deep.equal(
117+
Array.from(new Set(expected).values()).sort()
118118
);
119119
});
120120

packages/databricks-vscode/src/bundle/BundleProjectManager.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ export class BundleProjectManager {
155155
this.customWhenContext.setSubProjectsAvailable(
156156
this.subProjects?.length > 0
157157
);
158+
this.telemetry.recordEvent(Events.BUNDLE_SUB_PROJECTS, {
159+
count: this.subProjects?.length ?? 0,
160+
});
158161
}
159162

160163
public async openSubProjects() {
@@ -163,12 +166,19 @@ export class BundleProjectManager {
163166
}
164167
}
165168

169+
private setPendingManualMigration() {
170+
this.customWhenContext.setPendingManualMigration(true);
171+
this.telemetry.recordEvent(Events.CONNECTION_STATE_CHANGED, {
172+
newState: "PENDING_MANUAL_MIGRATION",
173+
});
174+
}
175+
166176
private async detectLegacyProjectConfig() {
167177
this.legacyProjectConfig = await this.loadLegacyProjectConfig();
168178
// If we have subprojects, we can't migrate automatically. We show the user option to
169179
// manually migrate the project (create a new databricks.yml based on selected auth)
170180
if (!this.legacyProjectConfig || (this.subProjects?.length ?? 0) > 0) {
171-
this.customWhenContext.setPendingManualMigration(true);
181+
this.setPendingManualMigration();
172182
return;
173183
}
174184
this.logger.debug(
@@ -182,7 +192,7 @@ export class BundleProjectManager {
182192
);
183193
} catch (error) {
184194
recordEvent({success: false});
185-
this.customWhenContext.setPendingManualMigration(true);
195+
this.setPendingManualMigration();
186196
const message =
187197
"Failed to perform automatic migration to Databricks Asset Bundles.";
188198
this.logger.error(message, error);
@@ -214,7 +224,7 @@ export class BundleProjectManager {
214224
this.logger.debug(
215225
"Legacy project auth was not successful, showing 'configure' welcome screen"
216226
);
217-
this.customWhenContext.setPendingManualMigration(true);
227+
this.setPendingManualMigration();
218228
recordEvent({success: false});
219229
return;
220230
}

packages/databricks-vscode/src/cli/CliWrapper.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ function createCliWrapper(logFilePath?: string) {
5050
);
5151
}
5252

53-
describe(__filename, () => {
53+
describe(__filename, function () {
54+
this.timeout("10s");
55+
5456
it("should embed a working databricks CLI", async () => {
5557
const result = await execFile(cliPath, ["--help"]);
5658
assert.ok(result.stdout.indexOf("databricks") > 0);

packages/databricks-vscode/src/configuration/ConnectionManager.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {Events, Telemetry} from "../telemetry";
2525
import {AutoLoginSource, ManualLoginSource} from "../telemetry/constants";
2626
import {Barrier} from "../locking/Barrier";
2727
import {WorkspaceFolderManager} from "../vscode-objs/WorkspaceFolderManager";
28+
import {ProjectConfigFile} from "../file-managers/ProjectConfigFile";
2829

2930
// eslint-disable-next-line @typescript-eslint/naming-convention
3031
const {NamedLogger} = logging;
@@ -256,6 +257,16 @@ export class ConnectionManager implements Disposable {
256257
});
257258
}
258259

260+
private async loadLegacyProjectConfig() {
261+
try {
262+
return await ProjectConfigFile.loadConfig(this.workspaceUri.fsPath);
263+
} catch (error) {
264+
const logger = NamedLogger.getOrCreate("Extension");
265+
logger.error(`Error loading legacy config`, error);
266+
return undefined;
267+
}
268+
}
269+
259270
@onError({popup: {prefix: "Failed to login."}})
260271
@Mutex.synchronise("loginLogoutMutex")
261272
private async resolveAuth() {
@@ -267,8 +278,13 @@ export class ConnectionManager implements Disposable {
267278
}
268279

269280
// Try to load a profile user had previously selected for this target
270-
const savedProfile = (await this.configModel.get("overrides"))
281+
let savedProfile = (await this.configModel.get("overrides"))
271282
?.authProfile;
283+
// Check if the profile is saved in the legacy project.json file
284+
if (!savedProfile) {
285+
const legacyConfig = await this.loadLegacyProjectConfig();
286+
savedProfile = legacyConfig?.profile;
287+
}
272288
if (savedProfile !== undefined) {
273289
const authProvider = await ProfileAuthProvider.from(
274290
savedProfile,

packages/databricks-vscode/src/file-managers/ProjectConfigFile.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,14 @@ export class ProjectConfigFile {
5353
return await ProfileAuthProvider.from(config.profile, cli);
5454
}
5555

56-
static async load(
57-
rootPath: string,
58-
cli: CliWrapper
59-
): Promise<ProjectConfigFile | undefined> {
56+
static async loadConfig(
57+
rootPath: string
58+
): Promise<Record<string, any> | undefined> {
6059
const projectConfigFilePath = path.join(
6160
path.normalize(rootPath),
6261
".databricks",
6362
"project.json"
6463
);
65-
6664
let rawConfig;
6765
try {
6866
rawConfig = await fs.readFile(projectConfigFilePath, {
@@ -75,9 +73,18 @@ export class ProjectConfigFile {
7573
throw error;
7674
}
7775
}
76+
return JSON.parse(rawConfig);
77+
}
7878

79+
static async load(
80+
rootPath: string,
81+
cli: CliWrapper
82+
): Promise<ProjectConfigFile | undefined> {
83+
const config = await ProjectConfigFile.loadConfig(rootPath);
84+
if (!config) {
85+
return undefined;
86+
}
7987
let authProvider: AuthProvider;
80-
const config = JSON.parse(rawConfig);
8188
if (!config.authType && config.profile) {
8289
authProvider = await this.importOldConfig(config, cli);
8390
} else {

packages/databricks-vscode/src/telemetry/constants.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export enum Events {
1818
MANUAL_MIGRATION = "manualMigration",
1919
BUNDLE_RUN = "bundleRun",
2020
BUNDLE_INIT = "bundleInit",
21+
BUNDLE_SUB_PROJECTS = "bundleSubProjects",
2122
CONNECTION_STATE_CHANGED = "connectionStateChanged",
2223
}
2324
/* eslint-enable @typescript-eslint/naming-convention */
@@ -139,6 +140,14 @@ export class EventTypes {
139140
> = {
140141
comment: "Initialize a new bundle project",
141142
};
143+
[Events.BUNDLE_SUB_PROJECTS]: EventType<{
144+
count: number;
145+
}> = {
146+
comment: "Sub-projects in the active workspace folder",
147+
count: {
148+
comment: "Amount of sub-projects in the active workspace folder",
149+
},
150+
};
142151
[Events.CONNECTION_STATE_CHANGED]: EventType<{
143152
newState: string;
144153
}> = {

0 commit comments

Comments
 (0)