Skip to content

Commit 5a33878

Browse files
authored
feat(api): silently remove duplicate hooks (#6116)
1 parent 7213842 commit 5a33878

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

engine/api/workflow/dao.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -927,7 +927,7 @@ func CompleteWorkflow(ctx context.Context, db gorp.SqlExecutor, w *sdk.Workflow,
927927
if err := checkProjectIntegration(proj, w, n); err != nil {
928928
return err
929929
}
930-
if err := checkHooks(db, w, n); err != nil {
930+
if err := checkHooks(ctx, db, w, n); err != nil {
931931
return err
932932
}
933933
if err := checkOutGoingHook(db, w, n); err != nil {
@@ -1026,7 +1026,8 @@ func checkOutGoingHook(db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.Node) error
10261026
return nil
10271027
}
10281028

1029-
func checkHooks(db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.Node) error {
1029+
func checkHooks(ctx context.Context, db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.Node) error {
1030+
var duplicateHookIdx []int64
10301031
for i := range n.Hooks {
10311032
h := &n.Hooks[i]
10321033
if h.HookModelID != 0 {
@@ -1083,11 +1084,23 @@ func checkHooks(db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.Node) error {
10831084
for j := range n.Hooks {
10841085
h2 := n.Hooks[j]
10851086
if i != j && h.Ref() == h2.Ref() {
1086-
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid workflow: duplicate hook %s", model.Name)
1087+
log.ErrorWithStackTrace(ctx, sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid workflow: duplicate hook %s", model.Name))
1088+
if !sdk.IsInInt64Array(int64(i), duplicateHookIdx) && !sdk.IsInInt64Array(int64(i), duplicateHookIdx) {
1089+
duplicateHookIdx = append(duplicateHookIdx, int64(j))
1090+
}
10871091
}
10881092
}
10891093
}
10901094

1095+
// Remove duplicate hooks
1096+
var newHooks = make([]sdk.NodeHook, 0, len(n.Hooks))
1097+
for i := range n.Hooks {
1098+
if !sdk.IsInInt64Array(int64(i), duplicateHookIdx) {
1099+
newHooks = append(newHooks, n.Hooks[i])
1100+
}
1101+
}
1102+
n.Hooks = newHooks
1103+
10911104
return nil
10921105
}
10931106

engine/api/workflow_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9+
"io/ioutil"
910
"net/http"
1011
"net/http/httptest"
1112
"reflect"
@@ -1752,7 +1753,15 @@ func Test_putWorkflowWithDuplicateHooksShouldRaiseAnError(t *testing.T) {
17521753
req = assets.NewAuthentifiedRequest(t, u, pass, "PUT", uri, &wf)
17531754
w = httptest.NewRecorder()
17541755
router.Mux.ServeHTTP(w, req)
1755-
require.Equal(t, 400, w.Code)
1756+
require.Equal(t, 200, w.Code)
1757+
1758+
btes, err := ioutil.ReadAll(w.Body)
1759+
require.NoError(t, err)
1760+
1761+
var resultWorkflow sdk.Workflow
1762+
require.NoError(t, sdk.JSONUnmarshal(btes, &resultWorkflow))
1763+
1764+
require.Len(t, resultWorkflow.WorkflowData.Node.Hooks, 1)
17561765
}
17571766

17581767
func Test_getWorkflowsHandler_FilterByRepo(t *testing.T) {

0 commit comments

Comments
 (0)