Skip to content

Commit

Permalink
fix(api): generation of jsonschema (#4943)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux committed Feb 4, 2020
1 parent 6f1e8ba commit 37cfcd0
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 24 deletions.
19 changes: 10 additions & 9 deletions engine/api/user_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"reflect"

"github.com/alecthomas/jsonschema"
"github.com/iancoleman/orderedmap"

"github.com/ovh/cds/engine/api/action"
"github.com/ovh/cds/engine/api/group"
Expand Down Expand Up @@ -61,11 +62,11 @@ func (api *API) getUserJSONSchema() service.Handler {
path = fmt.Sprintf("%s/%s", as[i].Group.Name, as[i].Name)
}
s := slug.Convert(path)
sch.Definitions["Step"].Properties[path] = &jsonschema.Type{
sch.Definitions["Step"].Properties.Set(path, &jsonschema.Type{
Version: "http://json-schema.org/draft-04/schema#",
Ref: "#/definitions/" + s,
Description: as[i].Description,
}
})
sch.Definitions["Step"].OneOf = append(sch.Definitions["Step"].OneOf, &jsonschema.Type{
Required: []string{
path,
Expand All @@ -74,25 +75,25 @@ func (api *API) getUserJSONSchema() service.Handler {
})

sch.Definitions[s] = &jsonschema.Type{
Properties: map[string]*jsonschema.Type{},
Properties: orderedmap.New(),
AdditionalProperties: sch.Definitions["Step"].AdditionalProperties,
Type: "object",
}
for j := range as[i].Parameters {
p := as[i].Parameters[j]
switch p.Type {
case "number":
sch.Definitions[s].Properties[p.Name] = &jsonschema.Type{
sch.Definitions[s].Properties.Set(p.Name, &jsonschema.Type{
Type: "integer",
}
})
case "boolean":
sch.Definitions[s].Properties[p.Name] = &jsonschema.Type{
sch.Definitions[s].Properties.Set(p.Name, &jsonschema.Type{
Type: "boolean",
}
})
default:
sch.Definitions[s].Properties[p.Name] = &jsonschema.Type{
sch.Definitions[s].Properties.Set(p.Name, &jsonschema.Type{
Type: "string",
}
})
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ require (
github.com/hashicorp/vault v0.0.0-20170114041158-f1c8b772fdec
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/hinshun/vt10x v0.0.0-20180809195222-d55458df857c // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0
github.com/imdario/mergo v0.0.0-20180119215619-163f41321a19 // indirect
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf
github.com/inconshreveable/mousetrap v1.0.0 // indirect
Expand Down Expand Up @@ -233,6 +234,8 @@ require (
sigs.k8s.io/yaml v1.1.0 // indirect
)

replace github.com/alecthomas/jsonschema => github.com/sguiheux/jsonschema v0.2.0

replace github.com/go-gorp/gorp => github.com/yesnault/gorp v2.0.1-0.20190906143353-6210446a0d92+incompatible

replace github.com/docker/docker => github.com/docker/engine v0.0.0-20180816081446-320063a2ad06
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/imdario/mergo v0.0.0-20180119215619-163f41321a19 h1:geJOJJZwkYI1yqxWrAMcgrwDvy4P1XyNNgIyN9d6UXc=
github.com/imdario/mergo v0.0.0-20180119215619-163f41321a19/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/go-update v0.0.0-20160112193335-8152e7eb6ccf h1:WfD7VjIE6z8dIvMsI4/s+1qr5EL+zoIGev1BQj1eoJ8=
Expand Down Expand Up @@ -495,6 +497,9 @@ github.com/sethgrid/pester v0.0.0-20171127025028-760f8913c048 h1:6Men5bEVIDWxw4f
github.com/sethgrid/pester v0.0.0-20171127025028-760f8913c048/go.mod h1:Ad7IjTpvzZO8Fl0vh9AzQ+j/jYZfyp2diGwI8m5q+ns=
github.com/sguiheux/go-coverage v0.0.0-20190710153556-287b082a7197 h1:qu90yDtRE5WEfRT5mn9v0Xz9RaopLguhbPwZKx4dHq8=
github.com/sguiheux/go-coverage v0.0.0-20190710153556-287b082a7197/go.mod h1:0hhKrsUsoT7yvxwNGKa+TSYNA26DNWMqReeZEQq/9FI=
github.com/sguiheux/jsonschema v0.0.0-20200131132618-0ed13897d53d/go.mod h1:/n6+1/DWPltRLWL/VKyUxg6tzsl5kHUCcraimt4vr60=
github.com/sguiheux/jsonschema v0.2.0 h1:hFHEPxudR6sNcsg50/iuJzHT5d3h3KOvtcg2Hrshs2k=
github.com/sguiheux/jsonschema v0.2.0/go.mod h1:/n6+1/DWPltRLWL/VKyUxg6tzsl5kHUCcraimt4vr60=
github.com/shirou/gopsutil v0.0.0-20170406131756-e49a95f3d5f8 h1:05R1OwSk31dkzqf2Jf27n2IOoF9zkK9LcPgPsEm8U7U=
github.com/shirou/gopsutil v0.0.0-20170406131756-e49a95f3d5f8/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/sirupsen/logrus v1.0.6/go.mod h1:pMByvHTf9Beacp5x1UXfOR9xyW/9antXMhjMPG0dEzc=
Expand Down
2 changes: 1 addition & 1 deletion sdk/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (w *Workflow) ValidateType() error {
//WorkflowNodeConditions is either an array of WorkflowNodeCondition or a lua script
type WorkflowNodeConditions struct {
PlainConditions []WorkflowNodeCondition `json:"plain,omitempty" yaml:"check,omitempty"`
LuaScript string `json:"lua_script,omitempty" yaml:"script,omitempty"`
LuaScript string `json:"script,omitempty" yaml:"script,omitempty"`
}

// Value returns driver.Value from WorkflowNodeConditions request.
Expand Down
2 changes: 1 addition & 1 deletion ui/src/app/model/workflow.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ export class WorkflowNodeHookConfigValue {

// WorkflowTriggerConditions is either a lua script to check conditions or a set of WorkflowTriggerCondition
export class WorkflowNodeConditions {
lua_script: string;
script: string;
plain: Array<WorkflowNodeCondition>;
}

Expand Down
6 changes: 3 additions & 3 deletions ui/src/app/shared/conditions/conditions.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class ConditionsComponent extends Table<WorkflowNodeCondition> implements
}
@Input('conditions') set conditions(conditions: WorkflowNodeConditions) {
this._conditions = conditions;
if (this._conditions.lua_script && this._conditions.lua_script !== '') {
if (this._conditions.script && this._conditions.script !== '') {
this.isAdvanced = true;
} else {
this.isAdvanced = false;
Expand Down Expand Up @@ -108,7 +108,7 @@ export class ConditionsComponent extends Table<WorkflowNodeCondition> implements
this.conditions.plain = new Array<WorkflowNodeCondition>();
}

this.previousValue = this.conditions.lua_script;
this.previousValue = this.conditions.script;
let condition = this.conditions.plain.find(cc => cc.variable === 'cds.manual');
if (condition) {
condition.value = <any>(condition.value === 'true' || <any>condition.value === true);
Expand Down Expand Up @@ -143,7 +143,7 @@ export class ConditionsComponent extends Table<WorkflowNodeCondition> implements
pushChange(event: string, e?: string): void {
if (event !== 'codemirror') {
this.conditionsChange.emit(this.conditions);
this.conditions.lua_script = '';
this.conditions.script = '';
return;
}
if (event === 'codemirror' && e && e !== this.previousValue) {
Expand Down
4 changes: 2 additions & 2 deletions ui/src/app/shared/conditions/conditions.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<sui-checkbox class="toggle" [(ngModel)]="isAdvanced">{{'workflow_node_condition_advanced' | translate }}
</sui-checkbox>
</div>
<div class="ui warning message" *ngIf="conditions.plain && conditions.plain.length && conditions.lua_script">
<div class="ui warning message" *ngIf="conditions.plain && conditions.plain.length && conditions.script">
<p>
{{'workflow_node_condition_warning' | translate}}
</p>
Expand Down Expand Up @@ -121,7 +121,7 @@ <h4 class="inline">{{'workflow_node_condition_lua_title' | translate}}</h4>
</div>
</div>
</div>
<codemirror [(ngModel)]="conditions.lua_script" (ngModelChange)="pushChange('codemirror', $event)"
<codemirror [(ngModel)]="conditions.script" (ngModelChange)="pushChange('codemirror', $event)"
(change)="changeCodeMirror($event)" [config]="codeMirrorConfig" #textareaCodeMirror>
</codemirror>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class WorkflowWizardNodeConditionComponent extends Table<WorkflowNodeCond
if (!this.editableNode.context.conditions.plain) {
this.editableNode.context.conditions.plain = new Array<WorkflowNodeCondition>();
}
this.previousValue = this.editableNode.context.conditions.lua_script;
this.previousValue = this.editableNode.context.conditions.script;
let condition = this.editableNode.context.conditions.plain.find(cc => cc.variable === 'cds.manual');
if (condition) {
condition.value = <any>(condition.value !== 'false');
Expand All @@ -63,7 +63,7 @@ export class WorkflowWizardNodeConditionComponent extends Table<WorkflowNodeCond
this.editableHook.conditions.plain = new Array<WorkflowNodeCondition>();
}

this.previousValue = this.editableHook.conditions.lua_script;
this.previousValue = this.editableHook.conditions.script;
let condition = this.editableHook.conditions.plain.find(cc => cc.variable === 'cds.manual');
if (condition) {
condition.value = <any>(condition.value !== 'false');
Expand Down Expand Up @@ -147,10 +147,10 @@ export class WorkflowWizardNodeConditionComponent extends Table<WorkflowNodeCond
updateWorkflow(): void {
this.loading = true;
if (this.editableNode != null) {
if (this.editableNode.context.conditions.lua_script && this.editableNode.context.conditions.lua_script !== '') {
if (this.editableNode.context.conditions.script && this.editableNode.context.conditions.script !== '') {
this.editableNode.context.conditions.plain = null;
} else {
this.editableNode.context.conditions.lua_script = '';
this.editableNode.context.conditions.script = '';
let sizeBefore = this.editableNode.context.conditions.plain.length;


Expand All @@ -171,10 +171,10 @@ export class WorkflowWizardNodeConditionComponent extends Table<WorkflowNodeCond
}
}
} else if (this.editableHook != null) {
if (this.editableHook.conditions.lua_script && this.editableHook.conditions.lua_script !== '') {
if (this.editableHook.conditions.script && this.editableHook.conditions.script !== '') {
this.editableHook.conditions.plain = null;
} else {
this.editableHook.conditions.lua_script = '';
this.editableHook.conditions.script = '';
let sizeBefore = this.editableHook.conditions.plain.length;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ export class PipelineWorkflowComponent implements OnInit, OnDestroy {
this.loadingStage = true;
switch (type) {
case 'update':
if (this.selectedStage.conditions.lua_script && this.selectedStage.conditions.lua_script !== '') {
if (this.selectedStage.conditions.script && this.selectedStage.conditions.script !== '') {
this.selectedStage.conditions.plain = null;
} else {
this.selectedStage.conditions.lua_script = '';
this.selectedStage.conditions.script = '';
}
this.store.dispatch(new pipelineActions.UpdatePipelineStage({
projectKey: this.project.key,
Expand Down

0 comments on commit 37cfcd0

Please sign in to comment.