-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathgitlab.ts
106 lines (93 loc) · 3.21 KB
/
gitlab.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
import type { Disposable, QuickInputButton } from 'vscode';
import { env, ThemeIcon, Uri, window } from 'vscode';
import type { SelfHostedIntegrationId } from '../../../constants.integrations';
import { HostingIntegrationId } from '../../../constants.integrations';
import type { Container } from '../../../container';
import type {
IntegrationAuthenticationService,
IntegrationAuthenticationSessionDescriptor,
} from './integrationAuthentication';
import {
CloudIntegrationAuthenticationProvider,
LocalIntegrationAuthenticationProvider,
} from './integrationAuthentication';
import type { ProviderAuthenticationSession } from './models';
type GitLabId = HostingIntegrationId.GitLab | SelfHostedIntegrationId.GitLabSelfHosted;
export class GitLabLocalAuthenticationProvider extends LocalIntegrationAuthenticationProvider<GitLabId> {
constructor(
container: Container,
authenticationService: IntegrationAuthenticationService,
protected readonly authProviderId: GitLabId,
) {
super(container, authenticationService);
}
override async createSession(
descriptor?: IntegrationAuthenticationSessionDescriptor,
): Promise<ProviderAuthenticationSession | undefined> {
const input = window.createInputBox();
input.ignoreFocusOut = true;
const disposables: Disposable[] = [];
let token;
try {
const infoButton: QuickInputButton = {
iconPath: new ThemeIcon(`link-external`),
tooltip: 'Open the GitLab Access Tokens Page',
};
token = await new Promise<string | undefined>(resolve => {
disposables.push(
input.onDidHide(() => resolve(undefined)),
input.onDidChangeValue(() => (input.validationMessage = undefined)),
input.onDidAccept(() => {
const value = input.value.trim();
if (!value) {
input.validationMessage = 'A personal access token is required';
return;
}
resolve(value);
}),
input.onDidTriggerButton(e => {
if (e === infoButton) {
void env.openExternal(
Uri.parse(
`https://${descriptor?.domain ?? 'gitlab.com'}/-/profile/personal_access_tokens`,
),
);
}
}),
);
input.password = true;
input.title = `GitLab Authentication${descriptor?.domain ? ` \u2022 ${descriptor.domain}` : ''}`;
input.placeholder = `Requires ${descriptor?.scopes.join(', ') ?? 'all'} scopes`;
input.prompt = `Paste your [GitLab Personal Access Token](https://${
descriptor?.domain ?? 'gitlab.com'
}/-/user_settings/personal_access_tokens?name=GitLens+Access+token&scopes=${
descriptor?.scopes.join(',') ?? 'all'
} "Get your GitLab Access Token")`;
input.buttons = [infoButton];
input.show();
});
} finally {
input.dispose();
disposables.forEach(d => void d.dispose());
}
if (!token) return undefined;
return {
id: this.getSessionId(descriptor),
accessToken: token,
scopes: descriptor?.scopes ?? [],
account: {
id: '',
label: '',
},
cloud: false,
};
}
}
export class GitLabCloudAuthenticationProvider extends CloudIntegrationAuthenticationProvider<GitLabId> {
protected override get authProviderId(): GitLabId {
return HostingIntegrationId.GitLab;
}
protected override getCompletionInputTitle(): string {
return 'Connect to GitLab';
}
}