From cd186457844e84728d71c1c4073dd07f6dd9057b Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 15 Apr 2015 23:45:24 -0700 Subject: [PATCH] contemplating nested storage of tasks and statuses --- common/build.go | 11 ++++++++- datastore/bolt/build.go | 53 +++++++++++++++++++++++++++++++++++++++++ datastore/datastore.go | 16 +++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/common/build.go b/common/build.go index 305542fcd..7e236db02 100644 --- a/common/build.go +++ b/common/build.go @@ -12,13 +12,14 @@ const ( type Build struct { Number int `json:"number"` State string `json:"state"` - Tasks int `json:"task_count"` Duration int64 `json:"duration"` Started int64 `json:"started_at"` Finished int64 `json:"finished_at"` Created int64 `json:"created_at"` Updated int64 `json:"updated_at"` + // Tasks int `json:"task_count"` + // Commit represents the commit data send in the // post-commit hook. This will not be populated when // a pull requests. @@ -28,6 +29,14 @@ type Build struct { // in the post-commit hook. This will only be populated // when a pull request. PullRequest *PullRequest `json:"pull_request,omitempty"` + + // Statuses represents a list of build statuses used + // to annotate the build. + Statuses []*Status `json:"statuses,omitempty"` + + // Tasks represents a list of build tasks. A build is + // comprised of one or many tasks. + Tasks []*Task `json:"tasks,omitempty"` } type Status struct { diff --git a/datastore/bolt/build.go b/datastore/bolt/build.go index 26e923673..3d20a1be0 100644 --- a/datastore/bolt/build.go +++ b/datastore/bolt/build.go @@ -77,6 +77,9 @@ func (db *DB) BuildLast(repo string) (*common.Build, error) { func (db *DB) SetBuild(repo string, build *common.Build) error { repokey := []byte(repo) build.Updated = time.Now().UTC().Unix() + if build.Created == 0 { + build.Created = build.Updated + } return db.Update(func(t *bolt.Tx) error { @@ -155,3 +158,53 @@ func (db *DB) SetStatus(repo string, build int, status *common.Status) error { return update(t, bucketBuildStatus, key, status) }) } + +// Experimental + +func (db *DB) SetBuildState(repo string, build *common.Build) error { + key := []byte(repo + "/" + strconv.Itoa(build.Number)) + + return db.Update(func(t *bolt.Tx) error { + build_ := &common.Build{} + err := get(t, bucketBuild, key, build_) + if err != nil { + return err + } + build_.Updated = time.Now().UTC().Unix() + build_.Duration = build.Duration + build_.Started = build.Started + build_.Finished = build.Finished + build_.State = build.State + return update(t, bucketBuild, key, build_) + }) +} + +func (db *DB) SetBuildStatus(repo string, build int, status *common.Status) error { + key := []byte(repo + "/" + strconv.Itoa(build)) + + return db.Update(func(t *bolt.Tx) error { + build_ := &common.Build{} + err := get(t, bucketBuild, key, build_) + if err != nil { + return err + } + build_.Updated = time.Now().UTC().Unix() + build_.Statuses = append(build_.Statuses, status) + return update(t, bucketBuild, key, build_) + }) +} + +func (db *DB) SetBuildTask(repo string, build int, task *common.Task) error { + key := []byte(repo + "/" + strconv.Itoa(build)) + + return db.Update(func(t *bolt.Tx) error { + build_ := &common.Build{} + err := get(t, bucketBuild, key, build_) + if err != nil { + return err + } + build_.Updated = time.Now().UTC().Unix() + build_.Tasks[task.Number] = task // TODO check index to prevent nil pointer / panic + return update(t, bucketBuild, key, build_) + }) +} diff --git a/datastore/datastore.go b/datastore/datastore.go index a26b0aaec..3bab2c5dc 100644 --- a/datastore/datastore.go +++ b/datastore/datastore.go @@ -138,4 +138,20 @@ type Datastore interface { // SetLogs inserts or updates a task logs for the // named repository and build number. SetLogs(string, int, int, []byte) error + + // Experimental + + // SetBuildState updates an existing build's start time, + // finish time, duration and state. No other fields are + // updated. + SetBuildState(string, *common.Build) error + + // SetBuildStatus appends a new build status to an + // existing build record. + SetBuildStatus(string, int, *common.Status) error + + // SetBuildTask updates an existing build task. The build + // and task must already exist. If the task does not exist + // an error is returned. + SetBuildTask(string, int, *common.Task) error }