Skip to content

Commit f1b182c

Browse files
authored
Bitbucket Cloud: Add Create, Update and Deleting of repository variables (#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.
1 parent 3422a81 commit f1b182c

File tree

2 files changed

+66
-3
lines changed

2 files changed

+66
-3
lines changed

atlassian/bitbucket/cloud/repositories/repositoryVariables.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ def __get_object(self, data):
1414
**self._new_session_args,
1515
)
1616

17+
def create(self, key, value, secured):
18+
"""
19+
Create a new repository variable for the given repository.
20+
21+
:param key: string: The unique name of the variable.
22+
:param value: string: The value of the variable. If the variable is secured, this will be empty.
23+
: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.
24+
25+
:return: The created RepositoryVariable object
26+
27+
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-post
28+
"""
29+
data = {
30+
"key": key,
31+
"value": value,
32+
"secured": secured,
33+
}
34+
return self.__get_object(self.post(None, data=data))
35+
1736
def each(self, q=None, sort=None):
1837
"""
1938
Returns the list of repository variables in this repository.
@@ -25,7 +44,7 @@ def each(self, q=None, sort=None):
2544
2645
:return: A generator for the RepositoryVariable objects
2746
28-
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pipelines_config/variables/#get
47+
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-get
2948
"""
3049
params = {}
3150
if sort is not None:
@@ -50,7 +69,7 @@ def get(self, uuid):
5069
5170
:return: The requested RepositoryVariable objects
5271
53-
API docs: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/pipelines_config/variables/%7Bvariable_uuid%7D#get
72+
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-variable-uuid-get
5473
"""
5574
return self.__get_object(super(RepositoryVariables, self).get(uuid))
5675

@@ -59,6 +78,28 @@ class RepositoryVariable(BitbucketCloudBase):
5978
def __init__(self, url, data, *args, **kwargs):
6079
super(RepositoryVariable, self).__init__(url, *args, data=data, expected_type="pipeline_variable", **kwargs)
6180

81+
def update(self, **kwargs):
82+
"""
83+
Update the repository variable properties. Fields not present in the request body are ignored.
84+
85+
:param kwargs: dict: The data to update.
86+
87+
:return: The updated repository variable
88+
89+
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-variable-uuid-put
90+
"""
91+
return self._update_data(self.put(None, data=kwargs))
92+
93+
def delete(self):
94+
"""
95+
Delete the repository variable.
96+
97+
:return: The response on success
98+
99+
API docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pipelines/#api-repositories-workspace-repo-slug-pipelines-config-variables-variable-uuid-delete
100+
"""
101+
return super(RepositoryVariable, self).delete(None)
102+
62103
@property
63104
def uuid(self):
64105
"""The repository variable uuid"""
@@ -69,6 +110,11 @@ def key(self):
69110
"""The repository variable key"""
70111
return self.get_data("key")
71112

113+
@key.setter
114+
def key(self, key):
115+
"""Setter for the repository variable is key"""
116+
return self.update(key=key)
117+
72118
@property
73119
def scope(self):
74120
"""The repository variable scope"""
@@ -93,3 +139,8 @@ def type(self):
93139
def value(self):
94140
"""The repository variable value"""
95141
return self.get_data("value")
142+
143+
@value.setter
144+
def value(self, value):
145+
"""Setter for the repository variable value"""
146+
return self.update(value=value)

docs/bitbucket.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,19 @@ Bitbucket Cloud
381381
repository.repository_variables.each():
382382
383383
# Get a single repository variable from a repository by repository variable key
384-
repository.repository_variables.get(repository_variable_key)
384+
repository_variable = repository.repository_variables.get(repository_variable_key)
385+
386+
# Create a new repository variable with a name of 'KEY', value of 'VALUE' and is not secured.
387+
new_repository_variable = repository.repository_variables.create("KEY", "VALUE", False)
388+
389+
# Update the 'key' field of repository_variable
390+
updated_repository_variable = repository_variable.update(key="UPDATED_REPOSITORY_VARIABLE_KEY")
391+
392+
# Update the 'value' field of repository_variable
393+
updated_repository_variable = repository_variable.update(value="UPDATED_REPOSITORY_VARIABLE_VALUE")
394+
395+
# Delete repository_variable
396+
repository_variable.delete()
385397
386398
Pipelines management
387399
--------------------

0 commit comments

Comments
 (0)