Skip to content

Commit

Permalink
refactor(build): update library build to API build and remove repo fr…
Browse files Browse the repository at this point in the history
…om executor (#576)

* init commit

* fix mock, add new server and sdk, revert compose
  • Loading branch information
ecrupper authored Apr 24, 2024
1 parent 34d5b49 commit 3d43998
Show file tree
Hide file tree
Showing 63 changed files with 490 additions and 878 deletions.
20 changes: 0 additions & 20 deletions api/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ func GetExecutor(c *gin.Context) {
return
}

// get repo on executor
executor.Repo, err = e.GetRepo()
if err != nil {
msg := fmt.Errorf("unable to retrieve repo: %w", err).Error()

c.AbortWithStatusJSON(http.StatusInternalServerError, types.Error{Message: &msg})

return
}

c.JSON(http.StatusOK, executor)
}

Expand Down Expand Up @@ -161,16 +151,6 @@ func GetExecutors(c *gin.Context) {
return
}

// get repo on executor
tmp.Repo, err = executor.GetRepo()
if err != nil {
msg := fmt.Errorf("unable to retrieve repo: %w", err).Error()

c.AbortWithStatusJSON(http.StatusInternalServerError, types.Error{Message: &msg})

return
}

executors = append(executors, tmp)
}

Expand Down
6 changes: 3 additions & 3 deletions api/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ import (
func GetRepo(c *gin.Context) {
e := executor.Retrieve(c)

repo, err := e.GetRepo()
build, err := e.GetBuild()
if err != nil {
msg := fmt.Errorf("unable to read repo: %w", err).Error()
msg := fmt.Errorf("unable to read build: %w", err).Error()

c.AbortWithStatusJSON(http.StatusInternalServerError, types.Error{Message: &msg})

return
}

c.JSON(http.StatusOK, repo)
c.JSON(http.StatusOK, build.GetRepo())
}
15 changes: 7 additions & 8 deletions cmd/vela-worker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (w *Worker) exec(index int, config *api.Worker) error {
}

// retrieve a build token from the server to setup the execBuildClient
bt, resp, err := w.VelaClient.Build.GetBuildToken(item.Repo.GetOrg(), item.Repo.GetName(), item.Build.GetNumber())
bt, resp, err := w.VelaClient.Build.GetBuildToken(item.Build.GetRepo().GetOrg(), item.Build.GetRepo().GetName(), item.Build.GetNumber())
if err != nil {
logrus.Errorf("unable to retrieve build token: %s", err)

Expand All @@ -78,7 +78,7 @@ func (w *Worker) exec(index int, config *api.Worker) error {
}

// request build executable containing pipeline.Build data using exec client
execBuildExecutable, _, err := execBuildClient.Build.GetBuildExecutable(item.Repo.GetOrg(), item.Repo.GetName(), item.Build.GetNumber())
execBuildExecutable, _, err := execBuildClient.Build.GetBuildExecutable(item.Build.GetRepo().GetOrg(), item.Build.GetRepo().GetName(), item.Build.GetNumber())
if err != nil {
return err
}
Expand All @@ -98,9 +98,9 @@ func (w *Worker) exec(index int, config *api.Worker) error {
"build": item.Build.GetNumber(),
"executor": w.Config.Executor.Driver,
"host": w.Config.API.Address.Hostname(),
"repo": item.Repo.GetFullName(),
"repo": item.Build.GetRepo().GetFullName(),
"runtime": w.Config.Runtime.Driver,
"user": item.Repo.GetOwner().GetName(),
"user": item.Build.GetRepo().GetOwner().GetName(),
"version": v.Semantic(),
})

Expand Down Expand Up @@ -137,7 +137,7 @@ func (w *Worker) exec(index int, config *api.Worker) error {
build.SetStatus(constants.StatusError)
build.SetFinished(time.Now().UTC().Unix())

_, _, err := execBuildClient.Build.Update(item.Repo.GetOrg(), item.Repo.GetName(), build)
_, _, err := execBuildClient.Build.Update(build)
if err != nil {
logrus.Errorf("Unable to set build status to %s: %s", constants.StatusFailure, err)
return err
Expand Down Expand Up @@ -181,7 +181,6 @@ func (w *Worker) exec(index int, config *api.Worker) error {
Runtime: w.Runtime,
Build: item.Build,
Pipeline: pipeline.Sanitize(w.Config.Runtime.Driver),
Repo: item.Repo,
Version: v.Semantic(),
})

Expand Down Expand Up @@ -237,9 +236,9 @@ func (w *Worker) exec(index int, config *api.Worker) error {
// capture the configured build timeout
t := w.Config.Build.Timeout
// check if the repository has a custom timeout
if item.Repo.GetTimeout() > 0 {
if item.Build.GetRepo().GetTimeout() > 0 {
// update timeout variable to repository custom timeout
t = time.Duration(item.Repo.GetTimeout()) * time.Minute
t = time.Duration(item.Build.GetRepo().GetTimeout()) * time.Minute
}

// create a build context (from a background context
Expand Down
4 changes: 2 additions & 2 deletions cmd/vela-worker/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (

_ "github.com/joho/godotenv/autoload"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/queue"
"github.com/go-vela/types/library"
"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime"
)
Expand Down Expand Up @@ -136,7 +136,7 @@ func run(c *cli.Context) error {

RegisterToken: make(chan string, 1),

RunningBuilds: make([]*library.Build, 0),
RunningBuilds: make([]*api.Build, 0),
}

// set the worker address if no flag was provided
Expand Down
4 changes: 2 additions & 2 deletions cmd/vela-worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"time"

"github.com/go-vela/sdk-go/vela"
api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/queue"
"github.com/go-vela/types/library"
"github.com/go-vela/worker/executor"
"github.com/go-vela/worker/runtime"
)
Expand Down Expand Up @@ -69,7 +69,7 @@ type (
VelaClient *vela.Client
RegisterToken chan string
CheckedIn bool
RunningBuilds []*library.Build
RunningBuilds []*api.Build
QueueCheckedIn bool
RunningBuildsMutex sync.Mutex
}
Expand Down
4 changes: 0 additions & 4 deletions executor/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func TestExecutor_FromContext(t *testing.T) {
_engine, err := linux.New(
linux.WithBuild(_build),
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithVelaClient(_client),
)
Expand Down Expand Up @@ -99,7 +98,6 @@ func TestExecutor_FromGinContext(t *testing.T) {
_engine, err := linux.New(
linux.WithBuild(_build),
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithVelaClient(_client),
)
Expand Down Expand Up @@ -169,7 +167,6 @@ func TestExecutor_WithContext(t *testing.T) {
_engine, err := linux.New(
linux.WithBuild(_build),
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithVelaClient(_client),
)
Expand Down Expand Up @@ -207,7 +204,6 @@ func TestExecutor_WithGinContext(t *testing.T) {
_engine, err := linux.New(
linux.WithBuild(_build),
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithVelaClient(_client),
)
Expand Down
8 changes: 2 additions & 6 deletions executor/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"sync"

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

Expand All @@ -25,16 +24,13 @@ type Engine interface {

// GetBuild defines a function for the API
// that gets the current build in execution.
GetBuild() (*library.Build, error)
GetBuild() (*api.Build, error)
// GetPipeline defines a function for the API
// that gets the current pipeline in execution.
GetPipeline() (*pipeline.Build, error)
// GetRepo defines a function for the API
// that gets the current repo in execution.
GetRepo() (*api.Repo, error)
// CancelBuild defines a function for the API
// that Cancels the current build in execution.
CancelBuild() (*library.Build, error)
CancelBuild() (*api.Build, error)

// Build Engine interface functions

Expand Down
67 changes: 29 additions & 38 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"github.com/go-vela/server/api/types/actions"
"github.com/go-vela/server/mock/server"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/go-vela/worker/executor/linux"
"github.com/go-vela/worker/executor/local"
Expand Down Expand Up @@ -43,7 +42,6 @@ func TestExecutor_New(t *testing.T) {
linux.WithHostname("localhost"),
linux.WithMaxLogSize(2097152),
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithVelaClient(_client),
linux.WithVersion("v1.0.0"),
Expand All @@ -56,7 +54,6 @@ func TestExecutor_New(t *testing.T) {
local.WithBuild(_build),
local.WithHostname("localhost"),
local.WithPipeline(_pipeline),
local.WithRepo(_repo),
local.WithRuntime(_runtime),
local.WithVelaClient(_client),
local.WithVersion("v1.0.0"),
Expand All @@ -81,7 +78,6 @@ func TestExecutor_New(t *testing.T) {
Client: _client,
Driver: constants.DriverDarwin,
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
Version: "v1.0.0",
},
Expand All @@ -97,7 +93,6 @@ func TestExecutor_New(t *testing.T) {
Driver: constants.DriverLinux,
MaxLogSize: 2097152,
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
Version: "v1.0.0",
},
Expand All @@ -112,7 +107,6 @@ func TestExecutor_New(t *testing.T) {
Client: _client,
Driver: "local",
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
Version: "v1.0.0",
},
Expand All @@ -127,7 +121,6 @@ func TestExecutor_New(t *testing.T) {
Client: _client,
Driver: constants.DriverWindows,
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
Version: "v1.0.0",
},
Expand All @@ -142,7 +135,6 @@ func TestExecutor_New(t *testing.T) {
Client: _client,
Driver: "invalid",
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
Version: "v1.0.0",
},
Expand All @@ -157,7 +149,6 @@ func TestExecutor_New(t *testing.T) {
Client: _client,
Driver: "",
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
Version: "v1.0.0",
},
Expand Down Expand Up @@ -198,33 +189,6 @@ func TestExecutor_New(t *testing.T) {

// setup global variables used for testing.
var (
_build = &library.Build{
ID: vela.Int64(1),
Number: vela.Int(1),
Parent: vela.Int(1),
Event: vela.String("push"),
Status: vela.String("success"),
Error: vela.String(""),
Enqueued: vela.Int64(1563474077),
Created: vela.Int64(1563474076),
Started: vela.Int64(1563474077),
Finished: vela.Int64(0),
Deploy: vela.String(""),
Clone: vela.String("https://github.com/github/octocat.git"),
Source: vela.String("https://github.com/github/octocat/abcdefghi123456789"),
Title: vela.String("push received from https://github.com/github/octocat"),
Message: vela.String("First commit..."),
Commit: vela.String("48afb5bdc41ad69bf22588491333f7cf71135163"),
Sender: vela.String("OctoKitty"),
Author: vela.String("OctoKitty"),
Branch: vela.String("main"),
Ref: vela.String("refs/heads/main"),
BaseRef: vela.String(""),
Host: vela.String("example.company.com"),
Runtime: vela.String("docker"),
Distribution: vela.String("linux"),
}

_pipeline = &pipeline.Build{
Version: "1",
ID: "github_octocat_1",
Expand Down Expand Up @@ -257,11 +221,10 @@ var (
},
}

_user = &library.User{
_user = &api.User{
ID: vela.Int64(1),
Name: vela.String("octocat"),
Token: vela.String("superSecretToken"),
Hash: vela.String("MzM4N2MzMDAtNmY4Mi00OTA5LWFhZDAtNWIzMTlkNTJkODMy"),
Favorites: vela.Strings([]string{"github/octocat"}),
Active: vela.Bool(true),
Admin: vela.Bool(false),
Expand Down Expand Up @@ -305,4 +268,32 @@ var (
AllowEvents: _allowEvents,
Owner: _user,
}

_build = &api.Build{
ID: vela.Int64(1),
Number: vela.Int(1),
Repo: _repo,
Parent: vela.Int(1),
Event: vela.String("push"),
Status: vela.String("success"),
Error: vela.String(""),
Enqueued: vela.Int64(1563474077),
Created: vela.Int64(1563474076),
Started: vela.Int64(1563474077),
Finished: vela.Int64(0),
Deploy: vela.String(""),
Clone: vela.String("https://github.com/github/octocat.git"),
Source: vela.String("https://github.com/github/octocat/abcdefghi123456789"),
Title: vela.String("push received from https://github.com/github/octocat"),
Message: vela.String("First commit..."),
Commit: vela.String("48afb5bdc41ad69bf22588491333f7cf71135163"),
Sender: vela.String("OctoKitty"),
Author: vela.String("OctoKitty"),
Branch: vela.String("main"),
Ref: vela.String("refs/heads/main"),
BaseRef: vela.String(""),
Host: vela.String("example.company.com"),
Runtime: vela.String("docker"),
Distribution: vela.String("linux"),
}
)
14 changes: 2 additions & 12 deletions executor/linux/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

// GetBuild gets the current build in execution.
func (c *client) GetBuild() (*library.Build, error) {
func (c *client) GetBuild() (*api.Build, error) {
// check if the build resource is available
if c.build == nil {
return nil, fmt.Errorf("build resource not found")
Expand All @@ -35,20 +35,10 @@ func (c *client) GetPipeline() (*pipeline.Build, error) {
return c.pipeline, nil
}

// GetRepo gets the current repo in execution.
func (c *client) GetRepo() (*api.Repo, error) {
// check if the repo resource is available
if c.repo == nil {
return nil, fmt.Errorf("repo resource not found")
}

return c.repo, nil
}

// CancelBuild cancels the current build in execution.
//
//nolint:funlen // process of going through steps/services/stages is verbose and could be funcitonalized
func (c *client) CancelBuild() (*library.Build, error) {
func (c *client) CancelBuild() (*api.Build, error) {
// get the current build from the client
b, err := c.GetBuild()
if err != nil {
Expand Down
Loading

0 comments on commit 3d43998

Please sign in to comment.