2016-05-02 19:21:25 +00:00
|
|
|
package server
|
2016-03-30 20:15:28 +00:00
|
|
|
|
|
|
|
import (
|
2016-05-03 00:52:34 +00:00
|
|
|
"encoding/base32"
|
2016-03-30 20:15:28 +00:00
|
|
|
"net/http"
|
2016-06-14 20:07:05 +00:00
|
|
|
"strconv"
|
2016-03-30 20:15:28 +00:00
|
|
|
|
2016-06-14 20:07:05 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2016-03-30 20:15:28 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-05-03 00:52:34 +00:00
|
|
|
"github.com/gorilla/securecookie"
|
2016-03-30 20:15:28 +00:00
|
|
|
|
|
|
|
"github.com/drone/drone/cache"
|
2016-06-14 22:20:17 +00:00
|
|
|
"github.com/drone/drone/model"
|
2016-03-30 20:15:28 +00:00
|
|
|
"github.com/drone/drone/router/middleware/session"
|
|
|
|
"github.com/drone/drone/shared/token"
|
|
|
|
"github.com/drone/drone/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func GetSelf(c *gin.Context) {
|
|
|
|
c.JSON(200, session.User(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetFeed(c *gin.Context) {
|
2016-06-15 00:34:47 +00:00
|
|
|
latest, _ := strconv.ParseBool(c.Query("latest"))
|
|
|
|
|
2016-03-30 20:15:28 +00:00
|
|
|
repos, err := cache.GetRepos(c, session.User(c))
|
|
|
|
if err != nil {
|
|
|
|
c.String(500, "Error fetching repository list. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-15 00:34:47 +00:00
|
|
|
feed, err := store.GetUserFeed(c, repos, latest)
|
2016-03-30 20:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(500, "Error fetching feed. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(200, feed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRepos(c *gin.Context) {
|
2016-06-14 22:20:17 +00:00
|
|
|
var (
|
|
|
|
user = session.User(c)
|
|
|
|
all, _ = strconv.ParseBool(c.Query("all"))
|
|
|
|
flush, _ = strconv.ParseBool(c.Query("flush"))
|
|
|
|
)
|
2016-06-14 20:07:05 +00:00
|
|
|
|
|
|
|
if flush {
|
|
|
|
log.Debugf("Evicting repository cache for user %s.", user.Login)
|
|
|
|
cache.DeleteRepos(c, user)
|
|
|
|
}
|
|
|
|
|
2016-06-14 22:20:17 +00:00
|
|
|
remote, err := cache.GetRepos(c, user)
|
2016-03-30 20:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(500, "Error fetching repository list. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-06-14 22:20:17 +00:00
|
|
|
repos, err := store.GetRepoListOf(c, remote)
|
2016-03-30 20:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
c.String(500, "Error fetching repository list. %s", err)
|
|
|
|
return
|
|
|
|
}
|
2016-06-14 22:20:17 +00:00
|
|
|
|
|
|
|
if !all {
|
|
|
|
c.JSON(http.StatusOK, repos)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// below we combine the two lists to include both active and inactive
|
|
|
|
// repositories. This is displayed on the settings screen to enable
|
|
|
|
// toggling on / off repository settings.
|
|
|
|
|
|
|
|
repom := map[string]bool{}
|
|
|
|
for _, repo := range repos {
|
|
|
|
repom[repo.FullName] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, repo := range remote {
|
|
|
|
if repom[repo.FullName] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
repos = append(repos, &model.Repo{
|
|
|
|
Avatar: repo.Avatar,
|
|
|
|
FullName: repo.FullName,
|
|
|
|
Owner: repo.Owner,
|
|
|
|
Name: repo.Name,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repos)
|
2016-03-30 20:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetRemoteRepos(c *gin.Context) {
|
|
|
|
repos, err := cache.GetRepos(c, session.User(c))
|
|
|
|
if err != nil {
|
|
|
|
c.String(500, "Error fetching repository list. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repos)
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostToken(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
|
|
|
|
|
|
|
token := token.New(token.UserToken, user.Login)
|
|
|
|
tokenstr, err := token.Sign(user.Hash)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.String(http.StatusOK, tokenstr)
|
|
|
|
}
|
|
|
|
|
2016-04-09 00:16:45 +00:00
|
|
|
func DeleteToken(c *gin.Context) {
|
|
|
|
user := session.User(c)
|
2016-05-03 00:52:34 +00:00
|
|
|
user.Hash = base32.StdEncoding.EncodeToString(
|
|
|
|
securecookie.GenerateRandomKey(32),
|
|
|
|
)
|
2016-04-09 00:16:45 +00:00
|
|
|
if err := store.UpdateUser(c, user); err != nil {
|
|
|
|
c.String(500, "Error revoking tokens. %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token := token.New(token.UserToken, user.Login)
|
|
|
|
tokenstr, err := token.Sign(user.Hash)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.String(http.StatusOK, tokenstr)
|
|
|
|
}
|