Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api/database)!: store deployment record in database for Vela-targeted deployments #1030

Closed
wants to merge 25 commits into from

Conversation

claire1618
Copy link
Contributor

When a repo has a lot of deployments historically, viewing them in the UI is cumbersome and slow.

The loading of deployments on the deployments tab in Vela takes a few more seconds to load than the other pages (schedules, secrets, audits...) even when there are no deployments for a repo. This is due to the way deployments are stored and retrieved.

The changes made in this pull request add a new database table so that we can store deployment information instead of having to go to the scm to retrieve them.

With these changes, any new deployments will be added to our database and pulled from there when requested, but old deployments are still pulled from GitHub.

@claire1618 claire1618 requested a review from a team as a code owner December 22, 2023 17:54

// create the deploymentz
for _, deployment := range resources.Deployments {
_, err := db.CreateDeployment(context.TODO(), deployment)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.CreateDeployment undefined (type Interface has no field or method CreateDeployment) (typecheck)

methods["CreateDeployment"] = true

// count the deployments
count, err := db.CountDeployments(context.TODO())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.CountDeployments undefined (type Interface has no field or method CountDeployments) (typecheck)

methods["CountDeployments"] = true

// count the deployments for a repo
count, err = db.CountDeploymentsForRepo(context.TODO(), resources.Repos[0])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.CountDeploymentsForRepo undefined (type Interface has no field or method CountDeploymentsForRepo) (typecheck)

methods["CountDeploymentsForRepo"] = true

// list the deployments
list, err := db.ListDeployments(context.TODO())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.ListDeployments undefined (type Interface has no field or method ListDeployments) (typecheck)

methods["ListDeployments"] = true

// list the deployments for a repo
list, err = db.ListDeploymentsForRepo(context.TODO(), resources.Repos[0], 1, 10)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.ListDeploymentsForRepo undefined (type Interface has no field or method ListDeploymentsForRepo) (typecheck)

// lookup the deployments by name
for _, deployment := range resources.Deployments {
repo := resources.Repos[deployment.GetRepoID()-1]
got, err := db.GetDeploymentForRepo(context.TODO(), repo, deployment.GetNumber())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.GetDeploymentForRepo undefined (type Interface has no field or method GetDeploymentForRepo) (typecheck)


// update the deployments
for _, deployment := range resources.Deployments {
_, err = db.UpdateDeployment(context.TODO(), deployment)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.UpdateDeployment undefined (type Interface has no field or method UpdateDeployment) (typecheck)

}

// lookup the deployment by ID
got, err := db.GetDeployment(context.TODO(), deployment.GetID())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.GetDeployment undefined (type Interface has no field or method GetDeployment) (typecheck)


// delete the deployments
for _, deployment := range resources.Deployments {
err = db.DeleteDeployment(context.TODO(), deployment)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
db.DeleteDeployment undefined (type Interface has no field or method DeleteDeployment) (typecheck)

go.mod Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
@ecrupper ecrupper changed the title feat: creating our own deployment database table feat(api/database)!: store deployment record in database for Vela-targeted deployments Dec 22, 2023
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

golangci

database/deployment/count.go|20 col 19| undefined: constants.TableDeployment
database/deployment/count_repo.go|25 col 19| undefined: constants.TableDeployment
database/deployment/create.go|21 col 25| undefined: database.DeploymentFromLibrary
database/deployment/create.go|29 col 37| undefined: constants.TableDeployment
database/deployment/delete.go|23 col 25| undefined: database.DeploymentFromLibrary
database/deployment/delete.go|27 col 19| undefined: constants.TableDeployment
database/deployment/deployment.go|73 col 69| undefined: constants.TableDeployment
database/deployment/deployment.go|79 col 81| undefined: constants.TableDeployment
database/deployment/get.go|21 col 20| undefined: database.Deployment
database/deployment/get.go|25 col 19| undefined: constants.TableDeployment
database/deployment/get.go|25 col 19| too many errors (typecheck)
scm/github/deployment.go|52 col 3| unknown field CreatedAt in struct literal of type library.Deployment
scm/github/deployment.go|53 col 3| unknown field CreatedBy in struct literal of type library.Deployment
scm/github/deployment.go|149 col 4| unknown field CreatedAt in struct literal of type library.Deployment
scm/github/deployment.go|150 col 4| unknown field CreatedBy in struct literal of type library.Deployment
scm/github/deployment.go|192 col 4| d.SetNumber undefined (type *library.Deployment has no field or method SetNumber)
scm/github/webhook.go|321 col 4| b.SetDeployNumber undefined (type *library.Build has no field or method SetDeployNumber)
scm/github/webhook.go|334 col 4| d.SetNumber undefined (type *library.Deployment has no field or method SetNumber)
scm/github/webhook.go|341 col 4| d.SetCreatedAt undefined (type *library.Deployment has no field or method SetCreatedAt)
scm/github/webhook.go|342 col 4| d.SetCreatedBy undefined (type *library.Deployment has no field or method SetCreatedBy)
scm/github/webhook.go|393 col 3| unknown field Deployment in struct literal of type types.Webhook
scm/github/webhook.go|393 col 3| too many errors) (typecheck)
router/middleware/build/build_test.go|64 col 7| want.SetDeployNumber undefined (type *library.Build has no field or method SetDeployNumber) (typecheck)
scm/github/deployment.go|52 col 3| unknown field CreatedAt in struct literal of type library.Deployment
scm/github/deployment.go|53 col 3| unknown field CreatedBy in struct literal of type library.Deployment
scm/github/deployment.go|149 col 4| unknown field CreatedAt in struct literal of type library.Deployment
scm/github/deployment.go|150 col 4| unknown field CreatedBy in struct literal of type library.Deployment
scm/github/deployment.go|192 col 4| d.SetNumber undefined (type *library.Deployment has no field or method SetNumber)
scm/github/webhook.go|321 col 4| b.SetDeployNumber undefined (type *library.Build has no field or method SetDeployNumber)
scm/github/webhook.go|334 col 4| d.SetNumber undefined (type *library.Deployment has no field or method SetNumber)
scm/github/webhook.go|341 col 4| d.SetCreatedAt undefined (type *library.Deployment has no field or method SetCreatedAt)
scm/github/webhook.go|342 col 4| d.SetCreatedBy undefined (type *library.Deployment has no field or method SetCreatedBy)
scm/github/webhook.go|393 col 3| unknown field Deployment in struct literal of type types.Webhook
scm/github/webhook.go|393 col 3| too many errors) (typecheck)
scm/github/deployment.go|52 col 3| unknown field CreatedAt in struct literal of type library.Deployment
scm/github/deployment.go|53 col 3| unknown field CreatedBy in struct literal of type library.Deployment
scm/github/deployment.go|149 col 4| unknown field CreatedAt in struct literal of type library.Deployment
scm/github/deployment.go|150 col 4| unknown field CreatedBy in struct literal of type library.Deployment
scm/github/deployment.go|192 col 4| d.SetNumber undefined (type *library.Deployment has no field or method SetNumber)
scm/github/webhook.go|321 col 4| b.SetDeployNumber undefined (type *library.Build has no field or method SetDeployNumber)
scm/github/webhook.go|334 col 4| d.SetNumber undefined (type *library.Deployment has no field or method SetNumber)
scm/github/webhook.go|341 col 4| d.SetCreatedAt undefined (type *library.Deployment has no field or method SetCreatedAt)
scm/github/webhook.go|342 col 4| d.SetCreatedBy undefined (type *library.Deployment has no field or method SetCreatedBy)
scm/github/webhook.go|393 col 3| unknown field Deployment in struct literal of type types.Webhook
scm/github/webhook.go|393 col 3| too many errors) (typecheck)
scm/github/deployment.go|52 col 3| unknown field CreatedAt in struct literal of type library.Deployment
scm/github/deployment.go|53 col 3| unknown field CreatedBy in struct literal of type library.Deployment
scm/github/deployment.go|149 col 4| unknown field CreatedAt in struct literal of type library.Deployment
scm/github/deployment.go|150 col 4| unknown field CreatedBy in struct literal of type library.Deployment
scm/github/deployment.go|192 col 4| d.SetNumber undefined (type *library.Deployment has no field or method SetNumber)
scm/github/webhook.go|321 col 4| b.SetDeployNumber undefined (type *library.Build has no field or method SetDeployNumber)
scm/github/webhook.go|334 col 4| d.SetNumber undefined (type *library.Deployment has no field or method SetNumber)
scm/github/webhook.go|341 col 4| d.SetCreatedAt undefined (type *library.Deployment has no field or method SetCreatedAt)
scm/github/webhook.go|342 col 4| d.SetCreatedBy undefined (type *library.Deployment has no field or method SetCreatedBy)
scm/github/webhook.go|393 col 3| unknown field Deployment in struct literal of type types.Webhook
scm/github/webhook.go|393 col 3| too many errors (typecheck)

