Skip to content

Commit

Permalink
feat(hooks): trigger on pull-request (#6825)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesnault authored Feb 13, 2024
1 parent 9644426 commit 47b9016
Show file tree
Hide file tree
Showing 84 changed files with 1,483 additions and 960 deletions.
2 changes: 1 addition & 1 deletion engine/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (api *API) postMaintenanceHandler() service.Handler {
return err
}
url := fmt.Sprintf("/admin/maintenance?enable=%v", enable)
_, code, errHooks := services.NewClient(api.mustDB(), srvs).DoJSONRequest(ctx, http.MethodPost, url, nil, nil)
_, code, errHooks := services.NewClient(srvs).DoJSONRequest(ctx, http.MethodPost, url, nil, nil)
if errHooks != nil || code >= 400 {
return fmt.Errorf("unable to change hook maintenant state to %v. Code result %d: %v", enable, code, errHooks)
}
Expand Down
4 changes: 2 additions & 2 deletions engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,7 +816,7 @@ func (a *API) Serve(ctx context.Context) error {
})

a.GoRoutines.RunWithRestart(ctx, "event_v2.dequeue", func(ctx context.Context) {
event_v2.Dequeue(ctx, a.mustDB(), a.Cache, a.GoRoutines)
event_v2.Dequeue(ctx, a.mustDB(), a.Cache, a.GoRoutines, a.Config.URL.UI)
})

log.Info(ctx, "Initializing internal routines...")
Expand Down Expand Up @@ -851,7 +851,7 @@ func (a *API) Serve(ctx context.Context) error {
auditCleanerRoutine(ctx, a.DBConnectionFactory.GetDBMap(gorpmapping.Mapper))
})
a.GoRoutines.RunWithRestart(ctx, "repositoriesmanager.ReceiveEvents", func(ctx context.Context) {
repositoriesmanager.ReceiveEvents(ctx, a.DBConnectionFactory.GetDBMap(gorpmapping.Mapper), a.Cache)
repositoriesmanager.ReceiveEvents(ctx, a.DBConnectionFactory.GetDBMap(gorpmapping.Mapper), a.Cache, a.Config.URL.UI)
})
a.GoRoutines.RunWithRestart(ctx, "services.KillDeadServices", func(ctx context.Context) {
services.KillDeadServices(ctx, a.mustDB)
Expand Down
14 changes: 3 additions & 11 deletions engine/api/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,9 @@ func (api *API) getApplicationVCSInfosHandler() service.Handler {
applicationName := vars["applicationName"]
remote := r.FormValue("remote")

tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WithStack(err)
}
defer tx.Rollback() // nolint
db := api.mustDB()

app, err := application.LoadByName(ctx, tx, projectKey, applicationName, application.LoadOptions.Default)
app, err := application.LoadByName(ctx, db, projectKey, applicationName, application.LoadOptions.Default)
if err != nil {
return sdk.WrapError(err, "cannot load application %s for project %s from db", applicationName, projectKey)
}
Expand All @@ -212,7 +208,7 @@ func (api *API) getApplicationVCSInfosHandler() service.Handler {
return service.WriteJSON(w, resp, http.StatusOK)
}

client, err := repositoriesmanager.AuthorizedClient(ctx, tx, api.Cache, projectKey, app.VCSServer)
client, err := repositoriesmanager.AuthorizedClient(ctx, db, api.Cache, projectKey, app.VCSServer)
if err != nil {
return sdk.NewErrorWithStack(err, sdk.NewErrorFrom(sdk.ErrNoReposManagerClientAuth, "cannot get vcs server %s for project %s", app.VCSServer, projectKey))
}
Expand Down Expand Up @@ -246,10 +242,6 @@ func (api *API) getApplicationVCSInfosHandler() service.Handler {
})
}

if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}

return service.WriteJSON(w, resp, http.StatusOK)
}
}
Expand Down
3 changes: 1 addition & 2 deletions engine/api/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"
"time"

