Use user/permissions bitbucket API instead of hooks

This commit is contained in:
folex 2018-04-27 18:59:36 +03:00
parent 9d8f8c3a44
commit 764c36f736
4 changed files with 49 additions and 13 deletions

1
.gitignore vendored
View file

@ -7,3 +7,4 @@ extras/
release/ release/
server/swagger/files/*.json server/swagger/files/*.json
.idea/

View file

@ -176,15 +176,21 @@ func (c *config) Perm(u *model.User, owner, name string) (*model.Perm, error) {
client := c.newClient(u) client := c.newClient(u)
perms := new(model.Perm) perms := new(model.Perm)
_, err := client.FindRepo(owner, name) repo, err := client.FindRepo(owner, name)
if err != nil { if err != nil {
return perms, err return perms, err
} }
_, err = client.ListHooks(owner, name, &internal.ListOpts{}) perm, err := client.GetPermission(repo.FullName)
if err == nil { if err == nil {
perms.Push = true switch perm.Permission {
perms.Admin = true case "admin":
perms.Push = true
perms.Admin = true
case "write":
perms.Push = true
}
} }
perms.Pull = true perms.Pull = true
return perms, nil return perms, nil

View file

@ -22,6 +22,7 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"github.com/pkg/errors"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"golang.org/x/oauth2/bitbucket" "golang.org/x/oauth2/bitbucket"
) )
@ -34,15 +35,16 @@ const (
) )
const ( const (
pathUser = "%s/2.0/user/" pathUser = "%s/2.0/user/"
pathEmails = "%s/2.0/user/emails" pathEmails = "%s/2.0/user/emails"
pathTeams = "%s/2.0/teams/?%s" pathPermissions = "%s/2.0/user/permissions/repositories?q=repository.full_name=\"%s\""
pathRepo = "%s/2.0/repositories/%s/%s" pathTeams = "%s/2.0/teams/?%s"
pathRepos = "%s/2.0/repositories/%s?%s" pathRepo = "%s/2.0/repositories/%s/%s"
pathHook = "%s/2.0/repositories/%s/%s/hooks/%s" pathRepos = "%s/2.0/repositories/%s?%s"
pathHooks = "%s/2.0/repositories/%s/%s/hooks?%s" pathHook = "%s/2.0/repositories/%s/%s/hooks/%s"
pathSource = "%s/1.0/repositories/%s/%s/src/%s/%s" pathHooks = "%s/2.0/repositories/%s/%s/hooks?%s"
pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build" pathSource = "%s/1.0/repositories/%s/%s/src/%s/%s"
pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build"
) )
type Client struct { type Client struct {
@ -152,6 +154,23 @@ func (c *Client) CreateStatus(owner, name, revision string, status *BuildStatus)
return c.do(uri, post, status, nil) return c.do(uri, post, status, nil)
} }
func (c *Client) GetPermission(full_name string) (*RepoPerm, error) {
out := new(RepoPermResp)
uri := fmt.Sprintf(pathPermissions, c.base, full_name)
err := c.do(uri, get, nil, out)
if err != nil {
return nil, err
}
if len(out.Values) == 0 {
err = errors.New(fmt.Sprint("no permissions in repository ", full_name))
return nil, err
} else {
return out.Values[0], nil
}
}
func (c *Client) do(rawurl, method string, in, out interface{}) error { func (c *Client) do(rawurl, method string, in, out interface{}) error {
uri, err := url.Parse(rawurl) uri, err := url.Parse(rawurl)

View file

@ -224,3 +224,13 @@ type Error struct {
func (e Error) Error() string { func (e Error) Error() string {
return e.Body.Message return e.Body.Message
} }
type RepoPermResp struct {
Page int `json:"page"`
Pages int `json:"pagelen"`
Values []*RepoPerm `json:"values"`
}
type RepoPerm struct {
Permission string `json:"permission"`
}