From 4c76396e5b53a9c423159fd3110e699d1648cf07 Mon Sep 17 00:00:00 2001 From: dominik003 Date: Mon, 3 Jul 2023 14:49:27 +0200 Subject: [PATCH 1/2] fix: Use correct revisions for readonly selection --- frontend/src/app/services/git/git.service.ts | 46 +++++++++--- ...eate-readonly-model-options.component.html | 11 +-- ...create-readonly-model-options.component.ts | 73 ++++++++----------- 3 files changed, 69 insertions(+), 61 deletions(-) diff --git a/frontend/src/app/services/git/git.service.ts b/frontend/src/app/services/git/git.service.ts index f854cc9b2..52c7efe3d 100644 --- a/frontend/src/app/services/git/git.service.ts +++ b/frontend/src/app/services/git/git.service.ts @@ -9,6 +9,7 @@ import { AbstractControl, AsyncValidatorFn, ValidationErrors, + ValidatorFn, } from '@angular/forms'; import { BehaviorSubject, map, Observable, take } from 'rxjs'; import { environment } from 'src/environments/environment'; @@ -50,22 +51,34 @@ export class GitService { }); } + getPrivateRevision( + gitUrl: string, + project_slug: string, + model_slug: string, + git_model_id: number + ): Observable { + return this.http.post( + this.BACKEND_URL_PREFIX + + `/projects/${project_slug}/models/${model_slug}/modelsources/git/${git_model_id}/revisions`, + gitUrl + ); + } + loadPrivateRevisions( gitUrl: string, project_slug: string, model_slug: string, git_model_id: number ): void { - this.http - .post( - this.BACKEND_URL_PREFIX + - `/projects/${project_slug}/models/${model_slug}/modelsources/git/${git_model_id}/revisions`, - gitUrl - ) - .subscribe({ - next: (revisions) => this._revisions.next(revisions), - error: () => this._revisions.next(undefined), - }); + this.getPrivateRevision( + gitUrl, + project_slug, + model_slug, + git_model_id + ).subscribe({ + next: (revisions) => this._revisions.next(revisions), + error: () => this._revisions.next(undefined), + }); } clearRevision(): void { @@ -87,3 +100,16 @@ export class GitService { }; } } + +export function existingRevisionValidator(revisions: Revisions): ValidatorFn { + return (control: AbstractControl): ValidationErrors | null => { + const revision = control.value; + + const isInBranches = revisions.branches.includes(revision); + const isInTags = revisions.tags.includes(revision); + + return !(isInBranches || isInTags) + ? { revisionNotFoundError: `${revision} does not exist` } + : null; + }; +} diff --git a/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.html b/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.html index 7cf9458e6..08025fc2b 100644 --- a/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.html +++ b/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.html @@ -5,7 +5,9 @@

- {{ model.name }} + {{ + modelOptions.model.name + }}

@@ -19,12 +21,7 @@

