Skip to content

Commit

Permalink
refactor(build)!: move build from types and nest the object (#1111)
Browse files Browse the repository at this point in the history
* init and broken commit

* working for now

* integration testing fixes

* more cleanup and remove repo usage where not needed

* address feedback

* remove redundant return on a couple extra funcs

---------

Co-authored-by: David May <49894298+wass3rw3rk@users.noreply.github.com>
  • Loading branch information
ecrupper and wass3rw3rk authored Apr 24, 2024
1 parent 71bfd94 commit b55aa2b
Show file tree
Hide file tree
Showing 251 changed files with 7,410 additions and 3,365 deletions.
4 changes: 2 additions & 2 deletions api/admin/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
)

// swagger:operation GET /api/v1/admin/builds/queue admin AllBuildsQueue
Expand Down Expand Up @@ -107,7 +107,7 @@ func UpdateBuild(c *gin.Context) {
ctx := c.Request.Context()

// capture body from API request
input := new(library.Build)
input := new(types.Build)

err := c.Bind(input)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/build/approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func ApproveBuild(c *gin.Context) {
ctx,
queue.FromGinContext(c),
database.FromContext(c),
models.ToItem(b, r),
models.ToItem(b),
b.GetHost(),
)

Expand Down
13 changes: 6 additions & 7 deletions api/build/auto_cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ import (
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal/token"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
)

// AutoCancel is a helper function that checks to see if any pending or running
// builds for the repo can be replaced by the current build.
func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *types.Repo, cancelOpts *pipeline.CancelOptions) (bool, error) {
func AutoCancel(c *gin.Context, b *types.Build, rB *types.Build, cancelOpts *pipeline.CancelOptions) (bool, error) {
// if build is the current build, continue
if rB.GetID() == b.GetID() {
return false, nil
Expand Down Expand Up @@ -52,7 +51,7 @@ func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *types.Re
}
case strings.EqualFold(status, constants.StatusRunning) && cancelOpts.Running:
// call cancelRunning routine for builds already running on worker
err := cancelRunning(c, rB, r)
err := cancelRunning(c, rB)
if err != nil {
return false, err
}
Expand All @@ -75,7 +74,7 @@ func AutoCancel(c *gin.Context, b *library.Build, rB *library.Build, r *types.Re

// cancelRunning is a helper function that determines the executor currently running a build and sends an API call
// to that executor's worker to cancel the build.
func cancelRunning(c *gin.Context, b *library.Build, r *types.Repo) error {
func cancelRunning(c *gin.Context, b *types.Build) error {
e := new([]types.Executor)
// retrieve the worker
w, err := database.FromContext(c).GetWorkerForHostname(c, b.GetHost())
Expand Down Expand Up @@ -133,7 +132,7 @@ func cancelRunning(c *gin.Context, b *library.Build, r *types.Repo) error {

for _, executor := range *e {
// check each executor on the worker running the build to see if it's running the build we want to cancel
if strings.EqualFold(executor.Repo.GetFullName(), r.GetFullName()) && *executor.GetBuild().Number == b.GetNumber() {
if executor.Build.GetID() == b.GetID() {
// prepare the request to the worker
client := http.DefaultClient
client.Timeout = 30 * time.Second
Expand Down Expand Up @@ -189,7 +188,7 @@ func cancelRunning(c *gin.Context, b *library.Build, r *types.Repo) error {

// isCancelable is a helper function that determines whether a `target` build should be auto-canceled
// given a current build that intends to supersede it.
func isCancelable(target *library.Build, current *library.Build) bool {
func isCancelable(target *types.Build, current *types.Build) bool {
switch target.GetEvent() {
case constants.EventPush:
// target is cancelable if current build is also a push event and the branches are the same
Expand All @@ -206,7 +205,7 @@ func isCancelable(target *library.Build, current *library.Build) bool {

// ShouldAutoCancel is a helper function that determines whether or not a build should be eligible to
// auto cancel currently running / pending builds.
func ShouldAutoCancel(opts *pipeline.CancelOptions, b *library.Build, defaultBranch string) bool {
func ShouldAutoCancel(opts *pipeline.CancelOptions, b *types.Build, defaultBranch string) bool {
// if the build is pending approval, it should always be eligible to auto cancel
if strings.EqualFold(b.GetStatus(), constants.StatusPendingApproval) {
return true
Expand Down
52 changes: 26 additions & 26 deletions api/build/auto_cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package build
import (
"testing"

"github.com/go-vela/server/api/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
)

Expand All @@ -25,53 +25,53 @@ func Test_isCancelable(t *testing.T) {

tests := []struct {
name string
target *library.Build
current *library.Build
target *types.Build
current *types.Build
want bool
}{
{
name: "Wrong Event",
target: &library.Build{
target: &types.Build{
Event: &tagEvent,
Branch: &branchDev,
},
current: &library.Build{
current: &types.Build{
Event: &pushEvent,
Branch: &branchDev,
},
want: false,
},
{
name: "Cancelable Push",
target: &library.Build{
target: &types.Build{
Event: &pushEvent,
Branch: &branchDev,
},
current: &library.Build{
current: &types.Build{
Event: &pushEvent,
Branch: &branchDev,
},
want: true,
},
{
name: "Push Branch Mismatch",
target: &library.Build{
target: &types.Build{
Event: &pushEvent,
Branch: &branchDev,
},
current: &library.Build{
current: &types.Build{
Event: &pushEvent,
Branch: &branchPatch,
},
want: false,
},
{
name: "Event Mismatch",
target: &library.Build{
target: &types.Build{
Event: &pushEvent,
Branch: &branchDev,
},
current: &library.Build{
current: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
HeadRef: &branchPatch,
Expand All @@ -80,13 +80,13 @@ func Test_isCancelable(t *testing.T) {
},
{
name: "Cancelable Pull",
target: &library.Build{
target: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
HeadRef: &branchPatch,
EventAction: &actionOpened,
},
current: &library.Build{
current: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
HeadRef: &branchPatch,
Expand All @@ -96,13 +96,13 @@ func Test_isCancelable(t *testing.T) {
},
{
name: "Pull Head Ref Mismatch",
target: &library.Build{
target: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
HeadRef: &branchPatch,
EventAction: &actionSync,
},
current: &library.Build{
current: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
HeadRef: &branchDev,
Expand All @@ -112,13 +112,13 @@ func Test_isCancelable(t *testing.T) {
},
{
name: "Pull Ineligible Action",
target: &library.Build{
target: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
HeadRef: &branchPatch,
EventAction: &actionEdited,
},
current: &library.Build{
current: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
HeadRef: &branchDev,
Expand Down Expand Up @@ -154,7 +154,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
tests := []struct {
name string
opts *pipeline.CancelOptions
build *library.Build
build *types.Build
branch string
want bool
}{
Expand All @@ -165,7 +165,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
Pending: true,
DefaultBranch: true,
},
build: &library.Build{
build: &types.Build{
Event: &tagEvent,
Branch: &branchPatch,
},
Expand All @@ -179,7 +179,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
Pending: false,
DefaultBranch: false,
},
build: &library.Build{
build: &types.Build{
Event: &pushEvent,
Branch: &branchPatch,
},
Expand All @@ -193,7 +193,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
Pending: true,
DefaultBranch: false,
},
build: &library.Build{
build: &types.Build{
Event: &pushEvent,
Branch: &branchPatch,
},
Expand All @@ -207,7 +207,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
Pending: true,
DefaultBranch: true,
},
build: &library.Build{
build: &types.Build{
Event: &pushEvent,
Branch: &branchDev,
},
Expand All @@ -221,7 +221,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
Pending: true,
DefaultBranch: false,
},
build: &library.Build{
build: &types.Build{
Event: &pushEvent,
Branch: &branchDev,
},
Expand All @@ -235,7 +235,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
Pending: true,
DefaultBranch: false,
},
build: &library.Build{
build: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
EventAction: &actionSync,
Expand All @@ -250,7 +250,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
Pending: true,
DefaultBranch: false,
},
build: &library.Build{
build: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
EventAction: &actionOpened,
Expand All @@ -265,7 +265,7 @@ func Test_ShouldAutoCancel(t *testing.T) {
Pending: false,
DefaultBranch: false,
},
build: &library.Build{
build: &types.Build{
Event: &pullEvent,
Branch: &branchDev,
EventAction: &actionOpened,
Expand Down
3 changes: 1 addition & 2 deletions api/build/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"net/http"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -105,7 +104,7 @@ func CancelBuild(c *gin.Context) {

for _, executor := range e {
// check each executor on the worker running the build to see if it's running the build we want to cancel
if strings.EqualFold(executor.Repo.GetFullName(), r.GetFullName()) && *executor.GetBuild().Number == b.GetNumber() {
if executor.Build.GetID() == b.GetID() {
// prepare the request to the worker
client := http.DefaultClient
client.Timeout = 30 * time.Second
Expand Down
3 changes: 2 additions & 1 deletion api/build/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/sirupsen/logrus"

"github.com/go-vela/server/api/types"
"github.com/go-vela/server/database"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
Expand All @@ -18,7 +19,7 @@ import (
// without execution. This will kill all resources,
// like steps and services, for the build in the
// configured backend.
func CleanBuild(ctx context.Context, database database.Interface, b *library.Build, services []*library.Service, steps []*library.Step, e error) {
func CleanBuild(ctx context.Context, database database.Interface, b *types.Build, services []*library.Service, steps []*library.Step, e error) {
// update fields in build object
b.SetError(fmt.Sprintf("unable to publish to queue: %s", e.Error()))
b.SetStatus(constants.StatusError)
Expand Down
13 changes: 6 additions & 7 deletions api/build/compile_publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import (

// CompileAndPublishConfig is a struct that contains information for the CompileAndPublish function.
type CompileAndPublishConfig struct {
Build *library.Build
Repo *types.Repo
Build *types.Build
Metadata *internal.Metadata
BaseErr string
Source string
Expand All @@ -50,11 +49,11 @@ func CompileAndPublish(
compiler compiler.Engine,
queue queue.Service,
) (*pipeline.Build, *models.Item, int, error) {
logrus.Debugf("generating queue items for build %s/%d", cfg.Repo.GetFullName(), cfg.Build.GetNumber())
logrus.Debugf("generating queue items for build %s/%d", cfg.Build.GetRepo().GetFullName(), cfg.Build.GetNumber())

// assign variables from form for readibility
r := cfg.Repo
u := cfg.Repo.GetOwner()
r := cfg.Build.GetRepo()
u := cfg.Build.GetRepo().GetOwner()
b := cfg.Build
baseErr := cfg.BaseErr

Expand Down Expand Up @@ -410,12 +409,12 @@ func CompileAndPublish(
return nil, nil, http.StatusInternalServerError, retErr
}

return p, models.ToItem(b, repo), http.StatusCreated, nil
return p, models.ToItem(b), http.StatusCreated, nil
}

// getPRNumberFromBuild is a helper function to
// extract the pull request number from a Build.
func getPRNumberFromBuild(b *library.Build) (int, error) {
func getPRNumberFromBuild(b *types.Build) (int, error) {
// parse out pull request number from base ref
//
// pattern: refs/pull/1/head
Expand Down
Loading

0 comments on commit b55aa2b

Please sign in to comment.