Skip to content
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
3 changes: 0 additions & 3 deletions atlassian/bitbucket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,6 @@ def delete_issue(self, workspace, repository_slug, id):
.repositories.get(repository_slug)
.issues.get(id)
.delete()
.data
)

@deprecated(
Expand Down Expand Up @@ -2647,7 +2646,6 @@ def delete_branch_restriction(self, workspace, repository_slug, id):
.repositories.get(repository_slug)
.branch_restrictions.get(id)
.delete()
.data
)

@deprecated(
Expand Down Expand Up @@ -2734,5 +2732,4 @@ def delete_default_reviewer(self, workspace, repository_slug, user):
.repositories.get(repository_slug)
.default_reviewers.get(user)
.delete()
.data
)
103 changes: 90 additions & 13 deletions atlassian/bitbucket/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# coding=utf-8

import copy
import re
import sys

from datetime import datetime
from pprint import PrettyPrinter
from ..rest_client import AtlassianRestAPI


Expand All @@ -17,27 +19,37 @@ class BitbucketBase(AtlassianRestAPI):
def __init__(self, url, *args, **kwargs):
"""
Init the rest api wrapper
:param url: The base url used for the rest api.
:param *args: The fixed arguments for the AtlassianRestApi.
:param **kwargs: The fixed arguments for the AtlassianRestApi.

:param url: string: The base url used for the rest api.
:param *args: list: The fixed arguments for the AtlassianRestApi.
:param **kwargs: dict: The keyword arguments for the AtlassianRestApi.

:return: nothing
"""
self._update_data(kwargs.pop("data", {}))
if url is None:
url = self.get_link("self")
if isinstance(url, list): # Server has a list of links
url = url[0]
self.timeformat_lambda = kwargs.pop("timeformat_lambda", lambda x: self._default_timeformat_lambda(x))
self._check_timeformat_lambda()
super(BitbucketBase, self).__init__(url, *args, **kwargs)

def __str__(self):
return PrettyPrinter(indent=4).pformat(self.__data if self.__data else self)

def _get_paged(self, url, params=None, data=None, flags=None, trailing=None, absolute=False):
"""
Used to get the paged data
:param url: The url to retrieve.
:param params: The parameters (optional).
:param data: The data (optional).
:param flags: The flags (optional).
:param trailing: If True, a trailing slash is added to the url (optional).
:param absolute: If True, the url is used absolute and not relative to the root (optional).

:return: A generator for the project objects
:param url: string: The url to retrieve
:param params: dict (default is None): The parameters
:param data: dict (default is None): The data
:param flags: string[] (default is None): The flags
:param trailing: bool (default is None): If True, a trailing slash is added to the url
:param absolute: bool (default is False): If True, the url is used absolute and not relative to the root

:return: A generator object for the data elements
"""

