Skip to content

Commit

Permalink
fix(api,ui): template audit save error and problem with base64 utf8 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt authored and sguiheux committed Feb 25, 2019
1 parent 3545489 commit 21560d9
Show file tree
Hide file tree
Showing 22 changed files with 122 additions and 88 deletions.
4 changes: 2 additions & 2 deletions engine/api/templates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_postTemplateApplyHandler(t *testing.T) {
GroupID: g.ID,
Name: name,
Slug: slug.Convert(name),
Value: base64.StdEncoding.EncodeToString([]byte(
Workflow: base64.StdEncoding.EncodeToString([]byte(
`name: [[.name]]
version: v1.0
workflow:
Expand Down Expand Up @@ -98,7 +98,7 @@ func Test_postTemplateBulkHandler(t *testing.T) {
GroupID: g.ID,
Name: name,
Slug: slug.Convert(name),
Value: base64.StdEncoding.EncodeToString([]byte(
Workflow: base64.StdEncoding.EncodeToString([]byte(
`name: [[.name]]
version: v1.0
workflow:
Expand Down
28 changes: 14 additions & 14 deletions engine/api/workflow/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ func (a addWorkflowAudit) Compute(db gorp.SqlExecutor, e sdk.Event) error {
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataAfter: buffer.String(),
DataType: "yaml",
},
WorkflowID: wEvent.Workflow.ID,
ProjectKey: e.ProjectKey,
DataType: "yaml",
DataAfter: buffer.String(),
})
}

Expand All @@ -110,12 +110,12 @@ func (u updateWorkflowAudit) Compute(db gorp.SqlExecutor, e sdk.Event) error {
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataAfter: newWorkflowBuffer.String(),
DataBefore: oldWorkflowBuffer.String(),
DataType: "yaml",
},
WorkflowID: wEvent.NewWorkflow.ID,
ProjectKey: e.ProjectKey,
DataType: "yaml",
DataAfter: newWorkflowBuffer.String(),
DataBefore: oldWorkflowBuffer.String(),
})
}

Expand All @@ -137,11 +137,11 @@ func (d deleteWorkflowAudit) Compute(db gorp.SqlExecutor, e sdk.Event) error {
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataBefore: oldWorkflowBuffer.String(),
DataType: "yaml",
},
WorkflowID: wEvent.Workflow.ID,
ProjectKey: e.ProjectKey,
DataType: "yaml",
DataBefore: oldWorkflowBuffer.String(),
})
}

Expand All @@ -163,11 +163,11 @@ func (a addWorkflowPermissionAudit) Compute(db gorp.SqlExecutor, e sdk.Event) er
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataAfter: string(b),
DataType: "json",
},
WorkflowID: wEvent.WorkflowID,
ProjectKey: e.ProjectKey,
DataType: "json",
DataAfter: string(b),
})
}

Expand All @@ -194,12 +194,12 @@ func (u updateWorkflowPermissionAudit) Compute(db gorp.SqlExecutor, e sdk.Event)
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataBefore: string(oldPerm),
DataAfter: string(newPerm),
DataType: "json",
},
WorkflowID: wEvent.WorkflowID,
ProjectKey: e.ProjectKey,
DataType: "json",
DataBefore: string(oldPerm),
DataAfter: string(newPerm),
})
}

Expand All @@ -221,11 +221,11 @@ func (a deleteWorkflowPermissionAudit) Compute(db gorp.SqlExecutor, e sdk.Event)
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataBefore: string(b),
DataType: "json",
},
ProjectKey: e.ProjectKey,
WorkflowID: wEvent.WorkflowID,
DataType: "json",
DataBefore: string(b),
})
}

Expand Down
6 changes: 3 additions & 3 deletions engine/api/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,14 +546,14 @@ func Test_postWorkflowRollbackHandler(t *testing.T) {
wfAudit := sdk.AuditWorkflow{
AuditCommon: sdk.AuditCommon{
Created: time.Now(),
DataBefore: string(wfBts),
DataAfter: string(wfUpdatedBts),
EventType: "WorkflowUpdate",
DataType: "yaml",
TriggeredBy: u.Username,
},
ProjectKey: proj.Key,
WorkflowID: wf.ID,
DataType: "yaml",
DataBefore: string(wfBts),
DataAfter: string(wfUpdatedBts),
}
test.NoError(t, workflow.InsertAudit(api.mustDB(), &wfAudit))

Expand Down
33 changes: 8 additions & 25 deletions engine/api/workflowtemplate/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,14 @@ func (a addWorkflowTemplateAudit) Compute(db gorp.SqlExecutor, e sdk.Event) erro
return sdk.WrapError(err, "Unable to decode payload")
}

b, err := json.Marshal(wtEvent.WorkflowTemplate)
if err != nil {
return sdk.WrapError(err, "Unable to marshal workflow template")
}

return InsertAudit(db, &sdk.AuditWorkflowTemplate{
AuditCommon: sdk.AuditCommon{
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataAfter: string(b),
DataType: "json",
},
WorkflowTemplateID: wtEvent.WorkflowTemplate.ID,
DataAfter: wtEvent.WorkflowTemplate,
})
}

