Skip to content

Commit

Permalink
feat: add support for processing schedules (#846)
Browse files Browse the repository at this point in the history
* feat(api/types): add support for schedules

* feat(database/types): add support for schedules

* feat(database): add support for schedules

* chore: update go dependencies

* feat(database): add schedule engine

* feat(api): add support for schedules

* add routes

* fix: parse entry for schedules

* more wip code

* add schedule allowlist

* fix tests

* add validation for entry

* add mocks w/o updated payloads

* fix issues with create

* update mock responses

* use schedule mocks

* make linter happy

* use proper func

* couple more updates

* fix mock pathing

* enhance: switch to adhocore/gronx

* chore: update go deps

* goimports

* yet another goimports

* sigh

* wildcard goimport

* chore: address linter feedback

* chore: save work

* chore: remove new types

* chore: updates for removed types

* chore: update go dependencies

* chore: address review feedback

* chore: remove new types

* feat: initial code for scheduler

* chore: misc updates

* chore: update go dependencies

* chore: updates for local testing

* chore: save work

* fix: introduce jitter

* chore: address review feedback

* chore: address review feedback

* chore: update go dependencies

* chore: address review feedback

* fix(scheduler): use WithCommit in compiler

* chore: address review feedback

---------

Co-authored-by: Jordan Sussman <[email protected]>
Co-authored-by: JordanSussman <[email protected]>
  • Loading branch information
3 people authored May 23, 2023
1 parent ffac676 commit 5a9a92d
Show file tree
Hide file tree
Showing 18 changed files with 675 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,4 @@ __debug_bin
.history
.ionide

# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode
# End of https://www.toptal.com/developers/gitignore/api/visualstudiocode
31 changes: 14 additions & 17 deletions api/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@ import (
"strings"
"time"

"github.com/go-vela/server/internal/token"
"github.com/go-vela/server/router/middleware/claims"
"github.com/go-vela/server/router/middleware/org"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal/token"
"github.com/go-vela/server/queue"
"github.com/go-vela/server/router/middleware/build"
"github.com/go-vela/server/router/middleware/claims"
"github.com/go-vela/server/router/middleware/executors"
"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"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -293,7 +290,7 @@ func CreateBuild(c *gin.Context) {
r.SetPipelineType(pipelineType)

// skip the build if only the init or clone steps are found
skip := skipEmptyBuild(p)
skip := SkipEmptyBuild(p)
if skip != "" {
// set build to successful status
input.SetStatus(constants.StatusSuccess)
Expand Down Expand Up @@ -343,7 +340,7 @@ func CreateBuild(c *gin.Context) {
input.SetPipelineID(pipeline.GetID())

// create the objects from the pipeline in the database
err = planBuild(database.FromContext(c), p, input, r)
err = PlanBuild(database.FromContext(c), p, input, r)
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)

Expand Down Expand Up @@ -372,7 +369,7 @@ func CreateBuild(c *gin.Context) {
}

// publish the build to the queue
go publishToQueue(
go PublishToQueue(
queue.FromGinContext(c),
database.FromContext(c),
p,
Expand All @@ -382,11 +379,11 @@ func CreateBuild(c *gin.Context) {
)
}

// skipEmptyBuild checks if the build should be skipped due to it
// SkipEmptyBuild checks if the build should be skipped due to it
// not containing any steps besides init or clone.
//
//nolint:goconst // ignore init and clone constants
func skipEmptyBuild(p *pipeline.Build) string {
func SkipEmptyBuild(p *pipeline.Build) string {
if len(p.Stages) == 1 {
if p.Stages[0].Name == "init" {
return "skipping build since only init stage found"
Expand Down Expand Up @@ -1223,7 +1220,7 @@ func RestartBuild(c *gin.Context) {
r.SetPipelineType(pipelineType)

// skip the build if only the init or clone steps are found
skip := skipEmptyBuild(p)
skip := SkipEmptyBuild(p)
if skip != "" {
// set build to successful status
b.SetStatus(constants.StatusSkipped)
Expand Down Expand Up @@ -1273,7 +1270,7 @@ func RestartBuild(c *gin.Context) {
b.SetPipelineID(pipeline.GetID())

// create the objects from the pipeline in the database
err = planBuild(database.FromContext(c), p, b, r)
err = PlanBuild(database.FromContext(c), p, b, r)
if err != nil {
util.HandleError(c, http.StatusInternalServerError, err)

Expand Down Expand Up @@ -1301,7 +1298,7 @@ func RestartBuild(c *gin.Context) {
}

// publish the build to the queue
go publishToQueue(
go PublishToQueue(
queue.FromGinContext(c),
database.FromContext(c),
p,
Expand Down Expand Up @@ -1568,12 +1565,12 @@ func getPRNumberFromBuild(b *library.Build) (int, error) {
return strconv.Atoi(parts[2])
}

// planBuild is a helper function to plan the build for
// PlanBuild is a helper function to plan the build for
// execution. This creates all resources, like steps
// and services, for the build in the configured backend.
// TODO:
// - return build and error.
func planBuild(database database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo) error {
func PlanBuild(database database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo) error {
// update fields in build object
b.SetCreated(time.Now().UTC().Unix())

Expand Down
8 changes: 4 additions & 4 deletions api/build_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand All @@ -10,7 +10,7 @@ import (
"github.com/go-vela/types/pipeline"
)

func Test_skipEmptyBuild(t *testing.T) {
func Test_SkipEmptyBuild(t *testing.T) {
type args struct {
p *pipeline.Build
}
Expand Down Expand Up @@ -72,8 +72,8 @@ func Test_skipEmptyBuild(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := skipEmptyBuild(tt.args.p); got != tt.want {
t.Errorf("skipEmptyBuild() = %v, want %v", got, tt.want)
if got := SkipEmptyBuild(tt.args.p); got != tt.want {
t.Errorf("SkipEmptyBuild() = %v, want %v", got, tt.want)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion api/schedule/create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand Down
2 changes: 1 addition & 1 deletion api/schedule/delete.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand Down
2 changes: 1 addition & 1 deletion api/schedule/get.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand Down
2 changes: 1 addition & 1 deletion api/schedule/list.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
// Copyright (c) 2023 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.

Expand Down
3 changes: 1 addition & 2 deletions api/schedule/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import (
"net/http"
"time"

"github.com/go-vela/server/router/middleware/schedule"

"github.com/gin-gonic/gin"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/schedule"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/sirupsen/logrus"
Expand Down
10 changes: 5 additions & 5 deletions api/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ func PostWebhook(c *gin.Context) {
repo.SetPipelineType(pipelineType)

// skip the build if only the init or clone steps are found
skip := skipEmptyBuild(p)
skip := SkipEmptyBuild(p)
if skip != "" {
// set build to successful status
b.SetStatus(constants.StatusSkipped)
Expand Down Expand Up @@ -609,7 +609,7 @@ func PostWebhook(c *gin.Context) {
// using the same Number and thus create a constraint
// conflict; consider deleting the partially created
// build object in the database
err = planBuild(database.FromContext(c), p, b, repo)
err = PlanBuild(database.FromContext(c), p, b, repo)
if err != nil {
retErr := fmt.Errorf("%s: %w", baseErr, err)

Expand Down Expand Up @@ -696,7 +696,7 @@ func PostWebhook(c *gin.Context) {
}

// publish the build to the queue
go publishToQueue(
go PublishToQueue(
queue.FromGinContext(c),
database.FromContext(c),
p,
Expand All @@ -706,9 +706,9 @@ func PostWebhook(c *gin.Context) {
)
}

// publishToQueue is a helper function that creates
// PublishToQueue is a helper function that creates
// a build item and publishes it to the queue.
func publishToQueue(queue queue.Service, db database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo, u *library.User) {
func PublishToQueue(queue queue.Service, db database.Interface, p *pipeline.Build, b *library.Build, r *library.Repo, u *library.User) {
item := types.ToItem(p, b, r, u)

logrus.Infof("Converting queue item to json for build %d for %s", b.GetNumber(), r.GetFullName())
Expand Down
Loading

0 comments on commit 5a9a92d

Please sign in to comment.