Skip to content

api.py

Bitbucket ¤

Actual class for making API calls.

Parameters:

Name Type Description Default
token str

Token used to authenticate.

required
url str

Full URL to Bitbucket server API. If not specified, environment variable BITBUCKET_URL will be used.

required
version str

API version (1.0).

required

create_pullrequest(self, project, repository, source_branch, target_branch, title, description) ¤

Creates Pull Request in specified repository.

Parameters:

Name Type Description Default
project str

Bitbucket project name (can be shortened).

required
repository str

Repository name inside aforementioned project.

required
source_branch str

Source branch.

required
target_branch str

Target branch.

required
description str

Description of your Pull Request.

required
title str

Title of your Pull Request

required
Source code in bitbucket/api.py
def create_pullrequest(
    self, project, repository, source_branch, target_branch, title, description
):
    """Creates Pull Request in specified repository.

    Args:
        project (str): Bitbucket project name (can be shortened).
        repository (str): Repository name inside aforementioned project.
        source_branch (str): Source branch.
        target_branch (str): Target branch.
        description (str): Description of your Pull Request.
        title (str): Title of your Pull Request

    Returns:

    """

    url = self._api_url(
        "projects/{}/repos/{}/pull-requests".format(project, repository)
    )
    return self._do_requests_post(
        url,
        {
            "description": "{}".format(description),
            "closed": "False",
            "fromRef": {
                "id": "refs/heads/{}".format(source_branch),
                "repository": {
                    "name": "null",
                    "project": {"key": "{}".format(project)},
                    "slug": "{}".format(repository),
                },
            },
            "state": "OPEN",
            "title": "{}".format(title),
            "locked": "False",
            "reviewers": [],
            "open": "True",
            "toRef": {
                "id": "refs/heads/{}".format(target_branch),
                "repository": {
                    "name": "null",
                    "project": {"key": "{}".format(project)},
                    "slug": "{}".format(repository),
                },
            },
        },
    ).json()

login(self, token=None) ¤

Logs into Bitbucket and gets a token

Token should be specified

Parameters:

Name Type Description Default
token str

Token used to authenticate.

None
Source code in bitbucket/api.py
def login(self, token=None):
    """Logs into Bitbucket and gets a token

    Token should be specified

    Args:
        token (str, optional): Token used to authenticate.

    Returns:

    """

    self._token = token
    if token is not None:
        # login with token
        self._auth = BitbucketAuth(token=token)
    else:
        # don't login
        return

    self._token = self._auth.token

BitbucketAuth ¤

__init__(self, token=None) special ¤

Helper class to store authentication token.

Parameters:

Name Type Description Default
token str

Bitbucket personal token.

None
Source code in bitbucket/api.py
def __init__(self, token=None):
    """Helper class to store authentication token.

    Args:
        token (str, optional): Bitbucket personal token.
    """

    if token is not None:
        self._token = token
        return
    raise ValueError("Need token for authentication")

parse_url(url) ¤

Parses a url into the base url and the query params

Parameters:

Name Type Description Default
url str

url with query string, or not.

required

Returns:

Type Description
(str, `dict` of `lists`)

url, query (dict of values).

Source code in bitbucket/api.py
def parse_url(url):
    """Parses a url into the base url and the query params

    Args:
        url (str): url with query string, or not.

    Returns:
        (str, `dict` of `lists`): url, query (dict of values).
    """
    f = furl(url)
    query = f.args
    query = {a[0]: a[1] for a in query.listitems()}
    f.remove(query=True).path.normalize()
    url = f.url

    return url, query