Skip to content

Commit

Permalink
fix(repositories): workflow ascode and tag (#5132)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored Apr 16, 2020
1 parent 7c9d5fc commit 9362311
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 208 deletions.
32 changes: 12 additions & 20 deletions engine/api/workflow/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,25 +297,18 @@ func createVCSConfiguration(ctx context.Context, db gorp.SqlExecutor, store cach
return sdk.WrapError(sdk.ErrInvalidHookConfiguration, "wrong webHookURL value (project: %s, repository: %s)", proj.Key, h.Config["repoFullName"].Value)
}

// If empty, take the first event
var valueSplitted = strings.Split(h.Config[sdk.HookConfigEventFilter].Value, ";")
if valueSplitted[0] == "" && webHookInfo.Events != nil {
h.Config[sdk.HookConfigEventFilter] = sdk.WorkflowNodeHookConfigValue{
Value: webHookInfo.Events[0],
Configurable: true,
Type: sdk.HookConfigTypeString,
}
valueSplitted = []string{h.Config[sdk.HookConfigEventFilter].Value}

}

// Prepare the hook that will be send to VCS
vcsHook := sdk.VCSHook{
Method: "POST",
URL: h.Config["webHookURL"].Value,
Events: valueSplitted,
Workflow: true,
}

// Set given event filters if exists, else default values will be set by CreateHook func.
if c, ok := h.Config[sdk.HookConfigEventFilter]; ok && c.Value != "" {
vcsHook.Events = strings.Split(c.Value, ";")
}

if err := client.CreateHook(ctx, h.Config["repoFullName"].Value, &vcsHook); err != nil {
return sdk.WrapError(err, "Cannot create hook on repository: %+v", vcsHook)
}
Expand Down Expand Up @@ -355,19 +348,18 @@ func updateVCSConfiguration(ctx context.Context, db gorp.SqlExecutor, store cach
return sdk.WrapError(errWH, "cannot get vcs web hook info")
}

var valueSlitted []string
if _, ok := h.Config[sdk.HookConfigEventFilter]; ok {
valueEvent := h.Config[sdk.HookConfigEventFilter].Value
valueSlitted = strings.Split(valueEvent, ";")
}

vcsHook := sdk.VCSHook{
ID: h.Config[sdk.HookConfigWebHookID].Value,
Method: "POST",
URL: h.Config["webHookURL"].Value,
Events: valueSlitted,
Workflow: true,
}

// Set given event filters if exists, else default values will be set by CreateHook func.
if c, ok := h.Config[sdk.HookConfigEventFilter]; ok && c.Value != "" {
vcsHook.Events = strings.Split(c.Value, ";")
}

if err := client.UpdateHook(ctx, h.Config["repoFullName"].Value, &vcsHook); err != nil {
return sdk.WrapError(err, "Cannot update hook on repository: %+v", vcsHook)
}
Expand Down
27 changes: 6 additions & 21 deletions engine/api/workflow_ascode_rename_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,27 +234,9 @@ version: v1.0`),
WebhooksDisabled: false,
Icon: sdk.GitHubIcon,
Events: []string{
"repo:refs_changed",
"repo:modified",
"repo:forked",
"repo:comment:added",
"repo:comment:edited",
"repo:comment:deleted",
"pr:opened",
"pr:modified",
"pr:reviewer:updated",
"pr:reviewer:approved",
"pr:reviewer:unapproved",
"pr:reviewer:needs_work",
"pr:merged",
"pr:declined",
"pr:deleted",
"pr:comment:added",
"pr:comment:edited",
"pr:comment:deleted",
"push",
},
}

return nil, 200, nil
},
).Times(2)
Expand All @@ -267,6 +249,10 @@ version: v1.0`),
DoAndReturn(
func(ctx context.Context, method, path string, in interface{}, out interface{}, _ interface{}) (http.Header, int, error) {
vcsHooks := in.(*sdk.VCSHook)

require.Len(t, vcsHooks.Events, 0, "events list should be empty, default value is set by vcs")
vcsHooks.Events = []string{"push"}

vcsHooks.ID = sdk.UUID()
*(out.(*sdk.VCSHook)) = *vcsHooks
return nil, 200, nil
Expand Down Expand Up @@ -359,13 +345,12 @@ version: v1.0`),
for _, h := range wk.WorkflowData.GetHooks() {
log.Debug("--> %T %+v", h, h)
assert.Equal(t, "RepositoryWebHook", h.HookModelName)
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
assert.Equal(t, "fsamin/go-repo", h.Config["repoFullName"].Value)
assert.Equal(t, "github", h.Config["vcsServer"].Value)
assert.Equal(t, wk.Name, h.Config["workflow"].Value)
}

}
119 changes: 28 additions & 91 deletions engine/api/workflow_ascode_with_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,7 @@ version: v1.0`),
WebhooksDisabled: false,
Icon: sdk.GitHubIcon,
Events: []string{
"repo:refs_changed",
"repo:modified",
"repo:forked",
"repo:comment:added",
"repo:comment:edited",
"repo:comment:deleted",
"pr:opened",
"pr:modified",
"pr:reviewer:updated",
"pr:reviewer:approved",
"pr:reviewer:unapproved",
"pr:reviewer:needs_work",
"pr:merged",
"pr:declined",
"pr:deleted",
"pr:comment:added",
"pr:comment:edited",
"pr:comment:deleted",
"push",
},
}

