2015-09-30 01:21:17 +00:00
|
|
|
package controller
|
2015-04-08 22:43:59 +00:00
|
|
|
|
|
|
|
import (
|
2015-09-30 01:21:17 +00:00
|
|
|
"net/http"
|
2015-09-09 21:34:28 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/drone/drone/model"
|
2015-10-21 23:14:02 +00:00
|
|
|
"github.com/drone/drone/remote"
|
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-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func GetSelf(c *gin.Context) {
|
|
|
|
c.IndentedJSON(200, session.User(c))
|
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func GetFeed(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
2015-10-21 23:39:43 +00:00
|
|
|
remote := remote.FromContext(c)
|
|
|
|
var repos []*model.RepoLite
|
|
|
|
|
|
|
|
// get the repository list from the cache
|
|
|
|
reposv, ok := c.Get("repos")
|
|
|
|
if ok {
|
|
|
|
repos = reposv.([]*model.RepoLite)
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
repos, err = remote.Repos(user)
|
|
|
|
if err != nil {
|
|
|
|
c.String(400, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
feed, err := store.GetUserFeed(c, repos)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-10-21 23:39:43 +00:00
|
|
|
c.String(400, err.Error())
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2015-10-21 23:39:43 +00:00
|
|
|
c.JSON(200, feed)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func GetRepos(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
2015-10-21 23:14:02 +00:00
|
|
|
remote := remote.FromContext(c)
|
2015-10-15 22:40:27 +00:00
|
|
|
var repos []*model.RepoLite
|
|
|
|
|
|
|
|
// get the repository list from the cache
|
|
|
|
reposv, ok := c.Get("repos")
|
|
|
|
if ok {
|
|
|
|
repos = reposv.([]*model.RepoLite)
|
|
|
|
} else {
|
|
|
|
var err error
|
|
|
|
repos, err = remote.Repos(user)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// for each repository in the remote system we get
|
|
|
|
// the intersection of those repostiories in Drone
|
2015-10-21 23:14:02 +00:00
|
|
|
repos_, err := store.GetRepoListOf(c, repos)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2015-10-15 22:40:27 +00:00
|
|
|
|
|
|
|
c.Set("repos", repos)
|
|
|
|
c.IndentedJSON(http.StatusOK, repos_)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2015-04-11 05:22:55 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func GetRemoteRepos(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
2015-10-21 23:14:02 +00:00
|
|
|
remote := remote.FromContext(c)
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2015-10-13 09:08:08 +00:00
|
|
|
reposv, ok := c.Get("repos")
|
2015-09-30 01:21:17 +00:00
|
|
|
if ok {
|
2015-10-13 09:08:08 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, reposv)
|
2015-09-30 01:21:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
repos, err := remote.Repos(user)
|
2015-08-18 23:09:27 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
|
|
return
|
2015-08-18 23:09:27 +00:00
|
|
|
}
|
2015-10-13 09:08:08 +00:00
|
|
|
|
|
|
|
c.Set("repos", repos)
|
2015-09-30 01:21:17 +00:00
|
|
|
c.IndentedJSON(http.StatusOK, repos)
|
2015-08-18 23:09:27 +00:00
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func PostToken(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
2015-09-09 21:05:52 +00:00
|
|
|
|
2015-09-09 21:34:28 +00:00
|
|
|
token := token.New(token.UserToken, user.Login)
|
|
|
|
tokenstr, err := token.Sign(user.Hash)
|
2015-04-13 23:33:29 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
2015-04-13 23:33:29 +00:00
|
|
|
} else {
|
2015-09-30 01:21:17 +00:00
|
|
|
c.String(http.StatusOK, tokenstr)
|
2015-04-13 23:33:29 +00:00
|
|
|
}
|
2015-04-11 05:22:55 +00:00
|
|
|
}
|