lazily load experimentation service in github-authentication

This commit is contained in:
Tyler Leonhardt 2021-07-23 09:07:32 -07:00
parent 9aab65c6ee
commit a8134002cd
No known key found for this signature in database
GPG key ID: 1BC2B6244363E77E
4 changed files with 47 additions and 41 deletions

View file

@ -9,10 +9,18 @@ import { getExperimentationService, IExperimentationService, IExperimentationTel
export class ExperimentationTelemetry implements IExperimentationTelemetry {
private sharedProperties: Record<string, string> = {};
private experimentationService: IExperimentationService | undefined;
constructor(private baseReporter: TelemetryReporter) { }
constructor(private readonly context: vscode.ExtensionContext, private baseReporter: TelemetryReporter) { }
/**
* @returns A promise that you shouldn't need to await because this is just telemetry.
*/
async sendTelemetryEvent(eventName: string, properties?: Record<string, string>, measurements?: Record<string, number>) {
if (!this.experimentationService) {
this.experimentationService = await createExperimentationService(this.context, this);
}
sendTelemetryEvent(eventName: string, properties?: Record<string, string>, measurements?: Record<string, number>) {
this.baseReporter.sendTelemetryEvent(
eventName,
{
@ -23,11 +31,18 @@ export class ExperimentationTelemetry implements IExperimentationTelemetry {
);
}
sendTelemetryErrorEvent(
/**
* @returns A promise that you shouldn't need to await because this is just telemetry.
*/
async sendTelemetryErrorEvent(
eventName: string,
properties?: Record<string, string>,
_measurements?: Record<string, number>,
_measurements?: Record<string, number>
) {
if (!this.experimentationService) {
this.experimentationService = await createExperimentationService(this.context, this);
}
this.baseReporter.sendTelemetryErrorEvent(eventName, {
...this.sharedProperties,
...properties,
@ -66,8 +81,10 @@ function getTargetPopulation(): TargetPopulation {
}
}
export async function createExperimentationService(context: vscode.ExtensionContext, telemetry: ExperimentationTelemetry): Promise<IExperimentationService> {
async function createExperimentationService(context: vscode.ExtensionContext, telemetry: ExperimentationTelemetry): Promise<IExperimentationService> {
const id = context.extension.id;
const version = context.extension.packageJSON.version;
return getExperimentationService(id, version, getTargetPopulation(), telemetry, context.globalState);
const experimentationService = getExperimentationService(id, version, getTargetPopulation(), telemetry, context.globalState);
await experimentationService.initialFetch;
return experimentationService;
}

View file

@ -5,23 +5,8 @@
import * as vscode from 'vscode';
import { GitHubAuthenticationProvider, AuthProviderType } from './github';
import TelemetryReporter from 'vscode-extension-telemetry';
import { createExperimentationService, ExperimentationTelemetry } from './experimentationService';
export async function activate(context: vscode.ExtensionContext) {
const { name, version, aiKey } = require('../package.json') as { name: string, version: string, aiKey: string };
const telemetryReporter = new ExperimentationTelemetry(new TelemetryReporter(name, version, aiKey));
const experimentationService = await createExperimentationService(context, telemetryReporter);
await experimentationService.initialFetch;
[
AuthProviderType.github,
AuthProviderType['github-enterprise']
].forEach(type => {
context.subscriptions.push(new GitHubAuthenticationProvider(context, type, telemetryReporter));
});
context.subscriptions.push(new GitHubAuthenticationProvider(context, AuthProviderType.github));
context.subscriptions.push(new GitHubAuthenticationProvider(context, AuthProviderType.githubEnterprise));
}
// this method is called when your extension is deactivated
export function deactivate() { }

View file

@ -10,6 +10,7 @@ import { GitHubServer, uriHandler } from './githubServer';
import Logger from './common/logger';
import { arrayEquals } from './common/utils';
import { ExperimentationTelemetry } from './experimentationService';
import TelemetryReporter from 'vscode-extension-telemetry';
interface SessionData {
id: string;
@ -24,35 +25,38 @@ interface SessionData {
export enum AuthProviderType {
github = 'github',
'github-enterprise' = 'github-enterprise'
githubEnterprise = 'github-enterprise'
}
export class GitHubAuthenticationProvider implements vscode.AuthenticationProvider, vscode.Disposable {
private _sessionChangeEmitter = new vscode.EventEmitter<vscode.AuthenticationProviderAuthenticationSessionsChangeEvent>();
private _githubServer: GitHubServer;
private _telemetryReporter: ExperimentationTelemetry;
private _keychain: Keychain;
private _sessionsPromise: Promise<vscode.AuthenticationSession[]>;
private _disposable: vscode.Disposable;
constructor(private context: vscode.ExtensionContext, private type: AuthProviderType, private telemetryReporter: ExperimentationTelemetry) {
this._keychain = new Keychain(context, `${type}.auth`);
this._githubServer = new GitHubServer(type, telemetryReporter);
constructor(private context: vscode.ExtensionContext, private type: AuthProviderType) {
const { name, version, aiKey } = require('../package.json') as { name: string, version: string, aiKey: string };
this._telemetryReporter = new ExperimentationTelemetry(context, new TelemetryReporter(name, version, aiKey));
this._keychain = new Keychain(context, `${type}.auth`);
this._githubServer = new GitHubServer(type, this._telemetryReporter);
// Contains the current state of the sessions we have available.
this._sessionsPromise = this.readAndVerifySessions(true);
let friendlyName = 'GitHub';
if (this.type === AuthProviderType['github-enterprise']) {
friendlyName = 'GitHub Enterprise';
}
const friendlyName = this.type === AuthProviderType.github ? 'GitHub' : 'GitHub Enterprise';
this._disposable = vscode.Disposable.from(
this._telemetryReporter,
this.type === AuthProviderType.github ? vscode.window.registerUriHandler(uriHandler) : { dispose() { } },
vscode.commands.registerCommand(`${this.type}.provide-token`, () => this.manuallyProvideToken()),
vscode.authentication.registerAuthenticationProvider(this.type, friendlyName, this, { supportsMultipleAccounts: false }),
this.context.secrets.onDidChange(() => this.checkForUpdates())
);
}
dispose() {
this._disposable.dispose();
}
@ -72,7 +76,7 @@ export class GitHubAuthenticationProvider implements vscode.AuthenticationProvid
if (this.type === AuthProviderType.github) {
this._githubServer.checkIsEdu(token);
}
if (this.type === AuthProviderType['github-enterprise']) {
if (this.type === AuthProviderType.githubEnterprise) {
this._githubServer.checkEnterpriseVersion(token);
}
}
@ -180,7 +184,7 @@ export class GitHubAuthenticationProvider implements vscode.AuthenticationProvid
"scopes": { "classification": "PublicNonPersonalData", "purpose": "FeatureInsight" }
}
*/
this.telemetryReporter?.sendTelemetryEvent('login', {
this._telemetryReporter?.sendTelemetryEvent('login', {
scopes: JSON.stringify(scopes),
});
@ -208,14 +212,14 @@ export class GitHubAuthenticationProvider implements vscode.AuthenticationProvid
/* __GDPR__
"loginCancelled" : { }
*/
this.telemetryReporter?.sendTelemetryEvent('loginCancelled');
this._telemetryReporter?.sendTelemetryEvent('loginCancelled');
throw e;
}
/* __GDPR__
"loginFailed" : { }
*/
this.telemetryReporter?.sendTelemetryEvent('loginFailed');
this._telemetryReporter?.sendTelemetryEvent('loginFailed');
vscode.window.showErrorMessage(`Sign in failed: ${e}`);
Logger.error(e);
@ -242,7 +246,7 @@ export class GitHubAuthenticationProvider implements vscode.AuthenticationProvid
/* __GDPR__
"logout" : { }
*/
this.telemetryReporter?.sendTelemetryEvent('logout');
this._telemetryReporter?.sendTelemetryEvent('logout');
Logger.info(`Logging out of ${id}`);
@ -262,7 +266,7 @@ export class GitHubAuthenticationProvider implements vscode.AuthenticationProvid
/* __GDPR__
"logoutFailed" : { }
*/
this.telemetryReporter?.sendTelemetryEvent('logoutFailed');
this._telemetryReporter?.sendTelemetryEvent('logoutFailed');
vscode.window.showErrorMessage(`Sign out failed: ${e}`);
Logger.error(e);

View file

@ -45,7 +45,7 @@ export class GitHubServer {
constructor(private type: AuthProviderType, private readonly telemetryReporter: ExperimentationTelemetry) { }
private isTestEnvironment(url: vscode.Uri): boolean {
return this.type === AuthProviderType['github-enterprise'] || /\.azurewebsites\.net$/.test(url.authority) || url.authority.startsWith('localhost:');
return this.type === AuthProviderType.githubEnterprise || /\.azurewebsites\.net$/.test(url.authority) || url.authority.startsWith('localhost:');
}
// TODO@joaomoreno TODO@RMacfarlane
@ -169,14 +169,14 @@ export class GitHubServer {
};
private getServerUri(path?: string) {
const apiUri = this.type === AuthProviderType['github-enterprise']
const apiUri = this.type === AuthProviderType.githubEnterprise
? vscode.Uri.parse(vscode.workspace.getConfiguration('github-enterprise').get<string>('uri') || '', true)
: vscode.Uri.parse('https://api.github.com');
if (!path) {
path = '';
}
if (this.type === AuthProviderType['github-enterprise']) {
if (this.type === AuthProviderType.githubEnterprise) {
path = '/api/v3' + path;
}