Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): export workflow purge info #5769

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions engine/api/workflow/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import (
"github.com/ovh/cds/sdk/telemetry"
)

const (
DefaultRetentionRule = "return (git_branch_exist == \"false\" and run_days_before < 2) or run_days_before < 365"
)

type PushSecrets struct {
ApplicationsSecrets map[int64][]sdk.Variable
EnvironmentdSecrets map[int64][]sdk.Variable
Expand Down Expand Up @@ -312,8 +308,10 @@ func Insert(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.St
if w.HistoryLength == 0 {
w.HistoryLength = sdk.DefaultHistoryLength
}
if w.RetentionPolicy == "" {
w.RetentionPolicy = sdk.DefaultRetentionRule
}
w.MaxRuns = maxRuns
w.RetentionPolicy = DefaultRetentionRule

w.LastModified = time.Now()
if err := db.QueryRow(`INSERT INTO workflow (
Expand Down Expand Up @@ -611,10 +609,6 @@ func Update(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.St
return err
}

if wf.RetentionPolicy == "" {
wf.RetentionPolicy = DefaultRetentionRule
}

if err := CheckValidity(ctx, db, wf); err != nil {
return err
}
Expand All @@ -633,6 +627,24 @@ func Update(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.St
return sdk.WrapError(err, "Unable to load existing workflow with proj:%s ID:%d", proj.Key, wf.ID)
}

// Set or keep HistoryLength
if wf.HistoryLength == 0 {
if oldWf.HistoryLength == 0 {
wf.HistoryLength = sdk.DefaultHistoryLength
} else {
wf.HistoryLength = oldWf.HistoryLength
}
}

// Set or keep RetentionPolicy
if wf.RetentionPolicy == "" {
if oldWf.RetentionPolicy == "" {
wf.RetentionPolicy = sdk.DefaultRetentionRule
} else {
wf.RetentionPolicy = oldWf.RetentionPolicy
}
}

// Keep MaxRun
wf.MaxRuns = oldWf.MaxRuns

Expand Down
5 changes: 0 additions & 5 deletions engine/api/workflow/workflow_importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ func Import(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.St
w.ProjectKey = proj.Key
w.ProjectID = proj.ID

// Default value of history length is 20
if w.HistoryLength == 0 {
w.HistoryLength = 20
}

if w.WorkflowData.Node.Context == nil {
w.WorkflowData.Node.Context = &sdk.NodeContext{}
}
Expand Down
14 changes: 6 additions & 8 deletions engine/api/workflow_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@ workflow:
depends_on:
- fork
pipeline: pip1
retention_policy: return (git_branch_exist == "false" and run_days_before < 2) or run_days_before < 365
`, rec.Body.String())

}

func Test_getWorkflowExportHandlerWithPermissions(t *testing.T) {
Expand Down Expand Up @@ -209,10 +207,11 @@ func Test_getWorkflowExportHandlerWithPermissions(t *testing.T) {
}))

