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 #1031

Merged
merged 42 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a712597
feat: adding deployment database
Sep 22, 2023
3afee3d
updating things
Sep 25, 2023
a189a6f
fix reserved word user
ecrupper Sep 25, 2023
1577eb3
testing and other
Sep 27, 2023
c49b7cf
tests
Sep 28, 2023
e5f7c3c
fixing tests
Oct 24, 2023
03da6ea
merging main into branch
Nov 28, 2023
14d1f4c
fixing things
Dec 15, 2023
183a91b
merging main into branch
Dec 15, 2023
ccb6747
help
Dec 15, 2023
c8cc0d7
fixing various tests
Dec 18, 2023
df3f450
new types changes
Dec 21, 2023
657b460
updating build deployNumber field to deploymentID
Dec 21, 2023
b8e4f1a
changing build DeployID field back to DeployNumber
Dec 22, 2023
027f67c
fixing migrations issues
Dec 22, 2023
1b7305f
Merge branch 'main' of github.com:go-vela/server into feat/deployment…
Dec 22, 2023
5ff37bd
fixing lint
Dec 22, 2023
fbea7f5
fixing lint things
Dec 22, 2023
8e4f2ee
fixing lint things
Dec 22, 2023
9694b4e
getting new types
Dec 22, 2023
dc495ae
fixing things
Dec 22, 2023
442913b
fix pls
Dec 22, 2023
70c641f
fix pls
Dec 22, 2023
13d051b
fix pls
Dec 22, 2023
43cc0fc
fix pls
Dec 22, 2023
5de0961
fixing things
Dec 22, 2023
e8d0d8e
...
Dec 22, 2023
56fe6a1
linter
Dec 22, 2023
f3f5825
linter
Dec 22, 2023
f93b7ed
linter
Dec 22, 2023
e9b7e1c
fixing 2022 copyright lint errors
Dec 22, 2023
f372ab2
hopefully fixing tests
Dec 27, 2023
0c00bcd
fixing integration tests
Dec 27, 2023
0758cb3
fixing vaders comments
Dec 28, 2023
9ba8f52
fixing vaders comments pt 2
Dec 28, 2023
dca4e8c
fix
Dec 28, 2023
85bbac6
fixing eastons comments
Dec 28, 2023
a5a78c5
linter
Dec 28, 2023
21e5481
linter
Dec 28, 2023
f8f6cfd
making david mays changes
Dec 29, 2023
16fc94c
fixy fix
Dec 29, 2023
245a6db
fixy fix
Dec 29, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion api/admin/step.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// SPDX-License-Identifier: Apache-2.0

//nolint:dupl // ignore similar code
package admin

