Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group privileges (for bitbucket v1.0) #136

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ func apiBaseUrl() (*url.URL, error) {
}

type Client struct {
Auth *auth
Users users
User user
Teams teams
Repositories *Repositories
Workspaces *Workspace
Pagelen uint64
MaxDepth uint64
apiBaseURL *url.URL
Auth *auth
Users users
User user
Teams teams
Repositories *Repositories
Workspaces *Workspace
GroupPrivileges *GroupPrivileges
Pagelen uint64
MaxDepth uint64
apiBaseURL *url.URL

HttpClient *http.Client
}
Expand Down Expand Up @@ -156,6 +157,7 @@ func injectClient(a *auth) *Client {
c.User = &User{c: c}
c.Teams = &Teams{c: c}
c.Workspaces = &Workspace{c: c, Repositories: c.Repositories, Permissions: &Permission{c: c}}
c.GroupPrivileges = &GroupPrivileges{c: c}
c.HttpClient = new(http.Client)
return c
}
Expand All @@ -173,21 +175,29 @@ func (c *Client) SetApiBaseURL(urlStr url.URL) {
}

func (c *Client) executeRaw(method string, urlStr string, text string) (io.ReadCloser, error) {
return c.executeRawContentType(method, urlStr, text, "application/json")
}

func (c *Client) executeRawContentType(method, urlStr, text, contentType string) (io.ReadCloser, error) {
body := strings.NewReader(text)

req, err := http.NewRequest(method, urlStr, body)
if err != nil {
return nil, err
}
if text != "" {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", contentType)
}

c.authenticateRequest(req)
return c.doRawRequest(req, false)
}

func (c *Client) execute(method string, urlStr string, text string) (interface{}, error) {
return c.executeContentType(method, urlStr, text, "application/json")
}