A revision is required - + {{ form.controls.revision.errors?.revisionNotFoundError }} diff --git a/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.ts b/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.ts index 7564367c0..0faf225a3 100644 --- a/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.ts +++ b/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.ts @@ -5,14 +5,14 @@ import { Component, Input, OnInit } from '@angular/core'; import { Validators, FormBuilder } from '@angular/forms'; -import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy'; import { filter } from 'rxjs'; -import { - getPrimaryGitModel, - Model, -} from 'src/app/projects/models/service/model.service'; +import { Model } from 'src/app/projects/models/service/model.service'; import { GetGitModel } from 'src/app/projects/project-detail/model-overview/model-detail/git-model.service'; -import { Revisions, GitService } from 'src/app/services/git/git.service'; +import { + Revisions, + GitService, + existingRevisionValidator, +} from 'src/app/services/git/git.service'; export type ModelOptions = { model: Model; @@ -22,7 +22,6 @@ export type ModelOptions = { deepClone: boolean; }; -@UntilDestroy() @Component({ selector: 'create-readonly-model-options', templateUrl: './create-readonly-model-options.component.html', @@ -34,28 +33,16 @@ export class CreateReadonlyModelOptionsComponent implements OnInit { constructor(private gitService: GitService, private fb: FormBuilder) {} - private revision?: Revisions; + private revisions?: Revisions; public filteredRevisions?: Revisions; public form = this.fb.group({ - include: this.fb.control(true), - revision: this.fb.control('', { - validators: Validators.required, - asyncValidators: this.gitService.asyncExistingRevisionValidator(), - }), - deepClone: this.fb.control(false), + include: [true], + revision: this.fb.control('', Validators.required), + deepClone: [false], }); - get model(): Model { - return this.modelOptions!.model; - } - ngOnInit(): void { - const primaryGitModel = getPrimaryGitModel(this.model); - if (!primaryGitModel) { - return; - } - this.form.controls.deepClone.setValue(this.modelOptions.deepClone); this.form.controls.include.setValue(this.modelOptions.include); @@ -67,39 +54,37 @@ export class CreateReadonlyModelOptionsComponent implements OnInit { this.modelOptions.include = value || false; }); - this.gitService.revisions - .pipe(untilDestroyed(this), filter(Boolean)) - .subscribe({ - next: (revisions) => { - this.revision = revisions; - this.filteredRevisions = revisions; + this.gitService + .getPrivateRevision( + this.modelOptions.primaryGitModel.path, + this.projectSlug, + this.modelOptions.model.slug, + this.modelOptions.primaryGitModel.id + ) + .pipe(filter(Boolean)) + .subscribe((revisions) => { + this.revisions = revisions; + this.filteredRevisions = revisions; - this.form.controls.revision.enable(); - this.form.controls.revision.setValue(primaryGitModel?.revision || ''); - this.form.controls.revision.updateValueAndValidity(); - }, - complete: () => this.gitService.clearRevision(), - }); + const revisionControl = this.form.controls.revision; - this.gitService.loadPrivateRevisions( - primaryGitModel.path, - this.projectSlug, - this.model.slug, - primaryGitModel.id - ); + revisionControl.addValidators(existingRevisionValidator(revisions)); + revisionControl.setValue(this.modelOptions.primaryGitModel.revision); + revisionControl.updateValueAndValidity(); + }); } filterRevisionsByPrefix(prefix: string): void { - if (!this.revision) { + if (!this.revisions) { this.filteredRevisions = undefined; return; } this.filteredRevisions = { - branches: this.revision!.branches.filter((branch) => + branches: this.revisions!.branches.filter((branch) => branch.toLowerCase().startsWith(prefix.toLowerCase()) ), - tags: this.revision!.tags.filter((tag) => + tags: this.revisions!.tags.filter((tag) => tag.toLowerCase().startsWith(prefix.toLowerCase()) ), }; From 8e7a8dacb12bd93eb36711cf4b8548073399e889 Mon Sep 17 00:00:00 2001 From: dominik003 Date: Mon, 3 Jul 2023 17:47:49 +0200 Subject: [PATCH 2/2] feat: Apply PR comments for #819 --- frontend/src/app/services/git/git.service.ts | 20 +++++++++---------- ...eate-readonly-model-options.component.html | 7 ++++++- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/frontend/src/app/services/git/git.service.ts b/frontend/src/app/services/git/git.service.ts index 52c7efe3d..e260549df 100644 --- a/frontend/src/app/services/git/git.service.ts +++ b/frontend/src/app/services/git/git.service.ts @@ -53,28 +53,28 @@ export class GitService { getPrivateRevision( gitUrl: string, - project_slug: string, - model_slug: string, - git_model_id: number + projectSlug: string, + modelSlug: string, + gitModelId: number ): Observable { return this.http.post( this.BACKEND_URL_PREFIX + - `/projects/${project_slug}/models/${model_slug}/modelsources/git/${git_model_id}/revisions`, + `/projects/${projectSlug}/models/${modelSlug}/modelsources/git/${gitModelId}/revisions`, gitUrl ); } loadPrivateRevisions( gitUrl: string, - project_slug: string, - model_slug: string, - git_model_id: number + projectSlug: string, + modelSlug: string, + gitModelId: number ): void { this.getPrivateRevision( gitUrl, - project_slug, - model_slug, - git_model_id + projectSlug, + modelSlug, + gitModelId ).subscribe({ next: (revisions) => this._revisions.next(revisions), error: () => this._revisions.next(undefined), diff --git a/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.html b/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.html index 08025fc2b..0c814ae27 100644 --- a/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.html +++ b/frontend/src/app/sessions/user-sessions-wrapper/create-sessions/create-readonly-session/create-readonly-model-options/create-readonly-model-options.component.html @@ -21,7 +21,12 @@

A revision is required - + {{ form.controls.revision.errors?.revisionNotFoundError }}