-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.ts
1114 lines (980 loc) · 32.7 KB
/
session.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
"use strict";
import { Agent as HttpAgent } from "http";
import { Agent as HttpsAgent } from "https";
import HttpsProxyAgent from "https-proxy-agent";
import { isEqual } from "lodash-es";
import * as path from "path";
import * as url from "url";
import {
CancellationToken,
Connection,
Emitter,
Event,
MessageActionItem,
WorkspaceFolder
} from "vscode-languageserver";
import { CodeStreamAgent } from "./agent";
import { AgentError, ServerError } from "./agentError";
import {
ApiProvider,
ApiProviderLoginResponse,
CodeStreamApiMiddlewareContext,
LoginOptions,
MessageType,
RTMessage
} from "./api/apiProvider";
import { CodeStreamApiProvider } from "./api/codestream/codestreamApi";
import { Team, User } from "./api/extensions";
import {
ApiVersionCompatibilityChangedEvent,
VersionCompatibilityChangedEvent,
VersionMiddlewareManager
} from "./api/middleware/versionMiddleware";
import { Container, SessionContainer } from "./container";
import { DocumentEventHandler } from "./documentEventHandler";
import { setGitPath } from "./git/git";
import { Logger } from "./logger";
import {
ApiRequestType,
ApiVersionCompatibility,
BaseAgentOptions,
BootstrapRequestType,
ChangeDataType,
CodeStreamEnvironment,
ConfirmRegistrationRequest,
ConfirmRegistrationRequestType,
ConnectionStatus,
DidChangeApiVersionCompatibilityNotificationType,
DidChangeConnectionStatusNotificationType,
DidChangeDataNotificationType,
DidChangeServerUrlNotificationType,
DidChangeVersionCompatibilityNotificationType,
DidEncounterMaintenanceModeNotificationType,
DidFailLoginNotificationType,
DidLoginNotificationType,
DidLogoutNotificationType,
DidStartLoginNotificationType,
FetchMarkerLocationsRequestType,
GetAccessTokenRequestType,
GetInviteInfoRequest,
GetInviteInfoRequestType,
isLoginFailResponse,
LoginResponse,
LogoutReason,
OtcLoginRequest,
OtcLoginRequestType,
PasswordLoginRequest,
PasswordLoginRequestType,
RegisterUserRequest,
RegisterUserRequestType,
ReportingMessageType,
RestartRequiredNotificationType,
SetServerUrlRequest,
SetServerUrlRequestType,
ThirdPartyProviders,
TokenLoginRequest,
TokenLoginRequestType,
UIStateRequestType,
VerifyConnectivityRequestType,
VerifyConnectivityResponse,
VersionCompatibility
} from "./protocol/agent.protocol";
import {
CSApiCapabilities,
CSCodemark,
CSCompany,
CSLoginResponse,
CSMarker,
CSMarkerLocations,
CSMe,
CSPost,
CSRegisterResponse,
CSRepository,
CSStream,
CSTeam,
CSUser,
LoginResult
} from "./protocol/api.protocol";
import { log, memoize, registerDecoratedHandlers, registerProviders } from "./system";
// FIXME: Must keep this in sync with vscode-codestream/src/api/session.ts
const envRegex = /https?:\/\/((?:(\w+)-)?api|localhost)\.codestream\.(?:us|com)(?::\d+$)?/i;
const FIRST_SESSION_TIMEOUT = 12 * 60 * 60 * 1000; // first session "times out" after 12 hours
export const loginApiErrorMappings: { [k: string]: LoginResult } = {
"USRC-1001": LoginResult.InvalidCredentials,
"USRC-1010": LoginResult.NotConfirmed,
"AUTH-1002": LoginResult.InvalidToken,
"AUTH-1003": LoginResult.InvalidToken,
"AUTH-1004": LoginResult.ExpiredToken,
"AUTH-1005": LoginResult.ExpiredToken,
"USRC-1005": LoginResult.InvalidToken,
"USRC-1002": LoginResult.InvalidToken,
"USRC-1006": LoginResult.AlreadyConfirmed,
// "RAPI-1001": "missing parameter" // shouldn't ever happen
"RAPI-1003": LoginResult.InvalidToken,
"USRC-1012": LoginResult.NotOnTeam,
"VERS-1001": LoginResult.VersionUnsupported,
"USRC-1023": LoginResult.MaintenanceMode,
"USRC-1024": LoginResult.MustSetPassword,
"USRC-1022": LoginResult.ProviderConnectFailed,
"USRC-1015": LoginResult.MultipleWorkspaces, // deprecated in favor of below...
"PRVD-1002": LoginResult.MultipleWorkspaces,
"PRVD-1005": LoginResult.SignupRequired,
"PRVD-1006": LoginResult.SignInRequired,
"USRC-1020": LoginResult.InviteConflict,
"AUTH-1006": LoginResult.TokenNotFound
};
export enum SessionStatus {
SignedOut = "signedOut",
SignedIn = "signedIn"
}
export interface SessionStatusChangedEvent {
getStatus(): SessionStatus;
session: CodeStreamSession;
}
export interface TelemetryData {
hasCreatedPost: boolean;
}
export interface VersionInfo {
extension: {
build: string;
buildEnv: string;
version: string;
versionFormatted: string;
};
ide: {
name: string;
version: string;
detail: string;
};
}
export class CodeStreamSession {
private _onDidChangeCodemarks = new Emitter<CSCodemark[]>();
get onDidChangeCodemarks(): Event<CSCodemark[]> {
return this._onDidChangeCodemarks.event;
}
private _onDidChangeCurrentUser = new Emitter<CSMe>();
get onDidChangeCurrentUser(): Event<CSMe> {
return this._onDidChangeCurrentUser.event;
}
private _onDidChangeMarkerLocations = new Emitter<CSMarkerLocations[]>();
get onDidChangeMarkerLocations(): Event<CSMarkerLocations[]> {
return this._onDidChangeMarkerLocations.event;
}
private _onDidChangeMarkers = new Emitter<CSMarker[]>();
get onDidChangeMarkers(): Event<CSMarker[]> {
return this._onDidChangeMarkers.event;
}
private _onDidChangePosts = new Emitter<CSPost[]>();
get onDidChangePosts(): Event<CSPost[]> {
return this._onDidChangePosts.event;
}
private _onDidChangeRepositories = new Emitter<CSRepository[]>();
get onDidChangeRepositories(): Event<CSRepository[]> {
return this._onDidChangeRepositories.event;
}
private _onDidChangeStreams = new Emitter<CSStream[]>();
get onDidChangeStreams(): Event<CSStream[]> {
return this._onDidChangeStreams.event;
}
private _onDidChangeUsers = new Emitter<CSUser[]>();
get onDidChangeUsers(): Event<CSUser[]> {
return this._onDidChangeUsers.event;
}
private _onDidChangeTeams = new Emitter<CSTeam[]>();
get onDidChangeTeams(): Event<CSTeam[]> {
return this._onDidChangeTeams.event;
}
private _onDidRequestReset = new Emitter<void>();
get onDidRequestReset(): Event<void> {
return this._onDidRequestReset.event;
}
private _onDidChangeSessionStatus = new Emitter<SessionStatusChangedEvent>();
get onDidChangeSessionStatus(): Event<SessionStatusChangedEvent> {
return this._onDidChangeSessionStatus.event;
}
get proxyAgent(): HttpsAgent | HttpsProxyAgent | undefined {
return this._httpsAgent;
}
private readonly _httpsAgent: HttpsAgent | HttpsProxyAgent | undefined;
private readonly _httpAgent: HttpAgent | undefined; // used if api server is http
private readonly _readyPromise: Promise<void>;
// in-memory store of what UI the user is current looking at
private uiState: string | undefined;
private _documentEventHandler: DocumentEventHandler | undefined;
// HACK in certain scenarios the agent may want to use more performance-intensive
// operations when handling document change and saves. This is true for when
// a user is looking at the review screen, where we need to be able to live-update
// the view based on documents changing & saving, as well as git operations removing
// and/or squashing commits.
get useEnhancedDocumentChangeHandler(): boolean {
return this.uiState === "new-review" || this.uiState === "people";
}
constructor(
public readonly agent: CodeStreamAgent,
private readonly _connection: Connection,
private readonly _options: BaseAgentOptions
) {
this._readyPromise = new Promise<void>(resolve =>
this.agent.onReady(() => {
Logger.log("Agent is ready");
resolve();
})
);
this._environment = this.getEnvironment(this._options.serverUrl);
Container.initialize(agent, this);
const redactProxyPasswdRegex = /(http:\/\/.*:)(.*)(@.*)/gi;
if (
_options.proxySupport === "override" ||
(_options.proxySupport == null && _options.proxy != null)
) {
if (_options.proxy != null) {
const redactedUrl = _options.proxy.url.replace(redactProxyPasswdRegex, "$1*****$3");
Logger.log(
`Proxy support is in override with url=${redactedUrl}, strictSSL=${_options.proxy.strictSSL}`
);
this._httpsAgent = new HttpsProxyAgent({
...url.parse(_options.proxy.url),
rejectUnauthorized: _options.proxy.strictSSL
} as any);
} else {
Logger.log("Proxy support is in override, but no proxy settings were provided");
}
} else if (_options.proxySupport === "on") {
const proxyUrl = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
if (proxyUrl) {
const strictSSL = _options.proxy ? _options.proxy.strictSSL : true;
const redactedUrl = proxyUrl.replace(redactProxyPasswdRegex, "$1*****$3");
Logger.log(`Proxy support is on with url=${redactedUrl}, strictSSL=${strictSSL}`);
let proxyUri;
try {
proxyUri = url.parse(proxyUrl);
} catch {}
if (proxyUri) {
this._httpsAgent = new HttpsProxyAgent({
...proxyUri,
rejectUnauthorized: this.rejectUnauthorized
} as any);
}
} else {
Logger.log("Proxy support is on, but no proxy url was found");
}
} else {
Logger.log("Proxy support is off");
}
if (!this._httpsAgent) {
this._httpsAgent = new HttpsAgent({
rejectUnauthorized: this.rejectUnauthorized
});
}
// if our api server is http (on-prem installation), create a separate http agent
const protocol = url.parse(_options.serverUrl).protocol;
if (protocol === "http:") {
this._httpAgent = new HttpAgent();
}
this._api = new CodeStreamApiProvider(
_options.serverUrl,
this.versionInfo,
this._httpAgent || this._httpsAgent,
this.rejectUnauthorized
);
this._api.useMiddleware({
get name() {
return "MaintenanceMode";
},
onResponse: async (context: Readonly<CodeStreamApiMiddlewareContext>, _) => {
if (
context.response?.headers.get("X-CS-API-Maintenance-Mode") &&
this._codestreamAccessToken
) {
this._didEncounterMaintenanceMode();
}
}
});
const versionManager = new VersionMiddlewareManager(this._api);
versionManager.onDidChangeCompatibility(this.onVersionCompatibilityChanged, this);
versionManager.onDidChangeApiCompatibility(this.onApiVersionCompatibilityChanged, this);
// this.connection.onHover(e => MarkerHandler.onHover(e));
registerDecoratedHandlers(this.agent);
this.agent.registerHandler(UIStateRequestType, e => {
if (e && e.context && e.context.panelStack && e.context.panelStack[0]) {
this.uiState = e.context.panelStack[0];
} else {
this.uiState = undefined;
}
});
this.agent.registerHandler(VerifyConnectivityRequestType, () => this.verifyConnectivity());
this.agent.registerHandler(GetAccessTokenRequestType, e => {
return { accessToken: this._codestreamAccessToken! };
});
this.agent.registerHandler(PasswordLoginRequestType, e => this.passwordLogin(e));
this.agent.registerHandler(TokenLoginRequestType, e => this.tokenLogin(e));
this.agent.registerHandler(OtcLoginRequestType, e => this.otcLogin(e));
this.agent.registerHandler(RegisterUserRequestType, e => this.register(e));
this.agent.registerHandler(ConfirmRegistrationRequestType, e => this.confirmRegistration(e));
this.agent.registerHandler(GetInviteInfoRequestType, e => this.getInviteInfo(e));
this.agent.registerHandler(ApiRequestType, (e, cancellationToken: CancellationToken) =>
this.api.fetch(e.url, e.init, e.token)
);
this.agent.registerHandler(SetServerUrlRequestType, e => this.setServerUrl(e));
this.agent.registerHandler(
BootstrapRequestType,
async (e, cancellationToken: CancellationToken) => {
const { companies, repos, streams, teams, users } = SessionContainer.instance();
const promise = Promise.all([
companies.get(),
repos.get(),
streams.get(),
teams.get(),
users.getUnreads({}),
users.get(),
users.getPreferences()
]);
const [
companiesResponse,
reposResponse,
streamsResponse,
teamsResponse,
unreadsResponse,
usersResponse,
preferencesResponse
] = await promise;
return {
companies: companiesResponse.companies,
preferences: preferencesResponse.preferences,
repos: reposResponse.repos,
streams: streamsResponse.streams,
teams: teamsResponse.teams,
unreads: unreadsResponse.unreads,
users: usersResponse.users,
providers: this.providers,
apiCapabilities: this.apiCapabilities
};
}
);
this.agent.registerHandler(FetchMarkerLocationsRequestType, r =>
this.api.fetchMarkerLocations(r)
);
}
setServerUrl(options: SetServerUrlRequest) {
this._options.serverUrl = options.serverUrl;
this._options.disableStrictSSL = options.disableStrictSSL;
this._environment = this.getEnvironment(this._options.serverUrl);
this._api?.setServerUrl(this._options.serverUrl);
this.agent.sendNotification(DidChangeServerUrlNotificationType, {
serverUrl: options.serverUrl
});
}
private _didEncounterMaintenanceMode() {
this.agent.sendNotification(DidEncounterMaintenanceModeNotificationType, {
teamId: this._teamId,
token: {
email: this._email!,
url: this._options.serverUrl,
value: this._codestreamAccessToken!
}
});
}
private async onRTMessageReceived(e: RTMessage) {
switch (e.type) {
case MessageType.Codemarks:
const codemarks = await SessionContainer.instance().codemarks.enrichCodemarks(e.data);
this._onDidChangeCodemarks.fire(codemarks);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Codemarks,
data: codemarks
});
break;
case MessageType.Companies:
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Companies,
data: e.data
});
break;
case MessageType.Connection:
if (e.data.status === ConnectionStatus.Reconnected && e.data.reset) {
void SessionContainer.instance().session.reset();
}
this.agent.sendNotification(DidChangeConnectionStatusNotificationType, e.data);
break;
case MessageType.MarkerLocations:
this._onDidChangeMarkerLocations.fire(e.data);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.MarkerLocations,
data: e.data
});
break;
case MessageType.Markers:
this._onDidChangeMarkers.fire(e.data);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Markers,
data: e.data
});
break;
case MessageType.Posts:
const posts = await SessionContainer.instance().posts.enrichPosts(e.data);
this._onDidChangePosts.fire(posts);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Posts,
data: posts
});
break;
case MessageType.Preferences:
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Preferences,
data: e.data
});
break;
case MessageType.Repositories:
this._onDidChangeRepositories.fire(e.data);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Repositories,
data: e.data
});
break;
case MessageType.Reviews:
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Reviews,
data: e.data
});
break;
case MessageType.Streams:
this._onDidChangeStreams.fire(e.data);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Streams,
data: e.data
});
break;
case MessageType.Teams:
this._onDidChangeTeams.fire(e.data);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Teams,
data: e.data
});
break;
case MessageType.Unreads:
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Unreads,
data: e.data
});
break;
case MessageType.Users:
const me = e.data.find(u => u.id === this._userId) as CSMe | undefined;
if (me != null) {
if (me.inMaintenanceMode) {
return this._didEncounterMaintenanceMode();
}
this._onDidChangeCurrentUser.fire(me as CSMe);
}
this._onDidChangeUsers.fire(e.data);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Users,
data: e.data
});
break;
}
}
@log()
private onVersionCompatibilityChanged(e: VersionCompatibilityChangedEvent) {
this.agent.sendNotification(DidChangeVersionCompatibilityNotificationType, e);
if (e.compatibility === VersionCompatibility.UnsupportedUpgradeRequired) {
this.logout(LogoutReason.UnsupportedVersion);
}
}
@log()
private async onApiVersionCompatibilityChanged(e: ApiVersionCompatibilityChangedEvent) {
this.agent.sendNotification(DidChangeApiVersionCompatibilityNotificationType, e);
if (
e.compatibility !== ApiVersionCompatibility.ApiUpgradeRequired &&
SessionContainer.isInitialized()
) {
const oldCapabilities = SessionContainer.instance().session.apiCapabilities;
const newCapabilities = await this.api.getApiCapabilities();
const currentTeam = await SessionContainer.instance().teams.getByIdFromCache(this.teamId);
if (!isEqual(oldCapabilities, newCapabilities)) {
this.registerApiCapabilities(newCapabilities, currentTeam);
this.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.ApiCapabilities,
data: newCapabilities
});
}
}
}
private _api: ApiProvider | undefined;
get api() {
return this._api!;
}
private _codestreamUserId: string | undefined;
get codestreamUserId() {
return this._codestreamUserId!;
}
private _email: string | undefined;
get email() {
return this._email!;
}
private _codestreamAccessToken: string | undefined;
get codestreamAccessToken() {
return this._codestreamAccessToken;
}
private _environment: CodeStreamEnvironment | string = CodeStreamEnvironment.Unknown;
get environment() {
return this._environment;
}
get disableStrictSSL(): boolean {
return this._options.disableStrictSSL != null ? this._options.disableStrictSSL : false;
}
get rejectUnauthorized(): boolean {
return !this.disableStrictSSL;
}
private _status: SessionStatus = SessionStatus.SignedOut;
get status() {
return this._status;
}
private setStatus(status: SessionStatus) {
this._status = status;
const e: SessionStatusChangedEvent = {
getStatus: () => this._status,
session: this
};
this._onDidChangeSessionStatus.fire(e);
}
private _teamId: string | undefined;
get teamId() {
return this._teamId!;
}
private _apiCapabilities: CSApiCapabilities = {};
get apiCapabilities() {
return this._apiCapabilities;
}
private _telemetryData: TelemetryData = {
hasCreatedPost: false
};
get telemetryData() {
return this._telemetryData;
}
set telemetryData(data: TelemetryData) {
this._telemetryData = data;
}
private _userId: string | undefined;
get userId() {
return this._userId!;
}
private _providers: ThirdPartyProviders = {};
get providers() {
return this._providers!;
}
@memoize
get versionInfo(): Readonly<VersionInfo> {
return {
extension: { ...this._options.extension },
ide: { ...this._options.ide }
};
}
get workspace() {
return this._connection.workspace;
}
public async getWorkspaceFolders() {
if (this.agent.supportsWorkspaces) {
return (await this.workspace.getWorkspaceFolders()) || [];
}
return new Promise<WorkspaceFolder[] | null>(resolve => {
if (this.agent.rootUri) {
const uri =
this.agent.rootUri[this.agent.rootUri.length - 1] === "/"
? this.agent.rootUri.substring(0, this.agent.rootUri.length - 1)
: this.agent.rootUri;
resolve([
{
uri: uri,
name: path.basename(this.agent.rootUri)
}
]);
} else {
resolve([]);
}
});
}
@log({ singleLine: true })
async verifyConnectivity(): Promise<VerifyConnectivityResponse> {
return this.api.verifyConnectivity();
}
@log({ singleLine: true })
async passwordLogin(request: PasswordLoginRequest) {
const cc = Logger.getCorrelationContext();
Logger.log(
cc,
`Logging ${request.email} into CodeStream (@ ${this._options.serverUrl}) via password`
);
return this.login({
type: "credentials",
...request
});
}
@log({ singleLine: true })
async tokenLogin(request: TokenLoginRequest) {
const { token } = request;
const cc = Logger.getCorrelationContext();
Logger.log(
cc,
`Logging ${token.email} into CodeStream (@ ${token.url}) via authentication token...`
);
return this.login({
type: "token",
...request
});
}
@log({ singleLine: true })
async otcLogin(request: OtcLoginRequest) {
const cc = Logger.getCorrelationContext();
Logger.log(cc, `Logging into CodeStream (@ ${this._options.serverUrl}) via otc code...`);
try {
return this.login({
type: "otc",
...request
});
} catch (e) {
debugger;
throw new Error();
}
}
@log({
singleLine: true
})
async login(options: LoginOptions): Promise<LoginResponse> {
if (this.status === SessionStatus.SignedIn) {
Container.instance().errorReporter.reportMessage({
type: ReportingMessageType.Warning,
source: "agent",
message: "There was a redundant attempt to login while already logged in.",
extra: {
loginType: options.type
}
});
return { error: LoginResult.AlreadySignedIn };
}
this.agent.sendNotification(DidStartLoginNotificationType, undefined);
let response: ApiProviderLoginResponse;
try {
response = await this.api.login(options);
} catch (ex) {
this.agent.sendNotification(DidFailLoginNotificationType, undefined);
if (ex instanceof ServerError) {
if (ex.statusCode !== undefined && ex.statusCode >= 400 && ex.statusCode < 500) {
let error = loginApiErrorMappings[ex.info.code] || LoginResult.Unknown;
if (error === LoginResult.ProviderConnectFailed) {
Container.instance().telemetry.track({
eventName: "Provider Connect Failed",
properties: {
Error: ex.info && ex.info.error,
Provider: ex.info && ex.info.provider
}
});
// map the reason for provider auth failure
error = loginApiErrorMappings[ex.info.error];
}
return {
error: error,
extra: ex.info
};
}
}
// api.login() will throw a failed response object if it needs to send some extra data back
if (isLoginFailResponse(ex)) {
return ex;
}
Container.instance().errorReporter.reportMessage({
type: ReportingMessageType.Error,
message: "Unexpected error logging in",
source: "agent",
extra: {
...ex
}
});
throw AgentError.wrap(ex, `Login failed:\n${ex.message}`);
}
const token = response.token;
this._codestreamAccessToken = token.value;
this._teamId = (this._options as any).teamId = token.teamId;
this._codestreamUserId = response.user.id;
const currentTeam = response.teams.find(t => t.id === this._teamId)!;
this.registerApiCapabilities(response.capabilities || {}, currentTeam);
if (response.provider === "codestream") {
if (
currentTeam.providerInfo !== undefined &&
Object.keys(currentTeam.providerInfo).length > 0
) {
// the user is using email/password for a CS team and being put into another type team
return { error: LoginResult.InvalidCredentials };
}
}
// note that there are no integrations if the api host is using http (as opposed to https),
// because OAuth won't work when calling back to http
this._providers = this._httpAgent ? {} : currentTeam.providerHosts || {};
registerProviders(this._providers, this);
const cc = Logger.getCorrelationContext();
// after initializing, wait for the initial search of git repositories to complete,
// otherwise newly matched repos might be returned to the webview before the bootstrap
// request can be processed, resulting in bad repo data known by the webview
// see https://trello.com/c/1IjQLhzh - Colin
SessionContainer.initialize(this);
await SessionContainer.instance().git.ensureSearchComplete();
// re-register to acknowledge lsp handlers from newly instantiated classes
registerDecoratedHandlers(this.agent);
// Make sure to update this after the slack/msteams switch as the userId will change
this._userId = response.user.id;
this._email = response.user.email;
this.setStatus(SessionStatus.SignedIn);
await setGitPath(this._options.gitPath);
this.api.onDidReceiveMessage(e => this.onRTMessageReceived(e), this);
Logger.log(cc, `Subscribing to real-time events...`);
await this.api.subscribe();
this._documentEventHandler = new DocumentEventHandler(
this,
SessionContainer.instance().session.agent.documents
);
SessionContainer.instance().git.onRepositoryCommitHashChanged(repo => {
SessionContainer.instance().markerLocations.flushUncommittedLocations(repo);
});
SessionContainer.instance().git.onRepositoryChanged(data => {
SessionContainer.instance().session.agent.sendNotification(DidChangeDataNotificationType, {
type: ChangeDataType.Commits,
data: data
});
});
// be sure to alias first if necessary
if ((options as OtcLoginRequest).alias || (options as TokenLoginRequest).alias) {
Container.instance().telemetry.alias(this._codestreamUserId);
}
// Initialize tracking
this.initializeTelemetry(response.user, currentTeam, response.companies);
const loginResponse = {
loginResponse: { ...response },
state: {
token: token,
capabilities: this.api.capabilities,
email: this._email!,
environment: this._environment,
serverUrl: this._options.serverUrl!,
teamId: this._teamId!,
userId: response.user.id
}
};
setImmediate(() =>
this.agent.sendNotification(DidLoginNotificationType, { data: loginResponse })
);
if (!response.user.timeZone) {
const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
this.api.updateUser({ timeZone });
}
return loginResponse;
}
@log({
singleLine: true
})
async register(request: RegisterUserRequest) {
function isCSLoginResponse(r: CSRegisterResponse | CSLoginResponse): r is CSLoginResponse {
return (r as any).accessToken !== undefined;
}
try {
const response = await (this._api as CodeStreamApiProvider).register(request);
if (isCSLoginResponse(response)) {
Container.instance().telemetry.alias(response.user.id);
if (response.teams.length === 0) {
return { status: LoginResult.NotOnTeam, token: response.accessToken };
}
this._teamId = response.teams[0].id;
return { status: LoginResult.AlreadyConfirmed, token: response.accessToken };
} else {
if (response.user) Container.instance().telemetry.alias(response.user.id);
return { status: LoginResult.Success };
}
} catch (error) {
if (error instanceof ServerError) {
if (error.statusCode !== undefined && error.statusCode >= 400 && error.statusCode < 500) {
return { status: loginApiErrorMappings[error.info.code] || LoginResult.Unknown };
}
}
Container.instance().errorReporter.reportMessage({
type: ReportingMessageType.Error,
message: "Unexpected error during registration",
source: "agent",
extra: {
...error
}
});
throw AgentError.wrap(error, `Registration failed:\n${error.message}`);
}
}
@log({ singleLine: true })
async confirmRegistration(request: ConfirmRegistrationRequest) {
try {
const response = await (this._api as CodeStreamApiProvider).confirmRegistration(request);
Container.instance().telemetry.alias(response.user.id);
if (response.teams.length === 0) {
return { status: LoginResult.NotOnTeam, token: response.accessToken };
}
this._teamId = response.teams[0].id;
return { status: LoginResult.Success, token: response.accessToken };
} catch (error) {
if (error instanceof ServerError) {
if (error.statusCode !== undefined && error.statusCode >= 400 && error.statusCode < 500) {
return { status: loginApiErrorMappings[error.info.code] || LoginResult.Unknown };
}
}
Container.instance().errorReporter.reportMessage({
type: ReportingMessageType.Error,
message: "Unexpected error confirming registration",
source: "agent",
extra: {
...error
}
});
throw AgentError.wrap(error, `Registration confirmation failed:\n${error.message}`);
// }
}
}
@log({ singleLine: true })
async getInviteInfo(request: GetInviteInfoRequest) {
try {
const response = await (this._api as CodeStreamApiProvider).getInviteInfo(request);
return { status: LoginResult.Success, info: response };
} catch (error) {
if (error instanceof ServerError) {
if (error.statusCode !== undefined && error.statusCode >= 400 && error.statusCode < 500) {
return { status: loginApiErrorMappings[error.info.code] || LoginResult.Unknown };
}
}
Container.instance().errorReporter.reportMessage({
type: ReportingMessageType.Error,
message: "Unexpected error getting invite info",
source: "agent",
extra: {
...error
}
});
throw AgentError.wrap(error, `Get invite info failed:\n${error.message}`);
}
}
@log()
logout(reason: LogoutReason) {
this.setStatus(SessionStatus.SignedOut);
return this.agent.sendNotification(DidLogoutNotificationType, { reason: reason });
}
async ready() {
return this._readyPromise;
}
@log()
async reset() {
this._onDidRequestReset.fire(undefined);
}
@log()
showErrorMessage<T extends MessageActionItem>(message: string, ...actions: T[]) {
return this._connection.window.showErrorMessage(message, ...actions);
}
@log()
showInformationMessage<T extends MessageActionItem>(message: string, ...actions: T[]) {
return this._connection.window.showInformationMessage(message, ...actions);
}
@log()
showWarningMessage<T extends MessageActionItem>(message: string, ...actions: T[]) {
return this._connection.window.showWarningMessage(message, ...actions);
}