Skip to content

Commit ec84649

Browse files
committed
Implement auth service switching for GitHub Enterprise
Uses a new, proposed alternative authentication provider (switch) to handle authentication and API queries. Fixed microsoft#1793, microsoft#2282. Depends on microsoft/vscode#115940
1 parent 851dd6d commit ec84649

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
"type": "object",
3838
"title": "GitHub Pull Requests",
3939
"properties": {
40+
"githubPullRequests.authenticationProvider": {
41+
"enum": ["github", "github-enterprise"],
42+
"enumDescriptions": [
43+
"github.com",
44+
"GitHub Enterprise Instance"
45+
],
46+
"default": "github",
47+
"description": "The authentication provider and service to query"
48+
},
4049
"githubPullRequests.pullRequestTitle": {
4150
"deprecationMessage": "The pull request title now uses the same defaults as GitHub, and can be edited before create.",
4251
"type": "string",

src/github/credentials.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@ const IGNORE_COMMAND = "Don't show again";
2323
const PROMPT_FOR_SIGN_IN_SCOPE = 'prompt for sign in';
2424
const PROMPT_FOR_SIGN_IN_STORAGE_KEY = 'login';
2525

26-
const AUTH_PROVIDER_ID = 'github';
2726
const SCOPES = ['read:user', 'user:email', 'repo'];
2827

28+
export enum AuthProvider {
29+
github = 'github',
30+
'github-enterprise' = 'github-enterprise'
31+
}
32+
2933
export interface GitHub {
3034
octokit: Octokit;
3135
graphql: ApolloClient<NormalizedCacheObject> | null;
@@ -51,7 +55,17 @@ export class CredentialStore implements vscode.Disposable {
5155
}
5256

5357
public async initialize(): Promise<void> {
54-
const session = await vscode.authentication.getSession(AUTH_PROVIDER_ID, SCOPES, { createIfNone: false });
58+
const authProviderId = vscode.workspace.getConfiguration('githubPullRequests').get<AuthProvider>('authenticationProvider') || AuthProvider.github;
59+
const session = await vscode.authentication.getSession(authProviderId, SCOPES, { createIfNone: false });
60+
61+
if (authProviderId === AuthProvider['github-enterprise']) {
62+
try {
63+
vscode.Uri.parse(vscode.workspace.getConfiguration('github-enterprise').get<string>('uri') || '', true);
64+
} catch {
65+
Logger.debug(`GitHub Enterprise provider selected without Uri.`, 'Authentication');
66+
return;
67+
}
68+
}
5569

5670
if (session) {
5771
const token = session.accessToken;
@@ -175,22 +189,37 @@ export class CredentialStore implements vscode.Disposable {
175189
}
176190

177191
private async getSessionOrLogin(): Promise<string> {
178-
const session = await vscode.authentication.getSession(AUTH_PROVIDER_ID, SCOPES, { createIfNone: true });
192+
const authProviderId = vscode.workspace.getConfiguration('githubPullRequests').get<AuthProvider>('authenticationProvider') || AuthProvider.github;
193+
const session = await vscode.authentication.getSession(authProviderId, SCOPES, { createIfNone: true });
179194
this._sessionId = session.id;
180195
return session.accessToken;
181196
}
182197

183198
private async createHub(token: string): Promise<GitHub> {
199+
const authProviderId = vscode.workspace.getConfiguration('githubPullRequests').get<AuthProvider>('authenticationProvider') || AuthProvider.github;
200+
let baseUrl = 'https://api.github.com';
201+
if (authProviderId === AuthProvider['github-enterprise']) {
202+
const server = vscode.workspace.getConfiguration('github-enterprise').get<string>('uri') || '';
203+
const serverUri = vscode.Uri.parse(server, true);
204+
baseUrl = `${serverUri.scheme}://${serverUri.authority}/api/v3`;
205+
}
206+
184207
const octokit = new Octokit({
185208
request: { agent },
186209
userAgent: 'GitHub VSCode Pull Requests',
187210
// `shadow-cat-preview` is required for Draft PR API access -- https://developer.github.com/v3/previews/#draft-pull-requests
188211
previews: ['shadow-cat-preview'],
189212
auth: `${token || ''}`,
213+
baseUrl: baseUrl,
190214
});
191215

216+
if (authProviderId === AuthProvider['github-enterprise']) {
217+
const server = vscode.workspace.getConfiguration('github-enterprise').get<string>('uri') || '';
218+
const serverUri = vscode.Uri.parse(server, true);
219+
baseUrl = `${serverUri.scheme}://${serverUri.authority}/api`;
220+
}
192221
const graphql = new ApolloClient({
193-
link: link('https://api.github.com', token || ''),
222+
link: link(baseUrl, token || ''),
194223
cache: new InMemoryCache(),
195224
defaultOptions: {
196225
query: {

0 commit comments

Comments
 (0)