From f5f30e1f3a67bacaeb00dc86098187d95998a89d Mon Sep 17 00:00:00 2001 From: Johnny5 Date: Fri, 16 Jun 2023 22:04:32 -0500 Subject: [PATCH] Bitbucket Cloud: Add Create, Update and deleting of deployment environment 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. --- .../repositories/deploymentEnvironments.py | 66 +++++++++++++++++-- docs/bitbucket.rst | 14 +++- 2 files changed, 74 insertions(+), 6 deletions(-) diff --git a/atlassian/bitbucket/cloud/repositories/deploymentEnvironments.py b/atlassian/bitbucket/cloud/repositories/deploymentEnvironments.py index b99e7f4a0..55b85e0de 100644 --- a/atlassian/bitbucket/cloud/repositories/deploymentEnvironments.py +++ b/atlassian/bitbucket/cloud/repositories/deploymentEnvironments.py @@ -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. @@ -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) diff --git a/docs/bitbucket.rst b/docs/bitbucket.rst index 307ed6035..9d1f31b08 100755 --- a/docs/bitbucket.rst +++ b/docs/bitbucket.rst @@ -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():