Skip to content

Commit 4feb0fe

Browse files
authored
fix: allow run condition on joins (#5071)
1 parent 064f428 commit 4feb0fe

File tree

2 files changed

+153
-1
lines changed

2 files changed

+153
-1
lines changed

engine/api/workflow/process_start.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func processAllJoins(ctx context.Context, db gorp.SqlExecutor, store cache.Store
114114

115115
//now checks if all sources have been completed
116116
var ok = true
117+
117118
nodeRunIDs := []int64{}
118119
sourcesParams := map[string]string{}
119120
for _, nodeRun := range sources {
@@ -122,11 +123,19 @@ func processAllJoins(ctx context.Context, db gorp.SqlExecutor, store cache.Store
122123
break
123124
}
124125

125-
if !sdk.StatusIsTerminated(nodeRun.Status) || nodeRun.Status == sdk.StatusFail || nodeRun.Status == sdk.StatusNeverBuilt || nodeRun.Status == sdk.StatusStopped {
126+
if !sdk.StatusIsTerminated(nodeRun.Status) {
126127
ok = false
127128
break
128129
}
129130

131+
// If there is no conditions on join, keep default condition ( only continue on success )
132+
if j.Context == nil || (len(j.Context.Conditions.PlainConditions) == 0 && j.Context.Conditions.LuaScript == "") {
133+
if nodeRun.Status == sdk.StatusFail || nodeRun.Status == sdk.StatusNeverBuilt || nodeRun.Status == sdk.StatusStopped {
134+
ok = false
135+
break
136+
}
137+
}
138+
130139
nodeRunIDs = append(nodeRunIDs, nodeRun.ID)
131140
//Merge build parameters from all sources
132141
sourcesParams = sdk.ParametersMapMerge(sourcesParams, sdk.ParametersToMap(nodeRun.BuildParameters))
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package workflow_test
2+
3+
import (
4+
"context"
5+
"github.com/ovh/cds/engine/api/authentication"
6+
"github.com/ovh/cds/engine/api/bootstrap"
7+
"github.com/ovh/cds/engine/api/test"
8+
"github.com/ovh/cds/engine/api/test/assets"
9+
"github.com/ovh/cds/engine/api/workflow"
10+
"github.com/ovh/cds/sdk"
11+
"github.com/stretchr/testify/require"
12+
"testing"
13+
)
14+
15+
func TestProcessJoinDefaultCondition(t *testing.T) {
16+
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
17+
defer end()
18+
u, _ := assets.InsertAdminUser(t, db)
19+
consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)
20+
21+
key := sdk.RandomString(10)
22+
proj := assets.InsertTestProject(t, db, cache, key, key)
23+
24+
// Test data
25+
wr := &sdk.WorkflowRun{
26+
ProjectID: proj.ID,
27+
WorkflowNodeRuns: map[int64][]sdk.WorkflowNodeRun{},
28+
Workflow: sdk.Workflow{
29+
Name: "myworkflow",
30+
ProjectKey: key,
31+
ProjectID: proj.ID,
32+
WorkflowData: sdk.WorkflowData{
33+
Node: sdk.Node{
34+
ID: 1,
35+
Name: "myfork",
36+
},
37+
Joins: []sdk.Node{
38+
{
39+
Name: "myjoin",
40+
ID: 666,
41+
JoinContext: []sdk.NodeJoin{
42+
{
43+
ParentID: 1,
44+
},
45+
},
46+
},
47+
},
48+
},
49+
},
50+
}
51+
52+
// Insert workflow
53+
require.NoError(t, workflow.Insert(context.TODO(), db, cache, *proj, &wr.Workflow))
54+
55+
// Create run
56+
wrr, err := workflow.CreateRun(db, &wr.Workflow, nil, u)
57+
require.NoError(t, err)
58+
wr.ID = wrr.ID
59+
wr.WorkflowID = wr.Workflow.ID
60+
require.NoError(t, workflow.UpdateWorkflowRun(context.TODO(), db, wr))
61+
62+
// Start workflow
63+
_, err = workflow.StartWorkflowRun(context.TODO(), db, cache, *proj, wr, &sdk.WorkflowRunPostHandlerOption{Manual: &sdk.WorkflowNodeRunManual{}}, consumer, nil)
64+
require.NoError(t, err)
65+
66+
wrUpdated, err := workflow.LoadRun(context.TODO(), db, proj.Key, wr.Workflow.Name, wr.Number, workflow.LoadRunOptions{})
67+
require.NoError(t, err)
68+
69+
// Fork and Join has been run
70+
require.Equal(t, 2, len(wrUpdated.WorkflowNodeRuns))
71+
require.Equal(t, sdk.StatusSuccess, wrUpdated.WorkflowNodeRuns[wr.Workflow.WorkflowData.Joins[0].ID][0].Status)
72+
}
73+
74+
func TestProcessJoinCustomCondition(t *testing.T) {
75+
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
76+
defer end()
77+
u, _ := assets.InsertAdminUser(t, db)
78+
consumer, _ := authentication.LoadConsumerByTypeAndUserID(context.TODO(), db, sdk.ConsumerLocal, u.ID, authentication.LoadConsumerOptions.WithAuthentifiedUser)
79+
80+
key := sdk.RandomString(10)
81+
proj := assets.InsertTestProject(t, db, cache, key, key)
82+
83+
// Test data
84+
wr := &sdk.WorkflowRun{
85+
ProjectID: proj.ID,
86+
WorkflowNodeRuns: map[int64][]sdk.WorkflowNodeRun{},
87+
Workflow: sdk.Workflow{
88+
Name: "myworkflow",
89+
ProjectKey: key,
90+
ProjectID: proj.ID,
91+
WorkflowData: sdk.WorkflowData{
92+
Node: sdk.Node{
93+
ID: 1,
94+
Name: "myfork",
95+
},
96+
Joins: []sdk.Node{
97+
{
98+
Name: "myjoin",
99+
ID: 666,
100+
JoinContext: []sdk.NodeJoin{
101+
{
102+
ParentID: 1,
103+
},
104+
},
105+
Context: &sdk.NodeContext{
106+
Conditions: sdk.WorkflowNodeConditions{
107+
PlainConditions: []sdk.WorkflowNodeCondition{
108+
{
109+
Variable: "cds.status",
110+
Operator: "eq",
111+
Value: sdk.StatusFail,
112+
},
113+
},
114+
},
115+
},
116+
},
117+
},
118+
},
119+
},
120+
}
121+
122+
// Insert workflow
123+
require.NoError(t, workflow.Insert(context.TODO(), db, cache, *proj, &wr.Workflow))
124+
125+
// Create run
126+
wrr, err := workflow.CreateRun(db, &wr.Workflow, nil, u)
127+
require.NoError(t, err)
128+
wr.ID = wrr.ID
129+
wr.WorkflowID = wr.Workflow.ID
130+
131+
require.NoError(t, workflow.UpdateWorkflowRun(context.TODO(), db, wr))
132+
133+
// Start run
134+
_, err = workflow.StartWorkflowRun(context.TODO(), db, cache, *proj, wr, &sdk.WorkflowRunPostHandlerOption{Manual: &sdk.WorkflowNodeRunManual{}}, consumer, nil)
135+
require.NoError(t, err)
136+
137+
wrUpdated, err := workflow.LoadRun(context.TODO(), db, proj.Key, wr.Workflow.Name, wr.Number, workflow.LoadRunOptions{})
138+
require.NoError(t, err)
139+
140+
// Only fork has run
141+
require.Equal(t, 1, len(wrUpdated.WorkflowNodeRuns))
142+
require.Equal(t, sdk.StatusSuccess, wrUpdated.WorkflowNodeRuns[wr.Workflow.WorkflowData.Node.ID][0].Status)
143+
}

0 commit comments

Comments
 (0)