Skip to content

Commit

Permalink
Bitbucket Cloud: Add Create, Update and Deleting of repository variab…
Browse files Browse the repository at this point in the history
…les (#1186)

* Bitbucket Cloud: Add Creating repository variables

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

* Bitbucket Cloud: Updating API documentation links for Repository Variables

* Bitbucket Cloud: Update bitbucket cloud docs for repository variables.
  • Loading branch information
djgoku authored Jun 17, 2023
1 parent 3422a81 commit f1b182c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
55 changes: 53 additions & 2 deletions atlassian/bitbucket/cloud/repositories/repositoryVariables.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ def __get_object(self, data):
**self._new_session_args,
)

def create(self, key, value, secured):
"""
Create a new repository 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 RepositoryVariable object
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-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 repository variables in this repository.
Expand All @@ -25,7 +44,7 @@ def each(self, q=None, sort=None):
:return: A generator for the RepositoryVariable objects
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pipelines_config/variables/#get
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-get
"""
params = {}
if sort is not None:
Expand All @@ -50,7 +69,7 @@ def get(self, uuid):
:return: The requested RepositoryVariable objects
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pipelines_config/variables/%7Bvariable_uuid%7D#get
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-variable-uuid-get
"""
return self.__get_object(super(RepositoryVariables, self).get(uuid))

Expand All @@ -59,6 +78,28 @@ class RepositoryVariable(BitbucketCloudBase):
def __init__(self, url, data, *args, **kwargs):
super(RepositoryVariable, 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-pipelines-config-variables-variable-uuid-put
"""
return self._update_data(self.put(None, 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-pipelines-config-variables-variable-uuid-delete
"""
return super(RepositoryVariable, self).delete(None)

@property
def uuid(self):
"""The repository variable uuid"""
Expand All @@ -69,6 +110,11 @@ def key(self):
"""The repository variable key"""
return self.get_data("key")

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

@property
def scope(self):
"""The repository variable scope"""
Expand All @@ -93,3 +139,8 @@ def type(self):
def value(self):
"""The repository variable value"""
return self.get_data("value")

@value.setter
def value(self, value):
"""Setter for the repository 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 @@ -381,7 +381,19 @@ Bitbucket Cloud
repository.repository_variables.each():
# Get a single repository variable from a repository by repository variable key
repository.repository_variables.get(repository_variable_key)
repository_variable = repository.repository_variables.get(repository_variable_key)
# Create a new repository variable with a name of 'KEY', value of 'VALUE' and is not secured.
new_repository_variable = repository.repository_variables.create("KEY", "VALUE", False)
# Update the 'key' field of repository_variable
updated_repository_variable = repository_variable.update(key="UPDATED_REPOSITORY_VARIABLE_KEY")
# Update the 'value' field of repository_variable
updated_repository_variable = repository_variable.update(value="UPDATED_REPOSITORY_VARIABLE_VALUE")
# Delete repository_variable
repository_variable.delete()
Pipelines management
--------------------
Expand Down

0 comments on commit f1b182c

Please sign in to comment.