Path: blob/main/extensions/github-authentication/src/extension.ts
3314 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as vscode from 'vscode';6import { GitHubAuthenticationProvider, UriEventHandler } from './github';78const settingNotSent = '"github-enterprise.uri" not set';9const settingInvalid = '"github-enterprise.uri" invalid';1011class NullAuthProvider implements vscode.AuthenticationProvider {12private _onDidChangeSessions = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();13onDidChangeSessions = this._onDidChangeSessions.event;1415private readonly _disposable: vscode.Disposable;1617constructor(private readonly _errorMessage: string) {18this._disposable = vscode.authentication.registerAuthenticationProvider('github-enterprise', 'GitHub Enterprise', this);19}2021createSession(): Thenable<vscode.AuthenticationSession> {22throw new Error(this._errorMessage);23}2425getSessions(): Thenable<vscode.AuthenticationSession[]> {26return Promise.resolve([]);27}28removeSession(): Thenable<void> {29throw new Error(this._errorMessage);30}3132dispose() {33this._onDidChangeSessions.dispose();34this._disposable.dispose();35}36}3738function initGHES(context: vscode.ExtensionContext, uriHandler: UriEventHandler): vscode.Disposable {39const settingValue = vscode.workspace.getConfiguration().get<string>('github-enterprise.uri');40if (!settingValue) {41const provider = new NullAuthProvider(settingNotSent);42context.subscriptions.push(provider);43return provider;44}4546// validate user value47let uri: vscode.Uri;48try {49uri = vscode.Uri.parse(settingValue, true);50} catch (e) {51vscode.window.showErrorMessage(vscode.l10n.t('GitHub Enterprise Server URI is not a valid URI: {0}', e.message ?? e));52const provider = new NullAuthProvider(settingInvalid);53context.subscriptions.push(provider);54return provider;55}5657const githubEnterpriseAuthProvider = new GitHubAuthenticationProvider(context, uriHandler, uri);58context.subscriptions.push(githubEnterpriseAuthProvider);59return githubEnterpriseAuthProvider;60}6162export function activate(context: vscode.ExtensionContext) {63const uriHandler = new UriEventHandler();64context.subscriptions.push(uriHandler);65context.subscriptions.push(vscode.window.registerUriHandler(uriHandler));6667context.subscriptions.push(new GitHubAuthenticationProvider(context, uriHandler));6869let before = vscode.workspace.getConfiguration().get<string>('github-enterprise.uri');70let githubEnterpriseAuthProvider = initGHES(context, uriHandler);71context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(e => {72if (e.affectsConfiguration('github-enterprise.uri')) {73const after = vscode.workspace.getConfiguration().get<string>('github-enterprise.uri');74if (before !== after) {75githubEnterpriseAuthProvider?.dispose();76before = after;77githubEnterpriseAuthProvider = initGHES(context, uriHandler);78}79}80}));8182// Listener to prompt for reload when the fetch implementation setting changes83const beforeFetchSetting = vscode.workspace.getConfiguration().get<boolean>('github-authentication.useElectronFetch', true);84context.subscriptions.push(vscode.workspace.onDidChangeConfiguration(async e => {85if (e.affectsConfiguration('github-authentication.useElectronFetch')) {86const afterFetchSetting = vscode.workspace.getConfiguration().get<boolean>('github-authentication.useElectronFetch', true);87if (beforeFetchSetting !== afterFetchSetting) {88const selection = await vscode.window.showInformationMessage(89vscode.l10n.t('GitHub Authentication - Reload required'),90{91modal: true,92detail: vscode.l10n.t('A reload is required for the fetch setting change to take effect.')93},94vscode.l10n.t('Reload Window')95);96if (selection) {97await vscode.commands.executeCommand('workbench.action.reloadWindow');98}99}100}101}));102}103104105