w := sdk.Workflow{
Name: "test_1",
ProjectID: proj.ID,
ProjectKey: proj.Key,
HistoryLength: 25,
Name: "test_1",
ProjectID: proj.ID,
ProjectKey: proj.Key,
RetentionPolicy: "return true",
HistoryLength: 25,
Groups: []sdk.GroupPermission{
{
Group: *group2,
Expand Down Expand Up @@ -279,10 +278,9 @@ workflow:
pipeline: pip1
permissions:
Test_getWorkflowExportHandlerWithPermissions-Group2: 7
retention_policy: return (git_branch_exist == "false" and run_days_before < 2) or run_days_before < 365
retention_policy: return true
history_length: 25
`, rec.Body.String())

}

func Test_getWorkflowPullHandler(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions sdk/exportentities/v1/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ func (w Workflow) GetWorkflow() (*sdk.Workflow, error) {
}
if w.HistoryLength != nil && *w.HistoryLength > 0 {
wf.HistoryLength = *w.HistoryLength
} else {
wf.HistoryLength = sdk.DefaultHistoryLength
}

rand.Seed(time.Now().Unix())
Expand Down
42 changes: 17 additions & 25 deletions sdk/exportentities/v1/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
wantErr: false,
want: sdk.Workflow{
Name: "myworkflow",
HistoryLength: sdk.DefaultHistoryLength,
Name: "myworkflow",
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "pipeline",
Expand Down Expand Up @@ -266,9 +265,8 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
wantErr: false,
want: sdk.Workflow{
Name: "myworkflow",
HistoryLength: sdk.DefaultHistoryLength,
Description: "this is my description",
Name: "myworkflow",
Description: "this is my description",
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "pipeline",
Expand Down Expand Up @@ -331,8 +329,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
wantErr: false,
want: sdk.Workflow{
Name: "myworkflow",
HistoryLength: sdk.DefaultHistoryLength,
Name: "myworkflow",
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "root",
Expand Down Expand Up @@ -396,8 +393,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
wantErr: false,
want: sdk.Workflow{
Name: "myworkflow",
HistoryLength: sdk.DefaultHistoryLength,
Name: "myworkflow",
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "root",
Expand Down Expand Up @@ -511,8 +507,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
wantErr: false,
want: sdk.Workflow{
Name: "myworkflow",
HistoryLength: sdk.DefaultHistoryLength,
Name: "myworkflow",
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "root",
Expand Down Expand Up @@ -601,8 +596,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
wantErr: false,
want: sdk.Workflow{
Name: "myworkflow",
HistoryLength: sdk.DefaultHistoryLength,
Name: "myworkflow",
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "A",
Expand Down Expand Up @@ -732,8 +726,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
wantErr: false,
want: sdk.Workflow{
Name: "myworkflow",
HistoryLength: sdk.DefaultHistoryLength,
Name: "myworkflow",
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "pipeline",
Expand Down Expand Up @@ -767,8 +760,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
},
wantErr: false,
want: sdk.Workflow{
Name: "myworkflow",
HistoryLength: sdk.DefaultHistoryLength,
Name: "myworkflow",
WorkflowData: sdk.WorkflowData{
Node: sdk.Node{
Name: "A",
Expand Down Expand Up @@ -838,17 +830,17 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
got.Environments = nil
got.ProjectIntegrations = nil

expextedValues, _ := dump.ToStringMap(tt.want)
expectedValues, _ := dump.ToStringMap(tt.want)
actualValues, _ := dump.ToStringMap(got)

var keysExpextedValues []string
for k := range expextedValues {
keysExpextedValues = append(keysExpextedValues, k)
var keysExpectedValues []string
for k := range expectedValues {
keysExpectedValues = append(keysExpectedValues, k)
}
sort.Strings(keysExpextedValues)
sort.Strings(keysExpectedValues)

for _, expectedKey := range keysExpextedValues {
expectedValue := expextedValues[expectedKey]
for _, expectedKey := range keysExpectedValues {
expectedValue := expectedValues[expectedKey]
actualValue, ok := actualValues[expectedKey]
if strings.Contains(expectedKey, ".Ref") {
assert.NotEmpty(t, actualValue, "value %s is empty but shoud not be empty", expectedKey)
Expand All @@ -859,7 +851,7 @@ func TestWorkflow_GetWorkflow(t *testing.T) {
}

for actualKey := range actualValues {
_, ok := expextedValues[actualKey]
_, ok := expectedValues[actualKey]
assert.True(t, ok, "got %s, but not found is expected workflow", actualKey)
}
})
Expand Down
14 changes: 9 additions & 5 deletions sdk/exportentities/v2/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Workflow struct {
Permissions map[string]int `json:"permissions,omitempty" yaml:"permissions,omitempty" jsonschema_description:"The permissions for the workflow (ex: myGroup: 7).\nhttps://ovh.github.io/cds/docs/concepts/permissions"`
Metadata map[string]string `json:"metadata,omitempty" yaml:"metadata,omitempty"`
PurgeTags []string `json:"purge_tags,omitempty" yaml:"purge_tags,omitempty"`
RetentionPolicy string `json:"retention_policy,omitempty" yaml:"retention_policy,omitempty"`
RetentionPolicy *string `json:"retention_policy,omitempty" yaml:"retention_policy,omitempty"`
Notifications []NotificationEntry `json:"notifications,omitempty" yaml:"notifications,omitempty"` // This is used when the workflow have only one pipeline
HistoryLength *int64 `json:"history_length,omitempty" yaml:"history_length,omitempty"`
}
Expand Down Expand Up @@ -104,7 +104,7 @@ func NewWorkflow(ctx context.Context, w sdk.Workflow, version string, opts ...Ex
exportedWorkflow.Version = version
exportedWorkflow.Workflow = map[string]NodeEntry{}
exportedWorkflow.Hooks = map[string][]HookEntry{}
exportedWorkflow.RetentionPolicy = w.RetentionPolicy

if len(w.Metadata) > 0 {
exportedWorkflow.Metadata = make(map[string]string, len(w.Metadata))
for k, v := range w.Metadata {
Expand All @@ -119,6 +119,10 @@ func NewWorkflow(ctx context.Context, w sdk.Workflow, version string, opts ...Ex
exportedWorkflow.HistoryLength = &w.HistoryLength
}

if w.RetentionPolicy != "" && w.RetentionPolicy != sdk.DefaultRetentionRule {
exportedWorkflow.RetentionPolicy = &w.RetentionPolicy
}

exportedWorkflow.PurgeTags = w.PurgeTags

nodes := w.WorkflowData.Array()
Expand Down Expand Up @@ -371,7 +375,6 @@ func (w Workflow) GetWorkflow() (*sdk.Workflow, error) {
wf.Pipelines = make(map[int64]sdk.Pipeline)
wf.Environments = make(map[int64]sdk.Environment)
wf.ProjectIntegrations = make(map[int64]sdk.ProjectIntegration)
wf.RetentionPolicy = w.RetentionPolicy

if err := w.CheckValidity(); err != nil {
return nil, sdk.WrapError(err, "unable to check validity")
Expand All @@ -388,8 +391,9 @@ func (w Workflow) GetWorkflow() (*sdk.Workflow, error) {
}
if w.HistoryLength != nil && *w.HistoryLength > 0 {
wf.HistoryLength = *w.HistoryLength
} else {
wf.HistoryLength = sdk.DefaultHistoryLength
}
if w.RetentionPolicy != nil && *w.RetentionPolicy != "" {
wf.RetentionPolicy = *w.RetentionPolicy
}

r := rand.New(rand.NewSource(time.Now().Unix()))
Expand Down
Loading