<commit_before>import { TranslateService } from '@ngx-translate/core';
import { CacheService } from '../../../../shared/services/cache.service';
import { AbstractControl } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { DeploymentCenterStateManager } from '../wizard-logic/deployment-center-state-manager';
import { DeploymentCenterConstants } from '../../../../shared/models/constants';
import { PortalResources } from '../../../../shared/models/portal-resources';

export class VstsValidators {
    static createVstsAccountNameValidator(_wizard: DeploymentCenterStateManager,
        _translateService: TranslateService,
        _cacheService: CacheService) {
        return (control: AbstractControl) => {
            if (control && control.value) {
                return Observable.timer(500)
                    .switchMap(() => _cacheService.get(`https://app.vssps.visualstudio.com/_AzureSpsAccount/ValidateAccountName?accountName=${control.value}`, true, _wizard.getVstsDirectHeaders()))
                    .map(res => {
                        const valid = res.json().valid;
                        if (!valid) {
                            return {
                                invalidName: _translateService.instant(PortalResources.vstsNameUnavailable)
                            };
                        }
                        return null;
                    });
            } else {
                return Observable.of(null);
            }
        };

    }
    static createProjectPermissionsValidator(_wizard: DeploymentCenterStateManager,
        _translateService: TranslateService,
        _cacheService: CacheService,
        _accountControl: AbstractControl) {
        return (projectControl: AbstractControl) => {

            const vstsAccountValue: string = _accountControl.value;
            const vstsProjectValue: string = projectControl.value;
            if (vstsAccountValue && vstsProjectValue) {
                return _cacheService.get(`https://${vstsAccountValue}.visualstudio.com/_apis/projects?includeCapabilities=true`)
                    .switchMap(r => {
                        const projectList: [{ id: string, name: string }] = r.json().value;
                        const currentProject = projectList.find(x => x.name.toLowerCase() === vstsProjectValue.toLowerCase());
                        if (currentProject) {
                            const callHeaders = _wizard.getVstsDirectHeaders();
                            callHeaders.append('accept', 'application/json;api-version=3.2-preview.2');
                            return Observable.forkJoin(
                                _cacheService.get(`https://${vstsAccountValue}.visualstudio.com/DefaultCollection/_apis/Permissions/${DeploymentCenterConstants.buildSecurityNameSpace}/${DeploymentCenterConstants.editBuildDefinitionBitMask}?tokens=${currentProject.id}`, true, callHeaders),
                                _cacheService.get(`https://${vstsAccountValue}.visualstudio.com/DefaultCollection/_apis/Permissions/${DeploymentCenterConstants.releaseSecurityNameSpace}/${DeploymentCenterConstants.editReleaseDefinitionPermission}?tokens=${currentProject.id}`, true, callHeaders)
                            );
                        }
                        return Observable.of(null);
                    })
                    .map(results => {<commit_msg>#fixes #2847 Ping VSO release RP before checking permissions (#2918)<commit_after>import { TranslateService } from '@ngx-translate/core';
import { CacheService } from '../../../../shared/services/cache.service';
import { AbstractControl } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import { DeploymentCenterStateManager } from '../wizard-logic/deployment-center-state-manager';
import { DeploymentCenterConstants } from '../../../../shared/models/constants';
import { PortalResources } from '../../../../shared/models/portal-resources';

export class VstsValidators {
    static createVstsAccountNameValidator(_wizard: DeploymentCenterStateManager,
        _translateService: TranslateService,
        _cacheService: CacheService) {
        return (control: AbstractControl) => {
            if (control && control.value) {
                return Observable.timer(500)
                    .switchMap(() => _cacheService.get(`https://app.vssps.visualstudio.com/_AzureSpsAccount/ValidateAccountName?accountName=${control.value}`, true, _wizard.getVstsDirectHeaders()))
                    .map(res => {
                        const valid = res.json().valid;
                        if (!valid) {
                            return {
                                invalidName: _translateService.instant(PortalResources.vstsNameUnavailable)
                            };
                        }
                        return null;
                    });
            } else {
                return Observable.of(null);
            }
        };

    }
    static createProjectPermissionsValidator(_wizard: DeploymentCenterStateManager,
        _translateService: TranslateService,
        _cacheService: CacheService,
        _accountControl: AbstractControl) {
        return (projectControl: AbstractControl) => {

            const vstsAccountValue: string = _accountControl.value;
            const vstsProjectValue: string = projectControl.value;
            if (vstsAccountValue && vstsProjectValue) {
                return _cacheService.get(`https://${vstsAccountValue}.visualstudio.com/_apis/projects?includeCapabilities=true`)
                    .concatMap(r => {
                        const projectList: [{ id: string, name: string }] = r.json().value;
                        const currentProject = projectList.find(x => x.name.toLowerCase() === vstsProjectValue.toLowerCase());
                        if (currentProject) {
                            const callHeaders = _wizard.getVstsDirectHeaders();
                            callHeaders.append('accept', 'application/json;api-version=3.2-preview.2');
                            // need to ping the release rp in vso in order to subscribe to user to the RP, otherwise the rp call will fail
                            return _cacheService.get(`https://${vstsAccountValue}.vsrm.visualstudio.com/${currentProject.id}/_apis/Release/definitions/environmenttemplates`, true, callHeaders)
                                .switchMap(() => {
                                    return Observable.forkJoin(
                                        _cacheService.get(`https://${vstsAccountValue}.visualstudio.com/DefaultCollection/_apis/Permissions/${DeploymentCenterConstants.buildSecurityNameSpace}/${DeploymentCenterConstants.editBuildDefinitionBitMask}?tokens=${currentProject.id}`, true, callHeaders),
                                        _cacheService.get(`https://${vstsAccountValue}.visualstudio.com/DefaultCollection/_apis/Permissions/${DeploymentCenterConstants.releaseSecurityNameSpace}/${DeploymentCenterConstants.editReleaseDefinitionPermission}?tokens=${currentProject.id}`, true, callHeaders)
                                    );
                                });
                        }
                        return Observable.of(null);
                    })
                    .map(results => {
{'fraction_non_alphanumeric': 0.1096395412342982, 'fraction_numerical': 0.002730748225013654, 'mean_word_length': 2.038158440481128, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 18, 'https://': 9, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3528072', 'n_tokens_mistral': 1713, 'n_tokens_neox': 1662, 'n_words': 277}