Expand All @@ -83,27 +77,16 @@ func (a updateWorkflowTemplateAudit) Compute(db gorp.SqlExecutor, e sdk.Event) e
return sdk.WrapError(err, "Unable to decode payload")
}

before, err := json.Marshal(wtEvent.OldWorkflowTemplate)
if err != nil {
return sdk.WrapError(err, "Unable to marshal workflow template")
}

after, err := json.Marshal(wtEvent.NewWorkflowTemplate)
if err != nil {
return sdk.WrapError(err, "Unable to marshal workflow template")
}

return InsertAudit(db, &sdk.AuditWorkflowTemplate{
AuditCommon: sdk.AuditCommon{
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataBefore: string(before),
DataAfter: string(after),
DataType: "json",
},
WorkflowTemplateID: wtEvent.NewWorkflowTemplate.ID,
ChangeMessage: wtEvent.ChangeMessage,
DataBefore: wtEvent.OldWorkflowTemplate,
DataAfter: wtEvent.NewWorkflowTemplate,
})
}

Expand All @@ -125,10 +108,10 @@ func (a addWorkflowTemplateInstanceAudit) Compute(db gorp.SqlExecutor, e sdk.Eve
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataAfter: string(b),
DataType: "json",
},
WorkflowTemplateInstanceID: wtEvent.WorkflowTemplateInstance.ID,
DataType: "json",
DataAfter: string(b),
})
}

