Skip to content

Commit 047f9f2

Browse files
committed
💄
1 parent a02abfe commit 047f9f2

File tree

5 files changed

+104
-105
lines changed

5 files changed

+104
-105
lines changed

src/authentication.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import * as vscode from 'vscode';
77
import { v4 as uuid } from 'uuid';
8-
import { Keychain } from './common/keychain';
9-
import { GitpodServer } from './gitpodServer';
8+
import Keychain from './common/keychain';
9+
import GitpodServer from './gitpodServer';
1010
import Log from './common/logger';
1111
import { arrayEquals } from './common/utils';
1212
import { Disposable } from './common/dispose';

src/common/keychain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import * as vscode from 'vscode';
99
import Log from './logger';
1010

11-
export class Keychain {
11+
export default class Keychain {
1212
constructor(
1313
private readonly context: vscode.ExtensionContext,
1414
private readonly serviceId: string,

src/extension.ts

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,25 @@ import * as vscode from 'vscode';
88
import Log from './common/logger';
99
import GitpodAuthenticationProvider from './authentication';
1010
import RemoteConnector from './remoteConnector';
11-
import { enableSettingsSync, updateSyncContext } from './settingsSync';
12-
import { GitpodServer } from './gitpodServer';
11+
import SettingsSync from './settingsSync';
12+
import GitpodServer from './gitpodServer';
1313
import TelemetryReporter from './telemetryReporter';
1414
import { exportLogs } from './exportLogs';
1515
import { registerReleaseNotesView } from './releaseNotes';
1616

17-
const EXTENSION_ID = 'gitpod.gitpod-desktop';
1817
const FIRST_INSTALL_KEY = 'gitpod-desktop.firstInstall';
1918

2019
let telemetry: TelemetryReporter;
2120
let remoteConnector: RemoteConnector;
2221

2322
export async function activate(context: vscode.ExtensionContext) {
24-
const packageJSON = vscode.extensions.getExtension(EXTENSION_ID)!.packageJSON;
23+
const extensionId = context.extension.id;
24+
const packageJSON = context.extension.packageJSON;
2525

2626
const logger = new Log('Gitpod');
27-
logger.info(`${EXTENSION_ID}/${packageJSON.version} (${os.release()} ${os.platform()} ${os.arch()}) vscode/${vscode.version} (${vscode.env.appName})`);
27+
logger.info(`${extensionId}/${packageJSON.version} (${os.release()} ${os.platform()} ${os.arch()}) vscode/${vscode.version} (${vscode.env.appName})`);
2828

29-
telemetry = new TelemetryReporter(EXTENSION_ID, packageJSON.version, packageJSON.segmentKey);
30-
31-
/* Gitpod settings sync */
32-
await updateSyncContext();
33-
context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(async e => {
34-
if (e.affectsConfiguration('gitpod.host') || e.affectsConfiguration('configurationSync.store')) {
35-
const addedSyncProvider = await updateSyncContext();
36-
if (!addedSyncProvider) {
37-
const action = 'Settings Sync: Enable Sign In with Gitpod';
38-
const result = await vscode.window.showInformationMessage('Gitpod Settings Sync configuration invalidated, Settings Sync is disabled.', action);
39-
if (result === action) {
40-
vscode.commands.executeCommand('gitpod.syncProvider.add');
41-
}
42-
}
43-
}
44-
}));
45-
46-
context.subscriptions.push(vscode.commands.registerCommand('gitpod.syncProvider.remove', async () => {
47-
try {
48-
await enableSettingsSync(false, telemetry);
49-
} catch (e) {
50-
const outputMessage = `Error setting up Settings Sync with Gitpod: ${e}`;
51-
vscode.window.showErrorMessage(outputMessage);
52-
logger.error(outputMessage);
53-
}
54-
}));
55-
context.subscriptions.push(vscode.commands.registerCommand('gitpod.syncProvider.add', async () => {
56-
try {
57-
await enableSettingsSync(true, telemetry);
58-
} catch (e) {
59-
const outputMessage = `Error setting up Settings Sync with Gitpod: ${e}`;
60-
vscode.window.showErrorMessage(outputMessage);
61-
logger.error(outputMessage);
62-
}
63-
}));
29+
telemetry = new TelemetryReporter(extensionId, packageJSON.version, packageJSON.segmentKey);
6430

6531
context.subscriptions.push(vscode.commands.registerCommand('gitpod.exportLogs', async () => {
6632
try {
@@ -72,6 +38,8 @@ export async function activate(context: vscode.ExtensionContext) {
7238
}
7339
}));
7440

41+
context.subscriptions.push(new SettingsSync(logger, telemetry));
42+
7543
const authProvider = new GitpodAuthenticationProvider(context, logger, telemetry);
7644
remoteConnector = new RemoteConnector(context, logger, telemetry);
7745
context.subscriptions.push(authProvider);

src/gitpodServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function getUserInfo(token: string, serviceUrl: string, logger: Log) {
2828
};
2929
}
3030

31-
export class GitpodServer extends Disposable {
31+
export default class GitpodServer extends Disposable {
3232

3333
public static AUTH_COMPLETE_PATH = '/complete-gitpod-auth';
3434

src/settingsSync.ts

Lines changed: 92 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*--------------------------------------------------------------------------------------------*/
55

66
import * as vscode from 'vscode';
7+
import { Disposable } from './common/dispose';
8+
import Log from './common/logger';
79
import TelemetryReporter from './telemetryReporter';
810

911
interface ConfigurationSyncStore {
@@ -14,72 +16,101 @@ interface ConfigurationSyncStore {
1416
authenticationProviders: Record<string, { scopes: string[] }>;
1517
}
1618

17-
function getGitpodSyncProviderConfig(serviceUrl: string): ConfigurationSyncStore {
18-
const syncStoreURL = `${new URL(serviceUrl).toString().replace(/\/$/, '')}/code-sync`;
19-
return {
20-
url: syncStoreURL,
21-
stableUrl: syncStoreURL,
22-
insidersUrl: syncStoreURL,
23-
canSwitch: false,
24-
authenticationProviders: {
25-
gitpod: {
26-
scopes: [
27-
'function:accessCodeSyncStorage',
28-
'function:getLoggedInUser',
29-
'resource:default'
30-
]
19+
export default class SettingsSync extends Disposable {
20+
constructor(private readonly logger: Log, private readonly telemetry: TelemetryReporter) {
21+
super();
22+
23+
this._register(vscode.workspace.onDidChangeConfiguration(async e => {
24+
if (e.affectsConfiguration('gitpod.host') || e.affectsConfiguration('configurationSync.store')) {
25+
const addedSyncProvider = await this.updateSyncContext();
26+
if (!addedSyncProvider) {
27+
const action = 'Settings Sync: Enable Sign In with Gitpod';
28+
const result = await vscode.window.showInformationMessage('Gitpod Settings Sync configuration invalidated, Settings Sync is disabled.', action);
29+
if (result === action) {
30+
vscode.commands.executeCommand('gitpod.syncProvider.add');
31+
}
32+
}
3133
}
32-
}
33-
};
34-
}
34+
}));
35+
this._register(vscode.commands.registerCommand('gitpod.syncProvider.add', () => this.enableSettingsSync(true)));
36+
this._register(vscode.commands.registerCommand('gitpod.syncProvider.remove', () => this.enableSettingsSync(false)));
3537

36-
/**
37-
* Updates the VS Code context to reflect whether the user added Gitpod as their Settings Sync provider.
38-
*/
39-
export async function updateSyncContext(): Promise<boolean> {
40-
const config = vscode.workspace.getConfiguration();
41-
const syncProviderConfig = config.get('configurationSync.store');
42-
const serviceUrl = config.get<string>('gitpod.host')!;
43-
const gitpodSyncProviderConfig = getGitpodSyncProviderConfig(serviceUrl);
44-
const addedSyncProvider = !!syncProviderConfig && JSON.stringify(syncProviderConfig) === JSON.stringify(gitpodSyncProviderConfig);
45-
await vscode.commands.executeCommand('setContext', 'gitpod.addedSyncProvider', addedSyncProvider);
46-
return addedSyncProvider;
47-
}
38+
this.updateSyncContext();
39+
}
4840

49-
/**
50-
* Adds an authentication provider as a possible provider for code sync.
51-
* It adds some key configuration to the user settings, so that the user can choose the Gitpod provider when deciding what to use with setting sync.
52-
* @param enabled - indicates whether to add or remove the configuration
53-
*/
54-
export async function enableSettingsSync(enabled: boolean, telemetry: TelemetryReporter): Promise<void> {
55-
let newSyncProviderConfig: ConfigurationSyncStore | undefined;
56-
let newIgnoredSettingsConfig: string[] | undefined;
57-
const config = vscode.workspace.getConfiguration();
58-
const currentSyncProviderConfig: ConfigurationSyncStore | undefined = config.get('configurationSync.store');
59-
const currentIgnoredSettingsConfig: string[] | undefined = config.get('settingsSync.ignoredSettings');
60-
const serviceUrl = config.get<string>('gitpod.host')!;
61-
const gitpodSyncProviderConfig = getGitpodSyncProviderConfig(serviceUrl);
62-
if (enabled) {
63-
if (JSON.stringify(currentSyncProviderConfig) === JSON.stringify(gitpodSyncProviderConfig)) {
64-
return;
65-
}
66-
newSyncProviderConfig = gitpodSyncProviderConfig;
67-
newIgnoredSettingsConfig = currentIgnoredSettingsConfig ?? [];
68-
if (!newIgnoredSettingsConfig.find(s => s === 'configurationSync.store')) {
69-
newIgnoredSettingsConfig.push('configurationSync.store');
70-
}
71-
} else {
72-
if (currentSyncProviderConfig === undefined) {
73-
return;
74-
}
75-
newSyncProviderConfig = undefined;
76-
newIgnoredSettingsConfig = currentIgnoredSettingsConfig?.filter(s => s !== 'configurationSync.store');
41+
/**
42+
* Updates the VS Code context to reflect whether the user added Gitpod as their Settings Sync provider.
43+
*/
44+
private async updateSyncContext(): Promise<boolean> {
45+
const config = vscode.workspace.getConfiguration();
46+
const syncProviderConfig = config.get('configurationSync.store');
47+
const serviceUrl = config.get<string>('gitpod.host')!;
48+
const gitpodSyncProviderConfig = this.getGitpodSyncProviderConfig(serviceUrl);
49+
const addedSyncProvider = !!syncProviderConfig && JSON.stringify(syncProviderConfig) === JSON.stringify(gitpodSyncProviderConfig);
50+
await vscode.commands.executeCommand('setContext', 'gitpod.addedSyncProvider', addedSyncProvider);
51+
return addedSyncProvider;
7752
}
7853

79-
await config.update('settingsSync.ignoredSettings', newIgnoredSettingsConfig, vscode.ConfigurationTarget.Global);
80-
await config.update('configurationSync.store', newSyncProviderConfig, vscode.ConfigurationTarget.Global);
54+
/**
55+
* Adds an authentication provider as a possible provider for code sync.
56+
* It adds some key configuration to the user settings, so that the user can choose the Gitpod provider when deciding what to use with setting sync.
57+
* @param enabled - indicates whether to add or remove the configuration
58+
*/
59+
private async enableSettingsSync(enabled: boolean): Promise<void> {
60+
try {
61+
let newSyncProviderConfig: ConfigurationSyncStore | undefined;
62+
let newIgnoredSettingsConfig: string[] | undefined;
63+
const config = vscode.workspace.getConfiguration();
64+
const currentSyncProviderConfig: ConfigurationSyncStore | undefined = config.get('configurationSync.store');
65+
const currentIgnoredSettingsConfig: string[] | undefined = config.get('settingsSync.ignoredSettings');
66+
const serviceUrl = config.get<string>('gitpod.host')!;
67+
const gitpodSyncProviderConfig = this.getGitpodSyncProviderConfig(serviceUrl);
68+
if (enabled) {
69+
if (JSON.stringify(currentSyncProviderConfig) === JSON.stringify(gitpodSyncProviderConfig)) {
70+
return;
71+
}
72+
newSyncProviderConfig = gitpodSyncProviderConfig;
73+
newIgnoredSettingsConfig = currentIgnoredSettingsConfig ?? [];
74+
if (!newIgnoredSettingsConfig.find(s => s === 'configurationSync.store')) {
75+
newIgnoredSettingsConfig.push('configurationSync.store');
76+
}
77+
} else {
78+
if (currentSyncProviderConfig === undefined) {
79+
return;
80+
}
81+
newSyncProviderConfig = undefined;
82+
newIgnoredSettingsConfig = currentIgnoredSettingsConfig?.filter(s => s !== 'configurationSync.store');
83+
}
84+
85+
await config.update('settingsSync.ignoredSettings', newIgnoredSettingsConfig, vscode.ConfigurationTarget.Global);
86+
await config.update('configurationSync.store', newSyncProviderConfig, vscode.ConfigurationTarget.Global);
8187

82-
telemetry.sendTelemetryEvent('gitpod_desktop_settings_sync', { enabled: String(enabled) });
88+
this.telemetry.sendTelemetryEvent('gitpod_desktop_settings_sync', { enabled: String(enabled) });
8389

84-
vscode.window.showInformationMessage('Quit VS Code for the new Settings Sync configuration to take effect.');
90+
vscode.window.showInformationMessage('Quit VS Code for the new Settings Sync configuration to take effect.', { modal: true });
91+
} catch (e) {
92+
const outputMessage = `Error setting up Settings Sync with Gitpod: ${e}`;
93+
vscode.window.showErrorMessage(outputMessage);
94+
this.logger.error(outputMessage);
95+
}
96+
}
97+
98+
private getGitpodSyncProviderConfig(serviceUrl: string): ConfigurationSyncStore {
99+
const syncStoreURL = `${new URL(serviceUrl).toString().replace(/\/$/, '')}/code-sync`;
100+
return {
101+
url: syncStoreURL,
102+
stableUrl: syncStoreURL,
103+
insidersUrl: syncStoreURL,
104+
canSwitch: false,
105+
authenticationProviders: {
106+
gitpod: {
107+
scopes: [
108+
'function:accessCodeSyncStorage',
109+
'function:getLoggedInUser',
110+
'resource:default'
111+
]
112+
}
113+
}
114+
};
115+
}
85116
}

0 commit comments

Comments
 (0)