mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-01-29 18:04:15 +02:00
storing user repos index inside users object, for now
This commit is contained in:
parent
fac0582b57
commit
950e4f4090
@ -37,8 +37,6 @@ type Keypair struct {
|
||||
// is displayed on the user dashboard and in the user
|
||||
// event feed.
|
||||
type Subscriber struct {
|
||||
Login string `json:"login,omitempty"`
|
||||
|
||||
// Determines if notifications should be
|
||||
// received from this repository.
|
||||
Subscribed bool `json:"subscribed"`
|
||||
|
@ -10,4 +10,8 @@ type User struct {
|
||||
Admin bool `json:"admin,omitempty"`
|
||||
Created int64 `json:"created_at,omitempty"`
|
||||
Updated int64 `json:"updated_at,omitempty"`
|
||||
|
||||
// Repos contains a list of subscriptions
|
||||
// to repositories the user is watching.
|
||||
Repos map[string]struct{} `json:"-"`
|
||||
}
|
||||
|
@ -84,25 +84,25 @@ func (db *DB) DeleteRepo(repo *common.Repo) error {
|
||||
return t.Commit()
|
||||
}
|
||||
|
||||
// GetSubscriber gets the subscriber by login for the
|
||||
// named repository.
|
||||
func (db *DB) GetSubscriber(repo string, login string) (*common.Subscriber, error) {
|
||||
sub := &common.Subscriber{}
|
||||
key := []byte(login + "/" + repo)
|
||||
err := get(db, bucketUserRepos, key, sub)
|
||||
return sub, err
|
||||
}
|
||||
// // GetSubscriber gets the subscriber by login for the
|
||||
// // named repository.
|
||||
// func (db *DB) GetSubscriber(repo string, login string) (*common.Subscriber, error) {
|
||||
// sub := &common.Subscriber{}
|
||||
// key := []byte(login + "/" + repo)
|
||||
// err := get(db, bucketUserRepos, key, sub)
|
||||
// return sub, err
|
||||
// }
|
||||
|
||||
// InsertSubscriber inserts a subscriber for the named
|
||||
// repository.
|
||||
func (db *DB) InsertSubscriber(repo string, sub *common.Subscriber) error {
|
||||
key := []byte(sub.Login + "/" + repo)
|
||||
return insert(db, bucketUserRepos, key, sub)
|
||||
}
|
||||
// // InsertSubscriber inserts a subscriber for the named
|
||||
// // repository.
|
||||
// func (db *DB) InsertSubscriber(repo string, sub *common.Subscriber) error {
|
||||
// key := []byte(sub.Login + "/" + repo)
|
||||
// return insert(db, bucketUserRepos, key, sub)
|
||||
// }
|
||||
|
||||
// DeleteSubscriber removes the subscriber by login for the
|
||||
// named repository.
|
||||
func (db *DB) DeleteSubscriber(repo string, sub *common.Subscriber) error {
|
||||
key := []byte(sub.Login + "/" + repo)
|
||||
return delete(db, bucketUserRepos, key)
|
||||
}
|
||||
// // DeleteSubscriber removes the subscriber by login for the
|
||||
// // named repository.
|
||||
// func (db *DB) DeleteSubscriber(repo string, sub *common.Subscriber) error {
|
||||
// key := []byte(sub.Login + "/" + repo)
|
||||
// return delete(db, bucketUserRepos, key)
|
||||
// }
|
||||
|
@ -57,22 +57,27 @@ func (db *DB) GetUserRepos(login string) ([]*common.Repo, error) {
|
||||
}
|
||||
defer t.Rollback()
|
||||
repos := []*common.Repo{}
|
||||
user := &common.User{}
|
||||
|
||||
// get the index of user repos and unmarshal
|
||||
// to a string array.
|
||||
// get the user struct from the database
|
||||
key := []byte(login)
|
||||
raw := t.Bucket(bucketUserRepos).Get(key)
|
||||
keys := [][]byte{}
|
||||
err = decode(raw, &keys)
|
||||
raw := t.Bucket(bucketUser).Get(key)
|
||||
err = decode(raw, user)
|
||||
if err != nil {
|
||||
return repos, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// for each item in the index, get the repository
|
||||
// and append to the array
|
||||
for _, key := range keys {
|
||||
for repoName, _ := range user.Repos {
|
||||
repo := &common.Repo{}
|
||||
key = []byte(repoName)
|
||||
raw = t.Bucket(bucketRepo).Get(key)
|
||||
if raw == nil {
|
||||
// this will happen when the repository has been deleted
|
||||
// TODO we should probably upate the index in this case.
|
||||
continue
|
||||
}
|
||||
err = decode(raw, repo)
|
||||
if err != nil {
|
||||
break
|
||||
|
@ -42,17 +42,17 @@ type Datastore interface {
|
||||
// DeleteUser deletes the token.
|
||||
DeleteToken(*common.Token) error
|
||||
|
||||
// GetSubscriber gets the subscriber by login for the
|
||||
// named repository.
|
||||
GetSubscriber(string, string) (*common.Subscriber, error)
|
||||
// // GetSubscriber gets the subscriber by login for the
|
||||
// // named repository.
|
||||
// GetSubscriber(string, string) (*common.Subscriber, error)
|
||||
|
||||
// InsertSubscriber inserts a subscriber for the named
|
||||
// repository.
|
||||
InsertSubscriber(string, *common.Subscriber) error
|
||||
// // InsertSubscriber inserts a subscriber for the named
|
||||
// // repository.
|
||||
// InsertSubscriber(string, *common.Subscriber) error
|
||||
|
||||
// DeleteSubscriber removes the subscriber by login for the
|
||||
// named repository.
|
||||
DeleteSubscriber(string, *common.Subscriber) error
|
||||
// // DeleteSubscriber removes the subscriber by login for the
|
||||
// // named repository.
|
||||
// DeleteSubscriber(string, *common.Subscriber) error
|
||||
|
||||
// GetRepo gets the repository by name.
|
||||
GetRepo(string) (*common.Repo, error)
|
||||
|
1
drone.go
1
drone.go
@ -79,7 +79,6 @@ func main() {
|
||||
subscribers.Use(server.SetPerm())
|
||||
subscribers.Use(server.CheckPull())
|
||||
|
||||
subscribers.GET("", server.GetSubscribers)
|
||||
subscribers.POST("", server.Subscribe)
|
||||
subscribers.DELETE("", server.Unsubscribe)
|
||||
}
|
||||
|
@ -60,9 +60,15 @@ func GetRepo(c *gin.Context) {
|
||||
}
|
||||
// if the user is authenticated, we should display
|
||||
// if she is watching the current repository.
|
||||
if user != nil {
|
||||
data.Watch, _ = store.GetSubscriber(repo.FullName, user.Login)
|
||||
if user == nil {
|
||||
c.JSON(200, data)
|
||||
return
|
||||
}
|
||||
|
||||
// check to see if the user is subscribing to the repo
|
||||
_, ok := user.Repos[repo.FullName]
|
||||
data.Watch = &common.Subscriber{Subscribed: ok}
|
||||
|
||||
c.JSON(200, data)
|
||||
}
|
||||
|
||||
@ -75,8 +81,8 @@ func GetRepo(c *gin.Context) {
|
||||
func PutRepo(c *gin.Context) {
|
||||
store := ToDatastore(c)
|
||||
perm := ToPerm(c)
|
||||
u := ToUser(c)
|
||||
r := ToRepo(c)
|
||||
user := ToUser(c)
|
||||
repo := ToRepo(c)
|
||||
|
||||
in := &repoReq{}
|
||||
if !c.BindWith(in, binding.JSON) {
|
||||
@ -84,37 +90,41 @@ func PutRepo(c *gin.Context) {
|
||||
}
|
||||
|
||||
if in.Params != nil {
|
||||
err := store.UpsertRepoParams(r.FullName, *in.Params)
|
||||
err := store.UpsertRepoParams(repo.FullName, *in.Params)
|
||||
if err != nil {
|
||||
c.Fail(400, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if in.Disabled != nil {
|
||||
r.Disabled = *in.Disabled
|
||||
repo.Disabled = *in.Disabled
|
||||
}
|
||||
if in.DisablePR != nil {
|
||||
r.DisablePR = *in.DisablePR
|
||||
repo.DisablePR = *in.DisablePR
|
||||
}
|
||||
if in.DisableTag != nil {
|
||||
r.DisableTag = *in.DisableTag
|
||||
repo.DisableTag = *in.DisableTag
|
||||
}
|
||||
if in.Trusted != nil && u.Admin {
|
||||
r.Trusted = *in.Trusted
|
||||
if in.Trusted != nil && user.Admin {
|
||||
repo.Trusted = *in.Trusted
|
||||
}
|
||||
if in.Timeout != nil && u.Admin {
|
||||
r.Timeout = *in.Timeout
|
||||
if in.Timeout != nil && user.Admin {
|
||||
repo.Timeout = *in.Timeout
|
||||
}
|
||||
|
||||
err := store.UpdateRepo(r)
|
||||
err := store.UpdateRepo(repo)
|
||||
if err != nil {
|
||||
c.Fail(400, err)
|
||||
return
|
||||
}
|
||||
|
||||
data := repoResp{r, perm, nil, nil}
|
||||
data.Params, _ = store.GetRepoParams(r.FullName)
|
||||
data.Watch, _ = store.GetSubscriber(r.FullName, u.Login)
|
||||
data := repoResp{repo, perm, nil, nil}
|
||||
data.Params, _ = store.GetRepoParams(repo.FullName)
|
||||
|
||||
// check to see if the user is subscribing to the repo
|
||||
_, ok := user.Repos[repo.FullName]
|
||||
data.Watch = &common.Subscriber{Subscribed: ok}
|
||||
|
||||
c.JSON(200, data)
|
||||
}
|
||||
|
||||
@ -194,30 +204,38 @@ func PostRepo(c *gin.Context) {
|
||||
keypair := &common.Keypair{}
|
||||
keypair.Public = sshutil.MarshalPublicKey(&key.PublicKey)
|
||||
keypair.Private = sshutil.MarshalPrivateKey(key)
|
||||
|
||||
// activate the repository before we make any
|
||||
// local changes to the database.
|
||||
err = remote.Activate(user, r, keypair, link)
|
||||
if err != nil {
|
||||
c.Fail(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// persist the repository
|
||||
err = store.InsertRepo(user, r)
|
||||
if err != nil {
|
||||
c.Fail(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// persisty the repository key pair
|
||||
err = store.UpsertRepoKeys(r.FullName, keypair)
|
||||
if err != nil {
|
||||
c.Fail(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
// store the repository and the users' permissions
|
||||
// in the datastore.
|
||||
err = store.InsertRepo(user, r)
|
||||
if err != nil {
|
||||
c.Fail(500, err)
|
||||
return
|
||||
}
|
||||
err = store.InsertSubscriber(r.FullName, &common.Subscriber{Subscribed: true})
|
||||
if err != nil {
|
||||
c.Fail(500, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = remote.Activate(user, r, keypair, link)
|
||||
if err != nil {
|
||||
c.Fail(500, err)
|
||||
return
|
||||
// subscribe the user to the repository
|
||||
// if this fails we'll ignore, since the user
|
||||
// can just go click the "watch" button in the
|
||||
// user interface.
|
||||
if user.Repos == nil {
|
||||
user.Repos = map[string]struct{}{}
|
||||
}
|
||||
user.Repos[r.FullName] = struct{}{}
|
||||
store.UpdateUser(user)
|
||||
|
||||
c.JSON(200, r)
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ type session struct {
|
||||
|
||||
func New(s *settings.Session) Session {
|
||||
// TODO (bradrydzewski) hook up the Session.Expires
|
||||
secret := s.Secret
|
||||
secret := []byte(s.Secret)
|
||||
expire := time.Hour * 72
|
||||
if len(secret) == 0 {
|
||||
securecookie.GenerateRandomKey(32)
|
||||
|
@ -6,41 +6,6 @@ import (
|
||||
"github.com/drone/drone/common"
|
||||
)
|
||||
|
||||
// GetSubscriber accepts a request to retrieve a repository
|
||||
// subscriber from the datastore for the given repository by
|
||||
// user Login.
|
||||
//
|
||||
// GET /api/subscribers/:owner/:name/:login
|
||||
//
|
||||
func GetSubscriber(c *gin.Context) {
|
||||
store := ToDatastore(c)
|
||||
repo := ToRepo(c)
|
||||
login := c.Params.ByName("login")
|
||||
subsc, err := store.GetSubscriber(repo.FullName, login)
|
||||
if err != nil {
|
||||
c.Fail(404, err)
|
||||
} else {
|
||||
c.JSON(200, subsc)
|
||||
}
|
||||
}
|
||||
|
||||
// GetSubscribers accepts a request to retrieve a repository
|
||||
// watchers from the datastore for the given repository.
|
||||
//
|
||||
// GET /api/subscribers/:owner/:name
|
||||
//
|
||||
func GetSubscribers(c *gin.Context) {
|
||||
// store := ToDatastore(c)
|
||||
// repo := ToRepo(c)
|
||||
// subs, err := store.GetSubscribers(repo.FullName)
|
||||
// if err != nil {
|
||||
// c.Fail(404, err)
|
||||
// } else {
|
||||
// c.JSON(200, subs)
|
||||
// }
|
||||
c.Writer.WriteHeader(501)
|
||||
}
|
||||
|
||||
// Unubscribe accapets a request to unsubscribe the
|
||||
// currently authenticated user to the repository.
|
||||
//
|
||||
@ -50,11 +15,9 @@ func Unsubscribe(c *gin.Context) {
|
||||
store := ToDatastore(c)
|
||||
repo := ToRepo(c)
|
||||
user := ToUser(c)
|
||||
sub, err := store.GetSubscriber(repo.FullName, user.Login)
|
||||
if err != nil {
|
||||
c.Fail(404, err)
|
||||
}
|
||||
err = store.DeleteSubscriber(repo.FullName, sub)
|
||||
|
||||
delete(user.Repos, repo.FullName)
|
||||
err := store.UpdateUser(user)
|
||||
if err != nil {
|
||||
c.Fail(400, err)
|
||||
} else {
|
||||
@ -71,14 +34,14 @@ func Subscribe(c *gin.Context) {
|
||||
store := ToDatastore(c)
|
||||
repo := ToRepo(c)
|
||||
user := ToUser(c)
|
||||
subscriber := &common.Subscriber{
|
||||
Login: user.Login,
|
||||
Subscribed: true,
|
||||
if user.Repos == nil {
|
||||
user.Repos = map[string]struct{}{}
|
||||
}
|
||||
err := store.InsertSubscriber(repo.FullName, subscriber)
|
||||
user.Repos[repo.FullName] = struct{}{}
|
||||
err := store.UpdateUser(user)
|
||||
if err != nil {
|
||||
c.Fail(400, err)
|
||||
} else {
|
||||
c.JSON(200, subscriber)
|
||||
c.JSON(200, &common.Subscriber{Subscribed: true})
|
||||
}
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ type Server struct {
|
||||
// used to generate, validate and expire authentication
|
||||
// sessions.
|
||||
type Session struct {
|
||||
Secret []byte `toml:"secret"`
|
||||
Secret string `toml:"secret"`
|
||||
Expires int64 `toml:"expires"`
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user