import (
Expand Down
16 changes: 14 additions & 2 deletions api/build/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,6 @@ func RestartBuild(c *gin.Context) {
}

// check if the pipeline did not already exist in the database
//
//nolint:dupl // ignore duplicate code
if pipeline == nil {
pipeline = compiled
pipeline.SetRepoID(r.GetID())
Expand Down Expand Up @@ -341,6 +339,20 @@ func RestartBuild(c *gin.Context) {

c.JSON(http.StatusCreated, b)

d, err := database.FromContext(c).GetDeploymentForRepo(c, r, b.GetDeployNumber())
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
logger.Errorf("unable to set get deployment for build %s: %v", entry, err)
}

build := append(d.Builds, b)

d.SetBuilds(build)

_, err = database.FromContext(c).UpdateDeployment(d)
if err != nil {
logger.Errorf("unable to set update deployment for build %s: %v", entry, err)
}

// send API call to set the status on the commit
err = scm.FromContext(c).Status(ctx, u, b, r.GetOrg(), r.GetName())
if err != nil {
Expand Down
17 changes: 15 additions & 2 deletions api/deployment/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package deployment
import (
"fmt"
"net/http"
"time"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
Expand Down Expand Up @@ -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())
input.SetCreatedAt(time.Now().Unix())

if len(input.GetDescription()) == 0 {
input.SetDescription("Deployment request from Vela")
Expand All @@ -107,5 +110,15 @@ func CreateDeployment(c *gin.Context) {
return
}

c.JSON(http.StatusCreated, input)
// send API call to create the deployment
d, err := database.FromContext(c).CreateDeployment(c, input)
if err != nil {
retErr := fmt.Errorf("unable to create new deployment for %s: %w", r.GetFullName(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

c.JSON(http.StatusCreated, d)
}
31 changes: 26 additions & 5 deletions api/deployment/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"strconv"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -65,6 +67,11 @@ func GetDeployment(c *gin.Context) {
deployment := util.PathParameter(c, "deployment")
ctx := c.Request.Context()

var (
dep *library.Deployment
err error
)

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

// update engine logger with API metadata
Expand All @@ -85,15 +92,29 @@ func GetDeployment(c *gin.Context) {
return
}

// send API call to capture the deployment
d, err := scm.FromContext(c).GetDeployment(ctx, u, r, int64(number))
// send API call to database to capture the deployment
d, err := database.FromContext(c).GetDeployment(int64(number))
if err != nil {
retErr := fmt.Errorf("unable to get deployment %s: %w", entry, err)
// send API call to SCM to capture the deployment
dep, err = scm.FromContext(c).GetDeployment(ctx, u, r, int64(number))
if err != nil {
retErr := fmt.Errorf("unable to get deployment %s: %w", entry, err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}
} else {
dep = d
}
claire1618 marked this conversation as resolved.
Show resolved Hide resolved

claire1618 marked this conversation as resolved.
Show resolved Hide resolved
util.HandleError(c, http.StatusInternalServerError, retErr)
if dep == nil {
retErr := fmt.Errorf("invalid deployment parameter provided: %s", deployment)

util.HandleError(c, http.StatusBadRequest, retErr)

return
}

c.JSON(http.StatusOK, d)
c.JSON(http.StatusOK, dep)
}
31 changes: 3 additions & 28 deletions api/deployment/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ import (
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -80,7 +78,6 @@ func ListDeployments(c *gin.Context) {
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
ctx := c.Request.Context()

// update engine logger with API metadata
//
Expand Down Expand Up @@ -115,7 +112,7 @@ func ListDeployments(c *gin.Context) {
perPage = util.MaxInt(1, util.MinInt(100, perPage))

// send API call to capture the total number of deployments for the repo
t, err := scm.FromContext(c).GetDeploymentCount(ctx, u, r)
t, err := database.FromContext(c).CountDeploymentsForRepo(c, r)
if err != nil {
retErr := fmt.Errorf("unable to get deployment count for %s: %w", r.GetFullName(), err)

Expand All @@ -125,7 +122,7 @@ func ListDeployments(c *gin.Context) {
}

// send API call to capture the list of deployments for the repo
d, err := scm.FromContext(c).GetDeploymentList(ctx, u, r, page, perPage)
d, err := database.FromContext(c).ListDeployments(c)
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
retErr := fmt.Errorf("unable to get deployments for %s: %w", r.GetFullName(), err)

Expand All @@ -134,28 +131,6 @@ func ListDeployments(c *gin.Context) {
return
}

dWithBs := []*library.Deployment{}

for _, deployment := range d {
b, _, err := database.FromContext(c).ListBuildsForDeployment(ctx, deployment, nil, 1, 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we delete the database function ListBuildsForDeployment? Can be a follow up PR but would be nice to remove any dead code.

if err != nil {
retErr := fmt.Errorf("unable to get builds for deployment %d: %w", deployment.GetID(), err)

util.HandleError(c, http.StatusInternalServerError, retErr)

return
}

builds := []library.Build{}
for _, build := range b {
builds = append(builds, *build)
}

deployment.SetBuilds(builds)

dWithBs = append(dWithBs, deployment)
}

// create pagination object
pagination := api.Pagination{
Page: page,
Expand All @@ -165,5 +140,5 @@ func ListDeployments(c *gin.Context) {
// set pagination headers
pagination.SetHeaderLink(c)

c.JSON(http.StatusOK, dWithBs)
c.JSON(http.StatusOK, d)
}
1 change: 1 addition & 0 deletions api/schedule/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func UpdateSchedule(c *gin.Context) {

// set the updated by field using claims
s.SetUpdatedBy(u.GetName())

if input.GetBranch() != "" {
s.SetBranch(input.GetBranch())
}
Expand Down
49 changes: 49 additions & 0 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -24,6 +25,7 @@
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)

var baseErr = "unable to process webhook"
Expand Down Expand Up @@ -177,7 +179,7 @@

defer func() {
// send API call to update the webhook
_, err = database.FromContext(c).UpdateHook(ctx, h)

Check failure on line 182 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L182

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:182:32: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		_, err = database.FromContext(c).UpdateHook(ctx, h)
		                             ^
if err != nil {
logrus.Errorf("unable to update webhook %s/%d: %v", r.GetFullName(), h.GetNumber(), err)
}
Expand Down Expand Up @@ -664,6 +666,53 @@
// set the BuildID field
h.SetBuildID(b.GetID())

// if event is deployment, update the deployment record to include this build
if b.GetEvent() == constants.EventDeploy {
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
builds := []*library.Build{}
builds = append(builds, b)
claire1618 marked this conversation as resolved.
Show resolved Hide resolved

d, err := database.FromContext(c).GetDeploymentForRepo(c, repo, webhook.Deployment.GetNumber())
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
deployment := webhook.Deployment

deployment.SetRepoID(repo.GetID())
deployment.SetBuilds(builds)

_, err := database.FromContext(c).CreateDeployment(c, deployment)
if err != nil {
retErr := fmt.Errorf("%s: failed to create deployment %s/%d: %w", baseErr, repo.GetFullName(), deployment.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
}
} else {
retErr := fmt.Errorf("%s: failed to get deployment %s/%d: %w", baseErr, repo.GetFullName(), webhook.Deployment.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
}
} else {
d.SetBuilds(builds)
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
_, err := database.FromContext(c).UpdateDeployment(d)
if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should an error like this stop the build? Probably? Not sure though, so noting it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

im not too sure what to do for this?

retErr := fmt.Errorf("%s: failed to update deployment %s/%d: %w", baseErr, repo.GetFullName(), d.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)

h.SetStatus(constants.StatusFailure)
h.SetError(retErr.Error())

return
}
}
}

c.JSON(http.StatusOK, b)

// determine queue route
Expand Down Expand Up @@ -787,7 +836,7 @@
case "archived", "unarchived", constants.ActionEdited:
logrus.Debugf("repository action %s for %s", h.GetEventAction(), r.GetFullName())
// send call to get repository from database
dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())

Check failure on line 839 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L839

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:839:38: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		dbRepo, err := database.FromContext(c).GetRepoForOrg(ctx, r.GetOrg(), r.GetName())
		                                   ^
if err != nil {
retErr := fmt.Errorf("%s: failed to get repo %s: %w", baseErr, r.GetFullName(), err)

Expand All @@ -798,7 +847,7 @@
}

// send API call to capture the last hook for the repo
lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)

Check failure on line 850 in api/webhook/post.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/webhook/post.go#L850

Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
Raw output
api/webhook/post.go:850:40: Non-inherited new context, use function like `context.WithXXX` instead (contextcheck)
		lastHook, err := database.FromContext(c).LastHookForRepo(ctx, dbRepo)
		                                     ^
if err != nil {
retErr := fmt.Errorf("unable to get last hook for repo %s: %w", r.GetFullName(), err)

Expand Down
3 changes: 2 additions & 1 deletion cmd/vela-server/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
//
// The previous occurrence of the schedule must be after the starting time of processing schedules.
if !prevTime.After(start) {
logrus.Tracef("%s %s: previous occurence not after starting point", scheduleWait, schedule.GetName())
logrus.Tracef("%s %s: previous occurrence not after starting point", scheduleWait, schedule.GetName())

continue
}
Expand Down Expand Up @@ -135,7 +135,7 @@
}

//nolint:funlen // ignore function length and number of statements
func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service, allowList []string) error {

Check failure on line 138 in cmd/vela-server/schedule.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] cmd/vela-server/schedule.go#L138

cyclomatic complexity 31 of func `processSchedule` is high (> 30) (gocyclo)
Raw output
cmd/vela-server/schedule.go:138:1: cyclomatic complexity 31 of func `processSchedule` is high (> 30) (gocyclo)
func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler.Engine, database database.Interface, metadata *types.Metadata, queue queue.Service, scm scm.Service, allowList []string) error {
^

Check failure on line 138 in cmd/vela-server/schedule.go

View workflow job for this annotation

GitHub Actions / full-review

cyclomatic complexity 31 of func `processSchedule` is high (> 30) (gocyclo)

Check failure on line 138 in cmd/vela-server/schedule.go

View workflow job for this annotation

GitHub Actions / diff-review

cyclomatic complexity 31 of func `processSchedule` is high (> 30) (gocyclo)
// send API call to capture the repo for the schedule
r, err := database.GetRepo(ctx, s.GetRepoID())
if err != nil {
Expand Down Expand Up @@ -277,6 +277,7 @@
// parent should be "1" if it's the first build ran
b.SetParent(1)
}

r.SetCounter(r.GetCounter() + 1)

// set the build link if a web address is provided
Expand Down
Loading
Loading