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

Added groups and user repository permissions #217

Merged
Merged
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
14 changes: 14 additions & 0 deletions bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,20 @@ type RepositoryEnvironmentsOptions struct {
RepoSlug string `json:"repo_slug"`
}

type RepositoryGroupPermissionsOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
Group string `json:"group"`
Permission string `json:"permission"`
}

type RepositoryUserPermissionsOptions struct {
Owner string `json:"owner"`
RepoSlug string `json:"repo_slug"`
User string `json:"user"`
Permission string `json:"permission"`
}

type RepositoryEnvironmentTypeOption int

const (
Expand Down
255 changes: 255 additions & 0 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,51 @@ type DefaultReviewers struct {
DefaultReviewers []DefaultReviewer
}

type Group struct {
AccountPrivilege string `mapstructure:"account_privilege"`
DefaultPermission string `mapstructure:"default_permission"`
EmailForwardingDisabled bool `mapstructure:"email_forwarding_disabled"`
FullSlug string `mapstructure:"full_slug"`
Links map[string]map[string]string
Name string `mapstructure:"name"`
Slug string `mapstructure:"slug"`
Type string `mapstructure:"type"`
Workspace map[string]interface{} `mapstructure:"workspace"`
Owner map[string]interface{}
}

type GroupPermission struct {
Type string
Group Group
Permission string
Links map[string]map[string]string
}

type GroupPermissions struct {
Page int
Pagelen int
MaxDepth int
Size int
Next string
GroupPermissions []GroupPermission
}

type UserPermission struct {
Type string
User User
Permission string
Links map[string]map[string]string
}

type UserPermissions struct {
Page int
Pagelen int
MaxDepth int
Size int
Next string
UserPermissions []UserPermission
}

func (r *Repository) Create(ro *RepositoryOptions) (*Repository, error) {
data, err := r.buildRepositoryBody(ro)
if err != nil {
Expand Down Expand Up @@ -834,6 +879,88 @@ func (r *Repository) UpdateDeploymentVariable(opt *RepositoryDeploymentVariableO
return decodeDeploymentVariable(response)
}

func (r *Repository) ListGroupPermissions(ro *RepositoryOptions) (*GroupPermissions, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/permissions-config/groups?pagelen=1", ro.Owner, ro.RepoSlug)

res, err := r.c.executePaginated("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeGroupsPermissions(res)
}

func (r *Repository) SetGroupPermissions(rgo *RepositoryGroupPermissionsOptions) (*GroupPermission, error) {
body, err := r.buildRepositoryGroupPermissionBody(rgo)
if err != nil {
return nil, err
}

urlStr := r.c.requestUrl("/repositories/%s/%s/permissions-config/groups/%s", rgo.Owner, rgo.RepoSlug, rgo.Group)

res, err := r.c.execute("PUT", urlStr, body)
if err != nil {
return nil, err
}

return decodeGroupPermissions(res)
}

func (r *Repository) DeleteGroupPermissions(rgo *RepositoryGroupPermissionsOptions) (interface{}, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/permissions-config/groups/%s", rgo.Owner, rgo.RepoSlug, rgo.Group)
return r.c.execute("DELETE", urlStr, "")
}

func (r *Repository) GetGroupPermissions(rgo *RepositoryGroupPermissionsOptions) (*GroupPermission, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/permissions-config/groups/%s", rgo.Owner, rgo.RepoSlug, rgo.Group)

res, err := r.c.executePaginated("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeGroupPermissions(res)
}

func (r *Repository) ListUserPermissions(ro *RepositoryOptions) (*UserPermissions, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/permissions-config/users?pagelen=1", ro.Owner, ro.RepoSlug)

res, err := r.c.executePaginated("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeUsersPermissions(res)
}

func (r *Repository) SetUserPermissions(rgo *RepositoryUserPermissionsOptions) (*UserPermission, error) {
body, err := r.buildRepositoryUserPermissionBody(rgo)
if err != nil {
return nil, err
}

urlStr := r.c.requestUrl("/repositories/%s/%s/permissions-config/users/%s", rgo.Owner, rgo.RepoSlug, rgo.User)

res, err := r.c.execute("PUT", urlStr, body)
if err != nil {
return nil, err
}

return decodeUserPermissions(res)
}

func (r *Repository) DeleteUserPermissions(rgo *RepositoryUserPermissionsOptions) (interface{}, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/permissions-config/users/%s", rgo.Owner, rgo.RepoSlug, rgo.User)
return r.c.execute("DELETE", urlStr, "")
}

func (r *Repository) GetUserPermissions(rgo *RepositoryUserPermissionsOptions) (*UserPermission, error) {
urlStr := r.c.requestUrl("/repositories/%s/%s/permissions-config/users/%s", rgo.Owner, rgo.RepoSlug, rgo.User)

res, err := r.c.executePaginated("GET", urlStr, "")
if err != nil {
return nil, err
}
return decodeUserPermissions(res)
}

func (r *Repository) buildRepositoryBody(ro *RepositoryOptions) (string, error) {
body := map[string]interface{}{}

Expand Down Expand Up @@ -1019,6 +1146,22 @@ func (r *Repository) buildDeploymentVariableBody(opt *RepositoryDeploymentVariab
return r.buildJsonBody(body)
}

func (r *Repository) buildRepositoryGroupPermissionBody(rpo *RepositoryGroupPermissionsOptions) (string, error) {
body := map[string]interface{}{}

body["permission"] = rpo.Permission

return r.buildJsonBody(body)
}

func (r *Repository) buildRepositoryUserPermissionBody(rpo *RepositoryUserPermissionsOptions) (string, error) {
body := map[string]interface{}{}

body["permission"] = rpo.Permission

return r.buildJsonBody(body)
}

func (r *Repository) buildJsonBody(body map[string]interface{}) (string, error) {
data, err := json.Marshal(body)
if err != nil {
Expand Down Expand Up @@ -1591,3 +1734,115 @@ func decodeDefaultReviewers(response interface{}) (*DefaultReviewers, error) {
}
return &defaultReviewerVariables, nil
}

func decodeGroupPermissions(response interface{}) (*GroupPermission, error) {
var groupPermission GroupPermission
err := mapstructure.Decode(response, &groupPermission)
if err != nil {
return nil, err
}
return &groupPermission, nil
}

func decodeGroupsPermissions(response interface{}) (*GroupPermissions, error) {
responseMap := response.(map[string]interface{})
values := responseMap["values"].([]interface{})

var variables []GroupPermission
for _, variable := range values {
var groupPermission GroupPermission
err := mapstructure.Decode(variable, &groupPermission)
if err == nil {
variables = append(variables, groupPermission)
}
}

page, ok := responseMap["page"].(float64)
if !ok {
page = 0
}

pagelen, ok := responseMap["pagelen"].(float64)
if !ok {
pagelen = 0
}
max_depth, ok := responseMap["max_depth"].(float64)
if !ok {
max_depth = 0
}
size, ok := responseMap["size"].(float64)
if !ok {
size = 0
}

next, ok := responseMap["next"].(string)
if !ok {
next = ""
}

groupPermissions := GroupPermissions{
Page: int(page),
Pagelen: int(pagelen),
MaxDepth: int(max_depth),
Size: int(size),
Next: next,
GroupPermissions: variables,
}
return &groupPermissions, nil
}

func decodeUserPermissions(response interface{}) (*UserPermission, error) {
var userPermission UserPermission
err := mapstructure.Decode(response, &userPermission)
if err != nil {
return nil, err
}
return &userPermission, nil
}

func decodeUsersPermissions(response interface{}) (*UserPermissions, error) {
responseMap := response.(map[string]interface{})
values := responseMap["values"].([]interface{})

var variables []UserPermission
for _, variable := range values {
var userPermission UserPermission
err := mapstructure.Decode(variable, &userPermission)
if err == nil {
variables = append(variables, userPermission)
}
}

page, ok := responseMap["page"].(float64)
if !ok {
page = 0
}

pagelen, ok := responseMap["pagelen"].(float64)
if !ok {
pagelen = 0
}
max_depth, ok := responseMap["max_depth"].(float64)
if !ok {
max_depth = 0
}
size, ok := responseMap["size"].(float64)
if !ok {
size = 0
}

next, ok := responseMap["next"].(string)
if !ok {
next = ""
}

userPermissions := UserPermissions{
Page: int(page),
Pagelen: int(pagelen),
MaxDepth: int(max_depth),
Size: int(size),
Next: next,
UserPermissions: variables,
}
return &userPermissions, nil
}
Loading