Skip to content
This repository has been archived by the owner on Jul 23, 2019. It is now read-only.

Commit

Permalink
fix: introduced validator for git repo
Browse files Browse the repository at this point in the history
  • Loading branch information
edewit authored and ia3andy committed Jul 5, 2018
1 parent 31306ac commit 9dd2412
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,43 @@ <h3>Authorized Account Information</h3>
(click)="connectAccount($event)">Log In &amp; Authorize Account</button>
</div>
</div>
<div class="form-group">
<label for="ghOrg" class="col-sm-2 control-label">Location</label>
<div class="col-sm-10">
<select id="ghOrg" class="form-control" name="ghOrg" placeholder="Select organization" #versionSelect
[disabled]="launcherComponent?.summary?.gitHubDetails?.organizations === undefined"
[(ngModel)]="launcherComponent.summary.gitHubDetails.organization"
(ngModelChange)="getGitHubRepos()">
<option [ngValue]="org"
*ngFor="let org of launcherComponent?.summary?.gitHubDetails?.organizations">
{{org}}
</option>
</select>
<form>
<div class="form-group">
<label for="ghOrg" class="col-sm-2 control-label">Location</label>
<div class="col-sm-10">
<select id="ghOrg" class="form-control" name="ghOrg" placeholder="Select organization"
[disabled]="launcherComponent?.summary?.gitHubDetails?.organizations === undefined"
[(ngModel)]="launcherComponent.summary.gitHubDetails.organization"
(ngModelChange)="getGitHubRepos()">
<option [ngValue]="org"
*ngFor="let org of launcherComponent?.summary?.gitHubDetails?.organizations">
{{org}}
</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<label for="ghRepo" class="col-sm-2 control-label">Repository</label>
<div class="col-sm-10"
[ngClass]="{'has-error': launcherComponent?.summary?.gitHubDetails?.repositoryAvailable === false}">
<input id="ghRepo" class="form-control" name="ghRepo" type="text"
placeholder="Enter a unique name for your repository"
[disabled]="launcherComponent.summary.gitHubDetails.organization === undefined"
[(ngModel)]="launcherComponent.summary.gitHubDetails.repository"
[value]="launcherComponent.summary.gitHubDetails.repository | lowercase"
(ngModelChange)="updateGitHubSelection()"
(keyup)="validateRepo()">
<span class="help-block"
*ngIf="launcherComponent?.summary?.gitHubDetails?.repositoryAvailable === false">
{{duplicateNameMessage}}
</span>
<div class="form-group">
<label for="ghRepo" class="col-sm-2 control-label">Repository</label>
<div class="col-sm-10"
[class.has-error]="ghRepo.invalid && (ghRepo.dirty || ghRepo.touched)">
<input id="ghRepo" class="form-control" name="ghRepo" type="text" #ghRepo="ngModel" validateRepository
placeholder="Enter a unique name for your repository"
[disabled]="!launcherComponent.summary.gitHubDetails.organization"
[(ngModel)]="launcherComponent.summary.gitHubDetails.repository"
[value]="launcherComponent.summary.gitHubDetails.repository | lowercase">
<span class="help-block"
*ngIf="ghRepo.invalid && (ghRepo.dirty || ghRepo.touched)">
<span *ngIf="ghRepo.errors.duplicate">
Duplicate Name: '{{ghRepo.value}}' is already in use
as {{launcherComponent.summary.gitHubDetails.organization}}/{{ghRepo.value}}.
</span>
<span *ngIf="ghRepo.errors.pattern">
'{{ghRepo.value}}' is not a valid name, only alphanumeric characters are allowed with "-", "_" or "."
</span>
</span>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Directive } from '@angular/core';
import { NG_ASYNC_VALIDATORS, Validator, ValidatorFn, AbstractControl } from '@angular/forms';

import { GitProviderService } from '../../service/git-provider.service';

@Directive({
selector: '[validateRepository]',
providers: [{provide: NG_ASYNC_VALIDATORS, useExisting: GitProviderRepositoryValidatorDirective, multi: true}]
})
export class GitProviderRepositoryValidatorDirective implements Validator {
private pattern = /^[a-zA-Z0-9][a-zA-Z0-9-._]{1,63}$/;

constructor(private gitProvider: GitProviderService) {}

validate( control : AbstractControl ) : Promise<{[key : string] : any}> {
return this.validRepositoryName(control);
}

validRepositoryName(control: AbstractControl): Promise<{[key : string] : any}> {
return new Promise(resolve => {
const valid = this.pattern.test(control.value);
const org = control.parent.get("ghOrg").value;
if (!valid) {
resolve(this.createError(control.value, 'pattern', valid));
} else if (org) {
this.gitProvider.isGitHubRepo(org, control.value).subscribe(
duplicate => resolve(this.createError(control.value, 'duplicate', !duplicate))
);
}
});
}

private createError(value: any, key: string, valid: boolean): any {
return valid ? null : {[key]: {value: value}};
}
}
3 changes: 3 additions & 0 deletions src/app/launcher/launcher.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import { DependencyEditorCreateappStepComponent }
from './create-app/dependency-editor-step/dependency-editor-step.component';
import { GitproviderCreateappStepComponent }
from './create-app/gitprovider-createapp-step/gitprovider-createapp-step.component';
import { GitProviderRepositoryValidatorDirective }
from './create-app/gitprovider-createapp-step/gitprovider-repository.validator';
import { MissionRuntimeCreateappStepComponent }
from './create-app/mission-runtime-createapp-step/mission-runtime-createapp-step.component';
import { ProjectProgressCreateappNextstepComponent }
Expand Down Expand Up @@ -80,6 +82,7 @@ export const providers: Provider[] = [
CancelOverlayComponent,
DependencyEditorCreateappStepComponent,
GitproviderCreateappStepComponent,
GitProviderRepositoryValidatorDirective,
GitproviderImportappStepComponent,
MissionRuntimeCreateappStepComponent,
ProjectProgressCreateappNextstepComponent,
Expand Down
2 changes: 1 addition & 1 deletion src/demo/service/demo-git-provider.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class DemoGitProviderService implements GitProviderService {
* @returns {Observable<boolean>} True if GitHub repo exists
*/
isGitHubRepo(org: string, repoName: string): Observable<boolean> {
let result = (repoName === 'patternfly/patternfly'); // Simulate a existing repo
let result = (org === 'patternfly' && repoName === 'patternfly'); // Simulate a existing repo
return Observable.of(result);
}

Expand Down

0 comments on commit 9dd2412

Please sign in to comment.