forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 417
/
features.ts
1092 lines (1034 loc) · 40.8 KB
/
features.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Gitpod. All rights reserved.
*--------------------------------------------------------------------------------------------*/
/// <reference path='../../../src/vscode-dts/vscode.d.ts'/>
// TODO get rid of loading inversify and reflect-metadata
require('reflect-metadata');
import { GitpodClient, GitpodServer, GitpodServiceImpl, WorkspaceInstanceUpdateListener } from '@gitpod/gitpod-protocol/lib/gitpod-service';
import { JsonRpcProxyFactory } from '@gitpod/gitpod-protocol/lib/messaging/proxy-factory';
import { NavigatorContext, User } from '@gitpod/gitpod-protocol/lib/protocol';
import { ErrorCodes } from '@gitpod/gitpod-protocol/lib/messaging/error';
import { GitpodHostUrl } from '@gitpod/gitpod-protocol/lib/util/gitpod-host-url';
import { ControlServiceClient } from '@gitpod/supervisor-api-grpc/lib/control_grpc_pb';
import { InfoServiceClient } from '@gitpod/supervisor-api-grpc/lib/info_grpc_pb';
import { WorkspaceInfoRequest, WorkspaceInfoResponse } from '@gitpod/supervisor-api-grpc/lib/info_pb';
import { NotificationServiceClient } from '@gitpod/supervisor-api-grpc/lib/notification_grpc_pb';
import { NotifyRequest, NotifyResponse, RespondRequest, SubscribeRequest, SubscribeResponse } from '@gitpod/supervisor-api-grpc/lib/notification_pb';
import { PortServiceClient } from '@gitpod/supervisor-api-grpc/lib/port_grpc_pb';
import { StatusServiceClient } from '@gitpod/supervisor-api-grpc/lib/status_grpc_pb';
import { ContentStatusRequest, TasksStatusRequest, TasksStatusResponse, TaskState, TaskStatus } from '@gitpod/supervisor-api-grpc/lib/status_pb';
import { TerminalServiceClient } from '@gitpod/supervisor-api-grpc/lib/terminal_grpc_pb';
import { ListenTerminalRequest, ListenTerminalResponse, ListTerminalsRequest, SetTerminalSizeRequest, ShutdownTerminalRequest, Terminal as SupervisorTerminal, TerminalSize as SupervisorTerminalSize, WriteTerminalRequest } from '@gitpod/supervisor-api-grpc/lib/terminal_pb';
import { TokenServiceClient } from '@gitpod/supervisor-api-grpc/lib/token_grpc_pb';
import { GetTokenRequest } from '@gitpod/supervisor-api-grpc/lib/token_pb';
import * as grpc from '@grpc/grpc-js';
import * as fs from 'fs';
import * as http from 'http';
import * as path from 'path';
import ReconnectingWebSocket from 'reconnecting-websocket';
import { URL } from 'url';
import * as util from 'util';
import * as vscode from 'vscode';
import { CancellationToken, ConsoleLogger, listen as doListen } from 'vscode-ws-jsonrpc';
import WebSocket = require('ws');
import { BaseGitpodAnalyticsEventPropeties, GitpodAnalyticsEvent } from './analytics';
import * as uuid from 'uuid';
import { RemoteTrackMessage } from '@gitpod/gitpod-protocol/lib/analytics';
import Log from './common/logger';
export class SupervisorConnection {
readonly deadlines = {
long: 30 * 1000,
normal: 15 * 1000,
short: 5 * 1000
};
private readonly addr = process.env.SUPERVISOR_ADDR || 'localhost:22999';
private readonly clientOptions: Partial<grpc.ClientOptions>;
readonly metadata = new grpc.Metadata();
readonly status: StatusServiceClient;
readonly control: ControlServiceClient;
readonly notification: NotificationServiceClient;
readonly token: TokenServiceClient;
readonly info: InfoServiceClient;
readonly port: PortServiceClient;
readonly terminal: TerminalServiceClient;
constructor(
context: vscode.ExtensionContext
) {
this.clientOptions = {
'grpc.primary_user_agent': `${vscode.env.appName}/${vscode.version} ${context.extension.id}/${context.extension.packageJSON.version}`,
};
this.status = new StatusServiceClient(this.addr, grpc.credentials.createInsecure(), this.clientOptions);
this.control = new ControlServiceClient(this.addr, grpc.credentials.createInsecure(), this.clientOptions);
this.notification = new NotificationServiceClient(this.addr, grpc.credentials.createInsecure(), this.clientOptions);
this.token = new TokenServiceClient(this.addr, grpc.credentials.createInsecure(), this.clientOptions);
this.info = new InfoServiceClient(this.addr, grpc.credentials.createInsecure(), this.clientOptions);
this.port = new PortServiceClient(this.addr, grpc.credentials.createInsecure(), this.clientOptions);
this.terminal = new TerminalServiceClient(this.addr, grpc.credentials.createInsecure(), this.clientOptions);
}
}
type UsedGitpodFunction = ['getWorkspace', 'openPort', 'stopWorkspace', 'setWorkspaceTimeout', 'getWorkspaceTimeout', 'getLoggedInUser', 'takeSnapshot', 'waitForSnapshot', 'controlAdmission', 'sendHeartBeat', 'trackEvent'];
type Union<Tuple extends any[], Union = never> = Tuple[number] | Union;
export type GitpodConnection = Omit<GitpodServiceImpl<GitpodClient, GitpodServer>, 'server'> & {
server: Pick<GitpodServer, Union<UsedGitpodFunction>>;
};
export class GitpodExtensionContext implements vscode.ExtensionContext {
readonly sessionId = uuid.v4();
readonly pendingActivate: Promise<void>[] = [];
readonly workspaceContextUrl: vscode.Uri;
constructor(
private readonly context: vscode.ExtensionContext,
readonly devMode: boolean,
readonly config: typeof import('./gitpod-plugin-model'),
readonly supervisor: SupervisorConnection,
readonly gitpod: GitpodConnection,
private readonly webSocket: Promise<ReconnectingWebSocket> | undefined,
readonly pendingWillCloseSocket: (() => Promise<void>)[],
readonly info: WorkspaceInfoResponse,
readonly owner: Promise<User>,
readonly user: Promise<User>,
readonly instanceListener: Promise<WorkspaceInstanceUpdateListener>,
readonly workspaceOwned: Promise<boolean>,
readonly logger: Log,
readonly ipcHookCli: string | undefined
) {
this.workspaceContextUrl = vscode.Uri.parse(info.getWorkspaceContextUrl());
}
get active() {
Object.freeze(this.pendingActivate);
return Promise.all(this.pendingActivate.map(p => p.catch(console.error)));
}
get subscriptions() {
return this.context.subscriptions;
}
get globalState() {
return this.context.globalState;
}
get workspaceState() {
return this.context.workspaceState;
}
get secrets() {
return this.context.secrets;
}
get extensionUri() {
return this.context.extensionUri;
}
get extensionPath() {
return this.context.extensionPath;
}
get environmentVariableCollection() {
return this.context.environmentVariableCollection;
}
asAbsolutePath(relativePath: string): string {
return this.context.asAbsolutePath(relativePath);
}
get storageUri() {
return this.context.storageUri;
}
get storagePath() {
return this.context.storagePath;
}
get globalStorageUri() {
return this.context.globalStorageUri;
}
get globalStoragePath() {
return this.context.globalStoragePath;
}
get logUri() {
return this.context.logUri;
}
get logPath() {
return this.context.logPath;
}
get extensionMode() {
return this.context.extensionMode;
}
get extension() {
return this.context.extension;
}
get extensionRuntime() {
return (this.context as any).extensionRuntime;
}
dispose() {
const pendingWebSocket = this.webSocket;
if (!pendingWebSocket) {
return;
}
return (async () => {
try {
const webSocket = await pendingWebSocket;
await Promise.allSettled(this.pendingWillCloseSocket.map(f => f()));
webSocket.close();
} catch (e) {
this.logger.error('failed to dispose context:', e);
console.error('failed to dispose context:', e);
}
})();
}
async fireAnalyticsEvent({ eventName, properties }: GitpodAnalyticsEvent): Promise<void> {
const baseProperties: BaseGitpodAnalyticsEventPropeties = {
sessionId: this.sessionId,
workspaceId: this.info.getWorkspaceId(),
instanceId: this.info.getInstanceId(),
appName: vscode.env.appName,
uiKind: vscode.env.uiKind === vscode.UIKind.Web ? 'web' : 'desktop',
devMode: this.devMode,
version: vscode.version,
timestamp: Date.now(),
};
const msg: RemoteTrackMessage = {
event: eventName,
properties: {
...baseProperties,
...properties,
}
};
if (this.devMode && vscode.env.uiKind === vscode.UIKind.Web) {
this.logger.trace(`ANALYTICS: ${JSON.stringify(msg)} `);
return Promise.resolve();
}
try {
await this.gitpod.server.trackEvent(msg);
} catch (e) {
this.logger.error('failed to track event:', e);
console.error('failed to track event:', e);
}
}
}
export async function createGitpodExtensionContext(context: vscode.ExtensionContext): Promise<GitpodExtensionContext | undefined> {
const logger = new Log('Gitpod Workspace');
const devMode = context.extensionMode === vscode.ExtensionMode.Development || !!process.env['VSCODE_DEV'];
const supervisor = new SupervisorConnection(context);
let contentAvailable = false;
while (!contentAvailable) {
try {
const contentStatusRequest = new ContentStatusRequest();
contentStatusRequest.setWait(true);
const result = await util.promisify(supervisor.status.contentStatus.bind(supervisor.status, contentStatusRequest, supervisor.metadata, {
deadline: Date.now() + supervisor.deadlines.long
}))();
contentAvailable = result.getAvailable();
} catch (e) {
if (e.code === grpc.status.UNAVAILABLE) {
logger.info('It does not look like we are running in a Gitpod workspace, supervisor is not available.');
return undefined;
}
console.error('cannot maintain connection to supervisor', e);
}
}
const workspaceInfo = await util.promisify(supervisor.info.workspaceInfo.bind(supervisor.info, new WorkspaceInfoRequest(), supervisor.metadata, {
deadline: Date.now() + supervisor.deadlines.long
}))();
const workspaceId = workspaceInfo.getWorkspaceId();
const gitpodHost = workspaceInfo.getGitpodHost();
const gitpodApi = workspaceInfo.getGitpodApi()!;
const factory = new JsonRpcProxyFactory<GitpodServer>();
const gitpodFunctions: UsedGitpodFunction = ['getWorkspace', 'openPort', 'stopWorkspace', 'setWorkspaceTimeout', 'getWorkspaceTimeout', 'getLoggedInUser', 'takeSnapshot', 'waitForSnapshot', 'controlAdmission', 'sendHeartBeat', 'trackEvent'];
const gitpodService: GitpodConnection = new GitpodServiceImpl<GitpodClient, GitpodServer>(factory.createProxy()) as any;
const gitpodScopes = new Set<string>([
'resource:workspace::' + workspaceId + '::get/update',
'function:accessCodeSyncStorage',
]);
for (const gitpodFunction of gitpodFunctions) {
gitpodScopes.add('function:' + gitpodFunction);
}
const pendingServerToken = (async () => {
const getTokenRequest = new GetTokenRequest();
getTokenRequest.setKind('gitpod');
getTokenRequest.setHost(gitpodApi.getHost());
for (const scope of gitpodScopes) {
getTokenRequest.addScope(scope);
}
const getTokenResponse = await util.promisify(supervisor.token.getToken.bind(supervisor.token, getTokenRequest, supervisor.metadata, {
deadline: Date.now() + supervisor.deadlines.long
}))();
return getTokenResponse.getToken();
})();
const pendingWillCloseSocket: (() => Promise<void>)[] = [];
const pendignWebSocket = (async () => {
const serverToken = await pendingServerToken;
class GitpodServerWebSocket extends WebSocket {
constructor(address: string, protocols?: string | string[]) {
super(address, protocols, {
headers: {
'Origin': new URL(gitpodHost).origin,
'Authorization': `Bearer ${serverToken}`,
'User-Agent': `${vscode.env.appName}/${vscode.version} ${context.extension.id}/${context.extension.packageJSON.version}`,
}
});
}
}
const webSocket = new ReconnectingWebSocket(gitpodApi.getEndpoint(), undefined, {
maxReconnectionDelay: 10000,
minReconnectionDelay: 1000,
reconnectionDelayGrowFactor: 1.3,
connectionTimeout: 10000,
maxRetries: Infinity,
debug: false,
startClosed: false,
WebSocket: GitpodServerWebSocket
});
webSocket.onerror = console.error;
doListen({
webSocket,
onConnection: connection => factory.listen(connection),
logger: new ConsoleLogger()
});
return webSocket;
})();
const pendingGetOwner = gitpodService.server.getLoggedInUser();
const pendingGetUser = (async () => {
if (devMode || vscode.env.uiKind !== vscode.UIKind.Web) {
return pendingGetOwner;
}
return vscode.commands.executeCommand('gitpod.api.getLoggedInUser') as typeof pendingGetOwner;
})();
const pendingInstanceListener = gitpodService.listenToInstance(workspaceId);
const pendingWorkspaceOwned = (async () => {
const owner = await pendingGetOwner;
const user = await pendingGetUser;
const workspaceOwned = owner.id === user.id;
vscode.commands.executeCommand('setContext', 'gitpod.workspaceOwned', workspaceOwned);
return workspaceOwned;
})();
const ipcHookCli = installCLIProxy(context, logger);
const config = await import('./gitpod-plugin-model');
return new GitpodExtensionContext(
context,
devMode,
config,
supervisor,
gitpodService,
pendignWebSocket,
pendingWillCloseSocket,
workspaceInfo,
pendingGetOwner,
pendingGetUser,
pendingInstanceListener,
pendingWorkspaceOwned,
logger,
ipcHookCli
);
}
export async function registerWorkspaceCommands(context: GitpodExtensionContext): Promise<void> {
context.subscriptions.push(vscode.commands.registerCommand('gitpod.open.dashboard', () => {
const url = context.info.getGitpodHost();
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.open.accessControl', () => {
const url = new GitpodHostUrl(context.info.getGitpodHost()).asAccessControl().toString();
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.open.settings', () => {
const url = new GitpodHostUrl(context.info.getGitpodHost()).asSettings().toString();
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.open.context', () => {
const url = context.workspaceContextUrl.toString();
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.open.documentation', () => {
const url = 'https://www.gitpod.io/docs';
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.open.twitter', () => {
const url = 'https://twitter.com/gitpod';
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.open.discord', () => {
const url = 'https://www.gitpod.io/chat';
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.reportIssue', () => {
const url = 'https://github.com/gitpod-io/gitpod/issues/new/choose';
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
if (vscode.env.uiKind === vscode.UIKind.Web) {
function openDesktop(scheme: 'vscode' | 'vscode-insiders'): void {
const uri = vscode.workspace.workspaceFile || vscode.workspace.workspaceFolders?.[0]?.uri;
vscode.env.openExternal(vscode.Uri.from({
scheme,
authority: 'gitpod.gitpod-desktop',
path: uri?.path || context.info.getWorkspaceLocationFile() || context.info.getWorkspaceLocationFolder() || context.info.getCheckoutLocation(),
query: JSON.stringify({
instanceId: context.info.getInstanceId(),
workspaceId: context.info.getWorkspaceId(),
gitpodHost: context.info.getGitpodHost()
})
}));
}
context.subscriptions.push(vscode.commands.registerCommand('gitpod.openInStable', () => {
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_change_vscode_type',
properties: { targetUiKind: 'desktop', targetQualifier: 'stable' }
});
return openDesktop('vscode');
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.openInInsiders', () => {
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_change_vscode_type',
properties: { targetUiKind: 'desktop', targetQualifier: 'insiders' }
});
return openDesktop('vscode-insiders');
}));
}
if (vscode.env.uiKind === vscode.UIKind.Desktop) {
context.subscriptions.push(vscode.commands.registerCommand('gitpod.openInBrowser', () => {
const url = context.info.getWorkspaceUrl();
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_change_vscode_type',
properties: { targetUiKind: 'web' }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
}
const workspaceOwned = await context.workspaceOwned;
if (!workspaceOwned) {
return;
}
context.subscriptions.push(vscode.commands.registerCommand('gitpod.stop.ws', () => {
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_workspace',
properties: { action: 'stop' }
});
return context.gitpod.server.stopWorkspace(context.info.getWorkspaceId());
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.upgradeSubscription', () => {
const url = new GitpodHostUrl(context.info.getGitpodHost()).asUpgradeSubscription().toString();
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_open_link',
properties: { url }
});
return vscode.env.openExternal(vscode.Uri.parse(url));
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.takeSnapshot', async () => {
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_workspace',
properties: { action: 'snapshot' }
});
try {
let snapshotId: string | undefined = undefined;
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: true,
title: 'Capturing workspace snapshot'
}, async (_, cancelToken: CancellationToken) => {
snapshotId = await context.gitpod.server.takeSnapshot({ workspaceId: context.info.getWorkspaceId() /*, layoutData?*/, dontWait: true });
while (!cancelToken.isCancellationRequested) {
try {
await context.gitpod.server.waitForSnapshot(snapshotId);
return;
} catch (err) {
if (err.code === ErrorCodes.SNAPSHOT_ERROR || err.code === ErrorCodes.NOT_FOUND) {
// this is indeed an error with snapshot creation itself, break here!
throw err;
}
// other errors (like connection errors): retry
await new Promise((resolve) => setTimeout(resolve, 3000));
}
}
});
if (!snapshotId) {
throw new Error('error taking snapshot');
}
const hostname = context.info.getGitpodApi()!.getHost();
const uri = `https://${hostname}#snapshot/${snapshotId}`;
const copyAction = await vscode.window.showInformationMessage(`The current state is captured in a snapshot. Using [this link](${uri}) anybody can create their own copy of this workspace.`,
'Copy URL to Clipboard');
if (copyAction === 'Copy URL to Clipboard') {
await vscode.env.clipboard.writeText(uri);
}
} catch (err) {
console.error('cannot capture workspace snapshot', err);
await vscode.window.showErrorMessage(`Cannot capture workspace snapshot: ${err.toString()}`);
}
}));
}
export async function registerWorkspaceSharing(context: GitpodExtensionContext): Promise<void> {
const owner = await context.owner;
const workspaceOwned = await context.workspaceOwned;
const workspaceSharingStatusBarItem = vscode.window.createStatusBarItem('gitpod.workspaceSharing', vscode.StatusBarAlignment.Left);
workspaceSharingStatusBarItem.name = 'Workspace Sharing';
context.subscriptions.push(workspaceSharingStatusBarItem);
function setWorkspaceShared(workspaceShared: boolean): void {
if (workspaceOwned) {
vscode.commands.executeCommand('setContext', 'gitpod.workspaceShared', workspaceShared);
if (workspaceShared) {
workspaceSharingStatusBarItem.text = '$(broadcast) Shared';
workspaceSharingStatusBarItem.tooltip = 'Your workspace is currently shared. Anyone with the link can access this workspace.';
workspaceSharingStatusBarItem.command = 'gitpod.stopSharingWorkspace';
} else {
workspaceSharingStatusBarItem.text = '$(live-share) Share';
workspaceSharingStatusBarItem.tooltip = 'Your workspace is currently not shared. Only you can access it.';
workspaceSharingStatusBarItem.command = 'gitpod.shareWorkspace';
}
} else {
workspaceSharingStatusBarItem.text = '$(broadcast) Shared by ' + owner.name;
workspaceSharingStatusBarItem.tooltip = `You are currently accessing the workspace shared by ${owner.name}.`;
}
workspaceSharingStatusBarItem.show();
}
const listener = await context.instanceListener;
setWorkspaceShared(listener.info.workspace.shareable || false);
if (!workspaceOwned) {
return;
}
async function controlAdmission(level: GitpodServer.AdmissionLevel): Promise<void> {
try {
if (level === 'everyone') {
const confirm = await vscode.window.showWarningMessage('Sharing your workspace with others also means sharing your access to your repository. Everyone with access to the workspace you share can commit in your name.', { modal: true }, 'Share');
if (confirm !== 'Share') {
return;
}
}
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: true,
title: level === 'everyone' ? 'Sharing workspace...' : 'Stopping workspace sharing...'
}, _ => {
return context.gitpod.server.controlAdmission(context.info.getWorkspaceId(), level);
});
setWorkspaceShared(level === 'everyone');
if (level === 'everyone') {
const uri = context.info.getWorkspaceUrl();
const copyToClipboard = 'Copy URL to Clipboard';
const res = await vscode.window.showInformationMessage(`Your workspace is currently shared. Anyone with [the link](${uri}) can access this workspace.`, copyToClipboard);
if (res === copyToClipboard) {
await vscode.env.clipboard.writeText(uri);
}
} else {
await vscode.window.showInformationMessage(`Your workspace is currently not shared. Only you can access it.`);
}
} catch (err) {
console.error('cannot controlAdmission', err);
if (level === 'everyone') {
await vscode.window.showErrorMessage(`Cannot share workspace: ${err.toString()}`);
} else {
await vscode.window.showInformationMessage(`Cannot stop workspace sharing: ${err.toString()}`);
}
}
}
context.subscriptions.push(vscode.commands.registerCommand('gitpod.shareWorkspace', () => {
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_workspace',
properties: { action: 'share' }
});
return controlAdmission('everyone');
}));
context.subscriptions.push(vscode.commands.registerCommand('gitpod.stopSharingWorkspace', () => {
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_workspace',
properties: { action: 'stop-sharing' }
});
return controlAdmission('owner');
}));
}
export async function registerWorkspaceTimeout(context: GitpodExtensionContext): Promise<void> {
const workspaceOwned = await context.workspaceOwned;
if (!workspaceOwned) {
return;
}
context.subscriptions.push(vscode.commands.registerCommand('gitpod.ExtendTimeout', async () => {
context.fireAnalyticsEvent({
eventName: 'vscode_execute_command_gitpod_workspace',
properties: {
action: 'extend-timeout'
}
});
try {
const result = await context.gitpod.server.setWorkspaceTimeout(context.info.getWorkspaceId(), '180m');
if (result.resetTimeoutOnWorkspaces?.length > 0) {
vscode.window.showWarningMessage('Workspace timeout has been extended to three hours. This reset the workspace timeout for other workspaces.');
} else {
vscode.window.showInformationMessage('Workspace timeout has been extended to three hours.');
}
} catch (err) {
vscode.window.showErrorMessage(`Cannot extend workspace timeout: ${err.toString()}`);
}
}));
const workspaceTimeout = await context.gitpod.server.getWorkspaceTimeout(context.info.getWorkspaceId());
if (!workspaceTimeout.canChange) {
return;
}
const listener = await context.instanceListener;
const extendTimeoutStatusBarItem = vscode.window.createStatusBarItem('gitpod.extendTimeout', vscode.StatusBarAlignment.Right, -100);
extendTimeoutStatusBarItem.name = 'Click to extend the workspace timeout.';
context.subscriptions.push(extendTimeoutStatusBarItem);
extendTimeoutStatusBarItem.text = '$(watch)';
extendTimeoutStatusBarItem.command = 'gitpod.ExtendTimeout';
const update = () => {
const instance = listener.info.latestInstance;
if (!instance) {
extendTimeoutStatusBarItem.hide();
return;
}
extendTimeoutStatusBarItem.tooltip = `Workspace Timeout: ${instance.status.timeout}. Click to extend.`;
extendTimeoutStatusBarItem.color = instance.status.timeout === '180m' ? new vscode.ThemeColor('notificationsWarningIcon.foreground') : undefined;
extendTimeoutStatusBarItem.show();
};
update();
context.subscriptions.push(listener.onDidChange(update));
}
export function registerNotifications(context: GitpodExtensionContext): void {
function observeNotifications(): vscode.Disposable {
let run = true;
let stopUpdates: Function | undefined;
(async () => {
while (run) {
try {
const evts = context.supervisor.notification.subscribe(new SubscribeRequest(), context.supervisor.metadata);
stopUpdates = evts.cancel.bind(evts);
await new Promise((resolve, reject) => {
evts.on('end', resolve);
evts.on('error', reject);
evts.on('data', async (result: SubscribeResponse) => {
const request = result.getRequest();
if (request) {
const level = request.getLevel();
const message = request.getMessage();
const actions = request.getActionsList();
let choice: string | undefined;
switch (level) {
case NotifyRequest.Level.ERROR:
choice = await vscode.window.showErrorMessage(message, ...actions);
break;
case NotifyRequest.Level.WARNING:
choice = await vscode.window.showWarningMessage(message, ...actions);
break;
case NotifyRequest.Level.INFO:
default:
choice = await vscode.window.showInformationMessage(message, ...actions);
}
const respondRequest = new RespondRequest();
const notifyResponse = new NotifyResponse();
notifyResponse.setAction(choice || '');
respondRequest.setResponse(notifyResponse);
respondRequest.setRequestid(result.getRequestid());
context.supervisor.notification.respond(respondRequest, context.supervisor.metadata, {
deadline: Date.now() + context.supervisor.deadlines.normal
}, (error, _) => {
if (error?.code !== grpc.status.DEADLINE_EXCEEDED) {
reject(error);
}
});
}
});
});
} catch (err) {
if ('code' in err && err.code === grpc.status.UNIMPLEMENTED) {
console.warn('supervisor does not implement the notification server');
run = false;
} else if (!('code' in err && err.code === grpc.status.CANCELLED)) {
console.error('cannot maintain connection to supervisor', err);
}
} finally {
stopUpdates = undefined;
}
await new Promise(resolve => setTimeout(resolve, 1000));
}
})();
return new vscode.Disposable(() => {
run = false;
if (stopUpdates) {
stopUpdates();
}
});
}
context.subscriptions.push(observeNotifications());
}
export function registerDefaultLayout(context: GitpodExtensionContext): void {
const layoutInitializedKey = 'gitpod:layoutInitialized';
const layoutInitialized = Boolean(context.globalState.get(layoutInitializedKey));
if (!layoutInitialized) {
context.globalState.update(layoutInitializedKey, true);
(async () => {
const listener = await context.instanceListener;
const workspaceContext = listener.info.workspace.context;
if (NavigatorContext.is(workspaceContext)) {
const location = vscode.Uri.file(path.join(context.info.getCheckoutLocation(), workspaceContext.path));
if (workspaceContext.isFile) {
vscode.window.showTextDocument(location);
} else {
vscode.commands.executeCommand('revealInExplorer', location);
}
}
})();
}
}
function installCLIProxy(context: vscode.ExtensionContext, logger: Log): string | undefined {
const vscodeIpcHookCli = process.env['VSCODE_IPC_HOOK_CLI'];
if (!vscodeIpcHookCli) {
return undefined;
}
const { dir, base } = path.parse(vscodeIpcHookCli);
const ipcHookCli = path.join(dir, 'gitpod-' + base);
const ipcProxy = http.createServer((req, res) => {
const chunks: string[] = [];
req.setEncoding('utf8');
req.on('data', (d: string) => chunks.push(d));
req.pipe(http.request({
socketPath: vscodeIpcHookCli,
method: req.method,
headers: req.headers
}, async res2 => {
if (res2.statusCode === 404) {
const data: { type: 'preview'; url: string } | any = JSON.parse(chunks.join(''));
if (data.type === 'preview') {
// should be aligned with https://github.com/gitpod-io/vscode/blob/4d36a5dbf36870beda891e5dd94ccf087fdc7eb5/src/vs/workbench/api/node/extHostCLIServer.ts#L207-L207
try {
const { url } = data;
await vscode.commands.executeCommand('simpleBrowser.api.open', url, {
viewColumn: vscode.ViewColumn.Beside,
preserveFocus: true
});
res.writeHead(200, { 'content-type': 'application/json' });
res.end(JSON.stringify(''));
} catch (e) {
console.error(e);
const message = e instanceof Error ? e.message : JSON.stringify(e);
res.writeHead(500, { 'content-type': 'application/json' });
res.end(JSON.stringify(message));
}
return;
}
}
res.setHeader('Content-Type', 'application/json');
res2.pipe(res);
}));
});
context.subscriptions.push(new vscode.Disposable(() => ipcProxy.close()));
new Promise((_, reject) => {
ipcProxy.on('error', err => reject(err));
ipcProxy.listen(ipcHookCli);
context.subscriptions.push(new vscode.Disposable(() =>
fs.promises.unlink(ipcHookCli)
));
}).catch(e => {
logger.error('failed to start cli proxy: ' + e);
console.error('failed to start cli proxy:' + e);
});
return ipcHookCli;
}
type TerminalOpenMode = 'tab-before' | 'tab-after' | 'split-left' | 'split-right' | 'split-top' | 'split-bottom';
export async function registerTasks(context: GitpodExtensionContext, createTerminal: (options: vscode.ExtensionTerminalOptions, parent: vscode.Terminal | undefined) => vscode.Terminal): Promise<void> {
const tokenSource = new vscode.CancellationTokenSource();
const token = tokenSource.token;
context.subscriptions.push({
dispose: () => tokenSource.cancel()
});
const tasks = new Map<string, TaskStatus>();
let synched = false;
while (!synched) {
let listener: vscode.Disposable | undefined;
try {
const req = new TasksStatusRequest();
req.setObserve(true);
const stream = context.supervisor.status.tasksStatus(req, context.supervisor.metadata);
const done = () => {
synched = true;
stream.cancel();
};
listener = token.onCancellationRequested(() => done());
await new Promise((resolve, reject) => {
stream.on('end', resolve);
stream.on('error', reject);
stream.on('data', (response: TasksStatusResponse) => {
if (response.getTasksList().every(status => {
tasks.set(status.getTerminal(), status);
return status.getState() !== TaskState.OPENING;
})) {
done();
}
});
});
} catch (err) {
if (!('code' in err && err.code === grpc.status.CANCELLED)) {
context.logger.error('code server: listening task updates failed:', err);
console.error('code server: listening task updates failed:', err);
}
} finally {
listener?.dispose();
}
if (!synched) {
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
context.logger.trace('Task status:', [...tasks.values()].map(status => {
const stateMap = { [TaskState.OPENING]: 'CLOSED', [TaskState.RUNNING]: 'RUNNING', [TaskState.CLOSED]: 'CLOSED' };
return `\t${status.getTerminal()} => ${stateMap[status.getState()]}`;
}).join('\n'));
if (token.isCancellationRequested) {
return;
}
const taskTerminals = new Map<string, SupervisorTerminal>();
try {
const response = await util.promisify(context.supervisor.terminal.list.bind(context.supervisor.terminal, new ListTerminalsRequest(), context.supervisor.metadata, {
deadline: Date.now() + context.supervisor.deadlines.long
}))();
for (const term of response.getTerminalsList()) {
taskTerminals.set(term.getAlias(), term);
}
} catch (e) {
context.logger.error('failed to list task terminals:', e);
console.error('failed to list task terminals:', e);
}
let prevTerminal: vscode.Terminal | undefined;
for (const [alias, taskStatus] of tasks.entries()) {
const taskTerminal = taskTerminals.get(alias);
if (taskTerminal) {
const openMode: TerminalOpenMode | undefined = taskStatus.getPresentation()?.getOpenMode() as TerminalOpenMode;
const parentTerminal = (openMode && openMode !== 'tab-before' && openMode !== 'tab-after') ? prevTerminal : undefined;
const pty = createTaskPty(alias, context, token);
// Delegate creation of the terminal to the extension caller
// if proposed API usage is required.
const terminal = createTerminal(
{
name: taskTerminal.getTitle(),
pty,
iconPath: new vscode.ThemeIcon('terminal')
},
parentTerminal
);
terminal.show();
prevTerminal = terminal;
}
}
}
function createTaskPty(alias: string, context: GitpodExtensionContext, contextToken: vscode.CancellationToken): vscode.Pseudoterminal {
const tokenSource = new vscode.CancellationTokenSource();
contextToken.onCancellationRequested(() => tokenSource.cancel());
const token = tokenSource.token;
const onDidWriteEmitter = new vscode.EventEmitter<string>();
const onDidCloseEmitter = new vscode.EventEmitter<number | void>();
const onDidChangeNameEmitter = new vscode.EventEmitter<string>();
const toDispose = vscode.Disposable.from(onDidWriteEmitter, onDidCloseEmitter, onDidChangeNameEmitter);
token.onCancellationRequested(() => toDispose.dispose());
let pendingWrite = Promise.resolve();
let pendingResize = Promise.resolve();
const pty: vscode.Pseudoterminal = {
onDidWrite: onDidWriteEmitter.event,
onDidClose: onDidCloseEmitter.event,
onDidChangeName: onDidChangeNameEmitter.event,
open: async (dimensions: vscode.TerminalDimensions | undefined) => {
if (dimensions) {
pty.setDimensions!(dimensions);
}
while (!token.isCancellationRequested) {
let notFound = false;
let exitCode: number | undefined;
let listener: vscode.Disposable | undefined;
try {
await new Promise((resolve, reject) => {
const request = new ListenTerminalRequest();
request.setAlias(alias);
const stream = context.supervisor.terminal.listen(request, context.supervisor.metadata);
listener = token.onCancellationRequested(() => stream.cancel());
stream.on('end', resolve);
stream.on('error', reject);
stream.on('data', (response: ListenTerminalResponse) => {
if (response.hasTitle()) {
const title = response.getTitle();
if (title) {
onDidChangeNameEmitter.fire(title);
}
} else if (response.hasData()) {
let data = '';
const buffer = response.getData();
if (typeof buffer === 'string') {
data += buffer;
} else {
data += Buffer.from(buffer).toString();
}
if (data) {
onDidWriteEmitter.fire(data);
}
} else if (response.hasExitCode()) {
exitCode = response.getExitCode();
}
});
});
} catch (e) {
notFound = 'code' in e && e.code === grpc.status.NOT_FOUND;
if (!token.isCancellationRequested && !notFound && !('code' in e && e.code === grpc.status.CANCELLED)) {
context.logger.error(`${alias} terminal: listening failed:`, e);
console.error(`${alias} terminal: listening failed:`, e);
}
} finally {
listener?.dispose();
}
if (token.isCancellationRequested) {
return;
}
if (notFound) {
context.logger.trace(`${alias} terminal not found`);
onDidCloseEmitter.fire();
tokenSource.cancel();
return;
}
if (typeof exitCode === 'number') {
context.logger.trace(`${alias} terminal exited with ${exitCode}`);
onDidCloseEmitter.fire(exitCode);
tokenSource.cancel();
return;
}
await new Promise(resolve => setTimeout(resolve, 2000));
}
},
close: async () => {
if (token.isCancellationRequested) {
return;
}
tokenSource.cancel();
// await to make sure that close is not cause by the extension host process termination
// in such case we don't want to stop supervisor terminals
setTimeout(async () => {
if (contextToken.isCancellationRequested) {
return;
}
// Attempt to kill the pty, it may have already been killed at this
// point but we want to make sure
try {
const request = new ShutdownTerminalRequest();
request.setAlias(alias);
await util.promisify(context.supervisor.terminal.shutdown.bind(context.supervisor.terminal, request, context.supervisor.metadata, {
deadline: Date.now() + context.supervisor.deadlines.short
}))();
context.logger.trace(`${alias} terminal closed`);
} catch (e) {
if (e && e.code === grpc.status.NOT_FOUND) {
// Swallow, the pty has already been killed
} else {
context.logger.error(`${alias} terminal: shutdown failed:`, e);
console.error(`${alias} terminal: shutdown failed:`, e);
}
}
}, 1000);
},
handleInput: async (data: string) => {
if (token.isCancellationRequested) {
return;
}
pendingWrite = pendingWrite.then(async () => {
if (token.isCancellationRequested) {
return;
}
try {
const request = new WriteTerminalRequest();
request.setAlias(alias);
request.setStdin(Buffer.from(data, 'utf8'));