diff --git a/src/build/buildController.ts b/src/build/buildController.ts index 439e5b16..25ba7013 100644 --- a/src/build/buildController.ts +++ b/src/build/buildController.ts @@ -172,7 +172,7 @@ export class BuildController { let localRepositoryUrl: string; try { - [localRepositoryUrl] = await getRepositoryInfoFromLocalFolder(localRepositoryPath); + [, localRepositoryUrl] = await getRepositoryInfoFromLocalFolder(localRepositoryPath); } catch (err) { throw new Error(`Cannot get the repository information for the current workspace folder(${err.message})`); } diff --git a/src/common/docsEnvironmentController.ts b/src/common/docsEnvironmentController.ts index 1f055b00..e111ce41 100644 --- a/src/common/docsEnvironmentController.ts +++ b/src/common/docsEnvironmentController.ts @@ -1,22 +1,32 @@ import vscode from 'vscode'; -import { Environment, EXTENSION_NAME } from '../shared'; +import { Environment, DocsRepoType, EXTENSION_NAME } from '../shared'; import { EnvironmentChanged } from './loggingEvents'; import { EventStream } from './eventStream'; import { EnvironmentController } from './environmentController'; +import { getRepositoryInfoFromLocalFolder } from '../utils/utils'; const ENVIRONMENT_CONFIG_NAME = 'environment'; export class DocsEnvironmentController implements EnvironmentController, vscode.Disposable { + + private activeWorkSpaceFolder: vscode.WorkspaceFolder; private environment: Environment; + private _docsRepoType: DocsRepoType; private configurationChangeListener: vscode.Disposable; constructor(private eventStream: EventStream) { - this.refreshEnv(); - this.configurationChangeListener = vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => { + this.activeWorkSpaceFolder = vscode.workspace.workspaceFolders ? vscode.workspace.workspaceFolders[0] : undefined; + } + + public static async CreateAsync(eventStream: EventStream): Promise { + const docsEnvironmentController = new DocsEnvironmentController(eventStream); + await docsEnvironmentController.refreshEnv(); + docsEnvironmentController.configurationChangeListener = vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => { if (event.affectsConfiguration(`${EXTENSION_NAME}.${ENVIRONMENT_CONFIG_NAME}`)) { - this.refreshEnv(); + docsEnvironmentController.refreshEnv(); } }); + return docsEnvironmentController; } public dispose(): void { @@ -27,16 +37,36 @@ export class DocsEnvironmentController implements EnvironmentController, vscode. return this.environment; } + public get docsRepoType(): DocsRepoType { + return this._docsRepoType; + } + private getEnv(): Environment { const extensionConfig: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration(EXTENSION_NAME, undefined); return extensionConfig.get(ENVIRONMENT_CONFIG_NAME, 'PROD'); } - private refreshEnv(): void { + private async getDocsRepoType(): Promise { + if (this.activeWorkSpaceFolder) { + try { + const [docsRepoType] = await getRepositoryInfoFromLocalFolder(this.activeWorkSpaceFolder.uri.fsPath); + return docsRepoType; + } catch { + return 'GitHub'; + } + } + return 'GitHub'; + } + + private async refreshEnv(): Promise { let newEnv = this.getEnv(); - if(this.environment && this.environment !== newEnv){ + let newDocsRepoType = await this.getDocsRepoType(); + + if((this.environment && this.environment !== newEnv) || + (this._docsRepoType && this._docsRepoType !== newDocsRepoType)) { this.eventStream.post(new EnvironmentChanged(newEnv)); } this.environment = newEnv; + this._docsRepoType = newDocsRepoType; } } \ No newline at end of file diff --git a/src/common/environmentController.ts b/src/common/environmentController.ts index 7974be18..53c23aad 100644 --- a/src/common/environmentController.ts +++ b/src/common/environmentController.ts @@ -1,5 +1,6 @@ -import { Environment } from '../shared'; +import { Environment, DocsRepoType } from '../shared'; export interface EnvironmentController { env: Environment; + docsRepoType: DocsRepoType; } \ No newline at end of file diff --git a/src/extension.ts b/src/extension.ts index c07148e5..aa6988ef 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -27,7 +27,7 @@ import TelemetryReporter from './telemetryReporter'; export async function activate(context: vscode.ExtensionContext): Promise { const eventStream = new EventStream(); const extensionContext = new ExtensionContext(context); - const environmentController = new DocsEnvironmentController(eventStream); + const environmentController = await DocsEnvironmentController.CreateAsync(eventStream); const platformInformation = await PlatformInformation.getCurrent(); // Telemetry diff --git a/src/observers/telemetryObserver.ts b/src/observers/telemetryObserver.ts index afdedcca..bd6c7097 100644 --- a/src/observers/telemetryObserver.ts +++ b/src/observers/telemetryObserver.ts @@ -1,6 +1,6 @@ import { BaseEvent, UserSignInTriggered, UserSignInCompleted, UserSignInSucceeded, UserSignInFailed, UserSignOutTriggered, UserSignOutCompleted, BuildTriggered, BuildCompleted, BuildSucceeded, BuildFailed, BuildCacheSizeCalculated, LearnMoreClicked, QuickPickTriggered, QuickPickCommandSelected, DependencyInstallStarted, DependencyInstallCompleted, PackageInstallCompleted, PackageInstallAttemptFailed } from '../common/loggingEvents'; import { EventType } from '../common/eventType'; -import { DocsSignInType } from '../shared'; +import { DocsRepoType } from '../shared'; import { DocsError } from '../error/docsError'; import { BuildType } from '../build/buildInput'; import { DocfxExecutionResult } from '../build/buildResult'; @@ -79,8 +79,7 @@ export class TelemetryObserver { }); } - // Send telemetry - let signInType: DocsSignInType; + let signInType: DocsRepoType; let userName: string; let userEmail: string; let errorCode: string; diff --git a/src/shared.ts b/src/shared.ts index c223beff..fdb90dcf 100644 --- a/src/shared.ts +++ b/src/shared.ts @@ -26,10 +26,10 @@ export const OP_CONFIG_FILE_NAME = '.openpublishing.publish.config.json'; // OAuth export type DocsSignInStatus = 'Initializing' | 'SigningIn' | 'SignedIn' | 'SignedOut'; -export type DocsSignInType = 'GitHub' | 'Azure DevOps'; +export type DocsRepoType = 'GitHub' | 'Azure DevOps'; export interface UserInfo { - readonly signType: DocsSignInType; + readonly signType: DocsRepoType; readonly userId: string; readonly userName: string; readonly userEmail: string; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 87357b7e..8f1d99cf 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -4,6 +4,7 @@ import gitUrlParse from 'git-url-parse'; import simpleGit from 'simple-git/promise'; import uuid from 'uuid/v1'; import du from 'du'; +import { DocsRepoType } from '../shared'; export function parseQuery(uri: vscode.Uri) { return uri.query.split('&').reduce((prev: any, current) => { @@ -30,7 +31,7 @@ export function safelyReadJsonFile(filePath: string) { return JSON.parse(fs.readFileSync(filePath, { encoding: 'utf-8' }).replace(/^\uFEFF/, '').replace(/\u00A0/g, ' ')); } -export async function getRepositoryInfoFromLocalFolder(repositoryPath: string): Promise { +export async function getRepositoryInfoFromLocalFolder(repositoryPath: string): Promise<[DocsRepoType, string, string, string]> { if (!fs.existsSync(repositoryPath)) { throw new Error(`Path(${repositoryPath}) is not existed on the current machine`); } @@ -49,12 +50,15 @@ export async function getRepositoryInfoFromLocalFolder(repositoryPath: string): let commit = await repository.revparse(['HEAD']); - return [normalizeRemoteUrl(remote), branch, commit]; + const [docsRepoType, normalizedRepositoryUrl] = normalizeRemoteUrl(remote); + + return [docsRepoType, normalizedRepositoryUrl, branch, commit]; } -function normalizeRemoteUrl(url: string): string { +function normalizeRemoteUrl(url: string): [DocsRepoType, string] { const repository = gitUrlParse(url); - return `https://${repository.resource}/${repository.full_name}`; + const docsRepoType = repository.resource.startsWith('github.com') ? 'GitHub' : 'Azure DevOps'; + return [docsRepoType, `https://${repository.resource}/${repository.full_name}`]; } export function basicAuth(token: string) { diff --git a/test/benchmarkTests/index.ts b/test/benchmarkTests/index.ts index 1767fcd8..a537a81f 100644 --- a/test/benchmarkTests/index.ts +++ b/test/benchmarkTests/index.ts @@ -24,7 +24,7 @@ export function run(): Promise { let buildDuration = 0; let totalDuration = 0; let workspace = vscode.workspace.workspaceFolders[0]; - let [url, branch, commit] = await getRepositoryInfoFromLocalFolder(workspace.uri.fsPath); + let [, url, branch, commit] = await getRepositoryInfoFromLocalFolder(workspace.uri.fsPath); let report = { name: workspace.name, url, diff --git a/test/utils/faker.ts b/test/utils/faker.ts index 1cf39ff2..b8f3543a 100644 --- a/test/utils/faker.ts +++ b/test/utils/faker.ts @@ -8,6 +8,7 @@ import { Credential } from '../../src/credential/credentialController'; export function getFakeEnvironmentController(): EnvironmentController { return { env: 'PROD', + docsRepoType: 'GitHub' }; } diff --git a/tslint.json b/tslint.json index 5c124bda..1e1ea862 100644 --- a/tslint.json +++ b/tslint.json @@ -18,7 +18,11 @@ ], "no-switch-case-fall-through": true, "no-null-keyword": true, - "variable-name": true + "variable-name": { + "options": [ + "allow-leading-underscore" + ] + } }, "rulesDirectory": [ "tslint-microsoft-contrib"