Skip to content

Bitbucket Cloud: Add Create, Update and Deleting of repository variables #1186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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