Expand All @@ -190,8 +173,10 @@ version: v1.0`),

vcsHooks, ok := in.(*sdk.VCSHook)
require.True(t, ok)
assert.Equal(t, 1, len(vcsHooks.Events))
assert.Equal(t, "repo:refs_changed", vcsHooks.Events[0])

require.Len(t, vcsHooks.Events, 0, "events list should be empty, default value is set by vcs")
vcsHooks.Events = []string{"push"}

assert.Equal(t, "POST", vcsHooks.Method)
assert.Equal(t, "http://lolcat.host", vcsHooks.URL)
vcsHooks.ID = sdk.UUID()
Expand Down Expand Up @@ -233,7 +218,7 @@ version: v1.0`),
for _, h := range wk.WorkflowData.GetHooks() {
log.Debug("--> %T %+v", h, h)
assert.Equal(t, "RepositoryWebHook", h.HookModelName)
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
Expand Down Expand Up @@ -400,24 +385,7 @@ version: v1.0`),
WebhooksDisabled: false,
Icon: sdk.GitHubIcon,
Events: []string{
"repo:refs_changed",
"repo:modified",
"repo:forked",
"repo:comment:added",
"repo:comment:edited",
"repo:comment:deleted",
"pr:opened",
"pr:modified",
"pr:reviewer:updated",
"pr:reviewer:approved",
"pr:reviewer:unapproved",
"pr:reviewer:needs_work",
"pr:merged",
"pr:declined",
"pr:deleted",
"pr:comment:added",
"pr:comment:edited",
"pr:comment:deleted",
"push",
},
}

Expand All @@ -436,8 +404,10 @@ version: v1.0`),

vcsHooks, ok := in.(*sdk.VCSHook)
require.True(t, ok)
assert.Equal(t, 1, len(vcsHooks.Events))
assert.Equal(t, "repo:refs_changed", vcsHooks.Events[0])

require.Len(t, vcsHooks.Events, 0, "events list should be empty, default value is set by vcs")
vcsHooks.Events = []string{"push"}

assert.Equal(t, "POST", vcsHooks.Method)
assert.Equal(t, "http://lolcat.host", vcsHooks.URL)

Expand Down Expand Up @@ -477,7 +447,7 @@ version: v1.0`),
for _, h := range wk.WorkflowData.GetHooks() {
log.Debug("--> %T %+v", h, h)
assert.Equal(t, "RepositoryWebHook", h.HookModelName)
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
Expand Down Expand Up @@ -531,15 +501,14 @@ version: v1.0`),
for _, h := range wk.WorkflowData.GetHooks() {
log.Debug("--> %T %+v", h, h)
assert.Equal(t, "RepositoryWebHook", h.HookModelName)
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
assert.Equal(t, "fsamin/go-repo", h.Config["repoFullName"].Value)
assert.Equal(t, "github", h.Config["vcsServer"].Value)
assert.Equal(t, wk.Name, h.Config["workflow"].Value)
}

}

func Test_WorkflowAsCodeWithDefaultHookAndAScheduler_ShouldGive_TheSameRepoWebHookAndTheScheduler(t *testing.T) {
Expand Down Expand Up @@ -734,24 +703,7 @@ version: v1.0`),
WebhooksDisabled: false,
Icon: sdk.GitHubIcon,
Events: []string{
"repo:refs_changed",
"repo:modified",
"repo:forked",
"repo:comment:added",
"repo:comment:edited",
"repo:comment:deleted",
"pr:opened",
"pr:modified",
"pr:reviewer:updated",
"pr:reviewer:approved",
"pr:reviewer:unapproved",
"pr:reviewer:needs_work",
"pr:merged",
"pr:declined",
"pr:deleted",
"pr:comment:added",
"pr:comment:edited",
"pr:comment:deleted",
"push",
},
}

Expand All @@ -770,9 +722,10 @@ version: v1.0`),

vcsHooks, ok := in.(*sdk.VCSHook)
require.True(t, ok)
assert.Equal(t, 1, len(vcsHooks.Events))
assert.Equal(t, "repo:refs_changed", vcsHooks.Events[0])
assert.Equal(t, "POST", vcsHooks.Method)

require.Len(t, vcsHooks.Events, 0, "events list should be empty, default value is set by vcs")
vcsHooks.Events = []string{"push"}

