Skip to content

Commit

Permalink
fix(ui, api): remove import workflow template from URL (#6192)
Browse files Browse the repository at this point in the history
Signed-off-by: francois  samin <[email protected]>
  • Loading branch information
fsamin authored Jun 7, 2022
1 parent 4fd962f commit dfa3c70
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 143 deletions.
78 changes: 7 additions & 71 deletions engine/api/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,9 @@ func (api *API) postTemplateHandler() service.Handler {
return err
}

var grp *sdk.Group
var err error
// if imported from url try to download files then overrides request
if data.ImportURL != "" {
t := new(bytes.Buffer)
if err := exportentities.DownloadTemplate(data.ImportURL, t); err != nil {
return sdk.NewError(sdk.ErrWrongRequest, err)
}
wt, err := exportentities.ReadTemplateFromTar(tar.NewReader(t))
if err != nil {
return err
}
wt.ImportURL = data.ImportURL
data = wt

// group name should be set
if data.Group == nil {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing group name")
}

grp, err = group.LoadByName(ctx, api.mustDB(), data.Group.Name, group.LoadOptions.WithMembers)
if err != nil {
return sdk.NewError(sdk.ErrWrongRequest, err)
}
data.GroupID = grp.ID

// check the workflow template extracted
if err := data.IsValid(); err != nil {
return err
}
} else {
grp, err = group.LoadByID(ctx, api.mustDB(), data.GroupID, group.LoadOptions.WithMembers)
if err != nil {
return err
}
grp, err := group.LoadByID(ctx, api.mustDB(), data.GroupID, group.LoadOptions.WithMembers)
if err != nil {
return err
}

data.Version = 1
Expand Down Expand Up @@ -201,42 +169,10 @@ func (api *API) putTemplateHandler() service.Handler {
return err
}

var grp *sdk.Group
// if imported from url try to download files then overrides request
if data.ImportURL != "" {
t := new(bytes.Buffer)
if err := exportentities.DownloadTemplate(data.ImportURL, t); err != nil {
return sdk.NewError(sdk.ErrWrongRequest, err)
}
wt, err := exportentities.ReadTemplateFromTar(tar.NewReader(t))
if err != nil {
return err
}
wt.ImportURL = data.ImportURL
data = wt

// group name should be set
if data.Group == nil {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing group name")
}

// check that the user is admin on the given template's group
grp, err = group.LoadByName(ctx, api.mustDB(), data.Group.Name, group.LoadOptions.WithMembers)
if err != nil {
return sdk.NewError(sdk.ErrWrongRequest, err)
}
data.GroupID = grp.ID

// check the workflow template extracted
if err := data.IsValid(); err != nil {
return err
}
} else {
// check that the group exists and user is admin for group id
grp, err = group.LoadByID(ctx, api.mustDB(), data.GroupID, group.LoadOptions.WithMembers)
if err != nil {
return err
}
// check that the group exists and user is admin for group id
grp, err := group.LoadByID(ctx, api.mustDB(), data.GroupID, group.LoadOptions.WithMembers)
if err != nil {
return err
}

if !isGroupAdmin(ctx, grp) {
Expand Down
10 changes: 0 additions & 10 deletions sdk/workflow_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type WorkflowTemplate struct {
Applications ApplicationTemplates `json:"applications" db:"applications"`
Environments EnvironmentTemplates `json:"environments" db:"environments"`
Version int64 `json:"version" db:"version"`
ImportURL string `json:"import_url" db:"import_url"`
// aggregates
Group *Group `json:"group,omitempty" db:"-"`
FirstAudit *AuditWorkflowTemplate `json:"first_audit,omitempty" db:"-"`
Expand Down Expand Up @@ -86,14 +85,6 @@ func (w *WorkflowTemplate) Scan(src interface{}) error {

// IsValid returns workflow template validity.
func (w *WorkflowTemplate) IsValid() error {
// no more checks if import url is set, fields will be overrited by downloaded files
if w.ImportURL != "" {
if !IsURL(w.ImportURL) || !strings.HasSuffix(w.ImportURL, ".yml") {
return NewErrorFrom(ErrWrongRequest, "invalid given import url")
}
return nil
}

if w.GroupID == 0 {
return NewErrorFrom(ErrWrongRequest, "invalid group id for template")
}
Expand Down Expand Up @@ -188,7 +179,6 @@ func (w *WorkflowTemplate) Update(data WorkflowTemplate) {
w.Applications = data.Applications
w.Environments = data.Environments
w.Version = w.Version + 1
w.ImportURL = data.ImportURL
}

func (w WorkflowTemplate) Path() string {
Expand Down
1 change: 0 additions & 1 deletion ui/src/app/model/workflow-template.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class WorkflowTemplate {
last_audit: AuditWorkflowTemplate;
editable: boolean;
change_message: string;
import_url: string;
}

export class WorkflowTemplateParameter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export class WorkflowTemplateFormComponent {
this._workflowTemplate = <WorkflowTemplate>{ editable: true };
}

this.importFromURL = !!this._workflowTemplate.import_url;

this.changeMessage = null;

this.parameterKeys = [];
Expand Down Expand Up @@ -125,7 +123,6 @@ export class WorkflowTemplateFormComponent {
environmentErrors: { [key: number]: WorkflowTemplateError; };
environmentKeys: Array<number>;
changeMessage: string;
importFromURL: boolean;

constructor(
private _sharedService: SharedService,
Expand All @@ -149,11 +146,6 @@ export class WorkflowTemplateFormComponent {
}

clickSave() {
if (this.importFromURL) {
this.save.emit({ import_url: this.workflowTemplate.import_url });
return;
}

if (!this.workflowTemplate.name || !this.workflowTemplate.group_id) {
return;
}
Expand Down Expand Up @@ -229,9 +221,4 @@ export class WorkflowTemplateFormComponent {
environmentValueChange(key: number, value: string) {
this.environmentValues[key] = value;
}

changeFromURL() {
this.importFromURL = !this.importFromURL;
this._cd.markForCheck();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
<div *ngIf="workflowTemplate.editable && groups && groups.length === 0" class="ui orange message">
{{'common_help_no_group' | translate}}
</div>
<div *ngIf="workflowTemplate.editable && importFromURL" class="ui blue message">
<div *ngIf="workflowTemplate.editable" class="ui blue message">
{{'workflow_template_help_edit_from' | translate}}
</div>
<div class="four fields">
<div class="field">
<label>{{'common_name' | translate}} *</label>
<input class="ui input" type="text" name="name" [(ngModel)]="workflowTemplate.name"
[readonly]="!workflowTemplate.editable || importFromURL">
[readonly]="!workflowTemplate.editable">
</div>
<div class="field">
<label>{{'common_slug' | translate}} *</label>
Expand All @@ -23,11 +23,11 @@
<div class="field">
<label>{{'common_group' | translate}} *</label>
<sm-select class="fluid search" name="group"
*ngIf="groups && workflowTemplate.editable && !importFromURL" [disabled]="loading"
*ngIf="groups && workflowTemplate.editable" [disabled]="loading"
[(model)]="workflowTemplate.group_id" [options]="{'fullTextSearch': true}">
<option *ngFor="let group of groups" [value]="group.id">{{group.name}}</option>
</sm-select>
<input *ngIf="!workflowTemplate.editable || importFromURL" class="ui input" type="text"
<input *ngIf="!workflowTemplate.editable" class="ui input" type="text"
name="group" [ngModel]="workflowTemplate.group?.name" [readonly]="true">
</div>
<div class="field">
Expand All @@ -40,65 +40,48 @@
<label>{{'common_description' | translate}}</label>
<textarea name="description" [(ngModel)]="workflowTemplate.description"
(ngModelChange)="descriptionChange()" [rows]="descriptionRows"
[readonly]="!workflowTemplate.editable || importFromURL"></textarea>
[readonly]="!workflowTemplate.editable"></textarea>
</div>
</div>
<div *ngIf="workflowTemplate.editable" class="ui horizontal divider">
{{'common_or' | translate}}
</div>
<div *ngIf="workflowTemplate.editable || importFromURL" class="sixteen wide column">
<div class="wide fields">
<div class="four wide field centeredField">
<sui-checkbox *ngIf="workflowTemplate.editable" name="import-from-url" [ngModel]="importFromURL"
class="toggle" (ngModelChange)="changeFromURL()">
{{'workflow_template_import_from_url' | translate}}
</sui-checkbox>
<span *ngIf="!workflowTemplate.editable">{{'workflow_template_imported_from_url' |
translate}}</span>
</div>
<div class="twelve wide field">
<input class="ui input" type="text" name="import-url" placeholder="https://host/my-template.yml"
[(ngModel)]="workflowTemplate.import_url"
[readonly]="!workflowTemplate.editable || !importFromURL">
</div>
</div>
</div>
<div class="sixteen wide column" *ngIf="!(mode === 'add' && importFromURL)">
<div *ngIf="(workflowTemplate.editable && !importFromURL) || parameterKeys.length > 0"
<div class="sixteen wide column" *ngIf="!(mode === 'add')">
<div *ngIf="(workflowTemplate.editable) || parameterKeys.length > 0"
class="wide field">
<label>{{ 'common_parameters' | translate }}</label>
</div>
<div class="sixteen wide fields" *ngFor="let k of parameterKeys; let index = index">
<div class="eleven wide field">
<input type="text" name="parameter-key-{{index}}" [placeholder]="'common_name' | translate"
[(ngModel)]="parameterValues[k].key"
[readonly]="!workflowTemplate.editable || importFromURL">
[readonly]="!workflowTemplate.editable">
</div>
<div class="three wide field">
<sui-select *ngIf="workflowTemplate.editable && !importFromURL" class="selection"
<sui-select *ngIf="workflowTemplate.editable" class="selection"
name="parameter-type-{{index}}" placeholder="{{'common_select' | translate}}"
[options]="templateParameterTypes" isSearchable="true" [(ngModel)]="parameterValues[k].type"
#selectType>
<sui-select-option *ngFor="let option of selectType.filteredOptions" [value]="option">
</sui-select-option>
</sui-select>
<input *ngIf="!workflowTemplate.editable || importFromURL" class="ui input" type="text"
<input *ngIf="!workflowTemplate.editable" class="ui input" type="text"
name="parameter-type-{{index}}" [ngModel]="parameterValues[k].type" [readonly]="true">
</div>
<div class="two wide field centeredField">
<div class="ui checked checkbox">
<input type="checkbox" name="parameter-required-{{index}}"
[(ngModel)]="parameterValues[k].required"
[disabled]="!workflowTemplate.editable || importFromURL">
[disabled]="!workflowTemplate.editable">
<label>{{'common_required' | translate}}</label>
</div>
</div>
<button *ngIf="workflowTemplate.editable && !importFromURL" class="ui icon red button"
<button *ngIf="workflowTemplate.editable" class="ui icon red button"
(click)="clickRemoveParameter(k)">
<i class="trash icon"></i>
</button>
</div>
<div *ngIf="workflowTemplate.editable && !importFromURL" class="sixteen wide fields">
<div *ngIf="workflowTemplate.editable" class="sixteen wide fields">
<div class="eleven wide field">
<input type="text" name="parameter-key-add" [placeholder]="'common_name' | translate"
[(ngModel)]="parameterValueAdd.key">
Expand All @@ -125,56 +108,56 @@
<div class="wide field">
<label>{{ 'common_workflow' | translate }}</label>
<app-workflow-template-editor name="workflow-value"
[editable]="workflowTemplate.editable && !importFromURL" [value]="workflowValue"
[editable]="workflowTemplate.editable" [value]="workflowValue"
(changeValue)="workflowValueChange($event)" [error]="workflowError">
</app-workflow-template-editor>
</div>
<div *ngIf="(workflowTemplate.editable && !importFromURL) || pipelineKeys.length > 0"
<div *ngIf="(workflowTemplate.editable) || pipelineKeys.length > 0"
class="wide field">
<label>Pipelines</label>
<div class="codeContainer field" *ngFor="let k of pipelineKeys">
<app-workflow-template-editor name="pipeline-value-{{k}}"
[editable]="workflowTemplate.editable && !importFromURL" [removable]="true"
[editable]="workflowTemplate.editable" [removable]="true"
[value]="pipelineValues[k]" (changeValue)="pipelineValueChange(k, $event)"
(remove)="clickRemovePipeline(k)" [error]="pipelineErrors[k]">
</app-workflow-template-editor>
</div>
<button *ngIf="workflowTemplate.editable && !importFromURL" class="ui icon green button"
<button *ngIf="workflowTemplate.editable" class="ui icon green button"
(click)="clickAddPipeline()">
<i class="plus icon"></i>
</button>
</div>
<div *ngIf="(workflowTemplate.editable && !importFromURL) || applicationKeys.length > 0"
<div *ngIf="(workflowTemplate.editable) || applicationKeys.length > 0"
class="wide field">
<label>Applications</label>
<div class="codeContainer field" *ngFor="let k of applicationKeys">
<app-workflow-template-editor name="application-value-{{k}}"
[editable]="workflowTemplate.editable && !importFromURL" [removable]="true"
[editable]="workflowTemplate.editable" [removable]="true"
[value]="applicationValues[k]" (changeValue)="applicationValueChange(k, $event)"
(remove)="clickRemoveApplication(k)" [error]="applicationErrors[k]">
</app-workflow-template-editor>
</div>
<button *ngIf="workflowTemplate.editable && !importFromURL" class="ui icon green button"
<button *ngIf="workflowTemplate.editable" class="ui icon green button"
(click)="clickAddApplication()">
<i class="plus icon"></i>
</button>
</div>
<div *ngIf="(workflowTemplate.editable && !importFromURL) || environmentKeys.length > 0"
<div *ngIf="(workflowTemplate.editable) || environmentKeys.length > 0"
class="wide field">
<label>Environments</label>
<div class="codeContainer field" *ngFor="let k of environmentKeys">
<app-workflow-template-editor name="environment-value-{{k}}"
[editable]="workflowTemplate.editable && !importFromURL" [removable]="true"
[editable]="workflowTemplate.editable" [removable]="true"
[value]="environmentValues[k]" (changeValue)="environmentValueChange(k, $event)"
(remove)="clickRemoveEnvironment(k)" [error]="environmentErrors[k]">
</app-workflow-template-editor>
</div>
<button *ngIf="workflowTemplate.editable && !importFromURL" class="ui icon green button"
<button *ngIf="workflowTemplate.editable" class="ui icon green button"
(click)="clickAddEnvironment()">
<i class="plus icon"></i>
</button>
</div>
<div *ngIf="workflowTemplate.editable && !importFromURL && mode === 'edit'" class="wide field">
<div *ngIf="workflowTemplate.editable && mode === 'edit'" class="wide field">
<textarea name="changeMessage" [(ngModel)]="changeMessage"
[placeholder]="'workflow_template_change_message' | translate" [rows]="3"></textarea>
</div>
Expand All @@ -184,12 +167,9 @@
<app-delete-button *ngIf="mode === 'edit'" class="left floated" (event)="clickDelete()"
[loading]="loading"></app-delete-button>
<button class="ui positive right floated button" [class.loading]="loading" (click)="clickSave()">
<ng-container *ngIf="!importFromURL">
<ng-container>
<i class="ui icon save"></i>{{ 'btn_save' | translate }}
</ng-container>
<ng-container *ngIf="importFromURL">
<i class="ui icon cloud download"></i>{{ 'btn_synchronize' | translate }}
</ng-container>
</button>
</div>
</div>
Expand Down
3 changes: 0 additions & 3 deletions ui/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@
"btn_add": "Add",
"btn_back": "Back",
"btn_filter": "Filter",
"btn_synchronize": "Synchronize",
"cdsctl_choice_os": "Choose your OS",
"cdsctl_choice_arch": "Choose your architecture",
"cdsctl_choice_options": "Choose your options",
Expand Down Expand Up @@ -859,8 +858,6 @@
"workflow_template_init_from_workflow": "Init a template",
"workflow_template_help_add_from": "Current workflow template was initialized with data from \"{{workflowName}}\" workflow in \"{{projectKey}}\" project. The data will not be stored until you save the template.",
"workflow_template_help_edit_from": "Current workflow template is synchronized from URL. You can't edit it.",
"workflow_template_import_from_url": "Import from URL",
"workflow_template_imported_from_url": "Imported from URL",
"workflow_template_param_select_key": "Select a key",
"workflow_warning_deprecated_yaml": "Yaml workflow configuration is deprecated",
"application_repository_help_line_1": "Your application was not imported from your code.",
Expand Down

0 comments on commit dfa3c70

Please sign in to comment.