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

refactor(build)!: move build from types and nest the object #1111

Merged
merged 9 commits into from
Apr 24, 2024
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
9 changes: 4 additions & 5 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, r *types.Repo, cancelOpts *pipeline.CancelOptions) (bool, error) {
// if build is the current build, continue
if rB.GetID() == b.GetID() {
return false, nil
Expand Down Expand Up @@ -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, r *types.Repo) error {
e := new([]types.Executor)
// retrieve the worker
w, err := database.FromContext(c).GetWorkerForHostname(c, b.GetHost())
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: 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
7 changes: 4 additions & 3 deletions api/build/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"

"github.com/go-vela/server/api/types"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/database"
"github.com/go-vela/server/internal"
Expand All @@ -18,7 +19,6 @@ import (
"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"
)

// swagger:operation POST /api/v1/repos/{org}/{repo}/builds builds CreateBuild
Expand Down Expand Up @@ -99,7 +99,7 @@ func CreateBuild(c *gin.Context) {
logger.Infof("creating new build for repo %s", r.GetFullName())

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

err := c.Bind(input)
if err != nil {
Expand All @@ -110,6 +110,8 @@ func CreateBuild(c *gin.Context) {
return
}

input.SetRepo(r)

// verify the build has a valid event and the repo allows that event type
if !r.GetAllowEvents().Allowed(input.GetEvent(), input.GetEventAction()) {
retErr := fmt.Errorf("unable to create new build: %s does not have %s events enabled", r.GetFullName(), input.GetEvent())
Expand All @@ -122,7 +124,6 @@ func CreateBuild(c *gin.Context) {
// create config
config := CompileAndPublishConfig{
Build: input,
Repo: r,
Metadata: m,
BaseErr: "unable to create build",
Source: "create",
Expand Down
Loading
Loading