diff --git a/controller/build.go b/controller/build.go index feb71f232..acebc505c 100644 --- a/controller/build.go +++ b/controller/build.go @@ -31,8 +31,12 @@ func GetBuilds(c *gin.Context) { } func GetBuild(c *gin.Context) { - repo := session.Repo(c) + if c.Param("number") == "latest" { + GetBuildLast(c) + return + } + repo := session.Repo(c) num, err := strconv.Atoi(c.Param("number")) if err != nil { c.AbortWithError(http.StatusBadRequest, err) @@ -54,6 +58,25 @@ func GetBuild(c *gin.Context) { c.IndentedJSON(http.StatusOK, &out) } +func GetBuildLast(c *gin.Context) { + repo := session.Repo(c) + branch := c.DefaultQuery("branch", repo.Branch) + + build, err := store.GetBuildLast(c, repo, branch) + if err != nil { + c.String(http.StatusInternalServerError, err.Error()) + return + } + jobs, _ := store.GetJobList(c, build) + + out := struct { + *model.Build + Jobs []*model.Job `json:"jobs"` + }{build, jobs} + + c.IndentedJSON(http.StatusOK, &out) +} + func GetBuildLogs(c *gin.Context) { repo := session.Repo(c) diff --git a/docs/swagger.yml b/docs/swagger.yml index 4406fca78..bd16eb83b 100644 --- a/docs/swagger.yml +++ b/docs/swagger.yml @@ -277,6 +277,42 @@ paths: description: | Unable to find the Repository or Build + + /repos/{owner}/{name}/builds/{number}: + get: + parameters: + - name: owner + in: path + type: string + description: owner of the repository + - name: name + in: path + type: string + description: name of the repository + - name: branch + in: query + type: string + description: name of the branch + required: false + - name: number + in: path + type: integer + description: sequential build number + tags: + - Builds + summary: Get the latest build + description: Returns the latest repository build. + security: + - accessToken: [] + responses: + 200: + description: The build. + schema: + $ref: "#/definitions/Build" + 404: + description: | + Unable to find the Repository or Build + post: parameters: - name: owner diff --git a/store/datastore/builds.go b/store/datastore/builds.go index 6ff91287d..5b819f326 100644 --- a/store/datastore/builds.go +++ b/store/datastore/builds.go @@ -103,6 +103,7 @@ SELECT * FROM builds WHERE build_repo_id = ? AND build_branch = ? + AND build_event = 'push' ORDER BY build_number DESC LIMIT 1 ` diff --git a/store/datastore/builds_test.go b/store/datastore/builds_test.go index c9d39732e..d775b94f1 100644 --- a/store/datastore/builds_test.go +++ b/store/datastore/builds_test.go @@ -165,12 +165,14 @@ func Test_buildstore(t *testing.T) { Status: model.StatusFailure, Branch: "master", Commit: "85f8c029b902ed9400bc600bac301a0aadb144ac", + Event: model.EventPush, } build2 := &model.Build{ RepoID: 1, Status: model.StatusSuccess, Branch: "master", Commit: "85f8c029b902ed9400bc600bac301a0aadb144aa", + Event: model.EventPush, } err1 := s.Builds().Create(build1, []*model.Job{}...) err2 := s.Builds().Create(build2, []*model.Job{}...) diff --git a/template/amber/swagger.amber b/template/amber/swagger.amber index d38c55143..fc7f1561b 100644 --- a/template/amber/swagger.amber +++ b/template/amber/swagger.amber @@ -57,7 +57,7 @@ block content p #{$result.Desc} aside h4 Endpoint - pre #{$op.Method} #{$op.Path} + pre #{$op.Method} /api#{$op.Path} each $param in $op.Params if $param.Example h4 Example Request