Skip to content

Commit

Permalink
refactor: use nested repository type in worker code (#571)
Browse files Browse the repository at this point in the history
* init commit

* remove user from executor config

* validate comment fix
  • Loading branch information
ecrupper committed Apr 11, 2024
1 parent d872e67 commit 12e24cd
Show file tree
Hide file tree
Showing 51 changed files with 311 additions and 498 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ jobs:
# Check that go fmt ./... produces a zero diff; clean up any changes afterwards.
go fmt ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code)
# Check that go fix ./... produces a zero diff; clean up any changes afterwards.
go fix ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code)
#
# Renable this after https://github.com/golang/go/commit/7fd62ba821b1044e8e4077df052b0a1232672d57
# has been released.
# go fix ./... && git diff --exit-code; code=$?; git checkout -- .; (exit $code)
- name: validate spec
run: |
Expand Down
8 changes: 4 additions & 4 deletions api/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/gin-gonic/gin"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/types"
"github.com/go-vela/types/library"
"github.com/go-vela/worker/executor"
exec "github.com/go-vela/worker/router/middleware/executor"
)
Expand Down Expand Up @@ -45,7 +45,7 @@ func GetExecutor(c *gin.Context) {
var err error

e := exec.Retrieve(c)
executor := &library.Executor{}
executor := &api.Executor{}

// TODO: Add this information from the context or helpers on executor
// tmp.SetHost(executor.GetHost())
Expand Down Expand Up @@ -129,11 +129,11 @@ func GetExecutors(c *gin.Context) {
return
}

executors := []*library.Executor{}
executors := []*api.Executor{}

for id, executor := range e {
// create a temporary executor to append results to response
tmp := &library.Executor{}
tmp := &api.Executor{}

// TODO: Add this information from the context or helpers on executor
// tmp.SetHost(executor.GetHost())
Expand Down
6 changes: 3 additions & 3 deletions cmd/vela-worker/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/sirupsen/logrus"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/queue/models"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/pipeline"
Expand Down Expand Up @@ -99,7 +100,7 @@ func (w *Worker) exec(index int, config *api.Worker) error {
"host": w.Config.API.Address.Hostname(),
"repo": item.Repo.GetFullName(),
"runtime": w.Config.Runtime.Driver,
"user": item.User.GetName(),
"user": item.Repo.GetOwner().GetName(),
"version": v.Semantic(),
})

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

// handle stale item queued before a Vela upgrade or downgrade.
if item.ItemVersion != types.ItemVersion {
if item.ItemVersion != models.ItemVersion {
// If the ItemVersion is older or newer than what we expect, then it might
// not be safe to process the build. Fail the build and loop to the next item.
// TODO: Ask the server to re-compile and requeue the build instead of failing it.
Expand Down Expand Up @@ -181,7 +182,6 @@ func (w *Worker) exec(index int, config *api.Worker) error {
Build: item.Build,
Pipeline: pipeline.Sanitize(w.Config.Runtime.Driver),
Repo: item.Repo,
User: item.User,
Version: v.Semantic(),
})

Expand Down
4 changes: 0 additions & 4 deletions executor/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func TestExecutor_FromContext(t *testing.T) {
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithUser(_user),
linux.WithVelaClient(_client),
)
if err != nil {
Expand Down Expand Up @@ -105,7 +104,6 @@ func TestExecutor_FromGinContext(t *testing.T) {
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithUser(_user),
linux.WithVelaClient(_client),
)
if err != nil {
Expand Down Expand Up @@ -176,7 +174,6 @@ func TestExecutor_WithContext(t *testing.T) {
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithUser(_user),
linux.WithVelaClient(_client),
)
if err != nil {
Expand Down Expand Up @@ -215,7 +212,6 @@ func TestExecutor_WithGinContext(t *testing.T) {
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithUser(_user),
linux.WithVelaClient(_client),
)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion executor/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"sync"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
)
Expand All @@ -30,7 +31,7 @@ type Engine interface {
GetPipeline() (*pipeline.Build, error)
// GetRepo defines a function for the API
// that gets the current repo in execution.
GetRepo() (*library.Repo, error)
GetRepo() (*api.Repo, error)
// CancelBuild defines a function for the API
// that Cancels the current build in execution.
CancelBuild() (*library.Build, error)
Expand Down
60 changes: 37 additions & 23 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/gin-gonic/gin"
"github.com/google/go-cmp/cmp"

api "github.com/go-vela/server/api/types"
"github.com/go-vela/server/api/types/actions"
"github.com/go-vela/server/mock/server"

"github.com/go-vela/worker/executor/linux"
Expand Down Expand Up @@ -47,7 +49,6 @@ func TestExecutor_New(t *testing.T) {
linux.WithPipeline(_pipeline),
linux.WithRepo(_repo),
linux.WithRuntime(_runtime),
linux.WithUser(_user),
linux.WithVelaClient(_client),
linux.WithVersion("v1.0.0"),
)
Expand All @@ -61,7 +62,6 @@ func TestExecutor_New(t *testing.T) {
local.WithPipeline(_pipeline),
local.WithRepo(_repo),
local.WithRuntime(_runtime),
local.WithUser(_user),
local.WithVelaClient(_client),
local.WithVersion("v1.0.0"),
)
Expand All @@ -87,7 +87,6 @@ func TestExecutor_New(t *testing.T) {
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
User: _user,
Version: "v1.0.0",
},
want: nil,
Expand All @@ -104,7 +103,6 @@ func TestExecutor_New(t *testing.T) {
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
User: _user,
Version: "v1.0.0",
},
want: _linux,
Expand All @@ -120,7 +118,6 @@ func TestExecutor_New(t *testing.T) {
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
User: _user,
Version: "v1.0.0",
},
want: _local,
Expand All @@ -136,7 +133,6 @@ func TestExecutor_New(t *testing.T) {
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
User: _user,
Version: "v1.0.0",
},
want: nil,
Expand All @@ -152,7 +148,6 @@ func TestExecutor_New(t *testing.T) {
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
User: _user,
Version: "v1.0.0",
},
want: nil,
Expand All @@ -168,7 +163,6 @@ func TestExecutor_New(t *testing.T) {
Pipeline: _pipeline,
Repo: _repo,
Runtime: _runtime,
User: _user,
Version: "v1.0.0",
},
want: nil,
Expand Down Expand Up @@ -267,7 +261,39 @@ var (
},
}

_repo = &library.Repo{
_user = &library.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),
}

_allowEvents = &api.Events{
Push: &actions.Push{
Branch: vela.Bool(true),
Tag: vela.Bool(true),
},
PullRequest: &actions.Pull{
Opened: vela.Bool(true),
Synchronize: vela.Bool(true),
Edited: vela.Bool(true),
Reopened: vela.Bool(true),
Labeled: vela.Bool(true),
Unlabeled: vela.Bool(true),
},
Comment: &actions.Comment{
Created: vela.Bool(true),
Edited: vela.Bool(true),
},
Deployment: &actions.Deploy{
Created: vela.Bool(true),
},
}

_repo = &api.Repo{
ID: vela.Int64(1),
Org: vela.String("github"),
Name: vela.String("octocat"),
Expand All @@ -280,19 +306,7 @@ var (
Private: vela.Bool(false),
Trusted: vela.Bool(false),
Active: vela.Bool(true),
AllowPull: vela.Bool(false),
AllowPush: vela.Bool(true),
AllowDeploy: vela.Bool(false),
AllowTag: vela.Bool(false),
}

_user = &library.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),
AllowEvents: _allowEvents,
Owner: _user,
}
)
3 changes: 2 additions & 1 deletion executor/linux/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"time"

api "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 Down Expand Up @@ -35,7 +36,7 @@ func (c *client) GetPipeline() (*pipeline.Build, error) {
}

// GetRepo gets the current repo in execution.
func (c *client) GetRepo() (*library.Repo, error) {
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")
Expand Down
2 changes: 1 addition & 1 deletion executor/linux/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ func (c *client) ExecBuild(ctx context.Context) error {

// load any lazy secrets into the container environment
c.err = loadLazySecrets(c, _step)
if err != nil {
if c.err != nil {
return fmt.Errorf("unable to plan step: %w", c.err)
}

Expand Down
Loading

0 comments on commit 12e24cd

Please sign in to comment.