added some initial code for build list. will revisit.

This commit is contained in:
Brad Rydzewski 2015-04-10 23:11:41 -07:00
parent 087f92f41f
commit ab3df4bd65
2 changed files with 36 additions and 4 deletions

View file

@ -26,10 +26,32 @@ func (db *DB) GetBuild(repo string, build int) (*common.Build, error) {
// GetBuildList gets a list of recent builds for the
// named repository.
func (db *DB) GetBuildList(repo string) ([]*common.Build, error) {
// get the last build sequence number (stored in key in `bucketBuildSeq`)
// get all builds where build number > sequent-20
// github.com/foo/bar/{number}
return nil, nil
// TODO (bradrydzewski) we can do this more efficiently
var builds []*common.Build
build, err := db.GetBuildLast(repo)
if err == ErrKeyNotFound {
return builds, nil
} else if err != nil {
return nil, err
}
err = db.View(func(t *bolt.Tx) error {
pos := build.Number - 25
if pos < 1 {
pos = 1
}
for i := pos; i <= build.Number; i++ {
key := []byte(repo + "/" + strconv.Itoa(i))
build := &common.Build{}
err = get(t, bucketBuild, key, build)
if err != nil {
return err
}
builds = append(builds, build)
}
return nil
})
return builds, err
}
// GetBuildLast gets the last executed build for the

View file

@ -52,5 +52,15 @@ func TestBuild(t *testing.T) {
g.Assert(build.State).Equal("pending")
g.Assert(build.Number).Equal(3)
})
g.It("Should get the recent list of builds", func() {
db.InsertBuild(repo, &common.Build{State: "success"})
db.InsertBuild(repo, &common.Build{State: "success"})
db.InsertBuild(repo, &common.Build{State: "pending"})
builds, err := db.GetBuildList(repo)
g.Assert(err).Equal(nil)
g.Assert(len(builds)).Equal(3)
})
})
}