Expand All @@ -155,10 +138,10 @@ func (a updateWorkflowTemplateInstanceAudit) Compute(db gorp.SqlExecutor, e sdk.
EventType: strings.Replace(e.EventType, "sdk.Event", "", -1),
Created: e.Timestamp,
TriggeredBy: e.Username,
DataBefore: string(before),
DataAfter: string(after),
DataType: "json",
},
WorkflowTemplateInstanceID: wtEvent.NewWorkflowTemplateInstance.ID,
DataType: "json",
DataBefore: string(before),
DataAfter: string(after),
})
}
2 changes: 1 addition & 1 deletion engine/api/workflowtemplate/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestCRUD(t *testing.T) {
{Key: "my-string", Type: sdk.ParameterTypeString, Required: true},
{Key: "my-repository", Type: sdk.ParameterTypeRepository, Required: true},
},
Value: "the-yml-workflow-encoded",
Workflow: "the-yml-workflow-encoded",
Pipelines: []sdk.PipelineTemplate{{Value: "the-yml-pipeline-encoded"}},
Applications: []sdk.ApplicationTemplate{{Value: "the-yml-application-encoded"}},
Environments: []sdk.EnvironmentTemplate{{Value: "the-yml-environment-encoded"}},
Expand Down
2 changes: 1 addition & 1 deletion engine/api/workflowtemplate/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Execute(wt *sdk.WorkflowTemplate, instance *sdk.WorkflowTemplateInstance) (

var multiErr sdk.MultiError

v, err := decodeTemplateValue(wt.Value)
v, err := decodeTemplateValue(wt.Workflow)
if err != nil {
return result, err
}
Expand Down
8 changes: 4 additions & 4 deletions engine/api/workflowtemplate/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func TestExecuteTemplate(t *testing.T) {
{Key: "object", Type: sdk.ParameterTypeJSON},
{Key: "list", Type: sdk.ParameterTypeJSON},
},
Value: base64.StdEncoding.EncodeToString([]byte(`
Workflow: base64.StdEncoding.EncodeToString([]byte(`
name: [[.name]]
description: Test simple workflow
description: Test simple workflow 👍
version: v1.0
workflow:
Node-1:
Expand Down Expand Up @@ -89,7 +89,7 @@ values:

assert.Equal(t, `
name: my-workflow
description: Test simple workflow
description: Test simple workflow 👍
version: v1.0
workflow:
Node-1:
Expand Down Expand Up @@ -143,7 +143,7 @@ func TestExecuteTemplateWithError(t *testing.T) {
{Key: "deployWhen", Type: sdk.ParameterTypeString},
{Key: "repo", Type: sdk.ParameterTypeRepository},
},
Value: base64.StdEncoding.EncodeToString([]byte(`
Workflow: base64.StdEncoding.EncodeToString([]byte(`
name: [[.name]
description: Test simple workflow with error
version: v1.0`)),
Expand Down
2 changes: 1 addition & 1 deletion engine/api/workflowtemplate/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Pull(wt *sdk.WorkflowTemplate, f exportentities.Format, w io.Writer) error
return sdk.WrapError(err, "Unable to copy tmpl buffer")
}

data, err := base64.StdEncoding.DecodeString(wt.Value)
data, err := base64.StdEncoding.DecodeString(wt.Workflow)
if err != nil {
return sdk.WrapError(err, "Unable to decode workflow value")
}
Expand Down
8 changes: 8 additions & 0 deletions engine/sql/160_workflow_template_audit.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- +migrate Up

ALTER TABLE workflow_template_audit DROP COLUMN IF EXISTS data_type;

-- +migrate Down

ALTER TABLE workflow_template_audit ADD COLUMN data_type VARCHAR(100);
UPDATE workflow_template_audit SET data_type = 'json';
17 changes: 11 additions & 6 deletions sdk/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ type AuditCommon struct {
ID int64 `json:"id" db:"id"`
TriggeredBy string `json:"triggered_by" db:"triggered_by"`
Created time.Time `json:"created" db:"created" mapstructure:"-"`
DataBefore string `json:"data_before" db:"data_before"`
DataAfter string `json:"data_after" db:"data_after"`
EventType string `json:"event_type" db:"event_type"`
DataType string `json:"data_type" db:"data_type"`
}

// AuditWorkflow represents an audit data on a workflow.
type AuditWorkflow struct {
AuditCommon
ProjectKey string `json:"project_key" db:"project_key"`
WorkflowID int64 `json:"workflow_id" db:"workflow_id"`
DataType string `json:"data_type" db:"data_type"`
DataBefore string `json:"data_before" db:"data_before"`
DataAfter string `json:"data_after" db:"data_after"`
}

// Audit represents audit interface.
Expand All @@ -39,12 +39,17 @@ type Audit interface {
// AuditWorkflowTemplate represents an audit data on a workflow template.
type AuditWorkflowTemplate struct {
AuditCommon
WorkflowTemplateID int64 `json:"workflow_template_id" db:"workflow_template_id"`
ChangeMessage string `json:"change_message,omitempty" db:"change_message"`
WorkflowTemplateID int64 `json:"workflow_template_id" db:"workflow_template_id"`
ChangeMessage string `json:"change_message,omitempty" db:"change_message"`
DataBefore WorkflowTemplate `json:"data_before" db:"data_before"`
DataAfter WorkflowTemplate `json:"data_after" db:"data_after"`
}

// AuditWorkflowTemplateInstance represents an audit data on a workflow template instance.
type AuditWorkflowTemplateInstance struct {
AuditCommon
WorkflowTemplateInstanceID int64 `json:"workflow_template_instance_id" db:"workflow_template_instance_id"`
WorkflowTemplateInstanceID int64 `json:"workflow_template_instance_id" db:"workflow_template_instance_id"`
DataType string `json:"data_type" db:"data_type"`
DataBefore string `json:"data_before" db:"data_before"`
DataAfter string `json:"data_after" db:"data_after"`
}
2 changes: 1 addition & 1 deletion sdk/exportentities/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (w Template) GetTemplate(wkf []byte, pips, apps, envs [][]byte) sdk.Workflo
Name: w.Group,
},
Description: w.Description,
Value: base64.StdEncoding.EncodeToString(wkf),
Workflow: base64.StdEncoding.EncodeToString(wkf),
Pipelines: make([]sdk.PipelineTemplate, len(pips)),
Applications: make([]sdk.ApplicationTemplate, len(apps)),
Environments: make([]sdk.EnvironmentTemplate, len(envs)),
Expand Down
19 changes: 17 additions & 2 deletions sdk/workflow_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type WorkflowTemplate struct {
Slug string `json:"slug" db:"slug"`
Description string `json:"description" db:"description"`
Parameters WorkflowTemplateParameters `json:"parameters" db:"parameters"`
Value string `json:"value" db:"value"`
Workflow string `json:"value" db:"value"`
Pipelines PipelineTemplates `json:"pipelines" db:"pipelines"`
Applications ApplicationTemplates `json:"applications" db:"applications"`
Environments EnvironmentTemplates `json:"environments" db:"environments"`
Expand All @@ -63,6 +63,21 @@ type WorkflowTemplate struct {
ChangeMessage string `json:"change_message,omitempty" db:"-"`
}

// Value returns driver.Value from workflow template.
func (w WorkflowTemplate) Value() (driver.Value, error) {
j, err := json.Marshal(w)
return j, WrapError(err, "cannot marshal WorkflowTemplate")
}

// Scan workflow template.
func (w *WorkflowTemplate) Scan(src interface{}) error {
source, ok := src.([]byte)
if !ok {
return WithStack(errors.New("type assertion .([]byte) failed"))
}
return WrapError(json.Unmarshal(source, w), "cannot unmarshal WorkflowTemplate")
}

// 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
Expand Down Expand Up @@ -158,7 +173,7 @@ func (w *WorkflowTemplate) Update(data WorkflowTemplate) {
w.Slug = data.Slug
w.GroupID = data.GroupID
w.Description = data.Description
w.Value = data.Value
w.Workflow = data.Workflow
w.Parameters = data.Parameters
w.Pipelines = data.Pipelines
w.Applications = data.Applications
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/template/simple/1.pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ jobs:
stage: Stage-1
steps:
- script:
- echo "Hello World!"
- echo "Hello World 👍!"
- echo "[[range .params.data]][[.]][[end]]"
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ jobs:
stage: Stage-1
steps:
- script:
- echo "Hello World!"
- "echo \"Hello World \U0001F44D!\""
- echo "onetwo"
Loading

0 comments on commit 21560d9

Please sign in to comment.