Skip to content

Commit

Permalink
Try to get saved profile from the legacy config (#1387)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
ilia-db authored Oct 10, 2024
1 parent b7d6651 commit e2fb4a3
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/databricks-vscode/src/bundle/BundleFileSet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ describe(__filename, async function () {
path.join(tmpdirUri.fsPath, "includes", "included.yaml")
),
].map((v) => v.fsPath);
expect(Array.from(new Set(actual).values())).to.deep.equal(
Array.from(new Set(expected).values())
expect(Array.from(new Set(actual).values()).sort()).to.deep.equal(
Array.from(new Set(expected).values()).sort()
);
});

Expand Down
16 changes: 13 additions & 3 deletions packages/databricks-vscode/src/bundle/BundleProjectManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export class BundleProjectManager {
this.customWhenContext.setSubProjectsAvailable(
this.subProjects?.length > 0
);
this.telemetry.recordEvent(Events.BUNDLE_SUB_PROJECTS, {
count: this.subProjects?.length ?? 0,
});
}

public async openSubProjects() {
Expand All @@ -163,12 +166,19 @@ export class BundleProjectManager {
}
}

private setPendingManualMigration() {
this.customWhenContext.setPendingManualMigration(true);
this.telemetry.recordEvent(Events.CONNECTION_STATE_CHANGED, {
newState: "PENDING_MANUAL_MIGRATION",
});
}

private async detectLegacyProjectConfig() {
this.legacyProjectConfig = await this.loadLegacyProjectConfig();
// If we have subprojects, we can't migrate automatically. We show the user option to
// manually migrate the project (create a new databricks.yml based on selected auth)
if (!this.legacyProjectConfig || (this.subProjects?.length ?? 0) > 0) {
this.customWhenContext.setPendingManualMigration(true);
this.setPendingManualMigration();
return;
}
this.logger.debug(
Expand All @@ -182,7 +192,7 @@ export class BundleProjectManager {
);
} catch (error) {
recordEvent({success: false});
this.customWhenContext.setPendingManualMigration(true);
this.setPendingManualMigration();
const message =
"Failed to perform automatic migration to Databricks Asset Bundles.";
this.logger.error(message, error);
Expand Down Expand Up @@ -214,7 +224,7 @@ export class BundleProjectManager {
this.logger.debug(
"Legacy project auth was not successful, showing 'configure' welcome screen"
);
this.customWhenContext.setPendingManualMigration(true);
this.setPendingManualMigration();
recordEvent({success: false});
return;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/databricks-vscode/src/cli/CliWrapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ function createCliWrapper(logFilePath?: string) {
);
}

describe(__filename, () => {
describe(__filename, function () {
this.timeout("10s");

it("should embed a working databricks CLI", async () => {
const result = await execFile(cliPath, ["--help"]);
assert.ok(result.stdout.indexOf("databricks") > 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {Events, Telemetry} from "../telemetry";
import {AutoLoginSource, ManualLoginSource} from "../telemetry/constants";
import {Barrier} from "../locking/Barrier";
import {WorkspaceFolderManager} from "../vscode-objs/WorkspaceFolderManager";
import {ProjectConfigFile} from "../file-managers/ProjectConfigFile";

// eslint-disable-next-line @typescript-eslint/naming-convention
const {NamedLogger} = logging;
Expand Down Expand Up @@ -256,6 +257,16 @@ export class ConnectionManager implements Disposable {
});
}

private async loadLegacyProjectConfig() {
try {
return await ProjectConfigFile.loadConfig(this.workspaceUri.fsPath);
} catch (error) {
const logger = NamedLogger.getOrCreate("Extension");
logger.error(`Error loading legacy config`, error);
return undefined;
}
}

@onError({popup: {prefix: "Failed to login."}})
@Mutex.synchronise("loginLogoutMutex")
private async resolveAuth() {
Expand All @@ -267,8 +278,13 @@ export class ConnectionManager implements Disposable {
}

// Try to load a profile user had previously selected for this target
const savedProfile = (await this.configModel.get("overrides"))
let savedProfile = (await this.configModel.get("overrides"))
?.authProfile;
// Check if the profile is saved in the legacy project.json file
if (!savedProfile) {
const legacyConfig = await this.loadLegacyProjectConfig();
savedProfile = legacyConfig?.profile;
}
if (savedProfile !== undefined) {
const authProvider = await ProfileAuthProvider.from(
savedProfile,
Expand Down
19 changes: 13 additions & 6 deletions packages/databricks-vscode/src/file-managers/ProjectConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,14 @@ export class ProjectConfigFile {
return await ProfileAuthProvider.from(config.profile, cli);
}

static async load(
rootPath: string,
cli: CliWrapper
): Promise<ProjectConfigFile | undefined> {
static async loadConfig(
rootPath: string
): Promise<Record<string, any> | undefined> {
const projectConfigFilePath = path.join(
path.normalize(rootPath),
".databricks",
"project.json"
);

let rawConfig;
try {
rawConfig = await fs.readFile(projectConfigFilePath, {
Expand All @@ -75,9 +73,18 @@ export class ProjectConfigFile {
throw error;
}
}
return JSON.parse(rawConfig);
}

static async load(
rootPath: string,
cli: CliWrapper
): Promise<ProjectConfigFile | undefined> {
const config = await ProjectConfigFile.loadConfig(rootPath);
if (!config) {
return undefined;
}
let authProvider: AuthProvider;
const config = JSON.parse(rawConfig);
if (!config.authType && config.profile) {
authProvider = await this.importOldConfig(config, cli);
} else {
Expand Down
9 changes: 9 additions & 0 deletions packages/databricks-vscode/src/telemetry/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export enum Events {
MANUAL_MIGRATION = "manualMigration",
BUNDLE_RUN = "bundleRun",
BUNDLE_INIT = "bundleInit",
BUNDLE_SUB_PROJECTS = "bundleSubProjects",
CONNECTION_STATE_CHANGED = "connectionStateChanged",
}
/* eslint-enable @typescript-eslint/naming-convention */
Expand Down Expand Up @@ -139,6 +140,14 @@ export class EventTypes {
> = {
comment: "Initialize a new bundle project",
};
[Events.BUNDLE_SUB_PROJECTS]: EventType<{
count: number;
}> = {
comment: "Sub-projects in the active workspace folder",
count: {
comment: "Amount of sub-projects in the active workspace folder",
},
};
[Events.CONNECTION_STATE_CHANGED]: EventType<{
newState: string;
}> = {
Expand Down

0 comments on commit e2fb4a3

Please sign in to comment.