Skip to content

Commit

Permalink
feat(api,ui): outgoing hooks (#3297)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsamin authored and bnjjj committed Oct 17, 2018
1 parent 5e0a621 commit 1d93bca
Show file tree
Hide file tree
Showing 258 changed files with 11,346 additions and 4,404 deletions.
7 changes: 5 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ TEST_REDIS_HOST = $(if ${CDS_API_CACHE_REDIS_HOST},${CDS_API_CACHE_REDIS_HOST},l
TEST_REDIS_PASSWORD = $(if ${CDS_API_CACHE_REDIS_PASSWORD},${CDS_API_CACHE_REDIS_PASSWORD},)
TEST_DB_START = docker run -d -p $(TEST_DB_PORT):5432 -e POSTGRES_PASSWORD=$(TEST_DB_PASSWORD) -e POSTGRES_USER=$(TEST_DB_USER) -e POSTGRES_DB=$(TEST_DB_NAME) --name postgres-cds postgres
TEST_DB_INIT = $(GO_BUILD) && ./engine database upgrade --db-host $(TEST_DB_HOST) --db-port $(TEST_DB_PORT) --db-user $(TEST_DB_USER) --db-password $(TEST_DB_PASSWORD) --db-name $(TEST_DB_NAME) --db-sslmode disable --migrate-dir ./sql && rm -rf ./engine
TEST_CMD = go test -v -timeout 180s -coverprofile=profile.coverprofile
TEST_CMD = go test -v -timeout 600s -coverprofile=profile.coverprofile

$(TARGET_DIR):
$(info create $(TARGET_DIR) directory)
Expand Down Expand Up @@ -115,6 +115,10 @@ test-xunit-report: $(GO_GO2XUNIT) $(TARGET_DIR)
if [ $$NO_TESTS -gt 0 ]; then \
echo "No tests found \t\t\t($$TST)"; \
else \
if [ ! -z "${CDS_VERSION}" ]; then \
echo "Sending $$TST to CDS"; \
worker upload --tag `echo $$TST | sed 's|../||' | sed 's|./||' | sed 's|/|_|g') | sed 's|_tests.log||'` $(abspath $$TST); \
fi; \
echo "Generating xUnit report \t$$TST.xml"; \
$(GO_GO2XUNIT) -input $$TST -output $$TST.xml; \
fi; \
Expand Down
4 changes: 2 additions & 2 deletions engine/api/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (api *API) updateActionHandler() service.Handler {

// Get body
var a sdk.Action
if err := UnmarshalBody(r, &a); err != nil {
if err := service.UnmarshalBody(r, &a); err != nil {
return err
}

Expand Down Expand Up @@ -140,7 +140,7 @@ func (api *API) updateActionHandler() service.Handler {
func (api *API) addActionHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
var a sdk.Action
if err := UnmarshalBody(r, &a); err != nil {
if err := service.UnmarshalBody(r, &a); err != nil {
return err
}

Expand Down
8 changes: 6 additions & 2 deletions engine/api/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
)

func Test_getActionExportHandler(t *testing.T) {
api, db, _ := newTestAPI(t)
api, db, _, end := newTestAPI(t)
defer end()

u, pass := assets.InsertAdminUser(db)

//Prepare request
Expand All @@ -36,7 +38,9 @@ func Test_getActionExportHandler(t *testing.T) {
}

func Test_postActionImportHandler(t *testing.T) {
api, db, _ := newTestAPI(t)
api, db, _, end := newTestAPI(t)
defer end()

u, pass := assets.InsertAdminUser(db)

uri := api.Router.GetRoute("POST", api.importActionHandler, nil)
Expand Down
18 changes: 14 additions & 4 deletions engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,6 @@ func (a *API) Serve(ctx context.Context) error {
a.Config.SMTP.TLS,
a.Config.SMTP.Disable)

if err := warning.Init(); err != nil {
return fmt.Errorf("Unable to init warning package: %v", err)
}

// Initialize feature packages
log.Info("Initializing feature flipping with izanami %s", a.Config.Features.Izanami.APIURL)
if a.Config.Features.Izanami.APIURL != "" {
Expand Down Expand Up @@ -545,6 +541,10 @@ func (a *API) Serve(ctx context.Context) error {
return fmt.Errorf("cannot setup builtin workflow hook models: %v", err)
}

if err := workflow.CreateBuiltinWorkflowOutgoingHookModels(a.DBConnectionFactory.GetDBMap()); err != nil {
return fmt.Errorf("cannot setup builtin workflow outgoing hook models: %v", err)
}

if err := platform.CreateBuiltinModels(a.DBConnectionFactory.GetDBMap()); err != nil {
return fmt.Errorf("cannot setup platforms: %v", err)
}
Expand All @@ -560,6 +560,16 @@ func (a *API) Serve(ctx context.Context) error {
return fmt.Errorf("cannot connect to cache store: %v", errCache)
}

log.Info("Initializing Events broker")
// Initialize event broker
a.eventsBroker = &eventsBroker{
cache: a.Cache,
clients: make(map[string]*eventsBrokerSubscribe),
dbFunc: a.DBConnectionFactory.GetDBMap,
messages: make(chan sdk.Event),
}
a.eventsBroker.Init(context.Background())

log.Info("Initializing HTTP router")
a.Router = &Router{
Mux: mux.NewRouter(),
Expand Down
5 changes: 4 additions & 1 deletion engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func (api *API) InitRouter() {
r.Handle("/project/{key}/workflows/{permWorkflowName}/groups/{groupName}", r.PUT(api.putWorkflowGroupHandler), r.DELETE(api.deleteWorkflowGroupHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/hooks/{uuid}", r.GET(api.getWorkflowHookHandler))
r.Handle("/project/{key}/workflow/{permWorkflowName}/node/{nodeID}/hook/model", r.GET(api.getWorkflowHookModelsHandler))
r.Handle("/project/{key}/workflow/{permWorkflowName}/node/{nodeID}/outgoinghook/model", r.GET(api.getWorkflowOutgoingHookModelsHandler))

// Preview workflows
r.Handle("/project/{permProjectKey}/preview/workflows", r.POST(api.postWorkflowPreviewHandler))
Expand All @@ -242,7 +243,7 @@ func (api *API) InitRouter() {
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/latest", r.GET(api.getLatestWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/tags", r.GET(api.getWorkflowRunTagsHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/num", r.GET(api.getWorkflowRunNumHandler), r.POST(api.postWorkflowRunNumHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}", r.GET(api.getWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}", r.GET(api.getWorkflowRunHandler, AllowServices(true)))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/stop", r.POSTEXECUTE(api.stopWorkflowRunHandler, EnableTracing()))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/vcs/resync", r.POSTEXECUTE(api.postResyncVCSWorkflowRunHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/resync", r.POST(api.resyncWorkflowRunHandler))
Expand All @@ -256,6 +257,8 @@ func (api *API) InitRouter() {
r.Handle("/project/{key}/workflows/{permWorkflowName}/artifact/{artifactId}", r.GET(api.getDownloadArtifactHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/node/{nodeID}/triggers/condition", r.GET(api.getWorkflowTriggerConditionHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/nodes/{nodeRunID}/release", r.POST(api.releaseApplicationWorkflowHandler))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/hooks/{hookRunID}/callback", r.POST(api.postWorkflowJobHookCallbackHandler, AllowServices(true)))
r.Handle("/project/{key}/workflows/{permWorkflowName}/runs/{number}/hooks/{hookRunID}/details", r.GET(api.getWorkflowJobHookDetailsHandler, NeedService()))

// Triggers
r.Handle("/project/{key}/application/{permApplicationName}/pipeline/{permPipelineKey}/trigger", r.GET(api.getTriggersHandler), r.POST(api.addTriggerHandler))
Expand Down
35 changes: 18 additions & 17 deletions engine/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,51 @@ import (

"github.com/ovh/cds/engine/api/auth"
"github.com/ovh/cds/engine/api/bootstrap"
"github.com/ovh/cds/engine/api/event"
"github.com/ovh/cds/engine/api/sessionstore"
"github.com/ovh/cds/engine/api/test"
"github.com/ovh/cds/engine/api/warning"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/sdk"
)

func newTestAPI(t *testing.T, bootstrapFunc ...test.Bootstrapf) (*API, *gorp.DbMap, *Router) {
func newTestAPI(t *testing.T, bootstrapFunc ...test.Bootstrapf) (*API, *gorp.DbMap, *Router, context.CancelFunc) {
bootstrapFunc = append(bootstrapFunc, bootstrap.InitiliazeDB)
db, cache := test.SetupPG(t, bootstrapFunc...)
db, cache, end := test.SetupPG(t, bootstrapFunc...)
router := newRouter(auth.TestLocalAuth(t, db, sessionstore.Options{Cache: cache, TTL: 30}), mux.NewRouter(), "/"+test.GetTestName(t))
var cancel context.CancelFunc
router.Background, cancel = context.WithCancel(context.Background())
api := &API{
StartupTime: time.Now(),
Router: router,
DBConnectionFactory: test.DBConnectionFactory,
Config: Configuration{},
Cache: cache,
}
_ = event.Initialize(event.KafkaConfig{}, api.Cache)
api.InitRouter()
api.warnChan = make(chan sdk.Event)
event.Subscribe(api.warnChan)

sdk.GoRoutine(context.TODO(), "workflow.ComputeAudit", func(ctx context.Context) { workflow.ComputeAudit(ctx, api.DBConnectionFactory.GetDBMap) })
sdk.GoRoutine(context.TODO(), "warning.Start", func(ctx context.Context) { warning.Start(ctx, api.DBConnectionFactory.GetDBMap, api.warnChan) })

return api, db, router
f := func() {
cancel()
end()
}
return api, db, router, f
}

func newTestServer(t *testing.T, bootstrapFunc ...test.Bootstrapf) (*API, string, func()) {
bootstrapFunc = append(bootstrapFunc, bootstrap.InitiliazeDB)
db, cache := test.SetupPG(t, bootstrapFunc...)
db, cache, end := test.SetupPG(t, bootstrapFunc...)
router := newRouter(auth.TestLocalAuth(t, db, sessionstore.Options{Cache: cache, TTL: 30}), mux.NewRouter(), "")
var cancel context.CancelFunc
router.Background, cancel = context.WithCancel(context.Background())
api := &API{
StartupTime: time.Now(),
Router: router,
DBConnectionFactory: test.DBConnectionFactory,
Config: Configuration{},
Cache: cache,
}
_ = event.Initialize(event.KafkaConfig{}, api.Cache)
api.InitRouter()
ts := httptest.NewServer(router.Mux)
url, _ := url.Parse(ts.URL)
return api, url.String(), ts.Close
f := func() {
end()
cancel()
ts.Close()
}
return api, url.String(), f
}
6 changes: 3 additions & 3 deletions engine/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ func (api *API) addApplicationHandler() service.Handler {
}

var app sdk.Application
if err := UnmarshalBody(r, &app); err != nil {
if err := service.UnmarshalBody(r, &app); err != nil {
return err
}

Expand Down Expand Up @@ -661,7 +661,7 @@ func (api *API) cloneApplicationHandler() service.Handler {
proj.Environments = envs

var newApp sdk.Application
if err := UnmarshalBody(r, &newApp); err != nil {
if err := service.UnmarshalBody(r, &newApp); err != nil {
return err
}

Expand Down Expand Up @@ -749,7 +749,7 @@ func (api *API) updateApplicationHandler() service.Handler {
}

var appPost sdk.Application
if err := UnmarshalBody(r, &appPost); err != nil {
if err := service.UnmarshalBody(r, &appPost); err != nil {
return err
}

Expand Down
21 changes: 14 additions & 7 deletions engine/api/application/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
)

func TestLoadByNameAsAdmin(t *testing.T) {
db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
event.Initialize(event.KafkaConfig{}, cache)
key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, cache, key, key, nil)
Expand All @@ -36,7 +37,8 @@ func TestLoadByNameAsAdmin(t *testing.T) {
}

func TestLoadByNameAsUser(t *testing.T) {
db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, cache, key, key, nil)
app := sdk.Application{
Expand All @@ -58,7 +60,8 @@ func TestLoadByNameAsUser(t *testing.T) {
}

func TestLoadByIDAsAdmin(t *testing.T) {
db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, cache, key, key, nil)
app := sdk.Application{
Expand All @@ -76,7 +79,8 @@ func TestLoadByIDAsAdmin(t *testing.T) {
}

func TestLoadByIDAsUser(t *testing.T) {
db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
key := sdk.RandomString(10)

proj := assets.InsertTestProject(t, db, cache, key, key, nil)
Expand All @@ -99,7 +103,8 @@ func TestLoadByIDAsUser(t *testing.T) {
}

func TestLoadAllAsAdmin(t *testing.T) {
db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, cache, key, key, nil)
app := sdk.Application{
Expand Down Expand Up @@ -131,7 +136,8 @@ func TestLoadAllAsAdmin(t *testing.T) {
}

func TestLoadAllAsUser(t *testing.T) {
db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
key := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, cache, key, key, nil)
app := sdk.Application{
Expand All @@ -156,7 +162,8 @@ func TestLoadAllAsUser(t *testing.T) {
}

func TestLoadByWorkflowID(t *testing.T) {
db, cache := test.SetupPG(t, bootstrap.InitiliazeDB)
db, cache, end := test.SetupPG(t, bootstrap.InitiliazeDB)
defer end()
u, _ := assets.InsertAdminUser(db)
key := sdk.RandomString(10)

Expand Down
2 changes: 1 addition & 1 deletion engine/api/application_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (api *API) postApplicationDeploymentStrategyConfigHandler() service.Handler
pfName := vars["platform"]

var pfConfig sdk.PlatformConfig
if err := UnmarshalBody(r, &pfConfig); err != nil {
if err := service.UnmarshalBody(r, &pfConfig); err != nil {
return err
}

Expand Down
9 changes: 6 additions & 3 deletions engine/api/application_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import (
)

func Test_getApplicationDeploymentStrategiesConfigHandler(t *testing.T) {
api, db, router := newTestAPI(t)
api, db, router, end := newTestAPI(t)
defer end()
u, pass := assets.InsertAdminUser(api.mustDB())
pkey := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey, u)
Expand All @@ -43,7 +44,8 @@ func Test_getApplicationDeploymentStrategiesConfigHandler(t *testing.T) {
}

func Test_postApplicationDeploymentStrategyConfigHandler(t *testing.T) {
api, db, router := newTestAPI(t)
api, db, router, end := newTestAPI(t)
defer end()
u, pass := assets.InsertAdminUser(api.mustDB())
pkey := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey, u)
Expand Down Expand Up @@ -165,7 +167,8 @@ func Test_postApplicationDeploymentStrategyConfigHandler(t *testing.T) {
}

func Test_postApplicationDeploymentStrategyConfigHandler_InsertTwoDifferentPlatforms(t *testing.T) {
api, db, router := newTestAPI(t)
api, db, router, end := newTestAPI(t)
defer end()
u, pass := assets.InsertAdminUser(api.mustDB())
pkey := sdk.RandomString(10)
proj := assets.InsertTestProject(t, db, api.Cache, pkey, pkey, u)
Expand Down
4 changes: 3 additions & 1 deletion engine/api/application_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

func Test_getApplicationExportHandler(t *testing.T) {
api, db, _ := newTestAPI(t)
api, db, _, end := newTestAPI(t)
defer end()

u, pass := assets.InsertAdminUser(db)
proj := assets.InsertTestProject(t, db, api.Cache, sdk.RandomString(10), sdk.RandomString(10), u)
test.NotNil(t, proj)
Expand Down
4 changes: 2 additions & 2 deletions engine/api/application_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (api *API) updateGroupRoleOnApplicationHandler() service.Handler {
groupName := vars["group"]

var groupApplication sdk.GroupPermission
if err := UnmarshalBody(r, &groupApplication); err != nil {
if err := service.UnmarshalBody(r, &groupApplication); err != nil {
return err
}

Expand Down Expand Up @@ -100,7 +100,7 @@ func (api *API) addGroupInApplicationHandler() service.Handler {
appName := vars["permApplicationName"]

var groupPermission sdk.GroupPermission
if err := UnmarshalBody(r, &groupPermission); err != nil {
if err := service.UnmarshalBody(r, &groupPermission); err != nil {
return sdk.WrapError(err, "addGroupInApplicationHandler> Cannot unmarshal request")
}

Expand Down
6 changes: 4 additions & 2 deletions engine/api/application_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (
)

func Test_deleteGroupFromApplicationHandlerWithError(t *testing.T) {
api, db, router := newTestAPI(t)
api, db, router, end := newTestAPI(t)
defer end()

//Create admin user
u, pass := assets.InsertAdminUser(api.mustDB())
Expand Down Expand Up @@ -67,7 +68,8 @@ func Test_deleteGroupFromApplicationHandlerWithError(t *testing.T) {
}

func Test_deleteGroupFromApplicationHandler(t *testing.T) {
api, db, router := newTestAPI(t)
api, db, router, end := newTestAPI(t)
defer end()

//Create admin user
u, pass := assets.InsertAdminUser(api.mustDB())
Expand Down
Loading

0 comments on commit 1d93bca

Please sign in to comment.