Skip to content

Commit

Permalink
Refactor away specific webhook functions
Browse files Browse the repository at this point in the history
  • Loading branch information
deadlycoconuts committed Nov 5, 2024
1 parent 3eb33c3 commit 85e1ba2
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 338 deletions.
12 changes: 6 additions & 6 deletions api/turing/api/deployment_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ func (c RouterDeploymentController) deployOrRollbackRouter(
err = errors.New(strings.Join(errorStrings, ". "))

// call webhook for router un-deployment event
if errWebhook := c.webhookClient.TriggerRouterDeploymentEvent(
ctx, webhook.OnRouterUndeployed, routerVersion, uint(router.ProjectID),
if errWebhook := c.webhookClient.TriggerWebhooks(
ctx, webhook.OnRouterUndeployed, routerVersion,
); errWebhook != nil {
log.Warnf(
"Error triggering webhook for event %s, router id: %d, router version id: %d, %v",
Expand All @@ -127,8 +127,8 @@ func (c RouterDeploymentController) deployOrRollbackRouter(
router.CurrRouterVersion.Version))

// call webhook for router un-deployment event
if errWebhook := c.webhookClient.TriggerRouterDeploymentEvent(
ctx, webhook.OnRouterUndeployed, currVersion, uint(router.ProjectID),
if errWebhook := c.webhookClient.TriggerWebhooks(
ctx, webhook.OnRouterUndeployed, currVersion,
); errWebhook != nil {
log.Warnf(
"Error triggering webhook for event %s, router id: %d, router version id: %d, %v",
Expand Down Expand Up @@ -301,8 +301,8 @@ func (c RouterDeploymentController) deployRouterVersion(
_, err = c.RouterVersionsService.Save(routerVersion)

// call webhook for router deployment event
if errWebhook := c.webhookClient.TriggerRouterDeploymentEvent(
ctx, webhook.OnRouterDeployed, routerVersion, uint(router.ProjectID),
if errWebhook := c.webhookClient.TriggerWebhooks(
ctx, webhook.OnRouterDeployed, routerVersion,
); errWebhook != nil {
log.Warnf(
"Error triggering webhook for event %s, router id: %d, router version id: %d, %v",
Expand Down
6 changes: 3 additions & 3 deletions api/turing/api/deployment_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func TestDeployVersionSuccess(t *testing.T) {
// Mock webhook service
webhookSvc := &webhookMock.Client{}
webhookSvc.On(
"TriggerRouterDeploymentEvent", mock.Anything, webhook.OnRouterDeployed, mock.Anything, mock.Anything,
"TriggerWebhooks", mock.Anything, webhook.OnRouterDeployed, mock.Anything,
).Return(nil)

// Run tests and validate
Expand Down Expand Up @@ -311,10 +311,10 @@ func TestRollbackVersionSuccess(t *testing.T) {
// Mock webhook service
webhookSvc := &webhookMock.Client{}
webhookSvc.On(
"TriggerRouterDeploymentEvent", mock.Anything, webhook.OnRouterDeployed, mock.Anything, mock.Anything,
"TriggerWebhooks", mock.Anything, webhook.OnRouterDeployed, mock.Anything,
).Return(nil)
webhookSvc.On(
"TriggerRouterDeploymentEvent", mock.Anything, webhook.OnRouterUndeployed, mock.Anything, mock.Anything,
"TriggerWebhooks", mock.Anything, webhook.OnRouterUndeployed, mock.Anything,
).Return(nil)

// Create test controller
Expand Down
6 changes: 3 additions & 3 deletions api/turing/api/ensemblers_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c EnsemblersController) CreateEnsembler(
}

// call webhook for ensembler creation event
if errWebhook := c.webhookClient.TriggerEnsemblerEvent(ctx, webhook.OnEnsemblerCreated, ensembler); errWebhook != nil {
if errWebhook := c.webhookClient.TriggerWebhooks(ctx, webhook.OnEnsemblerCreated, ensembler); errWebhook != nil {
log.Warnf(
"Error triggering webhook for event %s, ensembler id: %d, %v",
webhook.OnEnsemblerCreated, ensembler.GetID(), errWebhook,
Expand Down Expand Up @@ -165,7 +165,7 @@ func (c EnsemblersController) UpdateEnsembler(
}

// call webhook for ensembler update event
if errWebhook := c.webhookClient.TriggerEnsemblerEvent(ctx, webhook.OnEnsemblerUpdated, ensembler); errWebhook != nil {
if errWebhook := c.webhookClient.TriggerWebhooks(ctx, webhook.OnEnsemblerUpdated, ensembler); errWebhook != nil {
log.Warnf(
"Error triggering webhook for event %s, ensembler id: %d, %v",
webhook.OnEnsemblerUpdated, ensembler.GetID(), errWebhook,
Expand Down Expand Up @@ -238,7 +238,7 @@ func (c EnsemblersController) DeleteEnsembler(
}

// call webhook for ensembler deletion event
if errWebhook := c.webhookClient.TriggerEnsemblerEvent(ctx, webhook.OnEnsemblerDeleted, ensembler); errWebhook != nil {
if errWebhook := c.webhookClient.TriggerWebhooks(ctx, webhook.OnEnsemblerDeleted, ensembler); errWebhook != nil {
log.Warnf(
"Error triggering webhook for event %s, ensembler id: %d, %v",
webhook.OnEnsemblerDeleted, ensembler.GetID(), errWebhook,
Expand Down
8 changes: 4 additions & 4 deletions api/turing/api/routers_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (c RoutersController) CreateRouter(
}

// call webhook for router creation event
if errWebhook := c.webhookClient.TriggerRouterEvent(ctx, webhook.OnRouterCreated, router); errWebhook != nil {
if errWebhook := c.webhookClient.TriggerWebhooks(ctx, webhook.OnRouterCreated, router); errWebhook != nil {
log.Warnf("Error triggering webhook for event %s, router id: %d, %v",
webhook.OnRouterCreated, router.ID, errWebhook)
}
Expand Down Expand Up @@ -197,7 +197,7 @@ func (c RoutersController) UpdateRouter(req *http.Request, vars RequestVars, bod
}

// call webhook for router update event
if errWebhook := c.webhookClient.TriggerRouterEvent(ctx, webhook.OnRouterUpdated, router); errWebhook != nil {
if errWebhook := c.webhookClient.TriggerWebhooks(ctx, webhook.OnRouterUpdated, router); errWebhook != nil {
log.Warnf("Error triggering webhook for event %s, router id: %d, %v",
webhook.OnRouterUpdated, router.ID, errWebhook)
}
Expand Down Expand Up @@ -251,7 +251,7 @@ func (c RoutersController) DeleteRouter(
}

// call webhook for router deletion event
if errWebhook := c.webhookClient.TriggerRouterEvent(ctx, webhook.OnRouterDeleted, router); errWebhook != nil {
if errWebhook := c.webhookClient.TriggerWebhooks(ctx, webhook.OnRouterDeleted, router); errWebhook != nil {
log.Warnf(
"Error triggering webhook for event %s, router id: %d, %v",
webhook.OnRouterDeleted, router.ID, errWebhook,
Expand Down Expand Up @@ -346,7 +346,7 @@ func (c RoutersController) UndeployRouter(
}

// call webhook for router un-deployment event
if errWebhook := c.webhookClient.TriggerRouterEvent(ctx, webhook.OnRouterUndeployed, router); errWebhook != nil {
if errWebhook := c.webhookClient.TriggerWebhooks(ctx, webhook.OnRouterUndeployed, router); errWebhook != nil {
log.Warnf(
"Error triggering webhook for event %s, router id: %d, %v",
webhook.OnRouterUndeployed, router.ID, errWebhook,
Expand Down
11 changes: 4 additions & 7 deletions api/turing/api/routers_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,7 @@ func TestCreateRouter(t *testing.T) {

// Webhook service
webhookSvc := &webhookMock.Client{}
webhookSvc.On("TriggerRouterEvent", mock.Anything, webhook.OnRouterCreated, mock.Anything).Return(nil)
webhookSvc.On("TriggerRouterEvent", mock.Anything, webhook.OnRouterDeployed, mock.Anything).Return(nil)
webhookSvc.On("TriggerWebhooks", mock.Anything, webhook.OnRouterCreated, mock.Anything).Return(nil)

// Define tests
tests := map[string]struct {
Expand Down Expand Up @@ -405,8 +404,7 @@ func TestUpdateRouter(t *testing.T) {

// Webhook service
webhookSvc := &webhookMock.Client{}
webhookSvc.On("TriggerRouterEvent", mock.Anything, webhook.OnRouterUpdated, mock.Anything).Return(nil)
webhookSvc.On("TriggerRouterEvent", mock.Anything, webhook.OnRouterDeployed, mock.Anything).Return(nil)
webhookSvc.On("TriggerWebhooks", mock.Anything, webhook.OnRouterUpdated, mock.Anything).Return(nil)

// Define tests
tests := map[string]struct {
Expand Down Expand Up @@ -595,7 +593,7 @@ func TestDeleteRouter(t *testing.T) {

// Webhook service
webhookSvc := &webhookMock.Client{}
webhookSvc.On("TriggerRouterEvent", mock.Anything, webhook.OnRouterDeleted, mock.Anything).Return(nil)
webhookSvc.On("TriggerWebhooks", mock.Anything, webhook.OnRouterDeleted, mock.Anything).Return(nil)

// Define tests
tests := map[string]struct {
Expand Down Expand Up @@ -765,7 +763,6 @@ func TestDeployRouter(t *testing.T) {

// Webhook service
webhookSvc := &webhookMock.Client{}
webhookSvc.On("TriggerRouterEvent", mock.Anything, webhook.OnRouterDeployed, mock.Anything).Return(nil)

// Define tests
tests := map[string]struct {
Expand Down Expand Up @@ -917,7 +914,7 @@ func TestUndeployRouter(t *testing.T) {

// Webhook service
webhookSvc := &webhookMock.Client{}
webhookSvc.On("TriggerRouterEvent", mock.Anything, webhook.OnRouterUndeployed, mock.Anything).Return(nil)
webhookSvc.On("TriggerWebhooks", mock.Anything, webhook.OnRouterUndeployed, mock.Anything).Return(nil)

// Define tests
tests := map[string]struct {
Expand Down
45 changes: 0 additions & 45 deletions api/turing/webhook/constant.go

This file was deleted.

51 changes: 12 additions & 39 deletions api/turing/webhook/mocks/webhook.go

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

Loading

0 comments on commit 85e1ba2

Please sign in to comment.