assert.Equal(t, "http://lolcat.host", vcsHooks.URL)
vcsHooks.ID = sdk.UUID()
*(out.(*sdk.VCSHook)) = *vcsHooks
Expand Down Expand Up @@ -815,7 +768,7 @@ version: v1.0`),

switch h.HookModelName {
case "RepositoryWebHook":
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
Expand Down Expand Up @@ -887,7 +840,7 @@ version: v1.0`),
for _, h := range wk.WorkflowData.GetHooks() {
switch h.HookModelName {
case "RepositoryWebHook":
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
Expand All @@ -913,7 +866,6 @@ version: v1.0`),
t.Fatalf("unexpected hook: %T %+v", h, h)
}
}

}

func Test_WorkflowAsCodeWithJustAcheduler_ShouldGive_ARepoWebHookAndTheScheduler_ThenWeUpdateIt_ThenWheDeleteIt(t *testing.T) {
Expand Down Expand Up @@ -1278,24 +1230,7 @@ version: v1.0`),
WebhooksDisabled: false,
Icon: sdk.GitHubIcon,
Events: []string{
"repo:refs_changed",
"repo:modified",
"repo:forked",
"repo:comment:added",
"repo:comment:edited",
"repo:comment:deleted",
"pr:opened",
"pr:modified",
"pr:reviewer:updated",
"pr:reviewer:approved",
"pr:reviewer:unapproved",
"pr:reviewer:needs_work",
"pr:merged",
"pr:declined",
"pr:deleted",
"pr:comment:added",
"pr:comment:edited",
"pr:comment:deleted",
"push",
},
}

Expand All @@ -1314,8 +1249,10 @@ version: v1.0`),

vcsHooks, ok := in.(*sdk.VCSHook)
require.True(t, ok)
assert.Equal(t, 1, len(vcsHooks.Events))
assert.Equal(t, "repo:refs_changed", vcsHooks.Events[0])

require.Len(t, vcsHooks.Events, 0, "events list should be empty, default value is set by vcs")
vcsHooks.Events = []string{"push"}

assert.Equal(t, "POST", vcsHooks.Method)
assert.Equal(t, "http://lolcat.host", vcsHooks.URL)
vcsHooks.ID = sdk.UUID()
Expand Down Expand Up @@ -1367,7 +1304,7 @@ version: v1.0`),

switch h.HookModelName {
case "RepositoryWebHook":
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
Expand Down Expand Up @@ -1447,7 +1384,7 @@ version: v1.0`),
for _, h := range wk.WorkflowData.GetHooks() {
switch h.HookModelName {
case "RepositoryWebHook":
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
Expand Down Expand Up @@ -1519,7 +1456,7 @@ version: v1.0`),
for _, h := range wk.WorkflowData.GetHooks() {
switch h.HookModelName {
case "RepositoryWebHook":
assert.Equal(t, "repo:refs_changed", h.Config["eventFilter"].Value)
assert.Equal(t, "push", h.Config["eventFilter"].Value)
assert.Equal(t, "Github", h.Config["hookIcon"].Value)
assert.Equal(t, "POST", h.Config["method"].Value)
assert.Equal(t, proj.Key, h.Config["project"].Value)
Expand Down
3 changes: 2 additions & 1 deletion engine/hooks/tasks_gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/xanzy/go-gitlab"

"github.com/ovh/cds/engine/api/test"
"github.com/ovh/cds/sdk"
Expand All @@ -25,7 +26,7 @@ func Test_doWebHookExecutionGitlab(t *testing.T) {
WebHook: &sdk.WebHookExecution{
RequestBody: []byte(gitlabPushEvent),
RequestHeader: map[string][]string{
GitlabHeader: {"Push Hook"},
GitlabHeader: {string(gitlab.EventTypePush)},
},
RequestURL: "",
},
Expand Down
4 changes: 3 additions & 1 deletion engine/hooks/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"

dump "github.com/fsamin/go-dump"
"github.com/xanzy/go-gitlab"

"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/log"
)
Expand All @@ -30,7 +32,7 @@ func (s *Service) doWebHookExecution(ctx context.Context, e *sdk.TaskExecution)
func getRepositoryHeader(whe *sdk.WebHookExecution, events []string) string {
if v, ok := whe.RequestHeader[GithubHeader]; ok && ((len(events) == 0 && v[0] == "push") || sdk.IsInArray(v[0], events)) {
return GithubHeader
} else if v, ok := whe.RequestHeader[GitlabHeader]; ok && ((len(events) == 0 && v[0] == "Push Hook") || sdk.IsInArray(v[0], events)) {
} else if v, ok := whe.RequestHeader[GitlabHeader]; ok && ((len(events) == 0 && (v[0] == string(gitlab.EventTypePush) || v[0] == string(gitlab.EventTypeTagPush))) || sdk.IsInArray(v[0], events)) {
return GitlabHeader
} else if v, ok := whe.RequestHeader[BitbucketHeader]; ok && ((len(events) == 0 && v[0] == "repo:refs_changed") || sdk.IsInArray(v[0], events)) {
return BitbucketHeader
Expand Down
Loading

0 comments on commit 9362311

Please sign in to comment.