@@ -82,7 +84,8 @@ func CreateDeployment(c *gin.Context) {

// update fields in deployment object
input.SetRepoID(r.GetID())
input.SetUser(u.GetName())
input.SetCreatedBy(u.GetName())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
input.SetCreatedBy undefined (type *library.Deployment has no field or method SetCreatedBy) (typecheck)

@@ -82,7 +84,8 @@

// update fields in deployment object
input.SetRepoID(r.GetID())
input.SetUser(u.GetName())
input.SetCreatedBy(u.GetName())
input.SetCreatedAt(time.Now().Unix())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
input.SetCreatedAt undefined (type *library.Deployment has no field or method SetCreatedAt) (typecheck)

@@ -8,6 +8,7 @@ import (
"time"

"github.com/go-vela/server/database/build"
"github.com/go-vela/server/database/deployment"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
could not import github.com/go-vela/server/database/deployment (-: # github.com/go-vela/server/database/deployment


// send query to the database and store result in variable
err := e.client.
Table(constants.TableDeployment).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
undefined: constants.TableDeployment


// send query to the database and store result in variable
err := e.client.
Table(constants.TableDeployment).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
undefined: constants.TableDeployment

@@ -17,12 +17,14 @@ func TestBuild_Engine_ListBuilds(t *testing.T) {
_buildOne.SetID(1)
_buildOne.SetRepoID(1)
_buildOne.SetNumber(1)
_buildOne.SetDeployNumber(0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
_buildOne.SetDeployNumber undefined (type *library.Build has no field or method SetDeployNumber)

_buildOne.SetDeployPayload(nil)

_buildTwo := testBuild()
_buildTwo.SetID(2)
_buildTwo.SetRepoID(1)
_buildTwo.SetNumber(2)
_buildTwo.SetDeployNumber(0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
_buildTwo.SetDeployNumber undefined (type *library.Build has no field or method SetDeployNumber)

@@ -16,16 +16,17 @@ func TestBuild_Engine_UpdateBuild(t *testing.T) {
_build.SetID(1)
_build.SetRepoID(1)
_build.SetNumber(1)
_build.SetDeployNumber(0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
_build.SetDeployNumber undefined (type *library.Build has no field or method SetDeployNumber)

@@ -16,16 +16,17 @@
_build.SetID(1)
_build.SetRepoID(1)
_build.SetNumber(1)
_build.SetDeployNumber(0)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
too many errors (typecheck)

@@ -0,0 +1,25 @@
// SPDX-License-Identifier: Apache-2.0

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
: # github.com/go-vela/server/database/deployment [github.com/go-vela/server/database/deployment.test]

@@ -0,0 +1,33 @@
// SPDX-License-Identifier: Apache-2.0

package deployment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
3-33 lines are duplicate of database/deployment/update.go:5-35 (dupl)

//
// Use of this source code is governed by the LICENSE file in this repository.

package deployment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
5-35 lines are duplicate of database/deployment/create.go:3-33 (dupl)

build := append(d.Builds, b)

d.SetBuilds(build)
database.FromContext(c).UpdateDeployment(c, d)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
Error return value of (github.com/go-vela/server/database/deployment.DeploymentInterface).UpdateDeployment is not checked (errcheck)

@@ -0,0 +1,30 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
Actual: Copyright (c) 2022 Target Brands, Inc. All rights reserved.

@@ -0,0 +1,83 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
Actual: Copyright (c) 2022 Target Brands, Inc. All rights reserved.

@@ -0,0 +1,58 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
Actual: Copyright (c) 2022 Target Brands, Inc. All rights reserved.

@@ -0,0 +1,60 @@
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
Actual: Copyright (c) 2023 Target Brands, Inc. All rights reserved.

Copy link

codecov bot commented Dec 22, 2023

Codecov Report

Attention: 130 lines in your changes are missing coverage. Please review.

Comparison is base (2dd31d6) 67.79% compared to head (43cc0fc) 67.01%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1030      +/-   ##
==========================================
- Coverage   67.79%   67.01%   -0.79%     
==========================================
  Files         318      331      +13     
  Lines       13754    14120     +366     
==========================================
+ Hits         9325     9463     +138     
- Misses       3956     4173     +217     
- Partials      473      484      +11     
Files Coverage Δ
database/build/table.go 100.00% <ø> (ø)
database/database.go 63.38% <ø> (ø)
database/deployment/count.go 100.00% <100.00%> (ø)
database/deployment/count_repo.go 100.00% <100.00%> (ø)
database/deployment/delete.go 100.00% <100.00%> (ø)
database/deployment/index.go 100.00% <100.00%> (ø)
database/deployment/table.go 100.00% <100.00%> (ø)
router/middleware/logger.go 97.52% <100.00%> (+0.02%) ⬆️
scm/github/webhook.go 86.18% <100.00%> (+0.53%) ⬆️
api/schedule/update.go 0.00% <0.00%> (ø)
... and 11 more

api/build/restart.go Outdated Show resolved Hide resolved
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: Apache-2.0

package deployment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
3-35 lines are duplicate of database/deployment/update.go:5-37 (dupl)

//
// Use of this source code is governed by the LICENSE file in this repository.

package deployment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
5-37 lines are duplicate of database/deployment/create.go:3-35 (dupl)


// CreateDeployment creates a new deployment in the database.
//
//nolint:dupl // ignore similar code with update.go

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
directive //nolint:dupl // ignore similar code with update.go is unused for linter "dupl" (nolintlint)


// UpdateDeployment updates an existing deployment in the database.
//
//nolint:dupl // ignore similar code with create.go

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
directive //nolint:dupl // ignore similar code with create.go is unused for linter "dupl" (nolintlint)

@@ -341,6 +339,20 @@ func RestartBuild(c *gin.Context) {

c.JSON(http.StatusCreated, b)

d, _ := database.FromContext(c).GetDeploymentForRepo(c, r, b.GetDeployNumber())
if err != nil {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
if statements should only be cuddled with assignments used in the if statement itself (wsl)

@claire1618 claire1618 closed this Dec 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants