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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@
}

// 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 @@

c.JSON(http.StatusCreated, b)

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

Check failure on line 343 in api/build/restart.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] api/build/restart.go#L343

if statements should only be cuddled with assignments used in the if statement itself (wsl)
Raw output
api/build/restart.go:343:2: if statements should only be cuddled with assignments used in the if statement itself (wsl)
	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)

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(c, d)
if err != nil {
claire1618 marked this conversation as resolved.
Show resolved Hide resolved
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())

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)

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)


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)
}
25 changes: 19 additions & 6 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,21 @@ 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(c, 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)
util.HandleError(c, http.StatusInternalServerError, retErr)

return
return
}
} else {
dep = d
}

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)
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)
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 {
builds := []*library.Build{}
builds = append(builds, b)

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)
_, err := database.FromContext(c).UpdateDeployment(c, d)
if err != nil {
retErr := fmt.Errorf("%s: failed to update deployment %s/%d: %w", baseErr, repo.GetFullName(), d.GetNumber(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)

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 / diff-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 / full-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