if params is None:
Expand All @@ -64,7 +76,21 @@ def _get_paged(self, url, params=None, data=None, flags=None, trailing=None, abs

return

@staticmethod
def _default_timeformat_lambda(timestamp):
"""
Default time format function.

:param timestamp: datetime str: The datetime object of the parsed string or the raw value if parsing failed

:return: timestamp if it was a datetime object, else None
"""
return timestamp if isinstance(timestamp, datetime) else None

def _check_timeformat_lambda(self):
"""
Check the lambda for for the time format. Raise an exception if the the value is wrong
"""
LAMBDA = lambda: 0 # noqa: E731
if self.timeformat_lambda is None or (
isinstance(self.timeformat_lambda, type(LAMBDA)) and self.timeformat_lambda.__name__ == LAMBDA.__name__
Expand All @@ -73,11 +99,45 @@ def _check_timeformat_lambda(self):
else:
ValueError("Expected [None] or [lambda function] for argument [timeformat_func]")

@staticmethod
def _default_timeformat_lambda(timestamp):
return timestamp if isinstance(timestamp, datetime) else None
def _sub_url(self, url):
"""
Get the full url from a relative one.

:param url: string: The sub url

:return: The absolute url
"""
return self.url_joiner(self.url, url)

@property
def data(self):
"""
Get the internal cached data. For data integrity a deep copy is returned.

:return: A copy of the data cache
"""
return copy.copy(self.__data)

def get_data(self, id, default=None):
"""
Get a data element from the internal data cache. For data integrity a deep copy is returned.
If data isn't present, the default value is returned.

:param id: string: The data element to return
:param default: any (default is None): The value to return if id is not present

:return: The requested data element
"""
return copy.copy(self.__data[id]) if id in self.__data else default

def get_time(self, id):
"""
Return the time value with the expected format.

:param id: string: The id for the time data

:return: The time with the configured format, see timeformat_lambda.
"""
value_str = self.get_data(id)
if self.timeformat_lambda is None:
return value_str
Expand All @@ -92,8 +152,25 @@ def get_time(self, id):

return self.timeformat_lambda(value)

def _update_data(self, data):
"""
Internal function to update the data.

:param data: dict: The new data.

:return: The updated object
"""
self.__data = data

return self

@property
def _new_session_args(self):
"""
Get the kwargs for new objects (session, root, version,...).

:return: A dict with the kwargs for new objects
"""
return dict(
session=self._session,
cloud=self.cloud,
Expand Down
74 changes: 29 additions & 45 deletions atlassian/bitbucket/cloud/base.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
# coding=utf-8
import copy
from pprint import PrettyPrinter

from ..base import BitbucketBase


class BitbucketCloudBase(BitbucketBase):
def __init__(self, url, link="self", *args, **kwargs):
def __init__(self, url, *args, **kwargs):
"""
Init the rest api wrapper
:param url: The base url used for the rest api.
:param link: Attribute to resolve a url based on input data.
If None, no tries to receive an url from input data
:param *args: The fixed arguments for the AtlassianRestApi.
:param **kwargs: The keyword arguments for the AtlassianRestApi.

:param url: string: The base url used for the rest api.
:param *args: list: The fixed arguments for the AtlassianRestApi.
:param **kwargs: dict: The keyword arguments for the AtlassianRestApi.

:return: nothing
"""
if "data" in kwargs:
self.__data = kwargs.pop("data")
expected_type = kwargs.pop("expected_type")
if not self.get_data("type") == expected_type:
raise ValueError(
"Expected type of data is [{}], got [{}].".format(expected_type, self.get_data("type"))
)
if url is None and link is not None:
url = self.get_link(link)

expected_type = kwargs.pop("expected_type", None)
super(BitbucketCloudBase, self).__init__(url, *args, **kwargs)
if expected_type is not None and not expected_type == self.get_data("type"):
raise ValueError("Expected type of data is [{}], got [{}].".format(expected_type, self.get_data("type")))

def get_link(self, link):
"""
Get a link from the data.

:param link: string: The link identifier

def __str__(self):
return PrettyPrinter(indent=4).pformat(self.__data)
:return: The requested link or None if it isn't present
"""
links = self.get_data("links")
if links is None or link not in links:
return None
return links[link]["href"]

def _get_paged(self, url, params=None, data=None, flags=None, trailing=None, absolute=False):
"""
Used to get the paged data
:param url: The url to retrieve.
:param params: The parameters (optional).
:param data: The data (optional).
:param flags: The flags (optional).
:param trailing: If True, a trailing slash is added to the url (optional).
:param absolute: If True, the url is used absolute and not relative to the root (optional).

:return: A generator for the project objects

:param url: string: The url to retrieve
:param params: dict (default is None): The parameters
:param data: dict (default is None): The data
:param flags: string[] (default is None): The flags
:param trailing: bool (default is None): If True, a trailing slash is added to the url
:param absolute: bool (default is False): If True, the url is used absolute and not relative to the root

:return: A generator object for the data elements
"""

if params is None:
Expand Down Expand Up @@ -70,20 +71,3 @@ def _get_paged(self, url, params=None, data=None, flags=None, trailing=None, abs
absolute = True

return

def update(self, **kwargs):
"""
Fields not present in the request body are ignored.
"""
self.__data = super(BitbucketBase, self).put(None, data=kwargs)
return self

@property
def data(self):
return copy.copy(self.__data)

def get_data(self, id, default=None):
return copy.copy(self.__data[id]) if id in self.__data else default

def get_link(self, link):
return self.__data["links"][link]["href"]
Loading