import { Component , OnInit } from '@angular/core' ; import { FormControl , FormGroup , ValidatorFn , Validators } from '@angular/forms' ; import { ActivatedRoute , Router } from '@angular/router' ; import { finalize } from 'rxjs/operators' ; import { ErrorWithMetadata } from '../../../core/error-with-metadata' ; import { VcsAuthenticationTypes } from '../../../core/vcs' ; import { WorkspaceService } from '../../shared' ; import { ConfirmDialog } from '../../shared/confirm-dialog' ; import { VcsService } from '../../vcs' ; @ Component ( { selector : 'gd-wizard-cloning' , templateUrl : './wizard-cloning.component.html' , styleUrls : [ './wizard-cloning.component.scss' ] , } ) export class WizardCloningComponent implements OnInit { get loginCompleted ( ) : boolean { return this . alreadyLogin || this . _loginSuccess ; } private _authFormToggled : boolean = false ; get authFormToggled ( ) : boolean { return this . _authFormToggled ; } get authFormAvailable ( ) : boolean { return ! this . alreadyLogin && ! this . _loginSuccess ; } private _loginProcessing = false ; get loginProcessing ( ) : boolean { return this . _loginProcessing ; } private _loginErrorCaught = false ; get loginErrorCaught ( ) : boolean { return this . _loginErrorCaught ; } private _cloneProcessing = false ; get cloneProcessing ( ) : boolean { return this . _cloneProcessing ; } readonly remoteUrlFormControl = new FormControl ( '' , { updateOn : 'blur' } ) ; readonly workspaceDirectoryFormControl = new FormControl ( '' ) ; readonly authenticationTypes = VcsAuthenticationTypes ; readonly authenticationFormGroup = new FormGroup ( { type : new FormControl ( this . authenticationTypes . BASIC ) , userName : new FormControl ( '' ) , password : new FormControl ( '' ) , token : new FormControl ( '' ) , } ) ; private alreadyLogin : boolean = false ; private _loginSuccess : boolean = false ; constructor ( private vcs , private activatedRoute , private confirmDialog , private workspace , private router ) { } ngOnInit ( ) { if ( this . activatedRoute . snapshot . data . isAuthenticationInfoExists ) { this . alreadyLogin = true ; } /**
         * Currently we only have github provider for vcs remote.
         * Later, providers get increased, handling is required.
         */ this . vcs . setRemoveProvider ( 'github' ) ; /** Set remote url validation. */ const remoteUrlFormatValidator = ( control ) => { if ( control . value === '' || control . value . length === 0 ) { return null ; } return this . vcs . isRemoteRepositoryUrlValid ( control . value ) ? null : { invalidFormat : true } ; } ; this . remoteUrlFormControl . setValidators ( [ Validators . required , remoteUrlFormatValidator , ] ) ; this . workspaceDirectoryFormControl . patchValue ( this . workspace . configs . rootDirPath ) ; } goToBack ( ) { this . router . navigate ( [ '/' ] ) ; } toggleAuthForm ( ) { this . _authFormToggled = ! this . _authFormToggled ; } loginToVcsRemote ( ) { this . _loginProcessing = true ; this . _loginErrorCaught = false ; const { type , userName , password , token } = this . authenticationFormGroup . value as any ; switch ( type as VcsAuthenticationTypes ) { case VcsAuthenticationTypes . BASIC : this . vcs . loginRemoteWithBasicAuthorization ( userName as string , password as string ) . pipe ( finalize ( ( ) => this . _loginProcessing = false ) ) . subscribe ( ( ) => this . handleLoginToVcsRemoteSuccess ( ) , ( ) => this . handleLoginToVcsRemoteFail ( ) ) ; break ; case VcsAuthenticationTypes . OAUTH2_TOKEN : this . vcs . loginRemoteWithOauth2TokenAuthorization ( token as string ) . pipe ( finalize ( ( ) => this . _loginProcessing = false ) ) . subscribe ( ( ) => this . handleLoginToVcsRemoteSuccess ( ) , ( ) => this . handleLoginToVcsRemoteFail ( ) ) ; break ; } } clone ( ) { this . _cloneProcessing = true ; this . vcs . cloneRepository ( this . remoteUrlFormControl . value , this . workspace . configs . rootDirPath ) . subscribe ( ( ) => { this . workspace . initWorkspace ( ) . subscribe ( ( ) => this . _cloneProcessing = false , error => this . handleWorkspaceInitFail ( error ) ) ; } , ( error ) => { this . _cloneProcessing = false ; this . handleCloneFail ( error ) ; } ) ; } private handleLoginToVcsRemoteSuccess ( ) { this . _loginSuccess = true ; } private handleLoginToVcsRemoteFail ( ) { this . _loginErrorCaught = true ; } private handleCloneFail ( error ) { this . confirmDialog . open ( { isAlert : true , title : 'Git Error' , body : ( error as ErrorWithMetadata ) . errorDescription ? ( error as ErrorWithMetadata ) . errorDescription : 'Unknown Error' , } ) ; } private handleWorkspaceInitFail ( error ) { this . confirmDialog . open ( { isAlert : true , title : 'Workspace Error' , body : ( error as ErrorWithMetadata ) . errorDescription ? ( error as ErrorWithMetadata ) . errorDescription : 'Unknown Error' , } ) ; } }