2015-09-30 01:21:17 +00:00
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"github.com/drone/drone/controller"
|
|
|
|
"github.com/drone/drone/router/middleware/header"
|
2015-10-05 00:40:27 +00:00
|
|
|
"github.com/drone/drone/router/middleware/refresh"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/router/middleware/session"
|
|
|
|
"github.com/drone/drone/static"
|
|
|
|
"github.com/drone/drone/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Load(middleware ...gin.HandlerFunc) http.Handler {
|
|
|
|
e := gin.Default()
|
|
|
|
e.SetHTMLTemplate(template.Load())
|
|
|
|
e.StaticFS("/static", static.FileSystem())
|
|
|
|
|
|
|
|
e.Use(header.SetHeaders())
|
|
|
|
e.Use(middleware...)
|
|
|
|
e.Use(session.SetUser())
|
2015-10-05 00:40:27 +00:00
|
|
|
e.Use(refresh.Refresh)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
e.GET("/", controller.ShowIndex)
|
|
|
|
e.GET("/login", controller.ShowLogin)
|
|
|
|
e.GET("/logout", controller.GetLogout)
|
|
|
|
|
|
|
|
settings := e.Group("/settings")
|
|
|
|
{
|
|
|
|
settings.Use(session.MustUser())
|
|
|
|
settings.GET("/profile", controller.ShowUser)
|
|
|
|
settings.GET("/people", session.MustAdmin(), controller.ShowUsers)
|
|
|
|
settings.GET("/nodes", session.MustAdmin(), controller.ShowNodes)
|
|
|
|
}
|
|
|
|
repo := e.Group("/repos/:owner/:name")
|
|
|
|
{
|
|
|
|
repo.Use(session.SetRepo())
|
|
|
|
repo.Use(session.SetPerm())
|
|
|
|
repo.Use(session.MustPull)
|
|
|
|
|
|
|
|
repo.GET("", controller.ShowRepo)
|
|
|
|
repo.GET("/builds/:number", controller.ShowBuild)
|
|
|
|
repo.GET("/builds/:number/:job", controller.ShowBuild)
|
|
|
|
repo_settings := repo.Group("/settings")
|
|
|
|
{
|
2015-10-01 01:21:39 +00:00
|
|
|
repo_settings.GET("", session.MustPush, controller.ShowRepoConf)
|
|
|
|
repo_settings.GET("/encrypt", session.MustPush, controller.ShowRepoEncrypt)
|
|
|
|
repo_settings.GET("/badges", controller.ShowRepoBadges)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
user := e.Group("/api/user")
|
|
|
|
{
|
|
|
|
user.Use(session.MustUser())
|
|
|
|
user.GET("", controller.GetSelf)
|
2015-10-02 23:16:41 +00:00
|
|
|
user.GET("/builds", controller.GetFeed)
|
2015-09-30 01:21:17 +00:00
|
|
|
user.GET("/repos", controller.GetRepos)
|
|
|
|
user.GET("/repos/remote", controller.GetRemoteRepos)
|
2015-10-02 23:16:41 +00:00
|
|
|
user.POST("/token", controller.PostToken)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
users := e.Group("/api/users")
|
|
|
|
{
|
|
|
|
users.Use(session.MustAdmin())
|
|
|
|
users.GET("", controller.GetUsers)
|
|
|
|
users.POST("", controller.PostUser)
|
|
|
|
users.GET("/:login", controller.GetUser)
|
|
|
|
users.PATCH("/:login", controller.PatchUser)
|
|
|
|
users.DELETE("/:login", controller.DeleteUser)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := e.Group("/api/nodes")
|
|
|
|
{
|
|
|
|
nodes.Use(session.MustAdmin())
|
|
|
|
nodes.GET("", controller.GetNodes)
|
|
|
|
nodes.POST("", controller.PostNode)
|
|
|
|
nodes.DELETE("/:node", controller.DeleteNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
repos := e.Group("/api/repos/:owner/:name")
|
|
|
|
{
|
|
|
|
repos.POST("", controller.PostRepo)
|
|
|
|
|
|
|
|
repo := repos.Group("")
|
|
|
|
{
|
|
|
|
repo.Use(session.SetRepo())
|
|
|
|
repo.Use(session.SetPerm())
|
|
|
|
repo.Use(session.MustPull)
|
|
|
|
|
|
|
|
repo.GET("", controller.GetRepo)
|
|
|
|
repo.GET("/key", controller.GetRepoKey)
|
|
|
|
repo.GET("/builds", controller.GetBuilds)
|
|
|
|
repo.GET("/builds/:number", controller.GetBuild)
|
|
|
|
repo.GET("/logs/:number/:job", controller.GetBuildLogs)
|
|
|
|
|
|
|
|
// requires authenticated user
|
|
|
|
repo.POST("/starred", session.MustUser(), controller.PostStar)
|
|
|
|
repo.DELETE("/starred", session.MustUser(), controller.DeleteStar)
|
|
|
|
repo.POST("/encrypt", session.MustUser(), controller.PostSecure)
|
|
|
|
|
|
|
|
// requires push permissions
|
|
|
|
repo.PATCH("", session.MustPush, controller.PatchRepo)
|
|
|
|
repo.DELETE("", session.MustPush, controller.DeleteRepo)
|
|
|
|
|
|
|
|
repo.POST("/builds/:number", session.MustPush, controller.PostBuild)
|
2015-10-01 19:05:23 +00:00
|
|
|
repo.DELETE("/builds/:number/:job", session.MustPush, controller.DeleteBuild)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
badges := e.Group("/api/badges/:owner/:name")
|
|
|
|
{
|
|
|
|
badges.GET("/status.svg", controller.GetBadge)
|
|
|
|
badges.GET("/cc.xml", controller.GetCC)
|
|
|
|
}
|
|
|
|
|
2015-10-03 23:57:24 +00:00
|
|
|
e.POST("/hook", controller.PostHook)
|
|
|
|
e.POST("/api/hook", controller.PostHook)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
stream := e.Group("/api/stream")
|
|
|
|
{
|
|
|
|
stream.Use(session.SetRepo())
|
|
|
|
stream.Use(session.SetPerm())
|
|
|
|
stream.Use(session.MustPull)
|
|
|
|
stream.GET("/:owner/:name", controller.GetRepoEvents)
|
|
|
|
stream.GET("/:owner/:name/:build/:number", controller.GetStream)
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := e.Group("/authorize")
|
|
|
|
{
|
|
|
|
auth.GET("", controller.GetLogin)
|
|
|
|
auth.POST("", controller.GetLogin)
|
|
|
|
auth.POST("/token", controller.GetLoginToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
gitlab := e.Group("/api/gitlab/:owner/:name")
|
|
|
|
{
|
|
|
|
gitlab.Use(session.SetRepo())
|
|
|
|
gitlab.GET("/commits/:sha", controller.GetCommit)
|
|
|
|
gitlab.GET("/pulls/:number", controller.GetPullRequest)
|
|
|
|
|
|
|
|
redirects := gitlab.Group("/redirect")
|
|
|
|
{
|
|
|
|
redirects.GET("/commits/:sha", controller.RedirectSha)
|
|
|
|
redirects.GET("/pulls/:number", controller.RedirectPullRequest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return normalize(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// normalize is a helper function to work around the following
|
|
|
|
// issue with gin. https://github.com/gin-gonic/gin/issues/388
|
|
|
|
func normalize(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
parts := strings.Split(r.URL.Path, "/")[1:]
|
|
|
|
switch parts[0] {
|
|
|
|
case "settings", "api", "login", "logout", "", "authorize", "hook", "static":
|
|
|
|
// no-op
|
|
|
|
default:
|
|
|
|
|
|
|
|
if len(parts) > 2 && parts[2] != "settings" {
|
|
|
|
parts = append(parts[:2], append([]string{"builds"}, parts[2:]...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// prefix the URL with /repo so that it
|
|
|
|
// can be effectively routed.
|
|
|
|
parts = append([]string{"", "repos"}, parts...)
|
|
|
|
|
|
|
|
// reconstruct the path
|
|
|
|
r.URL.Path = strings.Join(parts, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|