Skip to content

Commit

Permalink
chore: add context to deployment functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rfigueroa committed Jan 17, 2024
1 parent 45f5ad3 commit a0eddf3
Show file tree
Hide file tree
Showing 11 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func RestartBuild(c *gin.Context) {

d.SetBuilds(build)

_, err = database.FromContext(c).UpdateDeployment(d)
_, err = database.FromContext(c).UpdateDeployment(ctx, d)
if err != nil {
logger.Errorf("unable to set update deployment for build %s: %v", entry, err)
}
Expand Down
2 changes: 1 addition & 1 deletion api/deployment/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func GetDeployment(c *gin.Context) {
}

// send API call to database to capture the deployment
d, err := database.FromContext(c).GetDeployment(int64(number))
d, err := database.FromContext(c).GetDeployment(ctx, int64(number))
if err != nil {
// send API call to SCM to capture the deployment
d, err = scm.FromContext(c).GetDeployment(ctx, u, r, int64(number))
Expand Down
2 changes: 1 addition & 1 deletion api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ func PostWebhook(c *gin.Context) {
} else {
build := append(d.GetBuilds(), b)
d.SetBuilds(build)
_, err := database.FromContext(c).UpdateDeployment(d)
_, err := database.FromContext(c).UpdateDeployment(ctx, d)
if err != nil {
retErr := fmt.Errorf("%s: failed to update deployment %s/%d: %w", baseErr, repo.GetFullName(), d.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
Expand Down
3 changes: 2 additions & 1 deletion database/deployment/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
package deployment

import (
"context"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// DeleteDeployment deletes an existing deployment from the database.
func (e *engine) DeleteDeployment(d *library.Deployment) error {
func (e *engine) DeleteDeployment(ctx context.Context, d *library.Deployment) error {
e.logger.WithFields(logrus.Fields{
"deployment": d.GetID(),
}).Tracef("deleting deployment %d in the database", d.GetID())
Expand Down
2 changes: 1 addition & 1 deletion database/deployment/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestDeployment_Engine_DeleteDeployment(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err = test.database.DeleteDeployment(_deploymentOne)
err = test.database.DeleteDeployment(context.TODO(), _deploymentOne)

if test.failure {
if err == nil {
Expand Down
3 changes: 2 additions & 1 deletion database/deployment/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package deployment

import (
"context"
"strconv"

"github.com/go-vela/types/constants"
Expand All @@ -11,7 +12,7 @@ import (
)

// GetDeployment gets a deployment by ID from the database.
func (e *engine) GetDeployment(id int64) (*library.Deployment, error) {
func (e *engine) GetDeployment(ctx context.Context, id int64) (*library.Deployment, error) {
e.logger.Tracef("getting deployment %d from the database", id)

// variable to store query results
Expand Down
2 changes: 1 addition & 1 deletion database/deployment/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestDeployment_Engine_GetDeployment(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.GetDeployment(1)
got, err := test.database.GetDeployment(context.TODO(), 1)

if test.failure {
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions database/deployment/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ type DeploymentInterface interface {
// CreateDeployment defines a function that creates a new deployment.
CreateDeployment(context.Context, *library.Deployment) (*library.Deployment, error)
// DeleteDeployment defines a function that deletes an existing deployment.
DeleteDeployment(*library.Deployment) error
DeleteDeployment(context.Context, *library.Deployment) error
// GetDeployment defines a function that gets a deployment by ID.
GetDeployment(int64) (*library.Deployment, error)
GetDeployment(context.Context, int64) (*library.Deployment, error)
// GetDeploymentForRepo defines a function that gets a deployment by repo ID and number.
GetDeploymentForRepo(context.Context, *library.Repo, int64) (*library.Deployment, error)
// ListDeployments defines a function that gets a list of all deployments.
ListDeployments(context.Context) ([]*library.Deployment, error)
// ListDeploymentsForRepo defines a function that gets a list of deployments by repo ID.
ListDeploymentsForRepo(context.Context, *library.Repo, int, int) ([]*library.Deployment, error)
// UpdateDeployment defines a function that updates an existing deployment.
UpdateDeployment(*library.Deployment) (*library.Deployment, error)
UpdateDeployment(context.Context, *library.Deployment) (*library.Deployment, error)
}
3 changes: 2 additions & 1 deletion database/deployment/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
package deployment

import (
"context"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/database"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// UpdateDeployment updates an existing deployment in the database.
func (e *engine) UpdateDeployment(d *library.Deployment) (*library.Deployment, error) {
func (e *engine) UpdateDeployment(ctx context.Context, d *library.Deployment) (*library.Deployment, error) {
e.logger.WithFields(logrus.Fields{
"deployment": d.GetID(),
}).Tracef("updating deployment %d in the database", d.GetID())
Expand Down
2 changes: 1 addition & 1 deletion database/deployment/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ WHERE "id" = $13`).
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err = test.database.UpdateDeployment(_deploymentOne)
_, err = test.database.UpdateDeployment(context.TODO(), _deploymentOne)

if test.failure {
if err == nil {
Expand Down
6 changes: 3 additions & 3 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,13 +547,13 @@ func testDeployments(t *testing.T, db Interface, resources *Resources) {

// update the deployments
for _, deployment := range resources.Deployments {
_, err = db.UpdateDeployment(deployment)
_, err = db.UpdateDeployment(context.TODO(), deployment)
if err != nil {
t.Errorf("unable to update deployment %d: %v", deployment.GetID(), err)
}

// lookup the deployment by ID
got, err := db.GetDeployment(deployment.GetID())
got, err := db.GetDeployment(context.TODO(), deployment.GetID())
if err != nil {
t.Errorf("unable to get deployment %d by ID: %v", deployment.GetID(), err)
}
Expand All @@ -566,7 +566,7 @@ func testDeployments(t *testing.T, db Interface, resources *Resources) {

// delete the deployments
for _, deployment := range resources.Deployments {
err = db.DeleteDeployment(deployment)
err = db.DeleteDeployment(context.TODO(), deployment)
if err != nil {
t.Errorf("unable to delete hook %d: %v", deployment.GetID(), err)
}
Expand Down

0 comments on commit a0eddf3

Please sign in to comment.