From eb12fb90eac4012629206c5d29798871ce080d5f Mon Sep 17 00:00:00 2001 From: Igor Vinokur Date: Wed, 26 Aug 2020 16:55:16 +0300 Subject: [PATCH] Register GitHub Authentication provider for the vscode Github PR plugin --- .../src/github-auth-plugin.ts | 46 +++++++++++++------ 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/plugins/github-auth-plugin/src/github-auth-plugin.ts b/plugins/github-auth-plugin/src/github-auth-plugin.ts index 30e8d3e927..5aeb921e80 100644 --- a/plugins/github-auth-plugin/src/github-auth-plugin.ts +++ b/plugins/github-auth-plugin/src/github-auth-plugin.ts @@ -11,22 +11,38 @@ import * as theia from '@theia/plugin'; import * as che from '@eclipse-che/plugin'; -export function start(context: theia.PluginContext): void { +export async function start(context: theia.PluginContext): Promise { + let session: theia.AuthenticationSession | undefined; if (theia.plugins.getPlugin('github.vscode-pull-request-github')) { - const command = { - id: 'github-plugin-authenticate', - label: 'GitHub authenticate' - }; - context.subscriptions.push(theia.commands.registerCommand(command, async () => { - const token = await che.github.getToken(); - const conf = theia.workspace.getConfiguration(); - await conf.update('githubPullRequests.hosts', [{ - host: 'github.com', - token - }], theia.ConfigurationTarget.Global); - theia.window.showWarningMessage('GitHub token has been set to preferences. ' + - 'Refresh the page to reinitialise the vscode GitHub pull-request plugin with the token'); - })); + const onDidChangeSessions = new theia.EventEmitter(); + theia.authentication.registerAuthenticationProvider({ + id: 'github', + label: 'GitHub', + supportsMultipleAccounts: false, + onDidChangeSessions: onDidChangeSessions.event, + getSessions: async () => { + if (session) { + return [session]; + } else { + return []; + } + }, + login: async (scopeList: string[]) => { + session = { + id: 'sessionId', + accessToken: await che.github.getToken(), + account: {label: 'accountName1', id: 'accountId'}, + scopes: scopeList + }; + onDidChangeSessions.fire({added: [session.id], removed: [], changed: []}); + return session; + }, + logout: async (id: string) => { + session = undefined; + onDidChangeSessions.fire({added: [], removed: [id], changed: []}); + } + } + ); } }