Skip to content

prefect_bitbucket.credentials

Module to enable authenticate interactions with BitBucket.

Classes

BitBucketCredentials

Bases: CredentialsBlock

Store BitBucket credentials to interact with private BitBucket repositories.

Attributes:

Name Type Description
token Optional[SecretStr]

An access token to authenticate with BitBucket. This is required for accessing private repositories.

username Optional[str]

Identification name unique across entire BitBucket site.

password Optional[SecretStr]

The password to authenticate to BitBucket.

url str

The base URL of your BitBucket instance.

Examples:

Load stored BitBucket credentials:

from prefect_bitbucket import BitBucketCredentials
bitbucket_credentials_block = BitBucketCredentials.load("BLOCK_NAME")

Source code in prefect_bitbucket/credentials.py
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
class BitBucketCredentials(CredentialsBlock):
    """Store BitBucket credentials to interact with private BitBucket repositories.

    Attributes:
        token: An access token to authenticate with BitBucket. This is required
            for accessing private repositories.
        username: Identification name unique across entire BitBucket site.
        password: The password to authenticate to BitBucket.
        url: The base URL of your BitBucket instance.


    Examples:
        Load stored BitBucket credentials:
        ```python
        from prefect_bitbucket import BitBucketCredentials
        bitbucket_credentials_block = BitBucketCredentials.load("BLOCK_NAME")
        ```


    """

    _block_type_name = "BitBucket Credentials"
    _logo_url = "https://cdn.sanity.io/images/3ugk85nk/production/5d729f7355fb6828c4b605268ded9cfafab3ae4f-250x250.png"  # noqa
    token: Optional[SecretStr] = Field(
        name="Personal Access Token",
        default=None,
        description=(
            "A BitBucket Personal Access Token - required for private repositories."
        ),
        example="x-token-auth:my-token",
    )
    username: Optional[str] = Field(
        default=None,
        description="Identification name unique across entire BitBucket site.",
    )
    password: Optional[SecretStr] = Field(
        default=None, description="The password to authenticate to BitBucket."
    )
    url: str = Field(
        default="https://api.bitbucket.org/",
        description="The base URL of your BitBucket instance.",
        title="URL",
    )

    @validator("username")
    def _validate_username(cls, value: str) -> str:
        """When username provided, will validate it."""
        pattern = "^[A-Za-z0-9_-]*$"

        if not re.match(pattern, value):
            raise ValueError(
                "Username must be alpha, num, dash and/or underscore only."
            )
        if not len(value) <= 30:
            raise ValueError("Username cannot be longer than 30 chars.")
        return value

    def get_client(
        self, client_type: Union[str, ClientType], **client_kwargs
    ) -> Union[Cloud, Bitbucket]:
        """Get an authenticated local or cloud Bitbucket client.

        Args:
            client_type: Whether to use a local or cloud client.

        Returns:
            An authenticated Bitbucket client.

        """
        # ref: https://atlassian-python-api.readthedocs.io/
        if isinstance(client_type, str):
            client_type = ClientType(client_type.lower())

        password = self.password.get_secret_value()
        input_client_kwargs = dict(
            url=self.url, username=self.username, password=password
        )
        input_client_kwargs.update(**client_kwargs)

        if client_type == ClientType.CLOUD:
            client = Cloud(**input_client_kwargs)
        else:
            client = Bitbucket(**input_client_kwargs)
        return client

Functions

get_client

Get an authenticated local or cloud Bitbucket client.

Parameters:

Name Type Description Default
client_type Union[str, ClientType]

Whether to use a local or cloud client.

required

Returns:

Type Description
Union[Cloud, Bitbucket]

An authenticated Bitbucket client.

Source code in prefect_bitbucket/credentials.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
def get_client(
    self, client_type: Union[str, ClientType], **client_kwargs
) -> Union[Cloud, Bitbucket]:
    """Get an authenticated local or cloud Bitbucket client.

    Args:
        client_type: Whether to use a local or cloud client.

    Returns:
        An authenticated Bitbucket client.

    """
    # ref: https://atlassian-python-api.readthedocs.io/
    if isinstance(client_type, str):
        client_type = ClientType(client_type.lower())

    password = self.password.get_secret_value()
    input_client_kwargs = dict(
        url=self.url, username=self.username, password=password
    )
    input_client_kwargs.update(**client_kwargs)

    if client_type == ClientType.CLOUD:
        client = Cloud(**input_client_kwargs)
    else:
        client = Bitbucket(**input_client_kwargs)
    return client

ClientType

Bases: Enum

The client type to use.

Source code in prefect_bitbucket/credentials.py
20
21
22
23
24
class ClientType(Enum):
    """The client type to use."""

    LOCAL = "local"
    CLOUD = "cloud"