func (c *Client) executeContentType(method string, urlStr string, text string, contentType string) (interface{}, error) {
// Use pagination if changed from default value
const DEC_RADIX = 10
if strings.Contains(urlStr, "/repositories/") {
Expand Down Expand Up @@ -221,7 +231,7 @@ func (c *Client) execute(method string, urlStr string, text string) (interface{}
return nil, err
}
if text != "" {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Content-Type", contentType)
}

c.authenticateRequest(req)
Expand Down
4 changes: 1 addition & 3 deletions commits.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package bitbucket

import (
"net/url"
"encoding/json"
"net/url"
)

type Commits struct {
Expand Down Expand Up @@ -50,7 +50,6 @@ func (cm *Commits) RemoveApprove(cmo *CommitsOptions) (interface{}, error) {
return cm.c.execute("DELETE", urlStr, "")
}


func (cm *Commits) CreateCommitStatus(cmo *CommitsOptions, cso *CommitStatusOptions) (interface{}, error) {
urlStr := cm.c.requestUrl("/repositories/%s/%s/commit/%s/statuses/build", cmo.Owner, cmo.RepoSlug, cmo.Revision)
data, err := json.Marshal(cso)
Expand All @@ -60,7 +59,6 @@ func (cm *Commits) CreateCommitStatus(cmo *CommitsOptions, cso *CommitStatusOpti
return cm.c.execute("POST", urlStr, string(data))
}


func (cm *Commits) buildCommitsQuery(include, exclude string) string {

p := url.Values{}
Expand Down
112 changes: 112 additions & 0 deletions groupprivileges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package bitbucket

import (
"fmt"

"github.com/mitchellh/mapstructure"
)

type GroupPrivileges struct {
c *Client
}

type GroupPrivilegesOptions struct {
Owner string
RepoSlug string
Group string
GroupOwner string
Permission string
}

type GroupPrivilege struct {
Repo string `mapstructure:"repo"`
Privilege string `mapstructure:"privilege"`
Group struct {
Owner struct {
DisplayName string `mapstructure:"display_name"`
UUID string `mapstructure:"uuid"`
IsActive bool `mapstructure:"is_active"`
IsTeam bool `mapstructure:"is_team"`
MentionID string `mapstructure:"mention_id"`
Avatar string `mapstructure:"avatar"`
Nickname string `mapstructure:"nickname"`
AccountID string `mapstructure:"account_id"`
} `mapstructure:"owner"`
Name string `mapstructure:"name"`
Members []interface{} `mapstructure:"members"`
Slug string `mapstructure:"slug"`
} `mapstructure:"group"`
Repository struct {
Owner struct {
DisplayName string `mapstructure:"display_name"`
UUID string `mapstructure:"uuid"`
IsActive bool `mapstructure:"is_active"`
IsTeam bool `mapstructure:"is_team"`
MentionID string `mapstructure:"mention_id"`
Avatar string `mapstructure:"avatar"`
Nickname string `mapstructure:"nickname"`
AccountID string `mapstructure:"account_id"`
} `mapstructure:"owner"`
Name string `mapstructure:"name"`
Slug string `mapstructure:"slug"`
} `mapstructure:"repository"`
}

func (g *GroupPrivileges) List(workspace, repoSlug string) ([]GroupPrivilege, error) {
urlStr := fmt.Sprintf("%s/1.0/group-privileges/%s/%s", g.c.GetApiHostnameURL(), workspace, repoSlug)
data, err := g.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}

return g.decodeGroupPrivileges(data)
}

func (g *GroupPrivileges) Add(gpo GroupPrivilegesOptions) ([]GroupPrivilege, error) {
groupOwner := gpo.GroupOwner
if gpo.GroupOwner == "" {
groupOwner = gpo.Owner

}
urlStr := fmt.Sprintf("%s/1.0/group-privileges/%s/%s/%s/%s/", g.c.GetApiHostnameURL(), gpo.Owner, gpo.RepoSlug, groupOwner, gpo.Group)
data, err := g.c.executeContentType("PUT", urlStr, gpo.Permission, "application/x-www-form-urlencoded")
if err != nil {
return nil, err
}

return g.decodeGroupPrivileges(data)
}

func (g *GroupPrivileges) Get(gpo GroupPrivilegesOptions) ([]GroupPrivilege, error) {
groupOwner := gpo.GroupOwner
if gpo.GroupOwner == "" {
groupOwner = gpo.Owner

}
urlStr := fmt.Sprintf("%s/1.0/group-privileges/%s/%s/%s/%s/", g.c.GetApiHostnameURL(), gpo.Owner, gpo.RepoSlug, groupOwner, gpo.Group)
data, err := g.c.execute("GET", urlStr, "")
if err != nil {
return nil, err
}

return g.decodeGroupPrivileges(data)
}

func (g *GroupPrivileges) Delete(gpo GroupPrivilegesOptions) (interface{}, error) {
groupOwner := gpo.GroupOwner
if gpo.GroupOwner == "" {
groupOwner = gpo.Owner

}
urlStr := fmt.Sprintf("%s/1.0/group-privileges/%s/%s/%s/%s/", g.c.GetApiHostnameURL(), gpo.Owner, gpo.RepoSlug, groupOwner, gpo.Group)
return g.c.execute("DELETE", urlStr, "")
}

func (g *GroupPrivileges) decodeGroupPrivileges(response interface{}) ([]GroupPrivilege, error) {
var gp []GroupPrivilege
err := mapstructure.Decode(response, &gp)
if err != nil {
return nil, err
}
return gp, nil
}
5 changes: 2 additions & 3 deletions teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (t *Teams) Repositories(teamname string) (interface{}, error) {
}

func (t *Teams) Projects(teamname string) (interface{}, error) {
urlStr := t.c.requestUrl("/teams/%s/projects/", teamname)
return t.c.execute("GET", urlStr, "")
urlStr := t.c.requestUrl("/teams/%s/projects/", teamname)
return t.c.execute("GET", urlStr, "")
}