diff --git a/.github/renovate.json b/.github/renovate.json index 6d795ad41..c8f5c890b 100644 --- a/.github/renovate.json +++ b/.github/renovate.json @@ -7,12 +7,11 @@ { "description": "Update docker images in go files", "fileMatch": [ - "^.*\\.go$" + "^cmd/vela-server/.+\\.go$" ], "matchStrings": [ - "\\/\\/ renovate: image=(?.*?)\\s+?.*[:|=]\\s+\"(?.*)\"\\,?" + "\"(?.*?):(?[^\"]*?)@(?sha256:[a-f0-9]+)\",? // renovate: container" ], - "versioningTemplate": "docker", "datasourceTemplate": "docker" } ] diff --git a/.github/workflows/spec.yml b/.github/workflows/spec.yml index ddfd46bbc..1dbe808bb 100644 --- a/.github/workflows/spec.yml +++ b/.github/workflows/spec.yml @@ -29,7 +29,7 @@ jobs: - name: create spec run: | - sudo make spec-install + make spec-install make spec - name: upload spec diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 4197e5785..8d9e58e02 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -43,5 +43,5 @@ jobs: - name: validate spec run: | - sudo make spec-install + make spec-install make spec diff --git a/Makefile b/Makefile index 83527e308..7c7a3ca31 100644 --- a/Makefile +++ b/Makefile @@ -279,11 +279,10 @@ spec-install: $(if $(shell command -v apt-get 2> /dev/null),,$(error 'apt-get' not found - install jq, sponge, and go-swagger manually)) @echo @echo "### Installing utilities (jq and sponge)" - @apt-get update - @apt-get install -y jq moreutils - @echo "### Downloading and installing go-swagger" - @curl -o /usr/local/bin/swagger -L "https://github.com/go-swagger/go-swagger/releases/download/v0.30.2/swagger_linux_amd64" - @chmod +x /usr/local/bin/swagger + @sudo apt-get update + @sudo apt-get install -y jq moreutils + @echo "### Installing go-swagger" + @go install github.com/go-swagger/go-swagger/cmd/swagger@v0.31.0 # The `spec-gen` target is intended to create an api-spec # using go-swagger (https://goswagger.io) diff --git a/api/jwks.go b/api/jwks.go index 166ccc15d..fb826048d 100644 --- a/api/jwks.go +++ b/api/jwks.go @@ -21,8 +21,6 @@ import ( // produces: // - application/json // parameters: -// security: -// - ApiKeyAuth: [] // responses: // '200': // description: Successfully retrieved the Vela JWKS diff --git a/api/log/get_service.go b/api/log/get_service.go index de223da08..a82f268ec 100644 --- a/api/log/get_service.go +++ b/api/log/get_service.go @@ -4,11 +4,13 @@ package log import ( + "errors" "fmt" "net/http" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" + "gorm.io/gorm" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" @@ -85,9 +87,15 @@ func GetServiceLog(c *gin.Context) { // send API call to capture the service logs sl, err := database.FromContext(c).GetLogForService(ctx, s) if err != nil { - retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err) + var status int + if errors.Is(err, gorm.ErrRecordNotFound) { + status = http.StatusNotFound + } else { + status = http.StatusInternalServerError + } - util.HandleError(c, http.StatusInternalServerError, retErr) + retErr := fmt.Errorf("unable to get logs for service %s: %w", entry, err) + util.HandleError(c, status, retErr) return } diff --git a/api/log/get_step.go b/api/log/get_step.go index 1bf6c4191..2a8f0f5e0 100644 --- a/api/log/get_step.go +++ b/api/log/get_step.go @@ -4,11 +4,13 @@ package log import ( + "errors" "fmt" "net/http" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" + "gorm.io/gorm" "github.com/go-vela/server/database" "github.com/go-vela/server/router/middleware/build" @@ -86,9 +88,15 @@ func GetStepLog(c *gin.Context) { // send API call to capture the step logs sl, err := database.FromContext(c).GetLogForStep(ctx, s) if err != nil { - retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err) + var status int + if errors.Is(err, gorm.ErrRecordNotFound) { + status = http.StatusNotFound + } else { + status = http.StatusInternalServerError + } - util.HandleError(c, http.StatusInternalServerError, retErr) + retErr := fmt.Errorf("unable to get logs for step %s: %w", entry, err) + util.HandleError(c, status, retErr) return } diff --git a/api/oi_config.go b/api/oi_config.go index ec2371c99..acd082951 100644 --- a/api/oi_config.go +++ b/api/oi_config.go @@ -22,8 +22,6 @@ import ( // produces: // - application/json // parameters: -// security: -// - ApiKeyAuth: [] // responses: // '200': // description: Successfully retrieved the Vela OpenID Configuration @@ -46,6 +44,7 @@ func GetOpenIDConfig(c *gin.Context) { "iat", "iss", "aud", + "branch", "build_number", "build_id", "repo", @@ -54,6 +53,8 @@ func GetOpenIDConfig(c *gin.Context) { "actor_scm_id", "commands", "image", + "image_name", + "image_tag", "request", "event", "sha", diff --git a/api/types/executor_test.go b/api/types/executor_test.go index e47472b8a..5c0e3011f 100644 --- a/api/types/executor_test.go +++ b/api/types/executor_test.go @@ -162,7 +162,7 @@ func testExecutor() *Executor { { ID: "step_github_octocat_1_clone", Directory: "/home/github/octocat", - Image: "target/vela-git:v0.3.0", + Image: "target/vela-git-slim:v0.12.0", Name: "clone", Number: 2, Pull: "always", diff --git a/api/types/oidc.go b/api/types/oidc.go index e472c3a42..c6ba560e7 100644 --- a/api/types/oidc.go +++ b/api/types/oidc.go @@ -4,7 +4,6 @@ package types import ( "github.com/golang-jwt/jwt/v5" - "github.com/lestrrat-go/jwx/v2/jwk" ) // OpenIDConfig is a struct that represents the OpenID Connect configuration. @@ -22,24 +21,35 @@ type OpenIDConfig struct { // OpenIDClaims struct is an extension of the JWT standard claims. It // includes information relevant to OIDC services. type OpenIDClaims struct { - BuildNumber int `json:"build_number,omitempty"` - BuildID int64 `json:"build_id,omitempty"` Actor string `json:"actor,omitempty"` ActorSCMID string `json:"actor_scm_id,omitempty"` - Repo string `json:"repo,omitempty"` - TokenType string `json:"token_type,omitempty"` - Image string `json:"image,omitempty"` - Request string `json:"request,omitempty"` + Branch string `json:"branch,omitempty"` + BuildID int64 `json:"build_id,omitempty"` + BuildNumber int `json:"build_number,omitempty"` Commands bool `json:"commands,omitempty"` Event string `json:"event,omitempty"` + Image string `json:"image,omitempty"` + ImageName string `json:"image_name,omitempty"` + ImageTag string `json:"image_tag,omitempty"` Ref string `json:"ref,omitempty"` + Repo string `json:"repo,omitempty"` + Request string `json:"request,omitempty"` SHA string `json:"sha,omitempty"` + TokenType string `json:"token_type,omitempty"` jwt.RegisteredClaims } -// JWKSet is a wrapper of lestrrat-go/jwx/jwk.Set for API Swagger gen. +// JWKSet exists solely to provide proper swagger documentation. +// It is not otherwise used in code. // // swagger:model JWKSet type JWKSet struct { - jwk.Set + Keys []JWK `json:"keys"` +} + +type JWK struct { + Kty string `json:"kty"` + Kid string `json:"kid"` + E string `json:"e"` + N string `json:"n"` } diff --git a/api/types/settings/compiler_test.go b/api/types/settings/compiler_test.go index a224a9cb7..a479fc8be 100644 --- a/api/types/settings/compiler_test.go +++ b/api/types/settings/compiler_test.go @@ -108,7 +108,7 @@ func TestTypes_Compiler_String(t *testing.T) { func testCompilerSettings() *Compiler { cs := new(Compiler) - cs.SetCloneImage("target/vela-git:latest") + cs.SetCloneImage("target/vela-git-slim:latest") cs.SetTemplateDepth(1) cs.SetStarlarkExecLimit(100) diff --git a/api/types/settings/platform_test.go b/api/types/settings/platform_test.go index 059c7b574..88bbe5dbd 100644 --- a/api/types/settings/platform_test.go +++ b/api/types/settings/platform_test.go @@ -179,7 +179,7 @@ func testPlatformSettings() *Platform { // setup compiler cs := new(Compiler) - cs.SetCloneImage("target/vela-git:latest") + cs.SetCloneImage("target/vela-git-slim:latest") cs.SetTemplateDepth(1) cs.SetStarlarkExecLimit(100) diff --git a/api/types/string.go b/api/types/string.go index 33ddacb59..a7637d714 100644 --- a/api/types/string.go +++ b/api/types/string.go @@ -7,8 +7,8 @@ import ( "strconv" "strings" - "github.com/buildkite/yaml" json "github.com/ghodss/yaml" + "gopkg.in/yaml.v3" ) // ToString is a helper function to convert diff --git a/api/webhook/post.go b/api/webhook/post.go index e8ce98ddd..c854c44d9 100644 --- a/api/webhook/post.go +++ b/api/webhook/post.go @@ -239,35 +239,77 @@ func PostWebhook(c *gin.Context) { b.SetRepo(repo) h.SetRepoID(repo.GetID()) - // send API call to capture the last hook for the repo - lastHook, err := database.FromContext(c).LastHookForRepo(ctx, repo) - if err != nil { - retErr := fmt.Errorf("unable to get last hook for repo %s: %w", repo.GetFullName(), err) - util.HandleError(c, http.StatusInternalServerError, retErr) + // number of times to retry + retryLimit := 3 + // implement a loop to process asynchronous operations with a retry limit + // + // Some operations taken during the webhook workflow can lead to race conditions + // failing to successfully process the request. This logic ensures we attempt our + // best efforts to handle these cases gracefully. + for i := 0; i < retryLimit; i++ { + // check if we're on the first iteration of the loop + if i > 0 { + // incrementally sleep in between retries + time.Sleep(time.Duration(i) * time.Second) + } - h.SetStatus(constants.StatusFailure) - h.SetError(retErr.Error()) + // send API call to capture the last hook for the repo + lastHook, err := database.FromContext(c).LastHookForRepo(ctx, repo) + if err != nil { + // format the error message with extra information + err = fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err) - return - } + // log the error for traceability + logrus.Error(err.Error()) - // set the Number field - if lastHook != nil { - h.SetNumber( - lastHook.GetNumber() + 1, - ) - } + // check if the retry limit has been exceeded + if i < retryLimit { + // continue to the next iteration of the loop + continue + } - // send API call to create the webhook - h, err = database.FromContext(c).CreateHook(ctx, h) - if err != nil { - retErr := fmt.Errorf("unable to create webhook %s/%d: %w", repo.GetFullName(), h.GetNumber(), err) - util.HandleError(c, http.StatusInternalServerError, retErr) + retErr := fmt.Errorf("%s: %w", baseErr, err) + util.HandleError(c, http.StatusInternalServerError, retErr) - h.SetStatus(constants.StatusFailure) - h.SetError(retErr.Error()) + h.SetStatus(constants.StatusFailure) + h.SetError(retErr.Error()) - return + return + } + + // set the Number field + if lastHook != nil { + h.SetNumber( + lastHook.GetNumber() + 1, + ) + } + + // send API call to create the webhook + h, err = database.FromContext(c).CreateHook(ctx, h) + if err != nil { + // format the error message with extra information + err = fmt.Errorf("unable to create webhook %s/%d: %w", r.GetFullName(), h.GetNumber(), err) + + // log the error for traceability + logrus.Error(err.Error()) + + // check if the retry limit has been exceeded + if i < retryLimit { + // continue to the next iteration of the loop + continue + } + + retErr := fmt.Errorf("%s: %w", baseErr, err) + util.HandleError(c, http.StatusInternalServerError, retErr) + + h.SetStatus(constants.StatusFailure) + h.SetError(retErr.Error()) + + return + } + + // hook was created successfully + break } l.WithFields(logrus.Fields{ @@ -366,8 +408,15 @@ func PostWebhook(c *gin.Context) { h.SetStatus(constants.StatusFailure) h.SetError(err.Error()) + b.SetStatus(constants.StatusError) + util.HandleError(c, code, err) + err = scm.FromContext(c).Status(ctx, repo.GetOwner(), b, repo.GetOrg(), repo.GetName()) + if err != nil { + l.Debugf("unable to set commit status for %s/%d: %v", repo.GetFullName(), b.GetNumber(), err) + } + return } diff --git a/cmd/vela-server/main.go b/cmd/vela-server/main.go index fe74f9d72..d5d0c34aa 100644 --- a/cmd/vela-server/main.go +++ b/cmd/vela-server/main.go @@ -94,8 +94,7 @@ func main() { EnvVars: []string{"VELA_CLONE_IMAGE"}, Name: "clone-image", Usage: "the clone image to use for the injected clone step", - // renovate: image=target/vela-git - Value: "target/vela-git:v0.8.0@sha256:02de004ae9dbf184c70039cb9ce431c31d6e7580eb9e6ec64a97ebf108aa65cb", + Value: "target/vela-git-slim:v0.12.0@sha256:533786ab3ef17c900b0105fdffbd7143d2601803f28b39e156132ad25819af0f", // renovate: container }, &cli.StringSliceFlag{ EnvVars: []string{"VELA_REPO_ALLOWLIST"}, diff --git a/compiler/native/clone_test.go b/compiler/native/clone_test.go index 304f703b2..1fc5b2813 100644 --- a/compiler/native/clone_test.go +++ b/compiler/native/clone_test.go @@ -12,7 +12,7 @@ import ( "github.com/go-vela/types/yaml" ) -const defaultCloneImage = "target/vela-git:latest" +const defaultCloneImage = "target/vela-git-slim:latest" func TestNative_CloneStage(t *testing.T) { // setup types diff --git a/compiler/native/compile.go b/compiler/native/compile.go index b72fdc73c..f73ff505e 100644 --- a/compiler/native/compile.go +++ b/compiler/native/compile.go @@ -12,9 +12,9 @@ import ( "strings" "time" - yml "github.com/buildkite/yaml" "github.com/hashicorp/go-cleanhttp" "github.com/hashicorp/go-retryablehttp" + yml "gopkg.in/yaml.v3" api "github.com/go-vela/server/api/types" "github.com/go-vela/types/constants" diff --git a/compiler/native/compile_test.go b/compiler/native/compile_test.go index fbac5c028..7b4907d45 100644 --- a/compiler/native/compile_test.go +++ b/compiler/native/compile_test.go @@ -13,11 +13,11 @@ import ( "testing" "time" - yml "github.com/buildkite/yaml" "github.com/gin-gonic/gin" "github.com/google/go-cmp/cmp" "github.com/google/go-github/v63/github" "github.com/urfave/cli/v2" + yml "gopkg.in/yaml.v3" api "github.com/go-vela/server/api/types" "github.com/go-vela/server/internal" @@ -208,11 +208,11 @@ func TestNative_Compile_StagesPipeline(t *testing.T) { Secrets: pipeline.StepSecretSlice{ &pipeline.StepSecret{ Source: "docker_username", - Target: "registry_username", + Target: "REGISTRY_USERNAME", }, &pipeline.StepSecret{ Source: "docker_password", - Target: "registry_password", + Target: "REGISTRY_PASSWORD", }, }, }, @@ -590,11 +590,11 @@ func TestNative_Compile_StepsPipeline(t *testing.T) { Secrets: pipeline.StepSecretSlice{ &pipeline.StepSecret{ Source: "docker_username", - Target: "registry_username", + Target: "REGISTRY_USERNAME", }, &pipeline.StepSecret{ Source: "docker_password", - Target: "registry_password", + Target: "REGISTRY_PASSWORD", }, }, }, @@ -828,11 +828,11 @@ func TestNative_Compile_StagesPipelineTemplate(t *testing.T) { Secrets: pipeline.StepSecretSlice{ &pipeline.StepSecret{ Source: "docker_username", - Target: "registry_username", + Target: "REGISTRY_USERNAME", }, &pipeline.StepSecret{ Source: "docker_password", - Target: "registry_password", + Target: "REGISTRY_PASSWORD", }, }, }, @@ -1075,11 +1075,11 @@ func TestNative_Compile_StepsPipelineTemplate(t *testing.T) { Secrets: pipeline.StepSecretSlice{ &pipeline.StepSecret{ Source: "docker_username", - Target: "registry_username", + Target: "REGISTRY_USERNAME", }, &pipeline.StepSecret{ Source: "docker_password", - Target: "registry_password", + Target: "REGISTRY_PASSWORD", }, }, }, @@ -1593,7 +1593,7 @@ func TestNative_Compile_Clone(t *testing.T) { ID: "step___0_clone", Directory: "/vela/src/foo//", Environment: cloneEnv, - Image: "target/vela-git:v0.5.1", + Image: "target/vela-git-slim:v0.12.0", Name: "clone", Number: 2, Pull: "always", @@ -2056,7 +2056,7 @@ func Test_client_modifyConfig(t *testing.T) { Name: "docker", Pull: "always", Parameters: map[string]interface{}{ - "init_options": map[interface{}]interface{}{ + "init_options": map[string]interface{}{ "get_plugins": "true", }, }, @@ -2089,7 +2089,7 @@ func Test_client_modifyConfig(t *testing.T) { Name: "docker", Pull: "always", Parameters: map[string]interface{}{ - "init_options": map[interface{}]interface{}{ + "init_options": map[string]interface{}{ "get_plugins": "true", }, }, @@ -3667,11 +3667,11 @@ func Test_CompileLite(t *testing.T) { Secrets: yaml.StepSecretSlice{ { Source: "docker_username", - Target: "registry_username", + Target: "REGISTRY_USERNAME", }, { Source: "docker_password", - Target: "registry_password", + Target: "REGISTRY_PASSWORD", }, }, Image: "plugins/docker:18.09", @@ -3783,11 +3783,11 @@ func Test_CompileLite(t *testing.T) { Secrets: yaml.StepSecretSlice{ { Source: "docker_username", - Target: "registry_username", + Target: "REGISTRY_USERNAME", }, { Source: "docker_password", - Target: "registry_password", + Target: "REGISTRY_PASSWORD", }, }, Image: "plugins/docker:18.09", diff --git a/compiler/native/expand_test.go b/compiler/native/expand_test.go index 56a9889c3..6e4c877d4 100644 --- a/compiler/native/expand_test.go +++ b/compiler/native/expand_test.go @@ -581,7 +581,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) { Secrets: yaml.StepSecretSlice{ { Source: "vault_token", - Target: "vault_token", + Target: "VAULT_TOKEN", }, }, Parameters: map[string]interface{}{ @@ -589,7 +589,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) { "auth_method": "token", "username": "octocat", "items": []interface{}{ - map[interface{}]interface{}{"path": "docker", "source": "secret/docker"}, + map[string]interface{}{"path": "docker", "source": "secret/docker"}, }, }, }, @@ -602,7 +602,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) { Secrets: yaml.StepSecretSlice{ { Source: "vault_token", - Target: "vault_token", + Target: "VAULT_TOKEN", }, }, Parameters: map[string]interface{}{ @@ -610,7 +610,7 @@ func TestNative_ExpandStepsMulti(t *testing.T) { "auth_method": "token", "username": "octocat", "items": []interface{}{ - map[interface{}]interface{}{"path": "docker", "source": "secret/docker"}, + map[string]interface{}{"path": "docker", "source": "secret/docker"}, }, }, }, diff --git a/compiler/native/parse.go b/compiler/native/parse.go index 0cd7a5819..a09f6964e 100644 --- a/compiler/native/parse.go +++ b/compiler/native/parse.go @@ -7,7 +7,7 @@ import ( "io" "os" - "github.com/buildkite/yaml" + "gopkg.in/yaml.v3" "github.com/go-vela/server/compiler/template/native" "github.com/go-vela/server/compiler/template/starlark" diff --git a/compiler/native/parse_test.go b/compiler/native/parse_test.go index e0aa9c512..74da7baa9 100644 --- a/compiler/native/parse_test.go +++ b/compiler/native/parse_test.go @@ -192,11 +192,11 @@ func TestNative_Parse_Parameters(t *testing.T) { Secrets: yaml.StepSecretSlice{ &yaml.StepSecret{ Source: "docker_username", - Target: "docker_username", + Target: "DOCKER_USERNAME", }, &yaml.StepSecret{ Source: "docker_password", - Target: "docker_password", + Target: "DOCKER_PASSWORD", }, }, }, @@ -302,11 +302,11 @@ func TestNative_Parse_StagesPipeline(t *testing.T) { Secrets: yaml.StepSecretSlice{ &yaml.StepSecret{ Source: "docker_username", - Target: "registry_username", + Target: "REGISTRY_USERNAME", }, &yaml.StepSecret{ Source: "docker_password", - Target: "registry_password", + Target: "REGISTRY_PASSWORD", }, }, }, @@ -407,11 +407,11 @@ func TestNative_Parse_StepsPipeline(t *testing.T) { Secrets: yaml.StepSecretSlice{ &yaml.StepSecret{ Source: "docker_username", - Target: "registry_username", + Target: "REGISTRY_USERNAME", }, &yaml.StepSecret{ Source: "docker_password", - Target: "registry_password", + Target: "REGISTRY_PASSWORD", }, }, }, diff --git a/compiler/native/substitute.go b/compiler/native/substitute.go index 9bbc717cd..53e9973e9 100644 --- a/compiler/native/substitute.go +++ b/compiler/native/substitute.go @@ -6,8 +6,8 @@ import ( "fmt" "strings" - "github.com/buildkite/yaml" "github.com/drone/envsubst" + "gopkg.in/yaml.v3" types "github.com/go-vela/types/yaml" ) diff --git a/compiler/native/testdata/clone_replace.yml b/compiler/native/testdata/clone_replace.yml index e258910b6..cf9d49b9d 100644 --- a/compiler/native/testdata/clone_replace.yml +++ b/compiler/native/testdata/clone_replace.yml @@ -5,7 +5,7 @@ metadata: steps: - name: clone - image: target/vela-git:v0.5.1 + image: target/vela-git-slim:v0.12.0 parameters: depth: 5 pull: always diff --git a/compiler/template/native/convert.go b/compiler/template/native/convert.go index ee45ef809..d59aa100e 100644 --- a/compiler/template/native/convert.go +++ b/compiler/template/native/convert.go @@ -5,7 +5,7 @@ package native import ( "strings" - "github.com/buildkite/yaml" + "gopkg.in/yaml.v3" "github.com/go-vela/types/raw" ) diff --git a/compiler/template/native/render.go b/compiler/template/native/render.go index a31f642b6..3de50bc4b 100644 --- a/compiler/template/native/render.go +++ b/compiler/template/native/render.go @@ -8,7 +8,7 @@ import ( "text/template" "github.com/Masterminds/sprig/v3" - "github.com/buildkite/yaml" + "gopkg.in/yaml.v3" "github.com/go-vela/types/raw" types "github.com/go-vela/types/yaml" diff --git a/compiler/template/native/render_test.go b/compiler/template/native/render_test.go index f4c92160d..70653fde5 100644 --- a/compiler/template/native/render_test.go +++ b/compiler/template/native/render_test.go @@ -6,8 +6,8 @@ import ( "os" "testing" - goyaml "github.com/buildkite/yaml" "github.com/google/go-cmp/cmp" + goyaml "gopkg.in/yaml.v3" "github.com/go-vela/types/raw" "github.com/go-vela/types/yaml" diff --git a/compiler/template/starlark/render.go b/compiler/template/starlark/render.go index 93b37cd78..b3e69f12f 100644 --- a/compiler/template/starlark/render.go +++ b/compiler/template/starlark/render.go @@ -7,9 +7,9 @@ import ( "errors" "fmt" - yaml "github.com/buildkite/yaml" "go.starlark.net/starlark" "go.starlark.net/starlarkstruct" + yaml "gopkg.in/yaml.v3" "github.com/go-vela/types/raw" types "github.com/go-vela/types/yaml" diff --git a/compiler/template/starlark/render_test.go b/compiler/template/starlark/render_test.go index a730f1088..fa30377d0 100644 --- a/compiler/template/starlark/render_test.go +++ b/compiler/template/starlark/render_test.go @@ -6,8 +6,8 @@ import ( "os" "testing" - goyaml "github.com/buildkite/yaml" "github.com/google/go-cmp/cmp" + goyaml "gopkg.in/yaml.v3" "github.com/go-vela/types/raw" "github.com/go-vela/types/yaml" diff --git a/database/build/clean.go b/database/build/clean.go index b7d876d16..81a461469 100644 --- a/database/build/clean.go +++ b/database/build/clean.go @@ -26,6 +26,7 @@ func (e *engine) CleanBuilds(ctx context.Context, msg string, before int64) (int // send query to the database result := e.client. + WithContext(ctx). Table(constants.TableBuild). Where("created < ?", before). Where("status = 'running' OR status = 'pending'"). diff --git a/database/build/count.go b/database/build/count.go index 3e86611d9..6b26c3f28 100644 --- a/database/build/count.go +++ b/database/build/count.go @@ -17,6 +17,7 @@ func (e *engine) CountBuilds(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Count(&b). Error diff --git a/database/build/count_deployment.go b/database/build/count_deployment.go index 46c8170fc..e00151cac 100644 --- a/database/build/count_deployment.go +++ b/database/build/count_deployment.go @@ -22,6 +22,7 @@ func (e *engine) CountBuildsForDeployment(ctx context.Context, d *library.Deploy // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Where("source = ?", d.GetURL()). Where(filters). diff --git a/database/build/count_org.go b/database/build/count_org.go index d9f4105ed..5a349c730 100644 --- a/database/build/count_org.go +++ b/database/build/count_org.go @@ -21,6 +21,7 @@ func (e *engine) CountBuildsForOrg(ctx context.Context, org string, filters map[ // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Joins("JOIN repos ON builds.repo_id = repos.id"). Where("repos.org = ?", org). diff --git a/database/build/count_repo.go b/database/build/count_repo.go index 298420caa..138b4d6d4 100644 --- a/database/build/count_repo.go +++ b/database/build/count_repo.go @@ -23,6 +23,7 @@ func (e *engine) CountBuildsForRepo(ctx context.Context, r *api.Repo, filters ma // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Where("repo_id = ?", r.GetID()). Where(filters). diff --git a/database/build/count_status.go b/database/build/count_status.go index 524d27d76..8fb401ffb 100644 --- a/database/build/count_status.go +++ b/database/build/count_status.go @@ -17,6 +17,7 @@ func (e *engine) CountBuildsForStatus(ctx context.Context, status string, filter // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Where("status = ?", status). Where(filters). diff --git a/database/build/create.go b/database/build/create.go index 3a3684102..7789434cb 100644 --- a/database/build/create.go +++ b/database/build/create.go @@ -30,7 +30,10 @@ func (e *engine) CreateBuild(ctx context.Context, b *api.Build) (*api.Build, err build = build.Crop() // send query to the database - err = e.client.Table(constants.TableBuild).Create(build).Error + err = e.client. + WithContext(ctx). + Table(constants.TableBuild). + Create(build).Error if err != nil { return nil, err } diff --git a/database/build/delete.go b/database/build/delete.go index 2bcb282e4..f03387a5f 100644 --- a/database/build/delete.go +++ b/database/build/delete.go @@ -22,6 +22,7 @@ func (e *engine) DeleteBuild(ctx context.Context, b *api.Build) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableBuild). Delete(build). Error diff --git a/database/build/get.go b/database/build/get.go index caaf1fe0c..dc7f0e2de 100644 --- a/database/build/get.go +++ b/database/build/get.go @@ -19,6 +19,7 @@ func (e *engine) GetBuild(ctx context.Context, id int64) (*api.Build, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/build/get_repo.go b/database/build/get_repo.go index b144cf432..8ac935881 100644 --- a/database/build/get_repo.go +++ b/database/build/get_repo.go @@ -25,6 +25,7 @@ func (e *engine) GetBuildForRepo(ctx context.Context, r *api.Repo, number int) ( // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/build/index.go b/database/build/index.go index 446395cc9..f224ccacc 100644 --- a/database/build/index.go +++ b/database/build/index.go @@ -47,23 +47,31 @@ func (e *engine) CreateBuildIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for builds table") // create the created column index for the builds table - err := e.client.Exec(CreateCreatedIndex).Error + err := e.client. + WithContext(ctx). + Exec(CreateCreatedIndex).Error if err != nil { return err } // create the repo_id column index for the builds table - err = e.client.Exec(CreateRepoIDIndex).Error + err = e.client. + WithContext(ctx). + Exec(CreateRepoIDIndex).Error if err != nil { return err } // create the source column index for the builds table - err = e.client.Exec(CreateSourceIndex).Error + err = e.client. + WithContext(ctx). + Exec(CreateSourceIndex).Error if err != nil { return err } // create the status column index for the builds table - return e.client.Exec(CreateStatusIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateStatusIndex).Error } diff --git a/database/build/last_repo.go b/database/build/last_repo.go index fe04af158..ccade3cc8 100644 --- a/database/build/last_repo.go +++ b/database/build/last_repo.go @@ -26,6 +26,7 @@ func (e *engine) LastBuildForRepo(ctx context.Context, r *api.Repo, branch strin // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/build/list.go b/database/build/list.go index 9e3e45c8c..4074618c7 100644 --- a/database/build/list.go +++ b/database/build/list.go @@ -32,6 +32,7 @@ func (e *engine) ListBuilds(ctx context.Context) ([]*api.Build, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Preload("Repo"). Preload("Repo.Owner"). Table(constants.TableBuild). diff --git a/database/build/list_dashboard.go b/database/build/list_dashboard.go index 8ddc6fa57..7ba0c379a 100644 --- a/database/build/list_dashboard.go +++ b/database/build/list_dashboard.go @@ -23,7 +23,10 @@ func (e *engine) ListBuildsForDashboardRepo(ctx context.Context, r *api.Repo, br b := new([]types.Build) builds := []*api.Build{} - query := e.client.Table(constants.TableBuild).Where("repo_id = ?", r.GetID()) + query := e.client. + WithContext(ctx). + Table(constants.TableBuild). + Where("repo_id = ?", r.GetID()) if len(branches) > 0 { query = query.Where("branch IN (?)", branches) diff --git a/database/build/list_org.go b/database/build/list_org.go index cfaa1992d..d1c33a7a7 100644 --- a/database/build/list_org.go +++ b/database/build/list_org.go @@ -40,6 +40,7 @@ func (e *engine) ListBuildsForOrg(ctx context.Context, org string, filters map[s offset := perPage * (page - 1) err = e.client. + WithContext(ctx). Table(constants.TableBuild). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/build/list_pending_running.go b/database/build/list_pending_running.go index 858b9c08e..6a08dcbac 100644 --- a/database/build/list_pending_running.go +++ b/database/build/list_pending_running.go @@ -20,6 +20,7 @@ func (e *engine) ListPendingAndRunningBuilds(ctx context.Context, after string) // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Select("builds.created, builds.number, builds.status, repos.full_name"). InnerJoins("INNER JOIN repos ON builds.repo_id = repos.id"). diff --git a/database/build/list_pending_running_repo.go b/database/build/list_pending_running_repo.go index cc8984265..f4028a57a 100644 --- a/database/build/list_pending_running_repo.go +++ b/database/build/list_pending_running_repo.go @@ -20,6 +20,7 @@ func (e *engine) ListPendingAndRunningBuildsForRepo(ctx context.Context, repo *a // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuild). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/build/list_repo.go b/database/build/list_repo.go index 2552c2ea3..880ea8e8c 100644 --- a/database/build/list_repo.go +++ b/database/build/list_repo.go @@ -41,6 +41,7 @@ func (e *engine) ListBuildsForRepo(ctx context.Context, r *api.Repo, filters map offset := perPage * (page - 1) err = e.client. + WithContext(ctx). Table(constants.TableBuild). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/build/table.go b/database/build/table.go index 8a7ae172c..2a7af528c 100644 --- a/database/build/table.go +++ b/database/build/table.go @@ -108,11 +108,15 @@ func (e *engine) CreateBuildTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the builds table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the builds table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/build/update.go b/database/build/update.go index e8ff1f274..07b029093 100644 --- a/database/build/update.go +++ b/database/build/update.go @@ -30,7 +30,10 @@ func (e *engine) UpdateBuild(ctx context.Context, b *api.Build) (*api.Build, err build = build.Crop() // send query to the database - err = e.client.Table(constants.TableBuild).Save(build).Error + err = e.client. + WithContext(ctx). + Table(constants.TableBuild). + Save(build).Error if err != nil { return nil, err } diff --git a/database/dashboard/create.go b/database/dashboard/create.go index 1af56728a..5d49d574f 100644 --- a/database/dashboard/create.go +++ b/database/dashboard/create.go @@ -26,7 +26,10 @@ func (e *engine) CreateDashboard(ctx context.Context, d *api.Dashboard) (*api.Da } // send query to the database - result := e.client.Table(constants.TableDashboard).Create(dashboard) + result := e.client. + WithContext(ctx). + Table(constants.TableDashboard). + Create(dashboard) return dashboard.ToAPI(), result.Error } diff --git a/database/dashboard/delete.go b/database/dashboard/delete.go index 2f49cca9f..1e8c40434 100644 --- a/database/dashboard/delete.go +++ b/database/dashboard/delete.go @@ -22,6 +22,7 @@ func (e *engine) DeleteDashboard(ctx context.Context, d *api.Dashboard) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableDashboard). Delete(dashboard). Error diff --git a/database/dashboard/get.go b/database/dashboard/get.go index 7c29c987f..d6d8f1f21 100644 --- a/database/dashboard/get.go +++ b/database/dashboard/get.go @@ -19,6 +19,7 @@ func (e *engine) GetDashboard(ctx context.Context, id string) (*api.Dashboard, e // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableDashboard). Where("id = ?", id). Take(d). diff --git a/database/dashboard/table.go b/database/dashboard/table.go index 1cedb30c3..cef302382 100644 --- a/database/dashboard/table.go +++ b/database/dashboard/table.go @@ -50,11 +50,15 @@ func (e *engine) CreateDashboardTable(ctx context.Context, driver string) error switch driver { case constants.DriverPostgres: // create the dashboards table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the dashboards table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/dashboard/update.go b/database/dashboard/update.go index e13387cb3..18089ff86 100644 --- a/database/dashboard/update.go +++ b/database/dashboard/update.go @@ -26,7 +26,10 @@ func (e *engine) UpdateDashboard(ctx context.Context, d *api.Dashboard) (*api.Da } // send query to the database - err = e.client.Table(constants.TableDashboard).Save(dashboard).Error + err = e.client. + WithContext(ctx). + Table(constants.TableDashboard). + Save(dashboard).Error if err != nil { return nil, err } diff --git a/database/deployment/count.go b/database/deployment/count.go index 26189cf10..f1c9e5b14 100644 --- a/database/deployment/count.go +++ b/database/deployment/count.go @@ -17,6 +17,7 @@ func (e *engine) CountDeployments(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableDeployment). Count(&d). Error diff --git a/database/deployment/count_repo.go b/database/deployment/count_repo.go index 94ed86558..8a2afd6fc 100644 --- a/database/deployment/count_repo.go +++ b/database/deployment/count_repo.go @@ -23,6 +23,7 @@ func (e *engine) CountDeploymentsForRepo(ctx context.Context, r *api.Repo) (int6 // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableDeployment). Where("repo_id = ?", r.GetID()). Count(&d). diff --git a/database/deployment/create.go b/database/deployment/create.go index c766596e8..923625bb6 100644 --- a/database/deployment/create.go +++ b/database/deployment/create.go @@ -27,7 +27,10 @@ func (e *engine) CreateDeployment(ctx context.Context, d *library.Deployment) (* return nil, err } - result := e.client.Table(constants.TableDeployment).Create(deployment) + result := e.client. + WithContext(ctx). + Table(constants.TableDeployment). + Create(deployment) // send query to the database return deployment.ToLibrary(d.Builds), result.Error diff --git a/database/deployment/delete.go b/database/deployment/delete.go index 606ef8262..cdb98fe31 100644 --- a/database/deployment/delete.go +++ b/database/deployment/delete.go @@ -23,6 +23,7 @@ func (e *engine) DeleteDeployment(ctx context.Context, d *library.Deployment) er // send query to the database return e.client. + WithContext(ctx). Table(constants.TableDeployment). Delete(deployment). Error diff --git a/database/deployment/get.go b/database/deployment/get.go index 214dcf57f..0086a03a2 100644 --- a/database/deployment/get.go +++ b/database/deployment/get.go @@ -20,6 +20,7 @@ func (e *engine) GetDeployment(ctx context.Context, id int64) (*library.Deployme // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableDeployment). Where("id = ?", id). Take(d). @@ -40,6 +41,7 @@ func (e *engine) GetDeployment(ctx context.Context, id int64) (*library.Deployme // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableBuild). Where("id = ?", bID). Take(b). diff --git a/database/deployment/get_repo.go b/database/deployment/get_repo.go index 1558b0428..f50ef3aba 100644 --- a/database/deployment/get_repo.go +++ b/database/deployment/get_repo.go @@ -27,6 +27,7 @@ func (e *engine) GetDeploymentForRepo(ctx context.Context, r *api.Repo, number i // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableDeployment). Where("repo_id = ?", r.GetID()). Where("number = ?", number). @@ -48,6 +49,7 @@ func (e *engine) GetDeploymentForRepo(ctx context.Context, r *api.Repo, number i // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableBuild). Where("id = ?", bID). Take(b). diff --git a/database/deployment/index.go b/database/deployment/index.go index f9b6a3419..c695313d0 100644 --- a/database/deployment/index.go +++ b/database/deployment/index.go @@ -20,5 +20,7 @@ func (e *engine) CreateDeploymentIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for deployments table") // create the repo_id column index for the deployments table - return e.client.Exec(CreateRepoIDIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateRepoIDIndex).Error } diff --git a/database/deployment/list.go b/database/deployment/list.go index f395f668b..7f942f859 100644 --- a/database/deployment/list.go +++ b/database/deployment/list.go @@ -21,6 +21,7 @@ func (e *engine) ListDeployments(ctx context.Context) ([]*library.Deployment, er // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableDeployment). Find(&d). Error @@ -45,6 +46,7 @@ func (e *engine) ListDeployments(ctx context.Context) ([]*library.Deployment, er // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableBuild). Where("id = ?", bID). Take(b). diff --git a/database/deployment/list_repo.go b/database/deployment/list_repo.go index 8f9b40eb4..4be4e071b 100644 --- a/database/deployment/list_repo.go +++ b/database/deployment/list_repo.go @@ -30,6 +30,7 @@ func (e *engine) ListDeploymentsForRepo(ctx context.Context, r *api.Repo, page, // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableDeployment). Where("repo_id = ?", r.GetID()). Order("number DESC"). @@ -58,6 +59,7 @@ func (e *engine) ListDeploymentsForRepo(ctx context.Context, r *api.Repo, page, // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableBuild). Where("id = ?", bID). Take(b). diff --git a/database/deployment/table.go b/database/deployment/table.go index b519c8abf..9baef0284 100644 --- a/database/deployment/table.go +++ b/database/deployment/table.go @@ -62,11 +62,15 @@ func (e *engine) CreateDeploymentTable(ctx context.Context, driver string) error switch driver { case constants.DriverPostgres: // create the deployments table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the deployments table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/deployment/update.go b/database/deployment/update.go index c2bbecff0..810450d79 100644 --- a/database/deployment/update.go +++ b/database/deployment/update.go @@ -27,7 +27,10 @@ func (e *engine) UpdateDeployment(ctx context.Context, d *library.Deployment) (* return nil, err } - result := e.client.Table(constants.TableDeployment).Save(deployment) + result := e.client. + WithContext(ctx). + Table(constants.TableDeployment). + Save(deployment) // send query to the database return deployment.ToLibrary(d.Builds), result.Error diff --git a/database/executable/clean.go b/database/executable/clean.go index b6bd2580a..ba4c06172 100644 --- a/database/executable/clean.go +++ b/database/executable/clean.go @@ -33,10 +33,16 @@ func (e *engine) CleanBuildExecutables(ctx context.Context) (int64, error) { switch e.config.Driver { case constants.DriverPostgres: - res := e.client.Exec(CleanExecutablesPostgres) + res := e.client. + WithContext(ctx). + Exec(CleanExecutablesPostgres) + return res.RowsAffected, res.Error default: - res := e.client.Exec(CleanExecutablesSqlite) + res := e.client. + WithContext(ctx). + Exec(CleanExecutablesSqlite) + return res.RowsAffected, res.Error } } diff --git a/database/executable/create.go b/database/executable/create.go index b619da068..e231f01fb 100644 --- a/database/executable/create.go +++ b/database/executable/create.go @@ -50,6 +50,7 @@ func (e *engine) CreateBuildExecutable(ctx context.Context, b *library.BuildExec // send query to the database return e.client. + WithContext(ctx). Table(constants.TableBuildExecutable). Create(executable). Error diff --git a/database/executable/pop.go b/database/executable/pop.go index 62dfde193..9625ad984 100644 --- a/database/executable/pop.go +++ b/database/executable/pop.go @@ -25,6 +25,7 @@ func (e *engine) PopBuildExecutable(ctx context.Context, id int64) (*library.Bui case constants.DriverPostgres: // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuildExecutable). Clauses(clause.Returning{}). Where("build_id = ?", id). @@ -37,6 +38,7 @@ func (e *engine) PopBuildExecutable(ctx context.Context, id int64) (*library.Bui case constants.DriverSqlite: // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableBuildExecutable). Where("id = ?", id). Take(b). @@ -47,6 +49,7 @@ func (e *engine) PopBuildExecutable(ctx context.Context, id int64) (*library.Bui // send query to the database to delete result just got err = e.client. + WithContext(ctx). Table(constants.TableBuildExecutable). Delete(b). Error diff --git a/database/executable/table.go b/database/executable/table.go index 9a683f245..0a7a3c147 100644 --- a/database/executable/table.go +++ b/database/executable/table.go @@ -42,11 +42,15 @@ func (e *engine) CreateBuildExecutableTable(ctx context.Context, driver string) switch driver { case constants.DriverPostgres: // create the build_executables table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the build_executables table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/hook/count.go b/database/hook/count.go index ce02e6fa0..9f3e3c264 100644 --- a/database/hook/count.go +++ b/database/hook/count.go @@ -17,6 +17,7 @@ func (e *engine) CountHooks(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableHook). Count(&h). Error diff --git a/database/hook/count_repo.go b/database/hook/count_repo.go index aa794b5f1..456019fc1 100644 --- a/database/hook/count_repo.go +++ b/database/hook/count_repo.go @@ -23,6 +23,7 @@ func (e *engine) CountHooksForRepo(ctx context.Context, r *api.Repo) (int64, err // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableHook). Where("repo_id = ?", r.GetID()). Count(&h). diff --git a/database/hook/create.go b/database/hook/create.go index c260eb2b5..badb47e82 100644 --- a/database/hook/create.go +++ b/database/hook/create.go @@ -31,7 +31,10 @@ func (e *engine) CreateHook(ctx context.Context, h *library.Hook) (*library.Hook return nil, err } - result := e.client.Table(constants.TableHook).Create(hook) + result := e.client. + WithContext(ctx). + Table(constants.TableHook). + Create(hook) // send query to the database return hook.ToLibrary(), result.Error diff --git a/database/hook/delete.go b/database/hook/delete.go index 0245477af..6eab73780 100644 --- a/database/hook/delete.go +++ b/database/hook/delete.go @@ -25,6 +25,7 @@ func (e *engine) DeleteHook(ctx context.Context, h *library.Hook) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableHook). Delete(hook). Error diff --git a/database/hook/get.go b/database/hook/get.go index 82e421dda..afe9b920d 100644 --- a/database/hook/get.go +++ b/database/hook/get.go @@ -19,6 +19,7 @@ func (e *engine) GetHook(ctx context.Context, id int64) (*library.Hook, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableHook). Where("id = ?", id). Take(h). diff --git a/database/hook/get_repo.go b/database/hook/get_repo.go index a88d593df..61afe28b2 100644 --- a/database/hook/get_repo.go +++ b/database/hook/get_repo.go @@ -26,6 +26,7 @@ func (e *engine) GetHookForRepo(ctx context.Context, r *api.Repo, number int) (* // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableHook). Where("repo_id = ?", r.GetID()). Where("number = ?", number). diff --git a/database/hook/get_webhook.go b/database/hook/get_webhook.go index 919092fd7..47ea57f6c 100644 --- a/database/hook/get_webhook.go +++ b/database/hook/get_webhook.go @@ -19,6 +19,7 @@ func (e *engine) GetHookByWebhookID(ctx context.Context, webhookID int64) (*libr // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableHook). Where("webhook_id = ?", webhookID). Take(h). diff --git a/database/hook/index.go b/database/hook/index.go index b86c9ac39..7f813ca8c 100644 --- a/database/hook/index.go +++ b/database/hook/index.go @@ -20,5 +20,7 @@ func (e *engine) CreateHookIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for hooks table") // create the repo_id column index for the hooks table - return e.client.Exec(CreateRepoIDIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateRepoIDIndex).Error } diff --git a/database/hook/last_repo.go b/database/hook/last_repo.go index 710053ad8..776f9b6c0 100644 --- a/database/hook/last_repo.go +++ b/database/hook/last_repo.go @@ -27,6 +27,7 @@ func (e *engine) LastHookForRepo(ctx context.Context, r *api.Repo) (*library.Hoo // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableHook). Where("repo_id = ?", r.GetID()). Order("number DESC"). diff --git a/database/hook/list.go b/database/hook/list.go index fcfba1e28..3957f57f2 100644 --- a/database/hook/list.go +++ b/database/hook/list.go @@ -32,6 +32,7 @@ func (e *engine) ListHooks(ctx context.Context) ([]*library.Hook, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableHook). Find(&h). Error diff --git a/database/hook/list_repo.go b/database/hook/list_repo.go index 28e80c03e..7d4487f63 100644 --- a/database/hook/list_repo.go +++ b/database/hook/list_repo.go @@ -41,6 +41,7 @@ func (e *engine) ListHooksForRepo(ctx context.Context, r *api.Repo, page, perPag // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableHook). Where("repo_id = ?", r.GetID()). Order("id DESC"). diff --git a/database/hook/table.go b/database/hook/table.go index 81ef7157f..a5d8d46ea 100644 --- a/database/hook/table.go +++ b/database/hook/table.go @@ -64,11 +64,15 @@ func (e *engine) CreateHookTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the hooks table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the hooks table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/hook/update.go b/database/hook/update.go index 16222a6d1..bd6fe93ef 100644 --- a/database/hook/update.go +++ b/database/hook/update.go @@ -31,7 +31,10 @@ func (e *engine) UpdateHook(ctx context.Context, h *library.Hook) (*library.Hook return nil, err } - result := e.client.Table(constants.TableHook).Save(hook) + result := e.client. + WithContext(ctx). + Table(constants.TableHook). + Save(hook) // send query to the database return hook.ToLibrary(), result.Error diff --git a/database/integration_test.go b/database/integration_test.go index ad3748c4f..973eb5495 100644 --- a/database/integration_test.go +++ b/database/integration_test.go @@ -1804,8 +1804,8 @@ func testServices(t *testing.T, db Interface, resources *Resources) { methods["ListServicesForBuild"] = true expected := map[string]float64{ - "#init": 1, - "target/vela-git:v0.3.0": 1, + "#init": 1, + "target/vela-git-slim:v0.12.0": 1, } images, err := db.ListServiceImageCount(context.TODO()) if err != nil { @@ -1959,8 +1959,8 @@ func testSteps(t *testing.T, db Interface, resources *Resources) { methods["ListStepsForBuild"] = true expected := map[string]float64{ - "#init": 1, - "target/vela-git:v0.3.0": 1, + "#init": 1, + "target/vela-git-slim:v0.12.0": 1, } images, err := db.ListStepImageCount(ctx) if err != nil { @@ -2288,7 +2288,7 @@ func testSettings(t *testing.T, db Interface, resources *Resources) { // update the settings for _, s := range resources.Platform { - s.SetCloneImage("target/vela-git:abc123") + s.SetCloneImage("target/vela-git-slim:abc123") got, err := db.UpdateSettings(context.TODO(), s) if err != nil { t.Errorf("unable to update settings %d: %v", s.GetID(), err) @@ -2757,7 +2757,7 @@ func newResources() *Resources { serviceTwo.SetRepoID(1) serviceTwo.SetNumber(2) serviceTwo.SetName("clone") - serviceTwo.SetImage("target/vela-git:v0.3.0") + serviceTwo.SetImage("target/vela-git-slim:v0.12.0") serviceTwo.SetStatus("pending") serviceTwo.SetError("") serviceTwo.SetExitCode(0) @@ -2793,7 +2793,7 @@ func newResources() *Resources { stepTwo.SetRepoID(1) stepTwo.SetNumber(2) stepTwo.SetName("clone") - stepTwo.SetImage("target/vela-git:v0.3.0") + stepTwo.SetImage("target/vela-git-slim:v0.12.0") stepTwo.SetStage("init") stepTwo.SetStatus("pending") stepTwo.SetError("") diff --git a/database/jwk/create.go b/database/jwk/create.go index 0ef8330b9..684f49b97 100644 --- a/database/jwk/create.go +++ b/database/jwk/create.go @@ -14,7 +14,7 @@ import ( ) // CreateJWK creates a new JWK in the database. -func (e *engine) CreateJWK(_ context.Context, j jwk.RSAPublicKey) error { +func (e *engine) CreateJWK(ctx context.Context, j jwk.RSAPublicKey) error { e.logger.WithFields(logrus.Fields{ "jwk": j.KeyID(), }).Tracef("creating key %s", j.KeyID()) @@ -23,5 +23,8 @@ func (e *engine) CreateJWK(_ context.Context, j jwk.RSAPublicKey) error { key.Active = sql.NullBool{Bool: true, Valid: true} // send query to the database - return e.client.Table(constants.TableJWK).Create(key).Error + return e.client. + WithContext(ctx). + Table(constants.TableJWK). + Create(key).Error } diff --git a/database/jwk/get.go b/database/jwk/get.go index b324ae3cc..a99dbbb63 100644 --- a/database/jwk/get.go +++ b/database/jwk/get.go @@ -12,7 +12,7 @@ import ( ) // GetActiveJWK gets a JWK by UUID (kid) from the database if active. -func (e *engine) GetActiveJWK(_ context.Context, id string) (jwk.RSAPublicKey, error) { +func (e *engine) GetActiveJWK(ctx context.Context, id string) (jwk.RSAPublicKey, error) { e.logger.Tracef("getting JWK key %s", id) // variable to store query results @@ -20,6 +20,7 @@ func (e *engine) GetActiveJWK(_ context.Context, id string) (jwk.RSAPublicKey, e // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableJWK). Where("id = ?", id). Where("active = ?", true). diff --git a/database/jwk/list.go b/database/jwk/list.go index 4c29a4c5c..8de0fcf63 100644 --- a/database/jwk/list.go +++ b/database/jwk/list.go @@ -12,7 +12,7 @@ import ( ) // ListJWKs gets a list of all configured JWKs from the database. -func (e *engine) ListJWKs(_ context.Context) (jwk.Set, error) { +func (e *engine) ListJWKs(ctx context.Context) (jwk.Set, error) { e.logger.Trace("listing all JWKs") k := new([]types.JWK) @@ -20,6 +20,7 @@ func (e *engine) ListJWKs(_ context.Context) (jwk.Set, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableJWK). Find(&k). Error diff --git a/database/jwk/rotate.go b/database/jwk/rotate.go index 793584037..bcd4462ce 100644 --- a/database/jwk/rotate.go +++ b/database/jwk/rotate.go @@ -11,13 +11,14 @@ import ( ) // RotateKeys removes all inactive keys and sets active keys to inactive. -func (e *engine) RotateKeys(_ context.Context) error { +func (e *engine) RotateKeys(ctx context.Context) error { e.logger.Trace("rotating jwks") k := types.JWK{} // remove inactive keys err := e.client. + WithContext(ctx). Table(constants.TableJWK). Where("active = ?", false). Delete(&k). @@ -28,6 +29,7 @@ func (e *engine) RotateKeys(_ context.Context) error { // set active keys to inactive err = e.client. + WithContext(ctx). Table(constants.TableJWK). Where("active = ?", true). Update("active", sql.NullBool{Bool: false, Valid: true}). diff --git a/database/jwk/table.go b/database/jwk/table.go index b2a40c844..ee5895fcb 100644 --- a/database/jwk/table.go +++ b/database/jwk/table.go @@ -40,11 +40,15 @@ func (e *engine) CreateJWKTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the jwks table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the jwks table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/log/count.go b/database/log/count.go index e3b38b316..d31c37f62 100644 --- a/database/log/count.go +++ b/database/log/count.go @@ -17,6 +17,7 @@ func (e *engine) CountLogs(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableLog). Count(&l). Error diff --git a/database/log/count_build.go b/database/log/count_build.go index 524ff2960..36cd2984f 100644 --- a/database/log/count_build.go +++ b/database/log/count_build.go @@ -18,6 +18,7 @@ func (e *engine) CountLogsForBuild(ctx context.Context, b *api.Build) (int64, er // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableLog). Where("build_id = ?", b.GetID()). Count(&l). diff --git a/database/log/create.go b/database/log/create.go index fdabab63c..4d0ee3428 100644 --- a/database/log/create.go +++ b/database/log/create.go @@ -50,6 +50,7 @@ func (e *engine) CreateLog(ctx context.Context, l *library.Log) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableLog). Create(log). Error diff --git a/database/log/delete.go b/database/log/delete.go index 31f4ea31e..394321159 100644 --- a/database/log/delete.go +++ b/database/log/delete.go @@ -27,6 +27,7 @@ func (e *engine) DeleteLog(ctx context.Context, l *library.Log) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableLog). Delete(log). Error diff --git a/database/log/get.go b/database/log/get.go index 2a825a6da..cf66dbfd7 100644 --- a/database/log/get.go +++ b/database/log/get.go @@ -19,6 +19,7 @@ func (e *engine) GetLog(ctx context.Context, id int64) (*library.Log, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableLog). Where("id = ?", id). Take(l). diff --git a/database/log/get_service.go b/database/log/get_service.go index aac501801..ad4156a8d 100644 --- a/database/log/get_service.go +++ b/database/log/get_service.go @@ -20,6 +20,7 @@ func (e *engine) GetLogForService(ctx context.Context, s *library.Service) (*lib // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableLog). Where("service_id = ?", s.GetID()). Take(l). diff --git a/database/log/get_step.go b/database/log/get_step.go index f2ca4a3a1..6d0084712 100644 --- a/database/log/get_step.go +++ b/database/log/get_step.go @@ -20,6 +20,7 @@ func (e *engine) GetLogForStep(ctx context.Context, s *library.Step) (*library.L // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableLog). Where("step_id = ?", s.GetID()). Take(l). diff --git a/database/log/index.go b/database/log/index.go index 230a10d8b..1c9ff5736 100644 --- a/database/log/index.go +++ b/database/log/index.go @@ -20,5 +20,7 @@ func (e *engine) CreateLogIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for logs table") // create the build_id column index for the logs table - return e.client.Exec(CreateBuildIDIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateBuildIDIndex).Error } diff --git a/database/log/list.go b/database/log/list.go index 367d8c15e..ed6ecdcb7 100644 --- a/database/log/list.go +++ b/database/log/list.go @@ -32,6 +32,7 @@ func (e *engine) ListLogs(ctx context.Context) ([]*library.Log, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableLog). Find(&l). Error diff --git a/database/log/list_build.go b/database/log/list_build.go index 43c117916..f27798b78 100644 --- a/database/log/list_build.go +++ b/database/log/list_build.go @@ -36,6 +36,7 @@ func (e *engine) ListLogsForBuild(ctx context.Context, b *api.Build, page, perPa // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableLog). Where("build_id = ?", b.GetID()). Order("service_id ASC NULLS LAST"). diff --git a/database/log/table.go b/database/log/table.go index 94ec398ff..fbf74cd28 100644 --- a/database/log/table.go +++ b/database/log/table.go @@ -50,11 +50,15 @@ func (e *engine) CreateLogTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the logs table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the logs table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/log/update.go b/database/log/update.go index f7a148aae..ae9819e4a 100644 --- a/database/log/update.go +++ b/database/log/update.go @@ -50,6 +50,7 @@ func (e *engine) UpdateLog(ctx context.Context, l *library.Log) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableLog). Save(log). Error diff --git a/database/pipeline/count.go b/database/pipeline/count.go index 3252f5edd..818982b52 100644 --- a/database/pipeline/count.go +++ b/database/pipeline/count.go @@ -17,6 +17,7 @@ func (e *engine) CountPipelines(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TablePipeline). Count(&p). Error diff --git a/database/pipeline/count_repo.go b/database/pipeline/count_repo.go index 6adf46984..147ae552b 100644 --- a/database/pipeline/count_repo.go +++ b/database/pipeline/count_repo.go @@ -23,6 +23,7 @@ func (e *engine) CountPipelinesForRepo(ctx context.Context, r *api.Repo) (int64, // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TablePipeline). Where("repo_id = ?", r.GetID()). Count(&p). diff --git a/database/pipeline/create.go b/database/pipeline/create.go index 4ee770b52..e2910e6a1 100644 --- a/database/pipeline/create.go +++ b/database/pipeline/create.go @@ -40,7 +40,10 @@ func (e *engine) CreatePipeline(ctx context.Context, p *library.Pipeline) (*libr } // send query to the database - err = e.client.Table(constants.TablePipeline).Create(pipeline).Error + err = e.client. + WithContext(ctx). + Table(constants.TablePipeline). + Create(pipeline).Error if err != nil { return nil, err } diff --git a/database/pipeline/delete.go b/database/pipeline/delete.go index b35cdd7d3..ff49a8406 100644 --- a/database/pipeline/delete.go +++ b/database/pipeline/delete.go @@ -25,6 +25,7 @@ func (e *engine) DeletePipeline(ctx context.Context, p *library.Pipeline) error // send query to the database return e.client. + WithContext(ctx). Table(constants.TablePipeline). Delete(pipeline). Error diff --git a/database/pipeline/get.go b/database/pipeline/get.go index 8e1c54b13..e685a9178 100644 --- a/database/pipeline/get.go +++ b/database/pipeline/get.go @@ -19,6 +19,7 @@ func (e *engine) GetPipeline(ctx context.Context, id int64) (*library.Pipeline, // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TablePipeline). Where("id = ?", id). Take(p). diff --git a/database/pipeline/get_repo.go b/database/pipeline/get_repo.go index 0fc493003..5724dd0a7 100644 --- a/database/pipeline/get_repo.go +++ b/database/pipeline/get_repo.go @@ -26,6 +26,7 @@ func (e *engine) GetPipelineForRepo(ctx context.Context, commit string, r *api.R // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TablePipeline). Where("repo_id = ?", r.GetID()). Where("\"commit\" = ?", commit). diff --git a/database/pipeline/index.go b/database/pipeline/index.go index f49ecb58a..4f0095f70 100644 --- a/database/pipeline/index.go +++ b/database/pipeline/index.go @@ -20,5 +20,7 @@ func (e *engine) CreatePipelineIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for pipelines table in the database") // create the repo_id column index for the pipelines table - return e.client.Exec(CreateRepoIDIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateRepoIDIndex).Error } diff --git a/database/pipeline/list.go b/database/pipeline/list.go index fd9e077e0..12fac0bf1 100644 --- a/database/pipeline/list.go +++ b/database/pipeline/list.go @@ -32,6 +32,7 @@ func (e *engine) ListPipelines(ctx context.Context) ([]*library.Pipeline, error) // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TablePipeline). Find(&p). Error diff --git a/database/pipeline/list_repo.go b/database/pipeline/list_repo.go index 4f855c746..22e9032cb 100644 --- a/database/pipeline/list_repo.go +++ b/database/pipeline/list_repo.go @@ -42,6 +42,7 @@ func (e *engine) ListPipelinesForRepo(ctx context.Context, r *api.Repo, page, pe offset := perPage * (page - 1) err = e.client. + WithContext(ctx). Table(constants.TablePipeline). Where("repo_id = ?", r.GetID()). Limit(perPage). diff --git a/database/pipeline/table.go b/database/pipeline/table.go index eb399772c..919b1b652 100644 --- a/database/pipeline/table.go +++ b/database/pipeline/table.go @@ -66,11 +66,15 @@ func (e *engine) CreatePipelineTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the pipelines table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the pipelines table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/pipeline/update.go b/database/pipeline/update.go index 37353d853..93cff9351 100644 --- a/database/pipeline/update.go +++ b/database/pipeline/update.go @@ -40,7 +40,10 @@ func (e *engine) UpdatePipeline(ctx context.Context, p *library.Pipeline) (*libr } // send query to the database - err = e.client.Table(constants.TablePipeline).Save(pipeline).Error + err = e.client. + WithContext(ctx). + Table(constants.TablePipeline). + Save(pipeline).Error if err != nil { return nil, err } diff --git a/database/repo/count.go b/database/repo/count.go index 165bf0f0b..6db509b06 100644 --- a/database/repo/count.go +++ b/database/repo/count.go @@ -17,6 +17,7 @@ func (e *engine) CountRepos(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableRepo). Count(&r). Error diff --git a/database/repo/count_org.go b/database/repo/count_org.go index 6e0ce6cb4..915a0d9a7 100644 --- a/database/repo/count_org.go +++ b/database/repo/count_org.go @@ -21,6 +21,7 @@ func (e *engine) CountReposForOrg(ctx context.Context, org string, filters map[s // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableRepo). Where("org = ?", org). Where(filters). diff --git a/database/repo/count_user.go b/database/repo/count_user.go index dfc002d9b..57f3ecb3c 100644 --- a/database/repo/count_user.go +++ b/database/repo/count_user.go @@ -22,6 +22,7 @@ func (e *engine) CountReposForUser(ctx context.Context, u *api.User, filters map // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableRepo). Where("user_id = ?", u.GetID()). Where(filters). diff --git a/database/repo/create.go b/database/repo/create.go index 166362de9..7500b4101 100644 --- a/database/repo/create.go +++ b/database/repo/create.go @@ -37,7 +37,10 @@ func (e *engine) CreateRepo(ctx context.Context, r *api.Repo) (*api.Repo, error) } // send query to the database - err = e.client.Table(constants.TableRepo).Create(repo).Error + err = e.client. + WithContext(ctx). + Table(constants.TableRepo). + Create(repo).Error if err != nil { return nil, err } diff --git a/database/repo/delete.go b/database/repo/delete.go index a12e2ec91..cfd6dffa3 100644 --- a/database/repo/delete.go +++ b/database/repo/delete.go @@ -24,6 +24,7 @@ func (e *engine) DeleteRepo(ctx context.Context, r *api.Repo) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableRepo). Delete(repo). Error diff --git a/database/repo/get.go b/database/repo/get.go index d29ba16c8..15a301290 100644 --- a/database/repo/get.go +++ b/database/repo/get.go @@ -19,6 +19,7 @@ func (e *engine) GetRepo(ctx context.Context, id int64) (*api.Repo, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableRepo). Preload("Owner"). Where("id = ?", id). diff --git a/database/repo/get_org.go b/database/repo/get_org.go index e66b97b37..38b32d4ca 100644 --- a/database/repo/get_org.go +++ b/database/repo/get_org.go @@ -24,6 +24,7 @@ func (e *engine) GetRepoForOrg(ctx context.Context, org, name string) (*api.Repo // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableRepo). Preload("Owner"). Where("org = ?", org). diff --git a/database/repo/index.go b/database/repo/index.go index 9c97ad962..e4b236064 100644 --- a/database/repo/index.go +++ b/database/repo/index.go @@ -20,5 +20,7 @@ func (e *engine) CreateRepoIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for repos table") // create the org and name columns index for the repos table - return e.client.Exec(CreateOrgNameIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateOrgNameIndex).Error } diff --git a/database/repo/list.go b/database/repo/list.go index abe27a434..980044d97 100644 --- a/database/repo/list.go +++ b/database/repo/list.go @@ -32,6 +32,7 @@ func (e *engine) ListRepos(ctx context.Context) ([]*api.Repo, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableRepo). Preload("Owner"). Find(&r). diff --git a/database/repo/list_org.go b/database/repo/list_org.go index 016044744..1b39feea8 100644 --- a/database/repo/list_org.go +++ b/database/repo/list_org.go @@ -42,6 +42,7 @@ func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filter switch sortBy { case "latest": query := e.client. + WithContext(ctx). Table(constants.TableBuild). Select("repos.id, MAX(builds.created) AS latest_build"). Joins("INNER JOIN repos repos ON builds.repo_id = repos.id"). @@ -49,6 +50,7 @@ func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filter Group("repos.id") err = e.client. + WithContext(ctx). Table(constants.TableRepo). Preload("Owner"). Select("repos.*"). @@ -65,6 +67,7 @@ func (e *engine) ListReposForOrg(ctx context.Context, org, sortBy string, filter fallthrough default: err = e.client. + WithContext(ctx). Table(constants.TableRepo). Preload("Owner"). Where("org = ?", org). diff --git a/database/repo/list_user.go b/database/repo/list_user.go index f50374de1..997fe15f4 100644 --- a/database/repo/list_user.go +++ b/database/repo/list_user.go @@ -42,6 +42,7 @@ func (e *engine) ListReposForUser(ctx context.Context, u *api.User, sortBy strin switch sortBy { case "latest": query := e.client. + WithContext(ctx). Table(constants.TableBuild). Select("repos.id, MAX(builds.created) AS latest_build"). Joins("INNER JOIN repos repos ON builds.repo_id = repos.id"). @@ -49,6 +50,7 @@ func (e *engine) ListReposForUser(ctx context.Context, u *api.User, sortBy strin Group("repos.id") err = e.client. + WithContext(ctx). Table(constants.TableRepo). Preload("Owner"). Select("repos.*"). @@ -65,6 +67,7 @@ func (e *engine) ListReposForUser(ctx context.Context, u *api.User, sortBy strin fallthrough default: err = e.client. + WithContext(ctx). Table(constants.TableRepo). Preload("Owner"). Where("user_id = ?", u.GetID()). diff --git a/database/repo/table.go b/database/repo/table.go index 65bd21fb2..0b16bae94 100644 --- a/database/repo/table.go +++ b/database/repo/table.go @@ -78,11 +78,15 @@ func (e *engine) CreateRepoTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the repos table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the repos table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/repo/update.go b/database/repo/update.go index 9a11b8010..05af67470 100644 --- a/database/repo/update.go +++ b/database/repo/update.go @@ -37,7 +37,10 @@ func (e *engine) UpdateRepo(ctx context.Context, r *api.Repo) (*api.Repo, error) } // send query to the database - err = e.client.Table(constants.TableRepo).Save(repo).Error + err = e.client. + WithContext(ctx). + Table(constants.TableRepo). + Save(repo).Error if err != nil { return nil, err } diff --git a/database/schedule/count.go b/database/schedule/count.go index 8da93e69f..e199e8ed0 100644 --- a/database/schedule/count.go +++ b/database/schedule/count.go @@ -17,6 +17,7 @@ func (e *engine) CountSchedules(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSchedule). Count(&s). Error diff --git a/database/schedule/count_active.go b/database/schedule/count_active.go index be2dd9b5e..5aa6e7209 100644 --- a/database/schedule/count_active.go +++ b/database/schedule/count_active.go @@ -17,6 +17,7 @@ func (e *engine) CountActiveSchedules(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSchedule). Where("active = ?", true). Count(&s). diff --git a/database/schedule/count_repo.go b/database/schedule/count_repo.go index 724ce19b3..41246b800 100644 --- a/database/schedule/count_repo.go +++ b/database/schedule/count_repo.go @@ -23,6 +23,7 @@ func (e *engine) CountSchedulesForRepo(ctx context.Context, r *api.Repo) (int64, // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSchedule). Where("repo_id = ?", r.GetID()). Count(&s). diff --git a/database/schedule/create.go b/database/schedule/create.go index 9b349a7d1..891237c26 100644 --- a/database/schedule/create.go +++ b/database/schedule/create.go @@ -28,7 +28,10 @@ func (e *engine) CreateSchedule(ctx context.Context, s *api.Schedule) (*api.Sche } // send query to the database - err = e.client.Table(constants.TableSchedule).Create(schedule).Error + err = e.client. + WithContext(ctx). + Table(constants.TableSchedule). + Create(schedule).Error if err != nil { return nil, err } diff --git a/database/schedule/delete.go b/database/schedule/delete.go index bc63c2f10..8f28c1173 100644 --- a/database/schedule/delete.go +++ b/database/schedule/delete.go @@ -23,6 +23,7 @@ func (e *engine) DeleteSchedule(ctx context.Context, s *api.Schedule) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableSchedule). Delete(schedule). Error diff --git a/database/schedule/get.go b/database/schedule/get.go index 1bcdc37cb..887d4e002 100644 --- a/database/schedule/get.go +++ b/database/schedule/get.go @@ -19,6 +19,7 @@ func (e *engine) GetSchedule(ctx context.Context, id int64) (*api.Schedule, erro // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSchedule). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/schedule/get_repo.go b/database/schedule/get_repo.go index e940694a4..57c9f485a 100644 --- a/database/schedule/get_repo.go +++ b/database/schedule/get_repo.go @@ -25,6 +25,7 @@ func (e *engine) GetScheduleForRepo(ctx context.Context, r *api.Repo, name strin // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSchedule). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/schedule/index.go b/database/schedule/index.go index 11834bf4f..b02543b93 100644 --- a/database/schedule/index.go +++ b/database/schedule/index.go @@ -20,5 +20,7 @@ func (e *engine) CreateScheduleIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for schedules table in the database") // create the repo_id column index for the schedules table - return e.client.Exec(CreateRepoIDIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateRepoIDIndex).Error } diff --git a/database/schedule/list.go b/database/schedule/list.go index 4282f554a..44f548352 100644 --- a/database/schedule/list.go +++ b/database/schedule/list.go @@ -32,6 +32,7 @@ func (e *engine) ListSchedules(ctx context.Context) ([]*api.Schedule, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableSchedule). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/schedule/list_active.go b/database/schedule/list_active.go index 84e575b1a..8952de929 100644 --- a/database/schedule/list_active.go +++ b/database/schedule/list_active.go @@ -32,6 +32,7 @@ func (e *engine) ListActiveSchedules(ctx context.Context) ([]*api.Schedule, erro // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableSchedule). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/schedule/list_repo.go b/database/schedule/list_repo.go index 9ec086589..38e0d1b3e 100644 --- a/database/schedule/list_repo.go +++ b/database/schedule/list_repo.go @@ -40,6 +40,7 @@ func (e *engine) ListSchedulesForRepo(ctx context.Context, r *api.Repo, page, pe // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableSchedule). Preload("Repo"). Preload("Repo.Owner"). diff --git a/database/schedule/table.go b/database/schedule/table.go index 82ae8fdaf..86eda10bc 100644 --- a/database/schedule/table.go +++ b/database/schedule/table.go @@ -60,11 +60,15 @@ func (e *engine) CreateScheduleTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the schedules table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the schedules table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/schedule/update.go b/database/schedule/update.go index 7dcc1256d..d2f9cbf33 100644 --- a/database/schedule/update.go +++ b/database/schedule/update.go @@ -32,9 +32,16 @@ func (e *engine) UpdateSchedule(ctx context.Context, s *api.Schedule, fields boo // we do this because Gorm will automatically set `updated_at` with the Save function // and the `updated_at` field should reflect the last time a user updated the record, rather than the scheduler if fields { - err = e.client.Table(constants.TableSchedule).Save(schedule).Error + err = e.client. + WithContext(ctx). + Table(constants.TableSchedule). + Save(schedule).Error } else { - err = e.client.Table(constants.TableSchedule).Model(schedule).UpdateColumn("scheduled_at", s.GetScheduledAt()).Error + err = e.client. + WithContext(ctx). + Table(constants.TableSchedule). + Model(schedule). + UpdateColumn("scheduled_at", s.GetScheduledAt()).Error } if err != nil { diff --git a/database/secret/count.go b/database/secret/count.go index bf91525fa..2b9b36ad0 100644 --- a/database/secret/count.go +++ b/database/secret/count.go @@ -17,6 +17,7 @@ func (e *engine) CountSecrets(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Count(&s). Error diff --git a/database/secret/count_org.go b/database/secret/count_org.go index 9b4a8c263..85fb17624 100644 --- a/database/secret/count_org.go +++ b/database/secret/count_org.go @@ -22,6 +22,7 @@ func (e *engine) CountSecretsForOrg(ctx context.Context, org string, filters map // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretOrg). Where("org = ?", org). diff --git a/database/secret/count_repo.go b/database/secret/count_repo.go index b8edddcb0..6430f7fe9 100644 --- a/database/secret/count_repo.go +++ b/database/secret/count_repo.go @@ -24,6 +24,7 @@ func (e *engine) CountSecretsForRepo(ctx context.Context, r *api.Repo, filters m // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretRepo). Where("org = ?", r.GetOrg()). diff --git a/database/secret/count_team.go b/database/secret/count_team.go index 1b5fc8361..af158dcbc 100644 --- a/database/secret/count_team.go +++ b/database/secret/count_team.go @@ -24,6 +24,7 @@ func (e *engine) CountSecretsForTeam(ctx context.Context, org, team string, filt // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretShared). Where("org = ?", org). @@ -56,6 +57,7 @@ func (e *engine) CountSecretsForTeams(ctx context.Context, org string, teams []s // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretShared). Where("org = ?", org). diff --git a/database/secret/create.go b/database/secret/create.go index be409d304..163e72776 100644 --- a/database/secret/create.go +++ b/database/secret/create.go @@ -61,7 +61,10 @@ func (e *engine) CreateSecret(ctx context.Context, s *library.Secret) (*library. } // create secret record - result := e.client.Table(constants.TableSecret).Create(secret.Nullify()) + result := e.client. + WithContext(ctx). + Table(constants.TableSecret). + Create(secret.Nullify()) if result.Error != nil { return nil, result.Error diff --git a/database/secret/delete.go b/database/secret/delete.go index bc3cba36f..aa87ba1ea 100644 --- a/database/secret/delete.go +++ b/database/secret/delete.go @@ -41,6 +41,7 @@ func (e *engine) DeleteSecret(ctx context.Context, s *library.Secret) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableSecret). Delete(secret). Error diff --git a/database/secret/get.go b/database/secret/get.go index 6f5bc7bf9..4f4d04199 100644 --- a/database/secret/get.go +++ b/database/secret/get.go @@ -19,6 +19,7 @@ func (e *engine) GetSecret(ctx context.Context, id int64) (*library.Secret, erro // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Where("id = ?", id). Take(s). diff --git a/database/secret/get_org.go b/database/secret/get_org.go index 7d1766c88..473d5a1a6 100644 --- a/database/secret/get_org.go +++ b/database/secret/get_org.go @@ -25,6 +25,7 @@ func (e *engine) GetSecretForOrg(ctx context.Context, org, name string) (*librar // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretOrg). Where("org = ?", org). diff --git a/database/secret/get_repo.go b/database/secret/get_repo.go index 690823c08..27a5f3cfb 100644 --- a/database/secret/get_repo.go +++ b/database/secret/get_repo.go @@ -27,6 +27,7 @@ func (e *engine) GetSecretForRepo(ctx context.Context, name string, r *api.Repo) // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretRepo). Where("org = ?", r.GetOrg()). diff --git a/database/secret/get_team.go b/database/secret/get_team.go index e03845947..ee82409c6 100644 --- a/database/secret/get_team.go +++ b/database/secret/get_team.go @@ -26,6 +26,7 @@ func (e *engine) GetSecretForTeam(ctx context.Context, org, team, name string) ( // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretShared). Where("org = ?", org). diff --git a/database/secret/index.go b/database/secret/index.go index e045031bb..786cad40a 100644 --- a/database/secret/index.go +++ b/database/secret/index.go @@ -36,17 +36,23 @@ func (e *engine) CreateSecretIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for secrets table") // create the type, org and repo columns index for the secrets table - err := e.client.Exec(CreateTypeOrgRepo).Error + err := e.client. + WithContext(ctx). + Exec(CreateTypeOrgRepo).Error if err != nil { return err } // create the type, org and team columns index for the secrets table - err = e.client.Exec(CreateTypeOrgTeam).Error + err = e.client. + WithContext(ctx). + Exec(CreateTypeOrgTeam).Error if err != nil { return err } // create the type and org columns index for the secrets table - return e.client.Exec(CreateTypeOrg).Error + return e.client. + WithContext(ctx). + Exec(CreateTypeOrg).Error } diff --git a/database/secret/list.go b/database/secret/list.go index a61e2088f..fe2031f89 100644 --- a/database/secret/list.go +++ b/database/secret/list.go @@ -32,6 +32,7 @@ func (e *engine) ListSecrets(ctx context.Context) ([]*library.Secret, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableSecret). Find(&s). Error diff --git a/database/secret/list_org.go b/database/secret/list_org.go index d8c7ced5a..c75fa8f8d 100644 --- a/database/secret/list_org.go +++ b/database/secret/list_org.go @@ -42,6 +42,7 @@ func (e *engine) ListSecretsForOrg(ctx context.Context, org string, filters map[ // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretOrg). Where("org = ?", org). diff --git a/database/secret/list_repo.go b/database/secret/list_repo.go index ef2fe546c..593139869 100644 --- a/database/secret/list_repo.go +++ b/database/secret/list_repo.go @@ -44,6 +44,7 @@ func (e *engine) ListSecretsForRepo(ctx context.Context, r *api.Repo, filters ma // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretRepo). Where("org = ?", r.GetOrg()). diff --git a/database/secret/list_team.go b/database/secret/list_team.go index 93a5d6db1..2e2a5773c 100644 --- a/database/secret/list_team.go +++ b/database/secret/list_team.go @@ -44,6 +44,7 @@ func (e *engine) ListSecretsForTeam(ctx context.Context, org, team string, filte // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretShared). Where("org = ?", org). @@ -120,6 +121,7 @@ func (e *engine) ListSecretsForTeams(ctx context.Context, org string, teams []st // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableSecret). Where("type = ?", constants.SecretShared). Where("org = ?", org). diff --git a/database/secret/table.go b/database/secret/table.go index e696409c2..650a3f3f4 100644 --- a/database/secret/table.go +++ b/database/secret/table.go @@ -68,11 +68,15 @@ func (e *engine) CreateSecretTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the secrets table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the secrets table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/secret/update.go b/database/secret/update.go index 0bb713e47..c046d18ed 100644 --- a/database/secret/update.go +++ b/database/secret/update.go @@ -60,7 +60,10 @@ func (e *engine) UpdateSecret(ctx context.Context, s *library.Secret) (*library. } } - err = e.client.Table(constants.TableSecret).Save(secret.Nullify()).Error + err = e.client. + WithContext(ctx). + Table(constants.TableSecret). + Save(secret.Nullify()).Error if err != nil { return nil, err } diff --git a/database/service/clean.go b/database/service/clean.go index b4becb0ad..d234b4478 100644 --- a/database/service/clean.go +++ b/database/service/clean.go @@ -26,6 +26,7 @@ func (e *engine) CleanServices(ctx context.Context, msg string, before int64) (i // send query to the database result := e.client. + WithContext(ctx). Table(constants.TableService). Where("created < ?", before). Where("status = 'running' OR status = 'pending'"). diff --git a/database/service/count.go b/database/service/count.go index f8bee25b1..86e76cfe2 100644 --- a/database/service/count.go +++ b/database/service/count.go @@ -17,6 +17,7 @@ func (e *engine) CountServices(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableService). Count(&s). Error diff --git a/database/service/count_build.go b/database/service/count_build.go index 1e84819a2..0f00403e2 100644 --- a/database/service/count_build.go +++ b/database/service/count_build.go @@ -22,6 +22,7 @@ func (e *engine) CountServicesForBuild(ctx context.Context, b *api.Build, filter // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableService). Where("build_id = ?", b.GetID()). Where(filters). diff --git a/database/service/create.go b/database/service/create.go index 6d2ec8f74..3ecb96e04 100644 --- a/database/service/create.go +++ b/database/service/create.go @@ -32,7 +32,10 @@ func (e *engine) CreateService(ctx context.Context, s *library.Service) (*librar } // send query to the database - result := e.client.Table(constants.TableService).Create(service) + result := e.client. + WithContext(ctx). + Table(constants.TableService). + Create(service) return service.ToLibrary(), result.Error } diff --git a/database/service/delete.go b/database/service/delete.go index 3f0bd331e..835a25992 100644 --- a/database/service/delete.go +++ b/database/service/delete.go @@ -25,6 +25,7 @@ func (e *engine) DeleteService(ctx context.Context, s *library.Service) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableService). Delete(service). Error diff --git a/database/service/get.go b/database/service/get.go index eee921123..37cf9a5d0 100644 --- a/database/service/get.go +++ b/database/service/get.go @@ -19,6 +19,7 @@ func (e *engine) GetService(ctx context.Context, id int64) (*library.Service, er // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableService). Where("id = ?", id). Take(s). diff --git a/database/service/get_build.go b/database/service/get_build.go index 81a95e0bc..e85ddc368 100644 --- a/database/service/get_build.go +++ b/database/service/get_build.go @@ -25,6 +25,7 @@ func (e *engine) GetServiceForBuild(ctx context.Context, b *api.Build, number in // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableService). Where("build_id = ?", b.GetID()). Where("number = ?", number). diff --git a/database/service/list.go b/database/service/list.go index 656beee10..6dd70fdd1 100644 --- a/database/service/list.go +++ b/database/service/list.go @@ -32,6 +32,7 @@ func (e *engine) ListServices(ctx context.Context) ([]*library.Service, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableService). Find(&w). Error diff --git a/database/service/list_build.go b/database/service/list_build.go index 047aba669..faf8c81fb 100644 --- a/database/service/list_build.go +++ b/database/service/list_build.go @@ -40,6 +40,7 @@ func (e *engine) ListServicesForBuild(ctx context.Context, b *api.Build, filters // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableService). Where("build_id = ?", b.GetID()). Where(filters). diff --git a/database/service/list_image.go b/database/service/list_image.go index a94447e6c..dbff3913c 100644 --- a/database/service/list_image.go +++ b/database/service/list_image.go @@ -22,6 +22,7 @@ func (e *engine) ListServiceImageCount(ctx context.Context) (map[string]float64, // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableService). Select("image", " count(image) as count"). Group("image"). diff --git a/database/service/list_status.go b/database/service/list_status.go index 9e5f106fd..b2570b55d 100644 --- a/database/service/list_status.go +++ b/database/service/list_status.go @@ -28,6 +28,7 @@ func (e *engine) ListServiceStatusCount(ctx context.Context) (map[string]float64 // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableService). Select("status", " count(status) as count"). Group("status"). diff --git a/database/service/table.go b/database/service/table.go index 56e78b249..426eb2333 100644 --- a/database/service/table.go +++ b/database/service/table.go @@ -66,11 +66,15 @@ func (e *engine) CreateServiceTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the services table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the services table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/service/update.go b/database/service/update.go index 6eb43a7e2..a53b9ae41 100644 --- a/database/service/update.go +++ b/database/service/update.go @@ -32,7 +32,10 @@ func (e *engine) UpdateService(ctx context.Context, s *library.Service) (*librar } // send query to the database - result := e.client.Table(constants.TableService).Save(service) + result := e.client. + WithContext(ctx). + Table(constants.TableService). + Save(service) return service.ToLibrary(), result.Error } diff --git a/database/settings/create.go b/database/settings/create.go index 8c5747320..1879483b1 100644 --- a/database/settings/create.go +++ b/database/settings/create.go @@ -10,7 +10,7 @@ import ( ) // CreateSettings creates a platform settings record in the database. -func (e *engine) CreateSettings(_ context.Context, s *settings.Platform) (*settings.Platform, error) { +func (e *engine) CreateSettings(ctx context.Context, s *settings.Platform) (*settings.Platform, error) { e.logger.Tracef("creating platform settings with %v", s.String()) // cast the api type to database type @@ -23,7 +23,10 @@ func (e *engine) CreateSettings(_ context.Context, s *settings.Platform) (*setti } // send query to the database - err = e.client.Table(TableSettings).Create(settings.Nullify()).Error + err = e.client. + WithContext(ctx). + Table(TableSettings). + Create(settings.Nullify()).Error if err != nil { return nil, err } diff --git a/database/settings/create_test.go b/database/settings/create_test.go index e621bf76e..c31df0847 100644 --- a/database/settings/create_test.go +++ b/database/settings/create_test.go @@ -14,7 +14,7 @@ func TestSettings_Engine_CreateSettings(t *testing.T) { // setup types _settings := testSettings() _settings.SetID(1) - _settings.SetCloneImage("target/vela-git:latest") + _settings.SetCloneImage("target/vela-git-slim:latest") _settings.SetTemplateDepth(10) _settings.SetStarlarkExecLimit(100) _settings.SetRoutes([]string{"vela"}) @@ -32,7 +32,7 @@ func TestSettings_Engine_CreateSettings(t *testing.T) { // ensure the mock expects the query _mock.ExpectQuery(`INSERT INTO "settings" ("compiler","queue","repo_allowlist","schedule_allowlist","created_at","updated_at","updated_by","id") VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING "id"`). - WithArgs(`{"clone_image":{"String":"target/vela-git:latest","Valid":true},"template_depth":{"Int64":10,"Valid":true},"starlark_exec_limit":{"Int64":100,"Valid":true}}`, + WithArgs(`{"clone_image":{"String":"target/vela-git-slim:latest","Valid":true},"template_depth":{"Int64":10,"Valid":true},"starlark_exec_limit":{"Int64":100,"Valid":true}}`, `{"routes":["vela"]}`, `{"octocat/hello-world"}`, `{"*"}`, 1, 1, ``, 1). WillReturnRows(_rows) diff --git a/database/settings/get.go b/database/settings/get.go index 96bc19ef1..160faf6a4 100644 --- a/database/settings/get.go +++ b/database/settings/get.go @@ -18,6 +18,7 @@ func (e *engine) GetSettings(ctx context.Context) (*settings.Platform, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(TableSettings). Where("id = ?", 1). Take(s). diff --git a/database/settings/get_test.go b/database/settings/get_test.go index 83196848b..40d64714a 100644 --- a/database/settings/get_test.go +++ b/database/settings/get_test.go @@ -16,7 +16,7 @@ func TestSettings_Engine_GetSettings(t *testing.T) { // setup types _settings := testSettings() _settings.SetID(1) - _settings.SetCloneImage("target/vela-git:latest") + _settings.SetCloneImage("target/vela-git-slim:latest") _settings.SetTemplateDepth(10) _settings.SetStarlarkExecLimit(100) _settings.SetRoutes([]string{"vela"}) @@ -32,7 +32,7 @@ func TestSettings_Engine_GetSettings(t *testing.T) { // create expected result in mock _rows := sqlmock.NewRows( []string{"id", "compiler", "queue", "repo_allowlist", "schedule_allowlist", "created_at", "updated_at", "updated_by"}). - AddRow(1, `{"clone_image":{"String":"target/vela-git:latest","Valid":true},"template_depth":{"Int64":10,"Valid":true},"starlark_exec_limit":{"Int64":100,"Valid":true}}`, + AddRow(1, `{"clone_image":{"String":"target/vela-git-slim:latest","Valid":true},"template_depth":{"Int64":10,"Valid":true},"starlark_exec_limit":{"Int64":100,"Valid":true}}`, `{"routes":["vela"]}`, `{"octocat/hello-world"}`, `{"*"}`, 1, 1, `octocat`) // ensure the mock expects the query diff --git a/database/settings/table.go b/database/settings/table.go index 18a4207ab..3eba03cfe 100644 --- a/database/settings/table.go +++ b/database/settings/table.go @@ -43,18 +43,22 @@ settings ( ) // CreateSettingsTable creates the settings table in the database. -func (e *engine) CreateSettingsTable(_ context.Context, driver string) error { +func (e *engine) CreateSettingsTable(ctx context.Context, driver string) error { e.logger.Tracef("creating settings table") // handle the driver provided to create the table switch driver { case constants.DriverPostgres: // create the steps table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the steps table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/settings/update.go b/database/settings/update.go index c8a3e1679..e4e2f7f55 100644 --- a/database/settings/update.go +++ b/database/settings/update.go @@ -10,7 +10,7 @@ import ( ) // UpdateSettings updates a platform settings in the database. -func (e *engine) UpdateSettings(_ context.Context, s *settings.Platform) (*settings.Platform, error) { +func (e *engine) UpdateSettings(ctx context.Context, s *settings.Platform) (*settings.Platform, error) { e.logger.Trace("updating platform settings in the database") // cast the api type to database type @@ -23,7 +23,10 @@ func (e *engine) UpdateSettings(_ context.Context, s *settings.Platform) (*setti } // send query to the database - err = e.client.Table(TableSettings).Save(dbS.Nullify()).Error + err = e.client. + WithContext(ctx). + Table(TableSettings). + Save(dbS.Nullify()).Error if err != nil { return nil, err } diff --git a/database/settings/update_test.go b/database/settings/update_test.go index 7b9f31a38..b5ee38253 100644 --- a/database/settings/update_test.go +++ b/database/settings/update_test.go @@ -16,7 +16,7 @@ func TestSettings_Engine_UpdateSettings(t *testing.T) { // setup types _settings := testSettings() _settings.SetID(1) - _settings.SetCloneImage("target/vela-git:latest") + _settings.SetCloneImage("target/vela-git-slim:latest") _settings.SetTemplateDepth(10) _settings.SetStarlarkExecLimit(100) _settings.SetRoutes([]string{"vela", "large"}) @@ -31,7 +31,7 @@ func TestSettings_Engine_UpdateSettings(t *testing.T) { // ensure the mock expects the query _mock.ExpectExec(`UPDATE "settings" SET "compiler"=$1,"queue"=$2,"repo_allowlist"=$3,"schedule_allowlist"=$4,"created_at"=$5,"updated_at"=$6,"updated_by"=$7 WHERE "id" = $8`). - WithArgs(`{"clone_image":{"String":"target/vela-git:latest","Valid":true},"template_depth":{"Int64":10,"Valid":true},"starlark_exec_limit":{"Int64":100,"Valid":true}}`, + WithArgs(`{"clone_image":{"String":"target/vela-git-slim:latest","Valid":true},"template_depth":{"Int64":10,"Valid":true},"starlark_exec_limit":{"Int64":100,"Valid":true}}`, `{"routes":["vela","large"]}`, `{"octocat/hello-world"}`, `{"*"}`, 1, testutils.AnyArgument{}, "octocat", 1). WillReturnResult(sqlmock.NewResult(1, 1)) diff --git a/database/step/clean.go b/database/step/clean.go index 3e75c906b..b7d8cb8f6 100644 --- a/database/step/clean.go +++ b/database/step/clean.go @@ -26,6 +26,7 @@ func (e *engine) CleanSteps(ctx context.Context, msg string, before int64) (int6 // send query to the database result := e.client. + WithContext(ctx). Table(constants.TableStep). Where("created < ?", before). Where("status = 'running' OR status = 'pending'"). diff --git a/database/step/count.go b/database/step/count.go index d5c64ba34..bb9f0ce3a 100644 --- a/database/step/count.go +++ b/database/step/count.go @@ -17,6 +17,7 @@ func (e *engine) CountSteps(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableStep). Count(&s). Error diff --git a/database/step/count_build.go b/database/step/count_build.go index 6b4d63fc4..4a0cefda5 100644 --- a/database/step/count_build.go +++ b/database/step/count_build.go @@ -22,6 +22,7 @@ func (e *engine) CountStepsForBuild(ctx context.Context, b *api.Build, filters m // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableStep). Where("build_id = ?", b.GetID()). Where(filters). diff --git a/database/step/create.go b/database/step/create.go index 99c9987b4..9e46cf146 100644 --- a/database/step/create.go +++ b/database/step/create.go @@ -32,7 +32,10 @@ func (e *engine) CreateStep(ctx context.Context, s *library.Step) (*library.Step } // send query to the database - result := e.client.Table(constants.TableStep).Create(step) + result := e.client. + WithContext(ctx). + Table(constants.TableStep). + Create(step) return step.ToLibrary(), result.Error } diff --git a/database/step/delete.go b/database/step/delete.go index dfd2d5bfa..7c9139f6b 100644 --- a/database/step/delete.go +++ b/database/step/delete.go @@ -25,6 +25,7 @@ func (e *engine) DeleteStep(ctx context.Context, s *library.Step) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableStep). Delete(step). Error diff --git a/database/step/get.go b/database/step/get.go index 7a4e18041..a05a56fce 100644 --- a/database/step/get.go +++ b/database/step/get.go @@ -19,6 +19,7 @@ func (e *engine) GetStep(ctx context.Context, id int64) (*library.Step, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableStep). Where("id = ?", id). Take(s). diff --git a/database/step/get_build.go b/database/step/get_build.go index 5d32ba582..de847a769 100644 --- a/database/step/get_build.go +++ b/database/step/get_build.go @@ -25,6 +25,7 @@ func (e *engine) GetStepForBuild(ctx context.Context, b *api.Build, number int) // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableStep). Where("build_id = ?", b.GetID()). Where("number = ?", number). diff --git a/database/step/list.go b/database/step/list.go index 0d53297bd..9eb72996c 100644 --- a/database/step/list.go +++ b/database/step/list.go @@ -32,6 +32,7 @@ func (e *engine) ListSteps(ctx context.Context) ([]*library.Step, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableStep). Find(&w). Error diff --git a/database/step/list_build.go b/database/step/list_build.go index ac0a608d1..1ba16fdb0 100644 --- a/database/step/list_build.go +++ b/database/step/list_build.go @@ -40,6 +40,7 @@ func (e *engine) ListStepsForBuild(ctx context.Context, b *api.Build, filters ma // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableStep). Where("build_id = ?", b.GetID()). Where(filters). diff --git a/database/step/list_image.go b/database/step/list_image.go index 5b0f46aec..c485ec741 100644 --- a/database/step/list_image.go +++ b/database/step/list_image.go @@ -22,6 +22,7 @@ func (e *engine) ListStepImageCount(ctx context.Context) (map[string]float64, er // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableStep). Select("image", " count(image) as count"). Group("image"). diff --git a/database/step/list_status.go b/database/step/list_status.go index c74b8db3c..75c19a805 100644 --- a/database/step/list_status.go +++ b/database/step/list_status.go @@ -28,6 +28,7 @@ func (e *engine) ListStepStatusCount(ctx context.Context) (map[string]float64, e // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableStep). Select("status", " count(status) as count"). Group("status"). diff --git a/database/step/table.go b/database/step/table.go index a7cd7eb74..c7560326d 100644 --- a/database/step/table.go +++ b/database/step/table.go @@ -70,11 +70,15 @@ func (e *engine) CreateStepTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the steps table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the steps table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/step/update.go b/database/step/update.go index 57fee9e74..d467a2b65 100644 --- a/database/step/update.go +++ b/database/step/update.go @@ -32,7 +32,10 @@ func (e *engine) UpdateStep(ctx context.Context, s *library.Step) (*library.Step } // send query to the database - result := e.client.Table(constants.TableStep).Save(step) + result := e.client. + WithContext(ctx). + Table(constants.TableStep). + Save(step) return step.ToLibrary(), result.Error } diff --git a/database/types/repo.go b/database/types/repo.go index e3780f851..584d12923 100644 --- a/database/types/repo.go +++ b/database/types/repo.go @@ -95,9 +95,15 @@ func (r *Repo) Decrypt(key string) error { } // decrypt owner - err = r.Owner.Decrypt(key) - if err != nil { - return err + // Note: In UpdateRepo() (database/repo/update.go), the incoming API repo object + // is cast to a database repo object. The owner object isn't set in this process + // resulting in a zero value for the owner object. A check is performed here + // before decrypting to prevent "unable to decrypt repo..." errors. + if r.Owner.ID.Valid { + err = r.Owner.Decrypt(key) + if err != nil { + return err + } } return nil diff --git a/database/types/settings_test.go b/database/types/settings_test.go index 40285eb34..45b6b3c5e 100644 --- a/database/types/settings_test.go +++ b/database/types/settings_test.go @@ -58,7 +58,7 @@ func TestTypes_Platform_ToAPI(t *testing.T) { want.SetUpdatedBy("") want.Compiler = new(api.Compiler) - want.SetCloneImage("target/vela-git:latest") + want.SetCloneImage("target/vela-git-slim:latest") want.SetTemplateDepth(10) want.SetStarlarkExecLimit(100) @@ -98,7 +98,7 @@ func TestTypes_Platform_Validate(t *testing.T) { settings: &Platform{ ID: sql.NullInt64{Int64: 1, Valid: true}, Compiler: Compiler{ - CloneImage: sql.NullString{String: "target/vela-git:latest", Valid: true}, + CloneImage: sql.NullString{String: "target/vela-git-slim:latest", Valid: true}, StarlarkExecLimit: sql.NullInt64{Int64: 100, Valid: true}, }, }, @@ -108,7 +108,7 @@ func TestTypes_Platform_Validate(t *testing.T) { settings: &Platform{ ID: sql.NullInt64{Int64: 1, Valid: true}, Compiler: Compiler{ - CloneImage: sql.NullString{String: "target/vela-git:latest", Valid: true}, + CloneImage: sql.NullString{String: "target/vela-git-slim:latest", Valid: true}, TemplateDepth: sql.NullInt64{Int64: 10, Valid: true}, }, }, @@ -118,7 +118,7 @@ func TestTypes_Platform_Validate(t *testing.T) { settings: &Platform{ ID: sql.NullInt64{Int64: 1, Valid: true}, Compiler: Compiler{ - CloneImage: sql.NullString{String: "target/vela-git:latest", Valid: true}, + CloneImage: sql.NullString{String: "target/vela-git-slim:latest", Valid: true}, TemplateDepth: sql.NullInt64{Int64: 10, Valid: true}, StarlarkExecLimit: sql.NullInt64{Int64: 100, Valid: true}, }, @@ -156,7 +156,7 @@ func TestTypes_Platform_PlatformFromAPI(t *testing.T) { s.SetUpdatedBy("") s.Compiler = new(api.Compiler) - s.SetCloneImage("target/vela-git:latest") + s.SetCloneImage("target/vela-git-slim:latest") s.SetTemplateDepth(10) s.SetStarlarkExecLimit(100) @@ -179,7 +179,7 @@ func testPlatform() *Platform { return &Platform{ ID: sql.NullInt64{Int64: 1, Valid: true}, Compiler: Compiler{ - CloneImage: sql.NullString{String: "target/vela-git:latest", Valid: true}, + CloneImage: sql.NullString{String: "target/vela-git-slim:latest", Valid: true}, TemplateDepth: sql.NullInt64{Int64: 10, Valid: true}, StarlarkExecLimit: sql.NullInt64{Int64: 100, Valid: true}, }, diff --git a/database/user/count.go b/database/user/count.go index c48fa00ed..b6b686fba 100644 --- a/database/user/count.go +++ b/database/user/count.go @@ -17,6 +17,7 @@ func (e *engine) CountUsers(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableUser). Count(&u). Error diff --git a/database/user/create.go b/database/user/create.go index e9cf0f2c6..c778349c4 100644 --- a/database/user/create.go +++ b/database/user/create.go @@ -40,7 +40,10 @@ func (e *engine) CreateUser(ctx context.Context, u *api.User) (*api.User, error) } // send query to the database - result := e.client.Table(constants.TableUser).Create(user) + result := e.client. + WithContext(ctx). + Table(constants.TableUser). + Create(user) // decrypt fields to return user err = user.Decrypt(e.config.EncryptionKey) diff --git a/database/user/delete.go b/database/user/delete.go index 3154461e2..28547c07c 100644 --- a/database/user/delete.go +++ b/database/user/delete.go @@ -23,6 +23,7 @@ func (e *engine) DeleteUser(ctx context.Context, u *api.User) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableUser). Delete(user). Error diff --git a/database/user/get.go b/database/user/get.go index a2f7d8468..30901c2ff 100644 --- a/database/user/get.go +++ b/database/user/get.go @@ -19,6 +19,7 @@ func (e *engine) GetUser(ctx context.Context, id int64) (*api.User, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableUser). Where("id = ?", id). Take(u). diff --git a/database/user/get_name.go b/database/user/get_name.go index 8d3633eb8..fb9a90090 100644 --- a/database/user/get_name.go +++ b/database/user/get_name.go @@ -23,6 +23,7 @@ func (e *engine) GetUserForName(ctx context.Context, name string) (*api.User, er // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableUser). Where("name = ?", name). Take(u). diff --git a/database/user/index.go b/database/user/index.go index d35e1accd..37dc9476e 100644 --- a/database/user/index.go +++ b/database/user/index.go @@ -20,5 +20,7 @@ func (e *engine) CreateUserIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for users table") // create the refresh_token column index for the users table - return e.client.Exec(CreateUserRefreshIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateUserRefreshIndex).Error } diff --git a/database/user/list.go b/database/user/list.go index 80c7f5681..035699489 100644 --- a/database/user/list.go +++ b/database/user/list.go @@ -32,6 +32,7 @@ func (e *engine) ListUsers(ctx context.Context) ([]*api.User, error) { // send query to the database and store result in variable err = e.client. + WithContext(ctx). Table(constants.TableUser). Find(&u). Error diff --git a/database/user/list_lite.go b/database/user/list_lite.go index 1a5c570f9..c961fe483 100644 --- a/database/user/list_lite.go +++ b/database/user/list_lite.go @@ -36,6 +36,7 @@ func (e *engine) ListLiteUsers(ctx context.Context, page, perPage int) ([]*api.U offset := perPage * (page - 1) err = e.client. + WithContext(ctx). Table(constants.TableUser). Select("id", "name"). Limit(perPage). diff --git a/database/user/table.go b/database/user/table.go index 183390b43..de1f964a7 100644 --- a/database/user/table.go +++ b/database/user/table.go @@ -52,11 +52,15 @@ func (e *engine) CreateUserTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the users table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the users table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/user/update.go b/database/user/update.go index 19f653f8d..d860e3293 100644 --- a/database/user/update.go +++ b/database/user/update.go @@ -36,7 +36,10 @@ func (e *engine) UpdateUser(ctx context.Context, u *api.User) (*api.User, error) } // send query to the database - result := e.client.Table(constants.TableUser).Save(user) + result := e.client. + WithContext(ctx). + Table(constants.TableUser). + Save(user) // decrypt fields to return user err = user.Decrypt(e.config.EncryptionKey) diff --git a/database/worker/count.go b/database/worker/count.go index 0d992cdca..6c440637c 100644 --- a/database/worker/count.go +++ b/database/worker/count.go @@ -17,6 +17,7 @@ func (e *engine) CountWorkers(ctx context.Context) (int64, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableWorker). Count(&w). Error diff --git a/database/worker/create.go b/database/worker/create.go index 445669deb..180ee7ef6 100644 --- a/database/worker/create.go +++ b/database/worker/create.go @@ -32,7 +32,10 @@ func (e *engine) CreateWorker(ctx context.Context, w *api.Worker) (*api.Worker, } // send query to the database - result := e.client.Table(constants.TableWorker).Create(worker) + result := e.client. + WithContext(ctx). + Table(constants.TableWorker). + Create(worker) return worker.ToAPI(w.GetRunningBuilds()), result.Error } diff --git a/database/worker/delete.go b/database/worker/delete.go index 8df8ff5e0..252dd7831 100644 --- a/database/worker/delete.go +++ b/database/worker/delete.go @@ -25,6 +25,7 @@ func (e *engine) DeleteWorker(ctx context.Context, w *api.Worker) error { // send query to the database return e.client. + WithContext(ctx). Table(constants.TableWorker). Delete(worker). Error diff --git a/database/worker/get.go b/database/worker/get.go index cde920413..47be938a1 100644 --- a/database/worker/get.go +++ b/database/worker/get.go @@ -19,6 +19,7 @@ func (e *engine) GetWorker(ctx context.Context, id int64) (*api.Worker, error) { // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableWorker). Where("id = ?", id). Take(w). diff --git a/database/worker/get_hostname.go b/database/worker/get_hostname.go index 1646e262e..d1150f80d 100644 --- a/database/worker/get_hostname.go +++ b/database/worker/get_hostname.go @@ -23,6 +23,7 @@ func (e *engine) GetWorkerForHostname(ctx context.Context, hostname string) (*ap // send query to the database and store result in variable err := e.client. + WithContext(ctx). Table(constants.TableWorker). Where("hostname = ?", hostname). Take(w). diff --git a/database/worker/index.go b/database/worker/index.go index 65fe7326f..6e1fc58e3 100644 --- a/database/worker/index.go +++ b/database/worker/index.go @@ -20,5 +20,7 @@ func (e *engine) CreateWorkerIndexes(ctx context.Context) error { e.logger.Tracef("creating indexes for workers table") // create the hostname and address columns index for the workers table - return e.client.Exec(CreateHostnameAddressIndex).Error + return e.client. + WithContext(ctx). + Exec(CreateHostnameAddressIndex).Error } diff --git a/database/worker/list.go b/database/worker/list.go index e2af759dd..34fc2ef79 100644 --- a/database/worker/list.go +++ b/database/worker/list.go @@ -21,7 +21,9 @@ func (e *engine) ListWorkers(ctx context.Context, active string, before, after i workers := []*api.Worker{} // build query with checked in constraints - query := e.client.Table(constants.TableWorker). + query := e.client. + WithContext(ctx). + Table(constants.TableWorker). Where("last_checked_in < ?", before). Where("last_checked_in > ?", after) diff --git a/database/worker/table.go b/database/worker/table.go index 403dd5ad3..921734f38 100644 --- a/database/worker/table.go +++ b/database/worker/table.go @@ -59,11 +59,15 @@ func (e *engine) CreateWorkerTable(ctx context.Context, driver string) error { switch driver { case constants.DriverPostgres: // create the workers table for Postgres - return e.client.Exec(CreatePostgresTable).Error + return e.client. + WithContext(ctx). + Exec(CreatePostgresTable).Error case constants.DriverSqlite: fallthrough default: // create the workers table for Sqlite - return e.client.Exec(CreateSqliteTable).Error + return e.client. + WithContext(ctx). + Exec(CreateSqliteTable).Error } } diff --git a/database/worker/update.go b/database/worker/update.go index 872d9c82d..49c78e420 100644 --- a/database/worker/update.go +++ b/database/worker/update.go @@ -32,7 +32,10 @@ func (e *engine) UpdateWorker(ctx context.Context, w *api.Worker) (*api.Worker, } // send query to the database - result := e.client.Table(constants.TableWorker).Save(worker) + result := e.client. + WithContext(ctx). + Table(constants.TableWorker). + Save(worker) return worker.ToAPI(w.GetRunningBuilds()), result.Error } diff --git a/go.mod b/go.mod index be3b818f3..8dd81bf8e 100644 --- a/go.mod +++ b/go.mod @@ -12,13 +12,12 @@ require ( github.com/adhocore/gronx v1.8.1 github.com/alicebob/miniredis/v2 v2.33.0 github.com/aws/aws-sdk-go v1.54.20 - github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3 github.com/distribution/reference v0.6.0 github.com/drone/envsubst v1.0.3 github.com/ghodss/yaml v1.0.0 github.com/gin-gonic/gin v1.10.0 github.com/go-playground/assert/v2 v2.2.0 - github.com/go-vela/types v0.24.1-0.20240801155449-d47730bc9734 + github.com/go-vela/types v0.24.1-0.20240826141537-76a66e72d5dc github.com/golang-jwt/jwt/v5 v5.2.1 github.com/google/go-cmp v0.6.0 github.com/google/go-github/v63 v63.0.0 @@ -42,6 +41,7 @@ require ( golang.org/x/crypto v0.25.0 golang.org/x/oauth2 v0.21.0 golang.org/x/sync v0.7.0 + gopkg.in/yaml.v3 v3.0.1 gorm.io/driver/postgres v1.5.9 gorm.io/driver/sqlite v1.5.6 gorm.io/gorm v1.25.11 @@ -130,7 +130,6 @@ require ( golang.org/x/time v0.5.0 // indirect google.golang.org/protobuf v1.34.1 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/klog/v2 v2.120.1 // indirect k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect ) diff --git a/go.sum b/go.sum index 47fd1a65a..1f2a95401 100644 --- a/go.sum +++ b/go.sum @@ -35,8 +35,6 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= -github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3 h1:q+sMKdA6L8LyGVudTkpGoC73h6ak2iWSPFiFo/pFOU8= -github.com/buildkite/yaml v0.0.0-20181016232759-0caa5f0796e3/go.mod h1:5hCug3EZaHXU3FdCA3gJm0YTNi+V+ooA2qNTiVpky4A= github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0= github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4= github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM= @@ -91,8 +89,8 @@ github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBEx github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw= github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= -github.com/go-vela/types v0.24.1-0.20240801155449-d47730bc9734 h1:+eOrT2tPVqjXZRIAPcG0lyYyiCbGq45lQUlgjNpdv8g= -github.com/go-vela/types v0.24.1-0.20240801155449-d47730bc9734/go.mod h1:YWj6BIapl9Kbj4yHq/fp8jltXdGiwD/gTy1ez32Rzag= +github.com/go-vela/types v0.24.1-0.20240826141537-76a66e72d5dc h1:VyT2tBwPVO9fMmn+22TC4rY4dp+d71To/Qo5Vk4cOSo= +github.com/go-vela/types v0.24.1-0.20240826141537-76a66e72d5dc/go.mod h1:WcSiFm8cWuErRw4aI08NrfM+SZzidnbUs2fmO5klFKo= github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= diff --git a/internal/token/mint.go b/internal/token/mint.go index c86757b07..4ab6ed4f7 100644 --- a/internal/token/mint.go +++ b/internal/token/mint.go @@ -6,6 +6,7 @@ import ( "context" "errors" "fmt" + "strings" "time" "github.com/golang-jwt/jwt/v5" @@ -120,7 +121,7 @@ func (tm *Manager) MintToken(mto *MintTokenOpts) (string, error) { tk := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) - //sign token with configured private signing key + // sign token with configured private signing key token, err := tk.SignedString([]byte(tm.PrivateKeyHMAC)) if err != nil { return "", fmt.Errorf("unable to sign token: %w", err) @@ -134,6 +135,8 @@ func (tm *Manager) MintIDToken(ctx context.Context, mto *MintTokenOpts, db datab // initialize claims struct var claims = new(api.OpenIDClaims) + var err error + // validate provided claims if len(mto.Repo) == 0 { return "", errors.New("missing repo for ID token") @@ -154,6 +157,7 @@ func (tm *Manager) MintIDToken(ctx context.Context, mto *MintTokenOpts, db datab // set claims based on input claims.Actor = mto.Build.GetSender() claims.ActorSCMID = mto.Build.GetSenderSCMID() + claims.Branch = mto.Build.GetBranch() claims.BuildNumber = mto.Build.GetNumber() claims.BuildID = mto.Build.GetID() claims.Repo = mto.Repo @@ -164,6 +168,12 @@ func (tm *Manager) MintIDToken(ctx context.Context, mto *MintTokenOpts, db datab claims.Audience = mto.Audience claims.TokenType = mto.TokenType claims.Image = mto.Image + + claims.ImageName, claims.ImageTag, err = imageParse(mto.Image) + if err != nil { + return "", err + } + claims.Request = mto.Request claims.Commands = mto.Commands @@ -175,7 +185,7 @@ func (tm *Manager) MintIDToken(ctx context.Context, mto *MintTokenOpts, db datab tk := jwt.NewWithClaims(jwt.SigningMethodRS256, claims) // verify key is active in the database before signing - _, err := db.GetActiveJWK(ctx, tm.RSAKeySet.KID) + _, err = db.GetActiveJWK(ctx, tm.RSAKeySet.KID) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return "", fmt.Errorf("unable to get active public key: %w", err) @@ -191,7 +201,7 @@ func (tm *Manager) MintIDToken(ctx context.Context, mto *MintTokenOpts, db datab // set KID header tk.Header["kid"] = tm.RSAKeySet.KID - //sign token with configured private signing key + // sign token with configured private signing key token, err := tk.SignedString(tm.RSAKeySet.PrivateKey) if err != nil { return "", fmt.Errorf("unable to sign token: %w", err) @@ -201,3 +211,19 @@ func (tm *Manager) MintIDToken(ctx context.Context, mto *MintTokenOpts, db datab return token, nil } + +// imageParse parses the given image string and returns the image name and tag. +// If no tag is provided in the image string, "latest" is used as the tag. +// If the image string is invalid, an error is returned. +func imageParse(image string) (string, string, error) { + parts := strings.Split(image, ":") + + switch len(parts) { + case 1: + return image, "latest", nil + case 2: + return parts[0], parts[1], nil + default: + return "", "", fmt.Errorf("invalid image format: %s", image) + } +} diff --git a/internal/token/mint_test.go b/internal/token/mint_test.go new file mode 100644 index 000000000..7ec01bc34 --- /dev/null +++ b/internal/token/mint_test.go @@ -0,0 +1,61 @@ +// SPDX-License-Identifier: Apache-2.0 + +package token + +import "testing" + +func Test_imageParse(t *testing.T) { + type args struct { + image string + } + tests := []struct { + name string + args args + wantName string + wantTag string + wantErr bool + }{ + { + name: "image with tag", + args: args{ + image: "alpine:1.20", + }, + wantName: "alpine", + wantTag: "1.20", + wantErr: false, + }, + { + name: "image without latest tag", + args: args{ + image: "alpine:latest", + }, + wantName: "alpine", + wantTag: "latest", + wantErr: false, + }, + { + name: "image without tag", + args: args{ + image: "alpine", + }, + wantName: "alpine", + wantTag: "latest", + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, got1, err := imageParse(tt.args.image) + if (err != nil) != tt.wantErr { + t.Errorf("imageParse() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got != tt.wantName { + t.Errorf("imageParse() got = %v, wantName %v", got, tt.wantName) + } + if got1 != tt.wantTag { + t.Errorf("imageParse() got1 = %v, wantName %v", got1, tt.wantTag) + } + }) + } +} diff --git a/mock/server/authentication.go b/mock/server/authentication.go index b1903eeb9..1d8ed2147 100644 --- a/mock/server/authentication.go +++ b/mock/server/authentication.go @@ -32,6 +32,7 @@ const ( "iat", "iss", "aud", + "branch", "build_number", "build_id", "repo", @@ -40,6 +41,8 @@ const ( "actor_scm_id", "commands", "image", + "image_name", + "image_tag", "request" ], "id_token_signing_alg_values_supported": [ diff --git a/mock/server/pipeline.go b/mock/server/pipeline.go index 0d90cf45d..aaa06daf7 100644 --- a/mock/server/pipeline.go +++ b/mock/server/pipeline.go @@ -8,8 +8,8 @@ import ( "net/http" "strings" - yml "github.com/buildkite/yaml" "github.com/gin-gonic/gin" + yml "gopkg.in/yaml.v3" "github.com/go-vela/types" "github.com/go-vela/types/library" diff --git a/mock/server/rotate_keys.go b/mock/server/rotate_keys.go new file mode 100644 index 000000000..3623f4c12 --- /dev/null +++ b/mock/server/rotate_keys.go @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: Apache-2.0 + +package server + +import ( + "net/http" + "strings" + + "github.com/gin-gonic/gin" + + "github.com/go-vela/server/router/middleware/auth" + "github.com/go-vela/types" +) + +// rotateKeys returns success message. Pass `invalid` to auth header to test 401 error. +func rotateKeys(c *gin.Context) { + tkn, _ := auth.RetrieveAccessToken(c.Request) + + if strings.EqualFold(tkn, "invalid") { + data := "unauthorized" + c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &data}) + + return + } + + c.JSON(http.StatusOK, "keys rotated successfully") +} diff --git a/mock/server/server.go b/mock/server/server.go index 4b249cec3..62cdc47fc 100644 --- a/mock/server/server.go +++ b/mock/server/server.go @@ -125,6 +125,8 @@ func FakeHandler() http.Handler { e.POST("/api/v1/users", addUser) e.PUT("/api/v1/users/:user", updateUser) e.DELETE("/api/v1/users/:user", removeUser) + e.GET("/api/v1/user", currentUser) + e.PUT("/api/v1/user", currentUser) // mock endpoints for worker calls e.GET("/api/v1/workers", getWorkers) @@ -151,6 +153,7 @@ func FakeHandler() http.Handler { // mock endpoints for oidc calls e.GET("/_services/token/.well-known/openid-configuration", openIDConfig) e.GET("/_services/token/.well-known/jwks", getJWKS) + e.POST("/api/v1/admin/rotate_oidc_keys", rotateKeys) // mock endpoint for queue credentials e.GET("/api/v1/queue/info", getQueueCreds) diff --git a/mock/server/settings.go b/mock/server/settings.go index 07e5c9419..05d6f6368 100644 --- a/mock/server/settings.go +++ b/mock/server/settings.go @@ -17,7 +17,7 @@ const ( { "id": 1, "compiler": { - "clone_image": "target/vela-git", + "clone_image": "target/vela-git-slim", "template_depth": 3, "starlark_exec_limit": 100 }, @@ -42,7 +42,7 @@ const ( { "id": 1, "compiler": { - "clone_image": "target/vela-git:latest", + "clone_image": "target/vela-git-slim:latest", "template_depth": 5, "starlark_exec_limit": 123 }, @@ -67,7 +67,7 @@ const ( { "id": 1, "compiler": { - "clone_image": "target/vela-git:latest", + "clone_image": "target/vela-git-slim:latest", "template_depth": 5, "starlark_exec_limit": 123 }, diff --git a/mock/server/user.go b/mock/server/user.go index 54fbedd64..9df1ef087 100644 --- a/mock/server/user.go +++ b/mock/server/user.go @@ -1,6 +1,5 @@ // SPDX-License-Identifier: Apache-2.0 -//nolint:dupl // ignore duplicate with user code package server import ( @@ -12,6 +11,7 @@ import ( "github.com/gin-gonic/gin" api "github.com/go-vela/server/api/types" + "github.com/go-vela/server/router/middleware/auth" "github.com/go-vela/types" ) @@ -82,6 +82,28 @@ func getUser(c *gin.Context) { c.JSON(http.StatusOK, body) } +// currentUser returns mock JSON for a http GET and http PUT. +// +// Pass "invalid" to auth header to test receiving 401 response. +func currentUser(c *gin.Context) { + tkn, _ := auth.RetrieveAccessToken(c.Request) + + if strings.Contains(tkn, "invalid") { + msg := "unauthorized" + + c.AbortWithStatusJSON(http.StatusUnauthorized, types.Error{Message: &msg}) + + return + } + + data := []byte(UserResp) + + var body api.User + _ = json.Unmarshal(data, &body) + + c.JSON(http.StatusOK, body) +} + // addUser returns mock JSON for a http POST. func addUser(c *gin.Context) { data := []byte(UserResp) diff --git a/router/middleware/compiler_test.go b/router/middleware/compiler_test.go index d018b06a9..962482d6b 100644 --- a/router/middleware/compiler_test.go +++ b/router/middleware/compiler_test.go @@ -20,8 +20,8 @@ import ( func TestMiddleware_CompilerNative(t *testing.T) { // setup types - defaultCloneImage := "target/vela-git" - wantCloneImage := "target/vela-git:latest" + defaultCloneImage := "target/vela-git-slim" + wantCloneImage := "target/vela-git-slim:latest" set := flag.NewFlagSet("", flag.ExitOnError) set.String("clone-image", defaultCloneImage, "doc") diff --git a/router/middleware/pipeline/pipeline_test.go b/router/middleware/pipeline/pipeline_test.go index 1408d0dea..28a6dc3c5 100644 --- a/router/middleware/pipeline/pipeline_test.go +++ b/router/middleware/pipeline/pipeline_test.go @@ -285,7 +285,7 @@ func TestPipeline_Establish_NoPipeline(t *testing.T) { } set := flag.NewFlagSet("test", 0) - set.String("clone-image", "target/vela-git:latest", "doc") + set.String("clone-image", "target/vela-git-slim:latest", "doc") comp, err := native.FromCLIContext(cli.NewContext(nil, set, nil)) if err != nil { diff --git a/router/middleware/settings/context_test.go b/router/middleware/settings/context_test.go index 0c17b4297..ca342024c 100644 --- a/router/middleware/settings/context_test.go +++ b/router/middleware/settings/context_test.go @@ -13,7 +13,7 @@ import ( func TestSettings_FromContext(t *testing.T) { // setup types num := int64(1) - cloneImage := "target/vela-git" + cloneImage := "target/vela-git-slim" cs := settings.Compiler{ CloneImage: &cloneImage, @@ -81,7 +81,7 @@ func TestSettings_FromContext_Empty(t *testing.T) { func TestSettings_ToContext(t *testing.T) { // setup types num := int64(1) - cloneImage := "target/vela-git" + cloneImage := "target/vela-git-slim" cs := settings.Compiler{ CloneImage: &cloneImage, diff --git a/router/middleware/settings_test.go b/router/middleware/settings_test.go index d0b5d2b58..70e212393 100644 --- a/router/middleware/settings_test.go +++ b/router/middleware/settings_test.go @@ -16,7 +16,7 @@ import ( func TestMiddleware_Settings(t *testing.T) { // setup types want := settings.PlatformMockEmpty() - want.SetCloneImage("target/vela-git") + want.SetCloneImage("target/vela-git-slim") got := settings.PlatformMockEmpty() diff --git a/scm/github/repo.go b/scm/github/repo.go index ad9db4271..395c5e375 100644 --- a/scm/github/repo.go +++ b/scm/github/repo.go @@ -335,7 +335,14 @@ func (c *client) Status(ctx context.Context, u *api.User, b *api.Build, org, nam description = "build was skipped as no steps/stages found" default: state = "error" - description = "there was an error" + + // if there is no build, then this status update is from a failed compilation + if b.GetID() == 0 { + description = "error compiling pipeline - check audit for more information" + url = fmt.Sprintf("%s/%s/%s/hooks", c.config.WebUIAddress, org, name) + } else { + description = "there was an error" + } } // check if the build event is deployment