Skip to content

Commit

Permalink
Feature/add workspace permissions for repositories (atlassian-api#1182)
Browse files Browse the repository at this point in the history
* Bitbucket Cloud: Removing workspace name
setter (workspace.name("new-name")) since it isn't listed under the
bitbucket v2 rest documents:

https://developer.atlassian.com/cloud/bitbucket/rest/api-group-workspaces/#api-group-workspaces

* Bitbucket Cloud: Add workspaces permissions.

* Bitbucket Cloud: Add workspaces repository permissions.

* Bitbucket Cloud: Updating bitbucket read the docs.
  • Loading branch information
djgoku authored Jun 8, 2023
1 parent 0497b61 commit 9cd0df8
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 5 deletions.
12 changes: 7 additions & 5 deletions atlassian/bitbucket/cloud/workspaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from requests import HTTPError
from ..base import BitbucketCloudBase

from .permissions import Permissions
from .projects import Projects
from ..repositories import WorkspaceRepositories

Expand Down Expand Up @@ -84,6 +85,7 @@ def exists(self, workspace):
class Workspace(BitbucketCloudBase):
def __init__(self, data, *args, **kwargs):
super(Workspace, self).__init__(None, *args, data=data, expected_type="workspace", **kwargs)
self.__permissions = Permissions(self.url_joiner(self.url, "permissions"), **self._new_session_args)
self.__projects = Projects(self.get_link("projects"), **self._new_session_args)
self.__repositories = WorkspaceRepositories(self.get_link("repositories"), **self._new_session_args)

Expand All @@ -92,11 +94,6 @@ def name(self):
"""The workspace name"""
return self.get_data("name")

@name.setter
def name(self, name):
"""Setter for the workspace name"""
return self.update(name=name)

@property
def slug(self):
"""The workspace slug"""
Expand Down Expand Up @@ -126,6 +123,11 @@ def get_avatar(self):
"""The project avatar"""
return self.get(self.get_link("avatar"), absolute=True)

@property
def permissions(self):
"""The workspace permissions repositories"""
return self.__permissions

@property
def projects(self):
"""The workspace projects"""
Expand Down
126 changes: 126 additions & 0 deletions atlassian/bitbucket/cloud/workspaces/permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# coding=utf-8


from ..base import BitbucketCloudBase


class Permissions(BitbucketCloudBase):
def __init__(self, url, *args, **kwargs):
super(Permissions, self).__init__(url, *args, **kwargs)

def __get_object_workspace_membership(self, data):
return WorkspaceMembership(
self.url,
data,
**self._new_session_args,
)

def __get_object_repository_permission(self, data):
return RepositoryPermission(self.url, data, **self._new_session_args)

def each(self, q=None, sort=None, pagelen=10):
"""
Returns the list of pipelines in this repository.
:param q: string: Query string to narrow down the response.
See https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering for details.
:param sort: string: Name of a response property to sort results.
See https://developer.atlassian.com/bitbucket/api/2/reference/meta/filtering for details.
:return: A generator for the Workspace Permission objects
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-workspaces/#api-workspaces-workspace-permissions-get
"""
params = {}
if sort is not None:
params["sort"] = sort
if q is not None:
params["q"] = q
params["pagelen"] = pagelen
for workspace_membership in self._get_paged(
None,
trailing=True,
paging_workaround=True,
params=params,
):
yield self.__get_object_workspace_membership(workspace_membership)

return

def repositories(self, repo_slug="", pagelen=10):
for permissions in self._get_paged(
"repositories/{}".format(repo_slug), trailing=True, params={"pagelen": pagelen}
):
yield self.__get_object_repository_permission(permissions)


class WorkspaceMembership(BitbucketCloudBase):
def __init__(self, url, data, *args, **kwargs):
super(WorkspaceMembership, self).__init__(url, *args, data=data, expected_type="workspace_membership", **kwargs)

@property
def uuid(self):
"""The workspace_membership uuid"""
return self.get_data("uuid")

@property
def type(self):
"""The workspace_membership type"""
return self.get_data("type")

@property
def user(self):
"""The workspace_membership user"""
return self.get_data("user")

@property
def workspace(self):
"""The workspace_membership workspace"""
return self.get_data("workspace")

@property
def links(self):
"""The workspace_membership links"""
return self.get_data("links")

@property
def added_on(self):
"""The workspace_membership added on"""
return self.get_time("added_on")

@property
def permission(self):
"""The workspace_membership permission"""
return self.get_data("permission")

@property
def last_accessed(self):
"""The workspace_membership last accessed"""
return self.get_time("last_accessed")


class RepositoryPermission(BitbucketCloudBase):
def __init__(self, url, data, *args, **kwargs):
super(RepositoryPermission, self).__init__(
url, *args, data=data, expected_type="repository_permission", **kwargs
)

@property
def repository(self):
"""The repository_permission uuid"""
return self.get_data("repository")

@property
def type(self):
"""The repository_permission type"""
return self.get_data("type")

@property
def permission(self):
"""The repository_permission permission"""
return self.get_data("permission")

@property
def user(self):
"""The repository_permission workspace"""
return self.get_data("user")
9 changes: 9 additions & 0 deletions docs/bitbucket.rst
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ Bitbucket Cloud
# Get a single workplace by workplace slug
workplace = cloud.workspaces.get(workspace_slug)
# Get a list of permissions in a workspace (this may not work depending on the size of your workspace)
workplace.permissions.each():
# Get a list of repository permissions in a workspace (this may not work depending on the size of your workspace)
workplace.permissions.repositories():
# Get a single repository permissions in a workspace
workplace.permissions.repositories(repo_slug):
# Get a list of projects in a workspace
workplace.projects.each():
Expand Down

0 comments on commit 9cd0df8

Please sign in to comment.