2016-03-31 06:27:53 +00:00
|
|
|
package web
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2016-03-05 05:15:50 +00:00
|
|
|
"github.com/drone/drone/cache"
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/model"
|
|
|
|
"github.com/drone/drone/router/middleware/session"
|
|
|
|
"github.com/drone/drone/shared/httputil"
|
|
|
|
"github.com/drone/drone/shared/token"
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/drone/drone/store"
|
2015-09-30 01:21:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ShowIndex(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
if user == nil {
|
2015-10-16 05:25:07 +00:00
|
|
|
c.Redirect(http.StatusSeeOther, "/login")
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-16 05:25:07 +00:00
|
|
|
// get the repository list from the cache
|
2016-03-05 05:15:50 +00:00
|
|
|
repos, err := cache.GetRepos(c, user)
|
|
|
|
if err != nil {
|
|
|
|
c.String(400, err.Error())
|
|
|
|
return
|
2015-10-16 05:25:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
c.HTML(200, "repos.html", gin.H{
|
2015-10-02 23:47:54 +00:00
|
|
|
"User": user,
|
2015-10-27 04:01:10 +00:00
|
|
|
"Repos": repos,
|
2015-09-30 01:21:17 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowLogin(c *gin.Context) {
|
|
|
|
c.HTML(200, "login.html", gin.H{"Error": c.Query("error")})
|
|
|
|
}
|
|
|
|
|
2015-10-22 23:36:43 +00:00
|
|
|
func ShowLoginForm(c *gin.Context) {
|
|
|
|
c.HTML(200, "login_form.html", gin.H{})
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func ShowUser(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
token, _ := token.New(
|
|
|
|
token.CsrfToken,
|
|
|
|
user.Login,
|
|
|
|
).Sign(user.Hash)
|
|
|
|
|
|
|
|
c.HTML(200, "user.html", gin.H{
|
|
|
|
"User": user,
|
|
|
|
"Csrf": token,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowUsers(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
if !user.Admin {
|
|
|
|
c.AbortWithStatus(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
2015-10-21 23:14:02 +00:00
|
|
|
users, _ := store.GetUserList(c)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
token, _ := token.New(
|
|
|
|
token.CsrfToken,
|
|
|
|
user.Login,
|
|
|
|
).Sign(user.Hash)
|
|
|
|
|
|
|
|
c.HTML(200, "users.html", gin.H{
|
|
|
|
"User": user,
|
|
|
|
"Users": users,
|
|
|
|
"Csrf": token,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowRepo(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
repo := session.Repo(c)
|
2015-10-01 01:21:39 +00:00
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
builds, _ := store.GetBuildList(c, repo)
|
2015-09-30 01:21:17 +00:00
|
|
|
groups := []*model.BuildGroup{}
|
|
|
|
|
|
|
|
var curr *model.BuildGroup
|
|
|
|
for _, build := range builds {
|
|
|
|
date := time.Unix(build.Created, 0).Format("Jan 2 2006")
|
|
|
|
if curr == nil || curr.Date != date {
|
|
|
|
curr = &model.BuildGroup{}
|
|
|
|
curr.Date = date
|
|
|
|
groups = append(groups, curr)
|
|
|
|
}
|
|
|
|
curr.Builds = append(curr.Builds, build)
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.SetCookie(c.Writer, c.Request, "user_last", repo.FullName)
|
|
|
|
|
|
|
|
c.HTML(200, "repo.html", gin.H{
|
|
|
|
"User": user,
|
|
|
|
"Repo": repo,
|
|
|
|
"Builds": builds,
|
|
|
|
"Groups": groups,
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowRepoConf(c *gin.Context) {
|
2015-10-21 23:14:02 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
user := session.User(c)
|
|
|
|
repo := session.Repo(c)
|
2015-10-21 23:14:02 +00:00
|
|
|
key, _ := store.GetKey(c, repo)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
token, _ := token.New(
|
|
|
|
token.CsrfToken,
|
|
|
|
user.Login,
|
|
|
|
).Sign(user.Hash)
|
|
|
|
|
2015-10-01 01:21:39 +00:00
|
|
|
c.HTML(200, "repo_config.html", gin.H{
|
2015-09-30 01:21:17 +00:00
|
|
|
"User": user,
|
|
|
|
"Repo": repo,
|
|
|
|
"Key": key,
|
|
|
|
"Csrf": token,
|
|
|
|
"Link": httputil.GetURL(c.Request),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-10-01 01:21:39 +00:00
|
|
|
func ShowRepoEncrypt(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
|
|
|
|
token, _ := token.New(
|
|
|
|
token.CsrfToken,
|
|
|
|
user.Login,
|
|
|
|
).Sign(user.Hash)
|
|
|
|
|
|
|
|
c.HTML(200, "repo_secret.html", gin.H{
|
|
|
|
"User": user,
|
|
|
|
"Repo": repo,
|
|
|
|
"Csrf": token,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShowRepoBadges(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
|
|
|
|
c.HTML(200, "repo_badge.html", gin.H{
|
|
|
|
"User": user,
|
|
|
|
"Repo": repo,
|
|
|
|
"Link": httputil.GetURL(c.Request),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func ShowBuild(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
repo := session.Repo(c)
|
|
|
|
num, _ := strconv.Atoi(c.Param("number"))
|
|
|
|
seq, _ := strconv.Atoi(c.Param("job"))
|
|
|
|
if seq == 0 {
|
|
|
|
seq = 1
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
build, err := store.GetBuildNumber(c, repo, num)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-21 23:14:02 +00:00
|
|
|
jobs, err := store.GetJobList(c, build)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var job *model.Job
|
|
|
|
for _, j := range jobs {
|
|
|
|
if j.Number == seq {
|
|
|
|
job = j
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
httputil.SetCookie(c.Writer, c.Request, "user_last", repo.FullName)
|
|
|
|
|
2015-10-01 01:42:32 +00:00
|
|
|
var csrf string
|
|
|
|
if user != nil {
|
|
|
|
csrf, _ = token.New(
|
|
|
|
token.CsrfToken,
|
|
|
|
user.Login,
|
|
|
|
).Sign(user.Hash)
|
|
|
|
}
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
c.HTML(200, "build.html", gin.H{
|
|
|
|
"User": user,
|
|
|
|
"Repo": repo,
|
|
|
|
"Build": build,
|
|
|
|
"Jobs": jobs,
|
|
|
|
"Job": job,
|
2015-10-01 01:42:32 +00:00
|
|
|
"Csrf": csrf,
|
2015-09-30 01:21:17 +00:00
|
|
|
})
|
|
|
|
}
|
2016-03-30 20:15:28 +00:00
|
|
|
|
|
|
|
func ShowNodes(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
nodes, _ := store.GetNodeList(c)
|
|
|
|
token, _ := token.New(token.CsrfToken, user.Login).Sign(user.Hash)
|
|
|
|
c.HTML(http.StatusOK, "nodes.html", gin.H{"User": user, "Nodes": nodes, "Csrf": token})
|
|
|
|
}
|