1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-12-30 10:11:23 +02:00

ability to get last build before N

This commit is contained in:
Brad Rydzewski 2015-10-07 17:53:28 -07:00
parent a63f942c1f
commit 7be93921bd
4 changed files with 66 additions and 10 deletions

View File

@ -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"), " "),

View File

@ -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{

View File

@ -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

View File

@ -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,