harness-drone/controller/gitlab.go

106 lines
2.3 KiB
Go
Raw Normal View History

2015-09-30 01:21:17 +00:00
package controller
2015-09-02 02:19:11 +00:00
import (
"fmt"
2015-09-30 01:21:17 +00:00
"net/http"
2015-09-02 02:19:11 +00:00
2015-09-30 01:21:17 +00:00
"github.com/gin-gonic/gin"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/model"
"github.com/drone/drone/router/middleware/context"
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/shared/token"
2015-09-02 02:19:11 +00:00
)
2015-09-30 01:21:17 +00:00
func GetCommit(c *gin.Context) {
db := context.Database(c)
repo := session.Repo(c)
2015-09-02 02:19:11 +00:00
2015-09-30 01:21:17 +00:00
parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
return repo.Hash, nil
})
2015-09-02 02:19:11 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusBadRequest, err)
2015-09-02 02:19:11 +00:00
return
}
2015-09-30 01:21:17 +00:00
if parsed.Text != repo.FullName {
c.AbortWithStatus(http.StatusUnauthorized)
2015-09-02 02:19:11 +00:00
return
}
2015-09-30 01:21:17 +00:00
commit := c.Param("sha")
branch := c.Query("branch")
if len(branch) == 0 {
branch = repo.Branch
}
build, err := model.GetBuildCommit(db, repo, commit, branch)
2015-09-02 02:19:11 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusNotFound, err)
2015-09-02 02:19:11 +00:00
return
}
2015-09-30 01:21:17 +00:00
c.JSON(http.StatusOK, build)
2015-09-02 02:19:11 +00:00
}
func GetPullRequest(c *gin.Context) {
2015-09-30 01:21:17 +00:00
db := context.Database(c)
repo := session.Repo(c)
refs := fmt.Sprintf("refs/pull/%s/head", c.Param("number"))
2015-09-02 02:19:11 +00:00
parsed, err := token.ParseRequest(c.Request, func(t *token.Token) (string, error) {
return repo.Hash, nil
})
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusBadRequest, err)
return
}
if parsed.Text != repo.FullName {
2015-09-30 01:21:17 +00:00
c.AbortWithStatus(http.StatusUnauthorized)
2015-09-02 02:19:11 +00:00
return
}
2015-09-30 01:21:17 +00:00
build, err := model.GetBuildRef(db, repo, refs)
2015-09-02 02:19:11 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusNotFound, err)
2015-09-02 02:19:11 +00:00
return
}
2015-09-30 01:21:17 +00:00
c.JSON(http.StatusOK, build)
}
2015-09-02 02:19:11 +00:00
2015-09-30 01:21:17 +00:00
func RedirectSha(c *gin.Context) {
db := context.Database(c)
repo := session.Repo(c)
2015-09-02 02:19:11 +00:00
2015-09-30 01:21:17 +00:00
commit := c.Param("sha")
branch := c.Query("branch")
if len(branch) == 0 {
2015-09-02 02:19:11 +00:00
branch = repo.Branch
}
2015-09-30 01:21:17 +00:00
build, err := model.GetBuildCommit(db, repo, commit, branch)
2015-09-02 02:19:11 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusNotFound, err)
2015-09-02 02:19:11 +00:00
return
}
2015-09-30 01:21:17 +00:00
path := fmt.Sprintf("/%s/%s/%d", repo.Owner, repo.Name, build.Number)
c.Redirect(http.StatusSeeOther, path)
}
func RedirectPullRequest(c *gin.Context) {
db := context.Database(c)
repo := session.Repo(c)
refs := fmt.Sprintf("refs/pull/%s/head", c.Param("number"))
build, err := model.GetBuildRef(db, repo, refs)
2015-09-02 02:19:11 +00:00
if err != nil {
2015-09-30 01:21:17 +00:00
c.AbortWithError(http.StatusNotFound, err)
return
2015-09-02 02:19:11 +00:00
}
2015-09-30 01:21:17 +00:00
path := fmt.Sprintf("/%s/%s/%d", repo.Owner, repo.Name, build.Number)
c.Redirect(http.StatusSeeOther, path)
2015-09-02 02:19:11 +00:00
}