Skip to content

Commit

Permalink
chore: add context to pipeline functions (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
plyr4 authored Aug 17, 2023
1 parent 26aa0b6 commit c41cb14
Show file tree
Hide file tree
Showing 39 changed files with 186 additions and 74 deletions.
4 changes: 2 additions & 2 deletions api/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func CreateBuild(c *gin.Context) {
)

// send API call to attempt to capture the pipeline
pipeline, err = database.FromContext(c).GetPipelineForRepo(input.GetCommit(), r)
pipeline, err = database.FromContext(c).GetPipelineForRepo(ctx, input.GetCommit(), r)
if err != nil { // assume the pipeline doesn't exist in the database yet
// send API call to capture the pipeline configuration file
config, err = scm.FromContext(c).ConfigBackoff(u, r, input.GetCommit())
Expand Down Expand Up @@ -309,7 +309,7 @@ func CreateBuild(c *gin.Context) {
pipeline.SetRef(input.GetRef())

// send API call to create the pipeline
pipeline, err = database.FromContext(c).CreatePipeline(pipeline)
pipeline, err = database.FromContext(c).CreatePipeline(ctx, pipeline)
if err != nil {
retErr := fmt.Errorf("unable to create new build: failed to create pipeline for %s: %w", r.GetFullName(), err)

Expand Down
4 changes: 2 additions & 2 deletions api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func RestartBuild(c *gin.Context) {
)

// send API call to attempt to capture the pipeline
pipeline, err = database.FromContext(c).GetPipelineForRepo(b.GetCommit(), r)
pipeline, err = database.FromContext(c).GetPipelineForRepo(ctx, b.GetCommit(), r)
if err != nil { // assume the pipeline doesn't exist in the database yet (before pipeline support was added)
// send API call to capture the pipeline configuration file
config, err = scm.FromContext(c).ConfigBackoff(u, r, b.GetCommit())
Expand Down Expand Up @@ -300,7 +300,7 @@ func RestartBuild(c *gin.Context) {
pipeline.SetRef(b.GetRef())

// send API call to create the pipeline
pipeline, err = database.FromContext(c).CreatePipeline(pipeline)
pipeline, err = database.FromContext(c).CreatePipeline(ctx, pipeline)
if err != nil {
retErr := fmt.Errorf("unable to create pipeline for %s: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func CreatePipeline(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -98,7 +99,7 @@ func CreatePipeline(c *gin.Context) {
input.SetRepoID(r.GetID())

// send API call to create the pipeline
p, err := database.FromContext(c).CreatePipeline(input)
p, err := database.FromContext(c).CreatePipeline(ctx, input)
if err != nil {
retErr := fmt.Errorf("unable to create pipeline %s/%s: %w", r.GetFullName(), input.GetCommit(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/pipeline/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func DeletePipeline(c *gin.Context) {
p := pipeline.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit())

Expand All @@ -79,7 +80,7 @@ func DeletePipeline(c *gin.Context) {
}).Infof("deleting pipeline %s", entry)

// send API call to remove the build
err := database.FromContext(c).DeletePipeline(p)
err := database.FromContext(c).DeletePipeline(ctx, p)
if err != nil {
retErr := fmt.Errorf("unable to delete pipeline %s: %w", entry, err)

Expand Down
3 changes: 2 additions & 1 deletion api/pipeline/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func ListPipelines(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -117,7 +118,7 @@ func ListPipelines(c *gin.Context) {
//nolint:gomnd // ignore magic number
perPage = util.MaxInt(1, util.MinInt(100, perPage))

p, t, err := database.FromContext(c).ListPipelinesForRepo(r, page, perPage)
p, t, err := database.FromContext(c).ListPipelinesForRepo(ctx, r, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to list pipelines for repo %s: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion api/pipeline/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func UpdatePipeline(c *gin.Context) {
p := pipeline.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

entry := fmt.Sprintf("%s/%s", r.GetFullName(), p.GetCommit())

Expand Down Expand Up @@ -170,7 +171,7 @@ func UpdatePipeline(c *gin.Context) {
}

// send API call to update the pipeline
p, err = database.FromContext(c).UpdatePipeline(p)
p, err = database.FromContext(c).UpdatePipeline(ctx, p)
if err != nil {
retErr := fmt.Errorf("unable to update pipeline %s: %w", entry, err)

Expand Down
4 changes: 2 additions & 2 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func PostWebhook(c *gin.Context) {
}

// send API call to attempt to capture the pipeline
pipeline, err = database.FromContext(c).GetPipelineForRepo(b.GetCommit(), repo)
pipeline, err = database.FromContext(c).GetPipelineForRepo(ctx, b.GetCommit(), repo)
if err != nil { // assume the pipeline doesn't exist in the database yet
// send API call to capture the pipeline configuration file
config, err = scm.FromContext(c).ConfigBackoff(u, repo, b.GetCommit())
Expand Down Expand Up @@ -562,7 +562,7 @@ func PostWebhook(c *gin.Context) {
pipeline.SetRef(b.GetRef())

// send API call to create the pipeline
pipeline, err = database.FromContext(c).CreatePipeline(pipeline)
pipeline, err = database.FromContext(c).CreatePipeline(ctx, pipeline)
if err != nil {
retErr := fmt.Errorf("%s: failed to create pipeline for %s: %w", baseErr, repo.GetFullName(), err)

Expand Down
4 changes: 2 additions & 2 deletions cmd/vela-server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler
}

// send API call to attempt to capture the pipeline
pipeline, err = database.GetPipelineForRepo(b.GetCommit(), r)
pipeline, err = database.GetPipelineForRepo(context.TODO(), b.GetCommit(), r)
if err != nil { // assume the pipeline doesn't exist in the database yet
// send API call to capture the pipeline configuration file
config, err = scm.ConfigBackoff(u, r, b.GetCommit())
Expand Down Expand Up @@ -326,7 +326,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler
pipeline.SetRef(b.GetRef())

// send API call to create the pipeline
pipeline, err = database.CreatePipeline(pipeline)
pipeline, err = database.CreatePipeline(context.TODO(), pipeline)
if err != nil {
err = fmt.Errorf("failed to create pipeline for %s: %w", r.GetFullName(), err)

Expand Down
18 changes: 9 additions & 9 deletions database/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,15 +664,15 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) {

// create the pipelines
for _, pipeline := range resources.Pipelines {
_, err := db.CreatePipeline(pipeline)
_, err := db.CreatePipeline(context.TODO(), pipeline)
if err != nil {
t.Errorf("unable to create pipeline %d: %v", pipeline.GetID(), err)
}
}
methods["CreatePipeline"] = true

// count the pipelines
count, err := db.CountPipelines()
count, err := db.CountPipelines(context.TODO())
if err != nil {
t.Errorf("unable to count pipelines: %v", err)
}
Expand All @@ -682,7 +682,7 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) {
methods["CountPipelines"] = true

// count the pipelines for a repo
count, err = db.CountPipelinesForRepo(resources.Repos[0])
count, err = db.CountPipelinesForRepo(context.TODO(), resources.Repos[0])
if err != nil {
t.Errorf("unable to count pipelines for repo %d: %v", resources.Repos[0].GetID(), err)
}
Expand All @@ -692,7 +692,7 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) {
methods["CountPipelinesForRepo"] = true

// list the pipelines
list, err := db.ListPipelines()
list, err := db.ListPipelines(context.TODO())
if err != nil {
t.Errorf("unable to list pipelines: %v", err)
}
Expand All @@ -702,7 +702,7 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) {
methods["ListPipelines"] = true

// list the pipelines for a repo
list, count, err = db.ListPipelinesForRepo(resources.Repos[0], 1, 10)
list, count, err = db.ListPipelinesForRepo(context.TODO(), resources.Repos[0], 1, 10)
if err != nil {
t.Errorf("unable to list pipelines for repo %d: %v", resources.Repos[0].GetID(), err)
}
Expand All @@ -717,7 +717,7 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) {
// lookup the pipelines by name
for _, pipeline := range resources.Pipelines {
repo := resources.Repos[pipeline.GetRepoID()-1]
got, err := db.GetPipelineForRepo(pipeline.GetCommit(), repo)
got, err := db.GetPipelineForRepo(context.TODO(), pipeline.GetCommit(), repo)
if err != nil {
t.Errorf("unable to get pipeline %d for repo %d: %v", pipeline.GetID(), repo.GetID(), err)
}
Expand All @@ -730,13 +730,13 @@ func testPipelines(t *testing.T, db Interface, resources *Resources) {
// update the pipelines
for _, pipeline := range resources.Pipelines {
pipeline.SetVersion("2")
_, err = db.UpdatePipeline(pipeline)
_, err = db.UpdatePipeline(context.TODO(), pipeline)
if err != nil {
t.Errorf("unable to update pipeline %d: %v", pipeline.GetID(), err)
}

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

// delete the pipelines
for _, pipeline := range resources.Pipelines {
err = db.DeletePipeline(pipeline)
err = db.DeletePipeline(context.TODO(), pipeline)
if err != nil {
t.Errorf("unable to delete pipeline %d: %v", pipeline.GetID(), err)
}
Expand Down
4 changes: 3 additions & 1 deletion database/pipeline/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
package pipeline

import (
"context"

"github.com/go-vela/types/constants"
)

// CountPipelines gets the count of all pipelines from the database.
func (e *engine) CountPipelines() (int64, error) {
func (e *engine) CountPipelines(ctx context.Context) (int64, error) {
e.logger.Tracef("getting count of all pipelines from the database")

// variable to store query results
Expand Down
4 changes: 3 additions & 1 deletion database/pipeline/count_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
package pipeline

import (
"context"

"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

// CountPipelinesForRepo gets the count of pipelines by repo ID from the database.
func (e *engine) CountPipelinesForRepo(r *library.Repo) (int64, error) {
func (e *engine) CountPipelinesForRepo(ctx context.Context, r *library.Repo) (int64, error) {
e.logger.WithFields(logrus.Fields{
"org": r.GetOrg(),
"repo": r.GetName(),
Expand Down
7 changes: 4 additions & 3 deletions database/pipeline/count_repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package pipeline

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -42,12 +43,12 @@ func TestPipeline_Engine_CountPipelinesForRepo(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreatePipeline(_pipelineOne)
_, err := _sqlite.CreatePipeline(context.TODO(), _pipelineOne)
if err != nil {
t.Errorf("unable to create test pipeline for sqlite: %v", err)
}

_, err = _sqlite.CreatePipeline(_pipelineTwo)
_, err = _sqlite.CreatePipeline(context.TODO(), _pipelineTwo)
if err != nil {
t.Errorf("unable to create test pipeline for sqlite: %v", err)
}
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestPipeline_Engine_CountPipelinesForRepo(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CountPipelinesForRepo(&library.Repo{ID: _pipelineOne.RepoID})
got, err := test.database.CountPipelinesForRepo(context.TODO(), &library.Repo{ID: _pipelineOne.RepoID})

if test.failure {
if err == nil {
Expand Down
7 changes: 4 additions & 3 deletions database/pipeline/count_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package pipeline

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -41,12 +42,12 @@ func TestPipeline_Engine_CountPipelines(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreatePipeline(_pipelineOne)
_, err := _sqlite.CreatePipeline(context.TODO(), _pipelineOne)
if err != nil {
t.Errorf("unable to create test pipeline for sqlite: %v", err)
}

_, err = _sqlite.CreatePipeline(_pipelineTwo)
_, err = _sqlite.CreatePipeline(context.TODO(), _pipelineTwo)
if err != nil {
t.Errorf("unable to create test pipeline for sqlite: %v", err)
}
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestPipeline_Engine_CountPipelines(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CountPipelines()
got, err := test.database.CountPipelines(context.TODO())

if test.failure {
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion database/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
package pipeline

import (
"context"

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

// CreatePipeline creates a new pipeline in the database.
func (e *engine) CreatePipeline(p *library.Pipeline) (*library.Pipeline, error) {
func (e *engine) CreatePipeline(ctx context.Context, p *library.Pipeline) (*library.Pipeline, error) {
e.logger.WithFields(logrus.Fields{
"pipeline": p.GetCommit(),
}).Tracef("creating pipeline %s in the database", p.GetCommit())
Expand Down
3 changes: 2 additions & 1 deletion database/pipeline/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package pipeline

import (
"context"
"reflect"
"testing"

Expand Down Expand Up @@ -59,7 +60,7 @@ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15) RETURNING "id"`).
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := test.database.CreatePipeline(_pipeline)
got, err := test.database.CreatePipeline(context.TODO(), _pipeline)

if test.failure {
if err == nil {
Expand Down
4 changes: 3 additions & 1 deletion database/pipeline/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
package pipeline

import (
"context"

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

// DeletePipeline deletes an existing pipeline from the database.
func (e *engine) DeletePipeline(p *library.Pipeline) error {
func (e *engine) DeletePipeline(ctx context.Context, p *library.Pipeline) error {
e.logger.WithFields(logrus.Fields{
"pipeline": p.GetCommit(),
}).Tracef("deleting pipeline %s from the database", p.GetCommit())
Expand Down
5 changes: 3 additions & 2 deletions database/pipeline/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package pipeline

import (
"context"
"testing"

"github.com/DATA-DOG/go-sqlmock"
Expand All @@ -31,7 +32,7 @@ func TestPipeline_Engine_DeletePipeline(t *testing.T) {
_sqlite := testSqlite(t)
defer func() { _sql, _ := _sqlite.client.DB(); _sql.Close() }()

_, err := _sqlite.CreatePipeline(_pipeline)
_, err := _sqlite.CreatePipeline(context.TODO(), _pipeline)
if err != nil {
t.Errorf("unable to create test pipeline for sqlite: %v", err)
}
Expand All @@ -57,7 +58,7 @@ func TestPipeline_Engine_DeletePipeline(t *testing.T) {
// run tests
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
err = test.database.DeletePipeline(_pipeline)
err = test.database.DeletePipeline(context.TODO(), _pipeline)

if test.failure {
if err == nil {
Expand Down
Loading

0 comments on commit c41cb14

Please sign in to comment.