"github.com/go-gorp/gorp"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -115,7 +114,7 @@ func TestUpdateAsCodeApplicationHandler(t *testing.T) {
defer ctrl.Finish()

servicesClients := mock_services.NewMockClient(ctrl)
services.NewClient = func(_ gorp.SqlExecutor, _ []sdk.Service) services.Client {
services.NewClient = func(_ []sdk.Service) services.Client {
return servicesClients
}
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion engine/api/ascode.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (api *API) postImportAsCodeHandler() service.Handler {
return sdk.WrapError(err, "cannot load project")
}

client, err := repositoriesmanager.AuthorizedClient(ctx, tx, api.Cache, p.Key, ope.VCSServer)
client, err := repositoriesmanager.AuthorizedClient(ctx, api.mustDB(), api.Cache, p.Key, ope.VCSServer)
if err != nil {
return sdk.NewErrorWithStack(err,
sdk.NewErrorFrom(sdk.ErrNoReposManagerClientAuth, "cannot get client for %s %s", key, ope.VCSServer))
Expand Down
5 changes: 2 additions & 3 deletions engine/api/ascode/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/ovh/cds/engine/api/operation"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/cache"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/sdk"
)

Expand Down Expand Up @@ -56,7 +55,7 @@ func UpdateAsCodeResult(ctx context.Context, db *gorp.DbMap, store cache.Store,
}
defer tx.Rollback() // nolint

asCodeEvent, err = createPullRequest(ctx, tx, store, proj, workflowHolder.ID, rootApp, ed, u, ope.Setup)
asCodeEvent, err = createPullRequest(ctx, db, store, proj, workflowHolder.ID, rootApp, ed, u, ope.Setup)
if err != nil {
return err
}
Expand Down Expand Up @@ -104,7 +103,7 @@ func UpdateAsCodeResult(ctx context.Context, db *gorp.DbMap, store cache.Store,
})
}

func createPullRequest(ctx context.Context, db gorpmapper.SqlExecutorWithTx, store cache.Store, proj sdk.Project, workflowHolderID int64, rootApp sdk.Application, ed EntityData, u sdk.Identifiable, opeSetup sdk.OperationSetup) (*sdk.AsCodeEvent, error) {
func createPullRequest(ctx context.Context, db *gorp.DbMap, store cache.Store, proj sdk.Project, workflowHolderID int64, rootApp sdk.Application, ed EntityData, u sdk.Identifiable, opeSetup sdk.OperationSetup) (*sdk.AsCodeEvent, error) {
client, err := repositoriesmanager.AuthorizedClient(ctx, db, store, proj.Key, rootApp.VCSServer)
if err != nil {
return nil, sdk.NewErrorFrom(err, "unable to create repositories manager client")
Expand Down
2 changes: 1 addition & 1 deletion engine/api/ascode/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func SyncEvents(ctx context.Context, db *gorp.DbMap, store cache.Store, proj sdk
}
defer tx.Rollback() //nolint

client, err := repositoriesmanager.AuthorizedClient(ctx, tx, store, proj.Key, rootApp.VCSServer)
client, err := repositoriesmanager.AuthorizedClient(ctx, db, store, proj.Key, rootApp.VCSServer)
if err != nil {
return res, err
}
Expand Down
3 changes: 1 addition & 2 deletions engine/api/environment_ascode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"testing"
"time"

"github.com/go-gorp/gorp"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -62,7 +61,7 @@ func TestUpdateAsCodeEnvironmentHandler(t *testing.T) {
defer ctrl.Finish()

servicesClients := mock_services.NewMockClient(ctrl)
services.NewClient = func(_ gorp.SqlExecutor, _ []sdk.Service) services.Client {
services.NewClient = func(_ []sdk.Service) services.Client {
return servicesClients
}
defer func() {
Expand Down
4 changes: 2 additions & 2 deletions engine/api/event/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func PushInElasticSearch(ctx context.Context, db gorp.SqlExecutor, store cache.S
}
e.Payload = nil
log.Info(ctx, "sending event %q to %s services", e.EventType, sdk.TypeElasticsearch)
_, code, errD := services.NewClient(db, esServices).DoJSONRequest(context.Background(), "POST", "/events", e, nil)
_, code, errD := services.NewClient(esServices).DoJSONRequest(context.Background(), "POST", "/events", e, nil)
if code >= 400 || errD != nil {
log.Error(ctx, "PushInElasticSearch> Unable to send event %s to elasticsearch [%d]: %v", e.EventType, code, errD)
continue
Expand All @@ -59,7 +59,7 @@ func GetEvents(ctx context.Context, db gorp.SqlExecutor, store cache.Store, filt
}

var esEvents []elastic.SearchHit
if _, _, err := services.NewClient(db, srvs).DoJSONRequest(context.Background(), "GET", "/events", filters, &esEvents); err != nil {
if _, _, err := services.NewClient(srvs).DoJSONRequest(context.Background(), "GET", "/events", filters, &esEvents); err != nil {
return nil, sdk.WrapError(err, "Unable to get events")
}

Expand Down
Loading

0 comments on commit 47b9016

Please sign in to comment.