Skip to content

Commit bce93be

Browse files
committed
move github credential provider to git
1 parent 065bb1b commit bce93be

5 files changed

Lines changed: 32 additions & 316 deletions

File tree

extensions/git/src/github.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as vscode from 'vscode';
7+
import { CredentialsProvider, Credentials } from './api/git';
8+
9+
export class GitHubCredentialProvider implements CredentialsProvider {
10+
11+
async getCredentials(host: vscode.Uri): Promise<Credentials | undefined> {
12+
if (!/github\.com/i.test(host.authority)) {
13+
return;
14+
}
15+
16+
const session = await this.getSession();
17+
return { username: session.account.id, password: await session.getAccessToken() };
18+
}
19+
20+
private async getSession(): Promise<vscode.AuthenticationSession> {
21+
const authenticationSessions = await vscode.authentication.getSessions('github', ['repo']);
22+
23+
if (authenticationSessions.length) {
24+
return await authenticationSessions[0];
25+
} else {
26+
return await vscode.authentication.login('github', ['repo']);
27+
}
28+
}
29+
}

extensions/git/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as path from 'path';
2222
import * as fs from 'fs';
2323
import { GitTimelineProvider } from './timelineProvider';
2424
import { registerAPICommands } from './api/api1';
25+
import { GitHubCredentialProvider } from './github';
2526

2627
const deactivateTasks: { (): Promise<any>; }[] = [];
2728

@@ -37,6 +38,7 @@ async function createModel(context: ExtensionContext, outputChannel: OutputChann
3738

3839
const askpass = await Askpass.create(outputChannel);
3940
disposables.push(askpass);
41+
context.subscriptions.push(askpass.registerCredentialsProvider(new GitHubCredentialProvider()));
4042

4143
const git = new Git({ gitPath: info.path, version: info.version, env: askpass.getEnv() });
4244
const model = new Model(git, askpass, context.globalState, outputChannel);

extensions/github-authentication/package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
"activationEvents": [
1515
"*"
1616
],
17-
"extensionDependencies": [
18-
"vscode.git"
19-
],
2017
"aiKey": "AIF-d9b70cd4-b9f9-4d70-929b-a071c400b217",
2118
"main": "./out/extension.js",
2219
"scripts": {

extensions/github-authentication/src/extension.ts

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,6 @@ import { GitHubAuthenticationProvider, onDidChangeSessions } from './github';
88
import { uriHandler } from './githubServer';
99
import Logger from './common/logger';
1010
import TelemetryReporter from 'vscode-extension-telemetry';
11-
import { GitExtension, CredentialsProvider, Credentials } from './typings/git';
12-
13-
class GitHubCredentialProvider implements CredentialsProvider {
14-
15-
async getCredentials(host: vscode.Uri): Promise<Credentials | undefined> {
16-
if (!/github\.com/i.test(host.authority)) {
17-
return;
18-
}
19-
20-
const session = await this.getSession();
21-
return { username: session.account.id, password: await session.getAccessToken() };
22-
}
23-
24-
private async getSession(): Promise<vscode.AuthenticationSession> {
25-
const authenticationSessions = await vscode.authentication.getSessions('github', ['repo']);
26-
27-
if (authenticationSessions.length) {
28-
return await authenticationSessions[0];
29-
} else {
30-
return await vscode.authentication.login('github', ['repo']);
31-
}
32-
}
33-
}
3411

3512
export async function activate(context: vscode.ExtensionContext) {
3613
const { name, version, aiKey } = require('../package.json') as { name: string, version: string, aiKey: string };
@@ -74,9 +51,7 @@ export async function activate(context: vscode.ExtensionContext) {
7451
}
7552
});
7653

77-
const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git')!.exports;
78-
const gitAPI = gitExtension.getAPI(1);
79-
context.subscriptions.push(gitAPI.registerCredentialsProvider(new GitHubCredentialProvider()));
54+
return;
8055
}
8156

8257
// this method is called when your extension is deactivated

extensions/github-authentication/src/typings/git.d.ts

Lines changed: 0 additions & 287 deletions
This file was deleted.

0 commit comments

Comments
 (0)