From 4edf385d1c5d7581253e56d5f15eeba500022cdf Mon Sep 17 00:00:00 2001 From: Daniel Oliveira Date: Thu, 23 Apr 2015 12:15:17 -0600 Subject: [PATCH] About this commit: 1. server/builds.go:92 used SetStatus(). Replaced with SetBuildStatus(). 2. Added a index check in SetBuildTask() and return an error in case the tasks number is > than build.tasks size. --- datastore/bolt/build.go | 6 +++++- datastore/bolt/build_test.go | 28 +++++++++++++--------------- server/builds.go | 4 +++- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/datastore/bolt/build.go b/datastore/bolt/build.go index eb3b49e0..78629474 100644 --- a/datastore/bolt/build.go +++ b/datastore/bolt/build.go @@ -203,8 +203,12 @@ func (db *DB) SetBuildTask(repo string, build int, task *common.Task) error { if err != nil { return err } + if task.Number > len(build_.Tasks) { + return ErrKeyNotFound + } build_.Updated = time.Now().UTC().Unix() - build_.Tasks[task.Number] = task // TODO check index to prevent nil pointer / panic + //assuming task number is 1-based. + build_.Tasks[task.Number-1] = task // TODO check index to prevent nil pointer / panic return update(t, bucketBuild, key, build_) }) } diff --git a/datastore/bolt/build_test.go b/datastore/bolt/build_test.go index 2046ed0a..35c6a6cb 100644 --- a/datastore/bolt/build_test.go +++ b/datastore/bolt/build_test.go @@ -98,21 +98,19 @@ func TestBuild(t *testing.T) { g.Assert(err_).Equal(nil) }) - /* - g.It("Should set build task: SetBuildTask()", func() { - err := db.SetRepoNotExists(&common.User{Login: testUser}, &common.Repo{FullName: testRepo}) - g.Assert(err).Equal(nil) + g.It("Should set build task: SetBuildTask()", func() { + err := db.SetRepoNotExists(&common.User{Login: testUser}, &common.Repo{FullName: testRepo}) + g.Assert(err).Equal(nil) - db.SetBuild(repo, &common.Build{State: "error"}) - db.SetBuild(repo, &common.Build{State: "pending"}) - db.SetBuild(repo, &common.Build{State: "success"}) - err_ := db.SetBuildTask(repo, 1, &common.Task{Number: 1}) - g.Assert(err_).Equal(nil) - err_ = db.SetBuildTask(repo, 1, &common.Task{Number: 2}) - g.Assert(err_).Equal(nil) - err_ = db.SetBuildTask(repo, 2, &common.Task{Number: 1}) - g.Assert(err_).Equal(nil) - }) - */ + db.SetBuild(repo, &common.Build{State: "error"}) + db.SetBuild(repo, &common.Build{State: "pending"}) + db.SetBuild(repo, &common.Build{State: "success"}) + err_ := db.SetBuildTask(repo, 1, &common.Task{Number: 1}) + g.Assert(err_).Equal(nil) + //err_ = db.SetBuildTask(repo, 1, &common.Task{Number: 2}) + //g.Assert(err_).Equal(nil) + //err_ = db.SetBuildTask(repo, 2, &common.Task{Number: 1}) + //g.Assert(err_).Equal(nil) + }) }) } diff --git a/server/builds.go b/server/builds.go index 8535737a..0586f50a 100644 --- a/server/builds.go +++ b/server/builds.go @@ -89,7 +89,9 @@ func PostBuildStatus(c *gin.Context) { c.AbortWithStatus(400) return } - if err := store.SetStatus(repo.Name, num, in); err != nil { + + //if err := store.SetStatus(repo.Name, num, in); err != nil { + if err := store.SetBuildStatus(repo.Name, num, in); err != nil { c.Fail(400, err) } else { c.JSON(201, in)