Skip to content

Commit

Permalink
Merge branch 'main' of github.com:go-vela/server into enhance/deleteE…
Browse files Browse the repository at this point in the history
…vent
  • Loading branch information
Claire.Nicholas authored and Claire.Nicholas committed Jan 11, 2024
2 parents 1c27ef6 + 78eca51 commit 9d7cdb7
Show file tree
Hide file tree
Showing 88 changed files with 2,833 additions and 586 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.GetBuilds(), 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)
}
2 changes: 1 addition & 1 deletion api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func recordGauges(c *gin.Context) {
// worker_build_limit, active_worker_count, inactive_worker_count, idle_worker_count, available_worker_count, busy_worker_count, error_worker_count
if q.WorkerBuildLimit || q.ActiveWorkerCount || q.InactiveWorkerCount || q.IdleWorkerCount || q.AvailableWorkerCount || q.BusyWorkerCount || q.ErrorWorkerCount {
// send API call to capture the workers
workers, err := database.FromContext(c).ListWorkers(ctx)
workers, err := database.FromContext(c).ListWorkers(ctx, "all", time.Now().Unix(), 0)
if err != nil {
logrus.Errorf("unable to get workers: %v", err)
}
Expand Down
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
18 changes: 16 additions & 2 deletions api/secret/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/go-vela/server/util"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/library/actions"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -207,8 +208,21 @@ func CreateSecret(c *gin.Context) {
input.SetImages(util.Unique(input.GetImages()))
}

if len(input.GetEvents()) > 0 {
input.SetEvents(util.Unique(input.GetEvents()))
// default event set for secrets
if input.GetAllowEvents().ToDatabase() == 0 {
e := new(library.Events)

push := new(actions.Push)
push.SetBranch(true)
push.SetTag(true)

deploy := new(actions.Deploy)
deploy.SetCreated(true)

e.SetPush(push)
e.SetDeployment(deploy)

input.SetAllowEvents(e)
}

if len(input.GetEvents()) == 0 {
Expand Down
48 changes: 47 additions & 1 deletion 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 @@ -256,7 +258,7 @@ func PostWebhook(c *gin.Context) {
}

// verify the build has a valid event and the repo allows that event type
if !repo.EventAllowed(b.GetEvent(), b.GetEventAction()) {
if !repo.GetAllowEvents().Allowed(b.GetEvent(), b.GetEventAction()) {
var actionErr string
if len(b.GetEventAction()) > 0 {
actionErr = ":" + b.GetEventAction()
Expand Down Expand Up @@ -663,6 +665,50 @@ 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 {
build := append(d.GetBuilds(), b)
d.SetBuilds(build)
_, 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
40 changes: 39 additions & 1 deletion api/worker/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package worker
import (
"fmt"
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
Expand All @@ -20,6 +22,20 @@ import (
// ---
// produces:
// - application/json
// parameters:
// - in: query
// name: active
// description: Filter workers based on active status
// type: boolean
// - in: query
// name: checked_in_before
// description: filter workers that have checked in before a certain time
// type: integer
// - in: query
// name: checked_in_after
// description: filter workers that have checked in after a certain time
// type: integer
// default: 0
// security:
// - ApiKeyAuth: []
// responses:
Expand Down Expand Up @@ -48,7 +64,29 @@ func ListWorkers(c *gin.Context) {
"user": u.GetName(),
}).Info("reading workers")

w, err := database.FromContext(c).ListWorkers(ctx)
active := c.Query("active")

// capture before query parameter if present, default to now
before, err := strconv.ParseInt(c.DefaultQuery("checked_in_before", strconv.FormatInt(time.Now().UTC().Unix(), 10)), 10, 64)
if err != nil {
retErr := fmt.Errorf("unable to convert `checked_in_before` query parameter: %w", err)

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

return
}

// capture after query parameter if present, default to 0
after, err := strconv.ParseInt(c.DefaultQuery("checked_in_after", "0"), 10, 64)
if err != nil {
retErr := fmt.Errorf("unable to convert `checked_in_after` query parameter: %w", err)

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

return
}

w, err := database.FromContext(c).ListWorkers(ctx, active, before, after)
if err != nil {
retErr := fmt.Errorf("unable to get workers: %w", 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 @@ 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 9d7cdb7

Please sign in to comment.