Skip to content

Commit

Permalink
fix(api): fix compute uuid / ref on hook (#4414)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored and richardlt committed Jul 2, 2019
1 parent 2b1d6bc commit 3619310
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
13 changes: 7 additions & 6 deletions engine/api/workflow/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"sort"
"strings"
"testing"

"github.com/fsamin/go-dump"
"github.com/ovh/cds/engine/api/bootstrap"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/api/services"
"github.com/ovh/cds/sdk/log"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v2"
"io/ioutil"
"net/http"
"reflect"
"sort"
"strings"
"testing"

"github.com/ovh/cds/engine/api/application"
"github.com/ovh/cds/engine/api/environment"
Expand Down
22 changes: 15 additions & 7 deletions engine/api/workflow/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/fsamin/go-dump"
"github.com/go-gorp/gorp"
Expand Down Expand Up @@ -74,8 +73,10 @@ func hookUnregistration(ctx context.Context, db gorp.SqlExecutor, store cache.St

func hookRegistration(ctx context.Context, db gorp.SqlExecutor, store cache.Store, p *sdk.Project, wf *sdk.Workflow, oldWorkflow *sdk.Workflow) error {
var oldHooks map[string]*sdk.NodeHook
var oldHooksByRef map[string]sdk.NodeHook
if oldWorkflow != nil {
oldHooks = oldWorkflow.WorkflowData.GetHooks()
oldHooksByRef = oldWorkflow.WorkflowData.GetHooksMapRef()
}
if len(wf.WorkflowData.Node.Hooks) <= 0 {
return nil
Expand All @@ -94,21 +95,28 @@ func hookRegistration(ctx context.Context, db gorp.SqlExecutor, store cache.Stor
hookToUpdate := make(map[string]sdk.NodeHook)
for i := range wf.WorkflowData.Node.Hooks {
h := &wf.WorkflowData.Node.Hooks[i]

if h.UUID == "" && h.Ref == "" {
// generate uuid
h.UUID = sdk.UUID()
if h.Ref == "" {
h.Ref = fmt.Sprintf("%d", time.Now().Unix())
h.Ref = fmt.Sprintf("%s.%d", wf.WorkflowData.Node.Name, i)
} else if h.Ref != "" && oldHooksByRef != nil {
// search previous hook configuration by ref
previousHook, has := oldHooksByRef[h.Ref]
h.UUID = previousHook.UUID
// If previous hook is the same, we do nothing
if has && h.Equals(previousHook) {
continue
}
} else if oldHooks != nil {
// search previous hook configuration
// search previous hook configuration by uuid
previousHook, has := oldHooks[h.UUID]
// If previous hook is the same, we do nothing
if has && h.Equals(*previousHook) {
continue
}
}
// initialize a UUID is there no uuid
if h.UUID == "" {
h.UUID = sdk.UUID()
}

h.Config[sdk.HookConfigProject] = sdk.WorkflowNodeHookConfigValue{
Value: wf.ProjectKey,
Expand Down

0 comments on commit 3619310

Please sign in to comment.