2016-03-31 06:27:53 +00:00
|
|
|
package web
|
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-02 03:42:18 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/router/middleware/session"
|
|
|
|
"github.com/drone/drone/shared/token"
|
2015-10-21 23:14:02 +00:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
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
|
|
|
|
2015-09-09 21:05:52 +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)
|
2015-09-09 21:05:52 +00:00
|
|
|
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-10-21 23:14:02 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
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"))
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
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
|
|
|
}
|