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/
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)
perms := new(model.Perm)
_, err := client.FindRepo(owner, name)
repo, err := client.FindRepo(owner, name)
if err != nil {
return perms, err
}
_, err = client.ListHooks(owner, name, &internal.ListOpts{})
perm, err := client.GetPermission(repo.FullName)
if err == nil {
perms.Push = true
perms.Admin = true
switch perm.Permission {
case "admin":
perms.Push = true
perms.Admin = true
case "write":
perms.Push = true
}
}
perms.Pull = true
return perms, nil

View file

@ -22,6 +22,7 @@ import (
"net/http"
"net/url"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"golang.org/x/oauth2/bitbucket"
)
@ -34,15 +35,16 @@ const (
)
const (
pathUser = "%s/2.0/user/"
pathEmails = "%s/2.0/user/emails"
pathTeams = "%s/2.0/teams/?%s"
pathRepo = "%s/2.0/repositories/%s/%s"
pathRepos = "%s/2.0/repositories/%s?%s"
pathHook = "%s/2.0/repositories/%s/%s/hooks/%s"
pathHooks = "%s/2.0/repositories/%s/%s/hooks?%s"
pathSource = "%s/1.0/repositories/%s/%s/src/%s/%s"
pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build"
pathUser = "%s/2.0/user/"
pathEmails = "%s/2.0/user/emails"
pathPermissions = "%s/2.0/user/permissions/repositories?q=repository.full_name=\"%s\""
pathTeams = "%s/2.0/teams/?%s"
pathRepo = "%s/2.0/repositories/%s/%s"
pathRepos = "%s/2.0/repositories/%s?%s"
pathHook = "%s/2.0/repositories/%s/%s/hooks/%s"
pathHooks = "%s/2.0/repositories/%s/%s/hooks?%s"
pathSource = "%s/1.0/repositories/%s/%s/src/%s/%s"
pathStatus = "%s/2.0/repositories/%s/%s/commit/%s/statuses/build"
)
type Client struct {
@ -152,6 +154,23 @@ func (c *Client) CreateStatus(owner, name, revision string, status *BuildStatus)
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 {
uri, err := url.Parse(rawurl)

View file

@ -224,3 +224,13 @@ type Error struct {
func (e Error) Error() string {
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"`
}