woodpecker/server/gitlab.go

101 lines
2.1 KiB
Go
Raw Normal View History

2016-05-02 19:21:25 +00:00
package server
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/router/middleware/session"
"github.com/drone/drone/shared/token"
"github.com/drone/drone/store"
2015-09-02 02:19:11 +00:00
)
2015-09-30 01:21:17 +00:00
func GetCommit(c *gin.Context) {
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 := store.GetBuildCommit(c, 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
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
}
build, err := store.GetBuildRef(c, 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) {
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
}
build, err := store.GetBuildCommit(c, 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) {
repo := session.Repo(c)
refs := fmt.Sprintf("refs/pull/%s/head", c.Param("number"))
build, err := store.GetBuildRef(c, 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
}