harness-drone/server/status.go

76 lines
1.6 KiB
Go
Raw Normal View History

2015-04-08 22:43:59 +00:00
package server
import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/drone/drone/common"
)
// GetStatus accepts a request to retrieve a build status
// from the datastore for the given repository and
// build number.
//
// GET /api/status/:owner/:name/:number/:context
//
func GetStatus(c *gin.Context) {
store := ToDatastore(c)
2015-04-08 22:43:59 +00:00
repo := ToRepo(c)
num, _ := strconv.Atoi(c.Params.ByName("number"))
ctx := c.Params.ByName("context")
status, err := store.Status(repo.FullName, num, ctx)
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(404, err)
} else {
c.JSON(200, status)
}
}
// PostStatus accepts a request to create a new build
// status. The created user status is returned in JSON
// format if successful.
//
// POST /api/status/:owner/:name/:number
//
func PostStatus(c *gin.Context) {
store := ToDatastore(c)
2015-04-08 22:43:59 +00:00
repo := ToRepo(c)
num, err := strconv.Atoi(c.Params.ByName("number"))
if err != nil {
c.Fail(400, err)
return
}
in := &common.Status{}
if !c.BindWith(in, binding.JSON) {
c.AbortWithStatus(400)
return
}
if err := store.SetStatus(repo.Name, num, in); err != nil {
2015-04-08 22:43:59 +00:00
c.Fail(400, err)
} else {
c.JSON(201, in)
}
}
// GetStatusList accepts a request to retrieve a list of
// all build status from the datastore for the given repository
// and build number.
//
// GET /api/status/:owner/:name/:number/:context
//
func GetStatusList(c *gin.Context) {
store := ToDatastore(c)
2015-04-08 22:43:59 +00:00
repo := ToRepo(c)
num, _ := strconv.Atoi(c.Params.ByName("number"))
list, err := store.StatusList(repo.FullName, num)
2015-04-08 22:43:59 +00:00
if err != nil {
c.Fail(404, err)
} else {
c.JSON(200, list)
}
}