Skip to content

Commit

Permalink
feat: implement PR requested changes
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Brecic <[email protected]>
  • Loading branch information
lbrecic committed Jul 30, 2024
1 parent 891b566 commit ac20c58
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
13 changes: 7 additions & 6 deletions internal/testing/server/build/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ func (s *InMemoryBuildStore) Find(hash string) (*build.Build, error) {
return result, nil
}

func (s *InMemoryBuildStore) List(state *string) ([]*build.Build, error) {
func (s *InMemoryBuildStore) List(filter *build.Filter) ([]*build.Build, error) {
builds := []*build.Build{}
for _, b := range s.builds {
if state != nil {
if string(b.State) != *state {
builds = append(builds, b)
}
} else {
if filter == nil {
builds = append(builds, b)
continue
}

if filter.State != nil && string(b.State) != *filter.State {
builds = append(builds, b)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewPoller(config PollerConfig) *BuildPoller {

func (p *BuildPoller) Poll() {
pendingState := string(BuildStatePending)
builds, err := p.buildStore.List(&pendingState)
builds, err := p.buildStore.List(&Filter{State: &pendingState})
if err != nil {
log.Error(err)
return
Expand Down
6 changes: 5 additions & 1 deletion pkg/build/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ import (
"errors"
)

type Filter struct {
State *string
}

type Store interface {
Find(hash string) (*Build, error)
List(state *string) ([]*Build, error)
List(filter *Filter) ([]*Build, error)
Save(build *Build) error
Delete(hash string) error
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/db/build_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,19 @@ func (b *BuildStore) Find(hash string) (*build.Build, error) {
return build, nil
}

func (b *BuildStore) List(state *string) ([]*build.Build, error) {
func (b *BuildStore) List(filter *build.Filter) ([]*build.Build, error) {
b.Lock.Lock()
defer b.Lock.Unlock()

buildDTOs := []BuildDTO{}
var tx *gorm.DB

if state == nil {
tx = b.db.Find(&buildDTOs)
} else {
tx = b.db.Where("state = ?", state).Find(&buildDTOs)
if filter != nil {
if filter.State != nil {
tx = b.db.Where("state = ?", *filter.State)
}
}
tx.Find(&buildDTOs)

if tx.Error != nil {
return nil, tx.Error
Expand Down

0 comments on commit ac20c58

Please sign in to comment.