Skip to content

Commit

Permalink
Bitbucket Cloud: Add Create, Update and deleting of deployment enviro…
Browse files Browse the repository at this point in the history
…nment variables (#1187)

* Bitbucket Cloud: Add Creating deployment environment variables.

* Bitbucket Cloud: Add Updating (key or value) and Deleting of
deployment environment variables

* Bitbucket Cloud: Removing commented code in deployment environment variables

* Bitbucket Cloud: Update bitbucket cloud docs for deployment environment variables.
  • Loading branch information
djgoku authored Jun 17, 2023
1 parent f1b182c commit f5f30e1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
66 changes: 61 additions & 5 deletions atlassian/bitbucket/cloud/repositories/deploymentEnvironments.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,26 @@ def __init__(self, url, *args, **kwargs):

def __get_object(self, data):
return DeploymentEnvironmentVariable(
# self.url_joiner(super().get_deployment_environment_variable_url(), data["uuid"]),
self.url,
data,
**self._new_session_args,
)

def create(self, key, value, secured):
"""
Create a new deployment environment variable for the given repository.
:param key: string: The unique name of the variable.
:param value: string: The value of the variable. If the variable is secured, this will be empty.
:param secured: boolean: If true, this variable will be treated as secured. The value will never be exposed in the logs or the REST API.
:return: The created DeploymentEnvironment object
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-deployments-config-environments-environment-uuid-variables-post
"""
data = {"key": key, "value": value, "secured": secured}
return self.__get_object(self.post(None, data=data))

def each(self, q=None, sort=None):
"""
Returns the list of deployment environment variables in this repository.
Expand Down Expand Up @@ -180,26 +194,68 @@ def each(self, q=None, sort=None):

class DeploymentEnvironmentVariable(BitbucketCloudBase):
def __init__(self, url, data, *args, **kwargs):
# This is needed when creating a new environment variable
# since the API doesn't return a 'type'.
if data.get("type") is None:
data["type"] = "pipeline_variable"

super(DeploymentEnvironmentVariable, self).__init__(
url, *args, data=data, expected_type="pipeline_variable", **kwargs
)

def update(self, **kwargs):
"""
Update the repository variable properties. Fields not present in the request body are ignored.
:param kwargs: dict: The data to update.
:return: The updated repository variable
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-deployments-config-environments-environment-uuid-variables-variable-uuid-put
"""
return self._update_data(self.put("/{}".format(self.uuid), data=kwargs))

def delete(self):
"""
Delete the repository variable.
:return: The response on success
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-deployments-config-environments-environment-uuid-variables-variable-uuid-delete
"""
return super(DeploymentEnvironmentVariable, self).delete("/{}".format(self.uuid))

@property
def uuid(self):
"""The deployment environment uuid"""
"""The deployment environment variable uuid"""
return self.get_data("uuid")

@property
def key(self):
"""The deployment environment variable key"""
return self.get_data("key")

@key.setter
def key(self, key):
"""Setter for the deployment environment variable key"""
return self.update(key=key)

@property
def secured(self):
"""The deployment environment secured"""
"""The deployment environment variable is secured"""
return self.get_data("secured")

@property
def type(self):
"""The deployment environment deployment gate enabled"""
"""The deployment environment variable type"""
return self.get_data("type")

@property
def value(self):
"""The deployment environment environment lock enabled"""
"""The deployment environment variable value"""
return self.get_data("value")

@value.setter
def value(self, value):
"""Setter for the deployment environment variable value"""
return self.update(value=value)
14 changes: 13 additions & 1 deletion docs/bitbucket.rst
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,19 @@ Bitbucket Cloud
deployment_environment = repository.deployment_environments.get(deployment_environment_key)
# Get a list of deployment environment variables from a deployment environment
deployment_environment.deployment_environment_variables.each():
deployment_environment_variables = deployment_environment.deployment_environment_variables.each():
# Create a new deployment environment variable with a name of 'KEY', value of 'VALUE' and is not secured.
new_deployment_environment_variable = deployment_environment.deployment_environment_variables.create("KEY", "VALUE", False)
# Update the 'key' field of repository_variable
updated_deployment_environment_variable = new_deployment_environment_variable.update(key="UPDATED_DEPLOYMENT_ENVIRONMENT_VARIABLE_KEY")
# Update the 'value' field of repository_variable
updated_deployment_environment_variable = new_deployment_environment_variable.update(value="UPDATED_DEPLOYMENT_ENVIRONMENT_VARIABLE_VALUE")
# Delete deployment environment variable
updated_deployment_environment_variable.delete()
# Get a list of group permissions from a repository
repository.group_permissions.each():
Expand Down

0 comments on commit f5f30e1

Please sign in to comment.