From 7be93921bdb3aef904b1ce0aad0f42a9ce29b9d9 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Wed, 7 Oct 2015 17:53:28 -0700 Subject: [PATCH] ability to get last build before N --- controller/build.go | 21 +++++++++++++-------- controller/hook.go | 2 +- model/build.go | 16 ++++++++++++++++ model/build_test.go | 37 ++++++++++++++++++++++++++++++++++++- 4 files changed, 66 insertions(+), 10 deletions(-) diff --git a/controller/build.go b/controller/build.go index fac9be2e..3bc19fed 100644 --- a/controller/build.go +++ b/controller/build.go @@ -218,16 +218,21 @@ func PostBuild(c *gin.Context) { c.JSON(202, build) + // get the previous build so taht we can send + // on status change notifications + last, _ := model.GetBuildLastBefore(db, repo, build.Branch, build.ID) + engine_ := context.Engine(c) go engine_.Schedule(&engine.Task{ - User: user, - Repo: repo, - Build: build, - Jobs: jobs, - Keys: key, - Netrc: netrc, - Config: string(raw), - Secret: string(sec), + User: user, + Repo: repo, + Build: build, + BuildPrev: last, + Jobs: jobs, + Keys: key, + Netrc: netrc, + Config: string(raw), + Secret: string(sec), System: &model.System{ Link: httputil.GetURL(c.Request), Plugins: strings.Split(os.Getenv("PLUGIN_FILTER"), " "), diff --git a/controller/hook.go b/controller/hook.go index 05b4f02b..28555fe7 100644 --- a/controller/hook.go +++ b/controller/hook.go @@ -203,7 +203,7 @@ func PostHook(c *gin.Context) { // get the previous build so taht we can send // on status change notifications - last, _ := model.GetBuildLast(db, repo, build.Branch) + last, _ := model.GetBuildLastBefore(db, repo, build.Branch, build.ID) engine_ := context.Engine(c) go engine_.Schedule(&engine.Task{ diff --git a/model/build.go b/model/build.go index e21dff4b..e7ce39f0 100644 --- a/model/build.go +++ b/model/build.go @@ -66,6 +66,12 @@ func GetBuildLast(db meddler.DB, repo *Repo, branch string) (*Build, error) { return build, err } +func GetBuildLastBefore(db meddler.DB, repo *Repo, branch string, number int64) (*Build, error) { + var build = new(Build) + var err = meddler.QueryRow(db, build, database.Rebind(buildLastBeforeQuery), repo.ID, branch, number) + return build, err +} + func GetBuildList(db meddler.DB, repo *Repo) ([]*Build, error) { var builds = []*Build{} var err = meddler.QueryAll(db, &builds, database.Rebind(buildListQuery), repo.ID) @@ -125,6 +131,16 @@ ORDER BY build_number DESC LIMIT 1 ` +const buildLastBeforeQuery = ` +SELECT * +FROM builds +WHERE build_repo_id = ? + AND build_branch = ? + AND build_id < ? +ORDER BY build_number DESC +LIMIT 1 +` + const buildCommitQuery = ` SELECT * FROM builds diff --git a/model/build_test.go b/model/build_test.go index 38bb9104..10ff5314 100644 --- a/model/build_test.go +++ b/model/build_test.go @@ -158,7 +158,7 @@ func TestBuild(t *testing.T) { g.Assert(build2.Branch).Equal(getbuild.Branch) }) - g.It("Should Get a Build by Commit", func() { + g.It("Should Get the last Build", func() { build1 := &Build{ RepoID: 1, Status: StatusFailure, @@ -185,6 +185,41 @@ func TestBuild(t *testing.T) { g.Assert(build2.Commit).Equal(getbuild.Commit) }) + g.It("Should Get the last Build Before Build N", func() { + build1 := &Build{ + RepoID: 1, + Status: StatusFailure, + Branch: "master", + Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac", + } + build2 := &Build{ + RepoID: 1, + Status: StatusSuccess, + Branch: "master", + Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa", + } + build3 := &Build{ + RepoID: 1, + Status: StatusRunning, + Branch: "master", + Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa", + } + err1 := CreateBuild(db, build1, []*Job{}...) + err2 := CreateBuild(db, build2, []*Job{}...) + err3 := CreateBuild(db, build3, []*Job{}...) + getbuild, err4 := GetBuildLastBefore(db, &Repo{ID: 1}, build3.Branch, build3.ID) + g.Assert(err1 == nil).IsTrue() + g.Assert(err2 == nil).IsTrue() + g.Assert(err3 == nil).IsTrue() + g.Assert(err4 == nil).IsTrue() + g.Assert(build2.ID).Equal(getbuild.ID) + g.Assert(build2.RepoID).Equal(getbuild.RepoID) + g.Assert(build2.Number).Equal(getbuild.Number) + g.Assert(build2.Status).Equal(getbuild.Status) + g.Assert(build2.Branch).Equal(getbuild.Branch) + g.Assert(build2.Commit).Equal(getbuild.Commit) + }) + g.It("Should get recent Builds", func() { build1 := &Build{ RepoID: 1,