Skip to content

Commit

Permalink
DEV2-3883 use external for remote setup
Browse files Browse the repository at this point in the history
  • Loading branch information
dimacodota committed Oct 17, 2023
1 parent 0790fbb commit 21467af
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 73 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"args": [
"--extensionDevelopmentPath=${workspaceRoot}",
"--logPluginHostCommunication",
"--disable-extensions",
],
"sourceMaps": true,
"preLaunchTask": "npm: build"
Expand Down
7 changes: 4 additions & 3 deletions src/authentication/authentication.api.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { tabNineProcess } from "../binary/requests/requests";
import { openLogin } from "../cloudEnvs/openLogin";
import { openExternalLogin } from "../cloudEnvs/openLogin";
import isCloudEnv from "../cloudEnvs/isCloudEnv";
import tabnineExtensionProperties from "../globals/tabnineExtensionProperties";

export function callForLogin(): Promise<unknown> {
if (isCloudEnv) {
return openLogin();
if (isCloudEnv || tabnineExtensionProperties.isRemote) {
return openExternalLogin();
}
return tabNineProcess.request({ Login: {} });
}
Expand Down
10 changes: 10 additions & 0 deletions src/binary/requests/startLoginServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { tabNineProcess } from "./requests";

export function startLoginServer(): Promise<string | null | undefined> {
return tabNineProcess.request(
{
StartLoginServer: {},
},
5000
);
}
35 changes: 11 additions & 24 deletions src/cloudEnvs/openLogin.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
import { env, Uri } from "vscode";
import { URL } from "url";
import { StateType, TABNINE_URL_QUERY_PARAM } from "../globals/consts";
import createHubWebView from "../hub/createHubWebView";
import hubUri from "../hub/hubUri";

export async function openLogin(): Promise<void> {
return open("https://app.tabnine.com/auth/sign-in");
}

export async function openSignup(): Promise<void> {
return open("https://app.tabnine.com/auth/signup");
}

async function open(url: string): Promise<void> {
const uri = await hubUri(StateType.AUTH);
if (uri) {
const callback = new URL(url);
callback.searchParams.set(TABNINE_URL_QUERY_PARAM, uri.toString());
callback.searchParams.set("sync", "false");

const panel = await createHubWebView(uri);
panel.reveal();
void env.openExternal(Uri.parse(callback.toString()));
import { env } from "vscode";
import { asExternal } from "../utils/asExternal";
import { startLoginServer } from "../binary/requests/startLoginServer";

export async function openExternalLogin(): Promise<void> {
const url = await startLoginServer();
if (!url) {
return;
}
const remapedUrl = await asExternal(url);

void env.openExternal(remapedUrl);
}
47 changes: 5 additions & 42 deletions src/hub/hubUri.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,16 @@
import { URL } from "url";
import { Uri, env } from "vscode";
import {
StateType,
TABNINE_URL_QUERY_PARAM,
LOCAL_ADDRESSES,
} from "../globals/consts";
import { Uri } from "vscode";
import { StateType } from "../globals/consts";
import { configuration } from "../binary/requests/requests";
import {
asExternalUri as asCodeServerExternalUri,
isCodeServer,
} from "../cloudEnvs/codeServer";

let asExternalUri = async (uri: Uri): Promise<Uri> => {
if (!LOCAL_ADDRESSES.includes(new URL(uri.toString()).hostname)) return uri;
if (isCodeServer) {
return asCodeServerExternalUri(uri);
}

return env.asExternalUri(uri);
};

export function setAsExternalUri(fn: typeof asExternalUri): void {
asExternalUri = fn;
}
import { asExternal } from "../utils/asExternal";

export default async function hubUri(
type: StateType,
hubPath?: string
path?: string
): Promise<Uri | null> {
const config = await configuration({ quiet: true, source: type });
if (!config?.message) {
return null;
}

const hubUrl = new URL(config.message);

const tabnineUrl = hubUrl.searchParams.get(TABNINE_URL_QUERY_PARAM);
if (tabnineUrl) {
hubUrl.searchParams.set(
TABNINE_URL_QUERY_PARAM,
(await asExternalUri(Uri.parse(tabnineUrl))).toString()
);
}

let parsedHubUri = Uri.parse(hubUrl.toString());

if (hubPath) {
parsedHubUri = Uri.joinPath(parsedHubUri, hubPath);
}

return asExternalUri(parsedHubUri);
return asExternal(config.message, path);
}
11 changes: 7 additions & 4 deletions src/test/suite/notification.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
PROMO_TYPE,
SAME_NOTIFICATION_ID,
} from "./utils/testData";
import { setAsExternalUri } from "../../hub/hubUri";
import * as asExternal from "../../utils/asExternal";

type OpenWebviewParams = [
viewType: string,
Expand Down Expand Up @@ -235,7 +235,9 @@ suite("Should poll notifications", () => {
Promise.resolve(Uri.parse(LOCAL_HUB_URL))
);

setAsExternalUri(asExternalUriSpy);
sinon
.fake(asExternal.asExternal)
.returned(Promise.resolve(Uri.parse(LOCAL_HUB_URL)));

showInformationMessage.onFirstCall().resolves(AN_OPTION_KEY);

Expand Down Expand Up @@ -309,8 +311,9 @@ suite("Should poll notifications", () => {
const asExternalUriSpy = sinon.spy<(uri: Uri) => Promise<Uri>>(() =>
Promise.resolve(Uri.parse(LOCAL_HUB_URL))
);

setAsExternalUri(asExternalUriSpy);
sinon
.fake(asExternal.asExternal)
.returned(Promise.resolve(Uri.parse(LOCAL_HUB_URL)));

showInformationMessage.onFirstCall().resolves(AN_OPTION_KEY);

Expand Down
36 changes: 36 additions & 0 deletions src/utils/asExternal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { URL } from "url";
import { Uri, env } from "vscode";
import { LOCAL_ADDRESSES, TABNINE_URL_QUERY_PARAM } from "../globals/consts";
import {
asExternalUri as asCodeServerExternalUri,
isCodeServer,
} from "../cloudEnvs/codeServer";

export async function asExternalUri(uri: Uri): Promise<Uri> {
if (!LOCAL_ADDRESSES.includes(new URL(uri.toString()).hostname)) return uri;
if (isCodeServer) {
return asCodeServerExternalUri(uri);
}

return env.asExternalUri(uri);
}

export async function asExternal(url: string, path?: string) {
const serviceUrl = new URL(url);

const tabnineUrl = serviceUrl.searchParams.get(TABNINE_URL_QUERY_PARAM);
if (tabnineUrl) {
serviceUrl.searchParams.set(
TABNINE_URL_QUERY_PARAM,
(await asExternalUri(Uri.parse(tabnineUrl))).toString()
);
}

let parsedUri = Uri.parse(serviceUrl.toString());

if (path) {
parsedUri = Uri.joinPath(parsedUri, path);
}

return asExternalUri(parsedUri);
}

0 comments on commit 21467af

Please sign in to comment.