Skip to content

Commit

Permalink
feat(api/database)!: store deployment record in database for Vela-tar…
Browse files Browse the repository at this point in the history
…geted deployments (#1031)

* feat: adding deployment database

* updating things

* fix reserved word user

* testing and other

* tests

* fixing tests

* fixing things

* help

* fixing various tests

* new types changes

* updating build deployNumber field to deploymentID

* changing build DeployID field back to DeployNumber

* fixing migrations issues

* fixing lint

* fixing lint things

* fixing lint things

* getting new types

* fixing things

* fix pls

* fix pls

* fix pls

* fix pls

* fixing things

* ...

* linter

* linter

* linter

* fixing 2022 copyright lint errors

* hopefully fixing tests

* fixing integration tests

* fixing vaders comments

* fixing vaders comments pt 2

* fix

* fixing eastons comments

* linter

* linter

* making david mays changes

* fixy fix

* fixy fix

---------

Co-authored-by: Claire.Nicholas <[email protected]>
Co-authored-by: ecrupper <[email protected]>
  • Loading branch information
3 people authored Dec 29, 2023
1 parent 2dd31d6 commit a7cd07f
Show file tree
Hide file tree
Showing 60 changed files with 2,539 additions and 506 deletions.
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
19 changes: 17 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,23 @@ func RestartBuild(c *gin.Context) {

c.JSON(http.StatusCreated, b)

// if the event is a deployment, update the build list
if !strings.EqualFold(b.GetEvent(), constants.EventDeploy) {
d, err := database.FromContext(c).GetDeploymentForRepo(c, r, b.GetDeployNumber())
if err != nil {
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)
}
21 changes: 17 additions & 4 deletions api/deployment/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
Expand Down Expand Up @@ -85,12 +86,24 @@ 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
d, 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
}
}

if d == nil {
retErr := fmt.Errorf("unable to get deployment: %s", deployment)

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

return
}
Expand Down
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).ListDeploymentsForRepo(c, r, page, perPage)
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
45 changes: 45 additions & 0 deletions api/webhook/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package webhook
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -24,6 +25,7 @@ import (
"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 @@ -663,6 +665,49 @@ func PostWebhook(c *gin.Context) {

// set the BuildID field
h.SetBuildID(b.GetID())
// if event is deployment, update the deployment record to include this build
if !strings.EqualFold(b.GetEvent(), constants.EventDeploy) {
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([]*library.Build{b})

_, 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([]*library.Build{b})
_, err := database.FromContext(c).UpdateDeployment(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)

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 @@ func processSchedules(ctx context.Context, start time.Time, compiler compiler.En
//
// 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 @@ -277,6 +277,7 @@ func processSchedule(ctx context.Context, s *library.Schedule, compiler compiler
// 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

0 comments on commit a7cd07f

Please sign in to comment.