Skip to content

Commit

Permalink
add docsRepoType in environmentController (#75)
Browse files Browse the repository at this point in the history
* add docsRepoType in environmentController
  • Loading branch information
mingwli authored Mar 2, 2020
1 parent a5bcfa5 commit 16a8858
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/build/buildController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`);
}
Expand Down
42 changes: 36 additions & 6 deletions src/common/docsEnvironmentController.ts
Original file line number Diff line number Diff line change
@@ -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<DocsEnvironmentController> {
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 {
Expand All @@ -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>(ENVIRONMENT_CONFIG_NAME, 'PROD');
}

private refreshEnv(): void {
private async getDocsRepoType(): Promise<DocsRepoType> {
if (this.activeWorkSpaceFolder) {
try {
const [docsRepoType] = await getRepositoryInfoFromLocalFolder(this.activeWorkSpaceFolder.uri.fsPath);
return docsRepoType;
} catch {
return 'GitHub';
}
}
return 'GitHub';
}

private async refreshEnv(): Promise<void> {
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;
}
}
3 changes: 2 additions & 1 deletion src/common/environmentController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Environment } from '../shared';
import { Environment, DocsRepoType } from '../shared';

export interface EnvironmentController {
env: Environment;
docsRepoType: DocsRepoType;
}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import TelemetryReporter from './telemetryReporter';
export async function activate(context: vscode.ExtensionContext): Promise<ExtensionExports> {
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
Expand Down
5 changes: 2 additions & 3 deletions src/observers/telemetryObserver.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -79,8 +79,7 @@ export class TelemetryObserver {
});
}

// Send telemetry
let signInType: DocsSignInType;
let signInType: DocsRepoType;
let userName: string;
let userEmail: string;
let errorCode: string;
Expand Down
4 changes: 2 additions & 2 deletions src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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<string[]> {
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`);
}
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/benchmarkTests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function run(): Promise<void> {
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 = <BenchmarkReport>{
name: workspace.name,
url,
Expand Down
1 change: 1 addition & 0 deletions test/utils/faker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Credential } from '../../src/credential/credentialController';
export function getFakeEnvironmentController(): EnvironmentController {
return {
env: 'PROD',
docsRepoType: 'GitHub'
};
}

Expand Down
6 changes: 5 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 16a8858

Please sign in to comment.