Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add docsRepoType in environmentController #75

Merged
merged 9 commits into from
Mar 2, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/build/buildController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,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
36 changes: 29 additions & 7 deletions src/common/docsEnvironmentController.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
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 configurationChangeListener: vscode.Disposable;
private _docsRepoType: DocsRepoType;

constructor(private eventStream: EventStream) {
this.refreshEnv();
this.configurationChangeListener = vscode.workspace.onDidChangeConfiguration((event: vscode.ConfigurationChangeEvent) => {
}

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 +36,29 @@ 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> {
const [docsRepoType] = await getRepositoryInfoFromLocalFolder(this.activeWorkSpaceFolder.uri.fsPath);
return docsRepoType;
}

private async refreshEnv(): Promise<void> {
let newEnv = this.getEnv();
if(this.environment && this.environment !== newEnv){
this.eventStream.post(new EnvironmentChanged(newEnv));
let newDocsRepoType = await this.getDocsRepoType();

if((this.environment && this.environment !== newEnv) ||
(this._docsRepoType && this._docsRepoType !== newDocsRepoType)) {
this.eventStream.post(new EnvironmentChanged(newEnv, newDocsRepoType));
}
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;
}
4 changes: 2 additions & 2 deletions src/common/loggingEvents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventType } from './eventType';
import { Credential } from '../credential/credentialController';
import { PlatformInformation } from './platformInformation';
import { Environment } from '../shared';
import { Environment, DocsRepoType } from '../shared';
import { BuildResult, DocfxExecutionResult } from '../build/buildResult';
import { BuildInput } from '../build/buildInput';
import { AbsolutePathPackage } from '../dependency/package';
Expand Down Expand Up @@ -214,7 +214,7 @@ export class PlatformInfoRetrieved implements BaseEvent {
// Others
export class EnvironmentChanged implements BaseEvent {
type = EventType.EnvironmentChanged;
constructor(public env: Environment) { }
constructor(public env: Environment, public docsRepoType: DocsRepoType) { }
}

export class QuickPickTriggered implements BaseEvent {
Expand Down
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
4 changes: 2 additions & 2 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 } 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 @@ -67,7 +67,7 @@ export class TelemetryObserver {
}

private handleUserSignInCompleted(event: UserSignInCompleted) {
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 userName: string;
readonly userEmail: string;
readonly userToken: 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
2 changes: 1 addition & 1 deletion test/unitTests/credential/credentialController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('CredentialController', () => {
}

[
new EnvironmentChanged('PPE')
new EnvironmentChanged('PPE', 'GitHub')
].forEach((event: BaseEvent) => {
describe(`Observer[${event.constructor.name}]: Credential should be refreshed`, () => {
beforeEach(() => {
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