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): silently remove duplicate hooks #6116

Merged
merged 2 commits into from
Mar 24, 2022
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
19 changes: 16 additions & 3 deletions engine/api/workflow/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ func CompleteWorkflow(ctx context.Context, db gorp.SqlExecutor, w *sdk.Workflow,
if err := checkProjectIntegration(proj, w, n); err != nil {
return err
}
if err := checkHooks(db, w, n); err != nil {
if err := checkHooks(ctx, db, w, n); err != nil {
return err
}
if err := checkOutGoingHook(db, w, n); err != nil {
Expand Down Expand Up @@ -1026,7 +1026,8 @@ func checkOutGoingHook(db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.Node) error
return nil
}

func checkHooks(db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.Node) error {
func checkHooks(ctx context.Context, db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.Node) error {
var duplicateHookIdx []int64
for i := range n.Hooks {
h := &n.Hooks[i]
if h.HookModelID != 0 {
Expand Down Expand Up @@ -1083,11 +1084,23 @@ func checkHooks(db gorp.SqlExecutor, w *sdk.Workflow, n *sdk.Node) error {
for j := range n.Hooks {
h2 := n.Hooks[j]
if i != j && h.Ref() == h2.Ref() {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid workflow: duplicate hook %s", model.Name)
log.ErrorWithStackTrace(ctx, sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid workflow: duplicate hook %s", model.Name))
if !sdk.IsInInt64Array(int64(i), duplicateHookIdx) && !sdk.IsInInt64Array(int64(i), duplicateHookIdx) {
duplicateHookIdx = append(duplicateHookIdx, int64(j))
}
}
}
}

// Remove duplicate hooks
var newHooks = make([]sdk.NodeHook, 0, len(n.Hooks))
for i := range n.Hooks {
if !sdk.IsInInt64Array(int64(i), duplicateHookIdx) {
newHooks = append(newHooks, n.Hooks[i])
}
}
n.Hooks = newHooks

return nil
}

Expand Down
11 changes: 10 additions & 1 deletion engine/api/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -1752,7 +1753,15 @@ func Test_putWorkflowWithDuplicateHooksShouldRaiseAnError(t *testing.T) {
req = assets.NewAuthentifiedRequest(t, u, pass, "PUT", uri, &wf)
w = httptest.NewRecorder()
router.Mux.ServeHTTP(w, req)
require.Equal(t, 400, w.Code)
require.Equal(t, 200, w.Code)

btes, err := ioutil.ReadAll(w.Body)
require.NoError(t, err)

var resultWorkflow sdk.Workflow
require.NoError(t, sdk.JSONUnmarshal(btes, &resultWorkflow))

require.Len(t, resultWorkflow.WorkflowData.Node.Hooks, 1)
}

func Test_getWorkflowsHandler_FilterByRepo(t *testing.T) {
Expand Down