Skip to content

Commit 723ca88

Browse files
authored
Merge pull request #1163 from python-gitlab/feat/instance-variables-api
Feat: add support for instance variables API
2 parents 6e1ed68 + 5a56b6b commit 723ca88

File tree

13 files changed

+443
-117
lines changed

13 files changed

+443
-117
lines changed

docs/api-objects.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ API examples
4848
gl_objects/templates
4949
gl_objects/todos
5050
gl_objects/users
51+
gl_objects/variables
5152
gl_objects/sidekiq
5253
gl_objects/wikis

docs/gl_objects/pipelines_and_jobs.rst

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -184,58 +184,6 @@ Delete a schedule variable::
184184

185185
var.delete()
186186

187-
Projects and groups variables
188-
=============================
189-
190-
You can associate variables to projects and groups to modify the build/job
191-
scripts behavior.
192-
193-
Reference
194-
---------
195-
196-
* v4 API
197-
198-
+ :class:`gitlab.v4.objects.ProjectVariable`
199-
+ :class:`gitlab.v4.objects.ProjectVariableManager`
200-
+ :attr:`gitlab.v4.objects.Project.variables`
201-
+ :class:`gitlab.v4.objects.GroupVariable`
202-
+ :class:`gitlab.v4.objects.GroupVariableManager`
203-
+ :attr:`gitlab.v4.objects.Group.variables`
204-
205-
* GitLab API
206-
207-
+ https://docs.gitlab.com/ce/api/project_level_variables.html
208-
+ https://docs.gitlab.com/ce/api/group_level_variables.html
209-
210-
Examples
211-
--------
212-
213-
List variables::
214-
215-
p_variables = project.variables.list()
216-
g_variables = group.variables.list()
217-
218-
Get a variable::
219-
220-
p_var = project.variables.get('key_name')
221-
g_var = group.variables.get('key_name')
222-
223-
Create a variable::
224-
225-
var = project.variables.create({'key': 'key1', 'value': 'value1'})
226-
var = group.variables.create({'key': 'key1', 'value': 'value1'})
227-
228-
Update a variable value::
229-
230-
var.value = 'new_value'
231-
var.save()
232-
233-
Remove a variable::
234-
235-
project.variables.delete('key_name')
236-
group.variables.delete('key_name')
237-
# or
238-
var.delete()
239187

240188
Jobs
241189
====

docs/gl_objects/variables.rst

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
###############
2+
CI/CD Variables
3+
###############
4+
5+
You can configure variables at the instance-level (admin only), or associate
6+
variables to projects and groups, to modify pipeline/job scripts behavior.
7+
8+
9+
Instance-level variables
10+
========================
11+
12+
This endpoint requires admin access.
13+
14+
Reference
15+
---------
16+
17+
* v4 API
18+
19+
+ :class:`gitlab.v4.objects.Variable`
20+
+ :class:`gitlab.v4.objects.VariableManager`
21+
+ :attr:`gitlab.Gitlab.variables`
22+
23+
* GitLab API
24+
25+
+ https://docs.gitlab.com/ce/api/instance_level_ci_variables.html
26+
27+
Examples
28+
--------
29+
30+
List all instance variables::
31+
32+
variables = gl.variables.list()
33+
34+
Get an instance variable by key::
35+
36+
variable = gl.variables.get('key_name')
37+
38+
Create an instance variable::
39+
40+
variable = gl.variables.create({'key': 'key1', 'value': 'value1'})
41+
42+
Update a variable value::
43+
44+
variable.value = 'new_value'
45+
variable.save()
46+
47+
Remove a variable::
48+
49+
gl.variables.delete('key_name')
50+
# or
51+
variable.delete()
52+
53+
Projects and groups variables
54+
=============================
55+
56+
Reference
57+
---------
58+
59+
* v4 API
60+
61+
+ :class:`gitlab.v4.objects.ProjectVariable`
62+
+ :class:`gitlab.v4.objects.ProjectVariableManager`
63+
+ :attr:`gitlab.v4.objects.Project.variables`
64+
+ :class:`gitlab.v4.objects.GroupVariable`
65+
+ :class:`gitlab.v4.objects.GroupVariableManager`
66+
+ :attr:`gitlab.v4.objects.Group.variables`
67+
68+
* GitLab API
69+
70+
+ https://docs.gitlab.com/ce/api/instance_level_ci_variables.html
71+
+ https://docs.gitlab.com/ce/api/project_level_variables.html
72+
+ https://docs.gitlab.com/ce/api/group_level_variables.html
73+
74+
Examples
75+
--------
76+
77+
List variables::
78+
79+
p_variables = project.variables.list()
80+
g_variables = group.variables.list()
81+
82+
Get a variable::
83+
84+
p_var = project.variables.get('key_name')
85+
g_var = group.variables.get('key_name')
86+
87+
Create a variable::
88+
89+
var = project.variables.create({'key': 'key1', 'value': 'value1'})
90+
var = group.variables.create({'key': 'key1', 'value': 'value1'})
91+
92+
Update a variable value::
93+
94+
var.value = 'new_value'
95+
var.save()
96+
97+
Remove a variable::
98+
99+
project.variables.delete('key_name')
100+
group.variables.delete('key_name')
101+
# or
102+
var.delete()

gitlab/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def __init__(
139139
self.pagesdomains = objects.PagesDomainManager(self)
140140
self.user_activities = objects.UserActivitiesManager(self)
141141
self.applications = objects.ApplicationManager(self)
142+
self.variables = objects.VariableManager(self)
142143

143144
def __enter__(self):
144145
return self
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
"""
2+
GitLab API:
3+
https://docs.gitlab.com/ee/api/instance_level_ci_variables.html
4+
https://docs.gitlab.com/ee/api/project_level_variables.html
5+
https://docs.gitlab.com/ee/api/group_level_variables.html
6+
"""
7+
8+
import re
9+
10+
import pytest
11+
import responses
12+
13+
from gitlab.v4.objects import GroupVariable, ProjectVariable, Variable
14+
15+
16+
key = "TEST_VARIABLE_1"
17+
value = "TEST_1"
18+
new_value = "TEST_2"
19+
20+
variable_content = {
21+
"key": key,
22+
"variable_type": "env_var",
23+
"value": value,
24+
"protected": False,
25+
"masked": True,
26+
}
27+
variables_url = re.compile(
28+
r"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/variables"
29+
)
30+
variables_key_url = re.compile(
31+
rf"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/variables/{key}"
32+
)
33+
34+
35+
@pytest.fixture
36+
def resp_list_variables():
37+
with responses.RequestsMock() as rsps:
38+
rsps.add(
39+
method=responses.GET,
40+
url=variables_url,
41+
json=[variable_content],
42+
content_type="application/json",
43+
status=200,
44+
)
45+
yield rsps
46+
47+
48+
@pytest.fixture
49+
def resp_get_variable():
50+
with responses.RequestsMock() as rsps:
51+
rsps.add(
52+
method=responses.GET,
53+
url=variables_key_url,
54+
json=variable_content,
55+
content_type="application/json",
56+
status=200,
57+
)
58+
yield rsps
59+
60+
61+
@pytest.fixture
62+
def resp_create_variable():
63+
with responses.RequestsMock() as rsps:
64+
rsps.add(
65+
method=responses.POST,
66+
url=variables_url,
67+
json=variable_content,
68+
content_type="application/json",
69+
status=200,
70+
)
71+
yield rsps
72+
73+
74+
@pytest.fixture
75+
def resp_update_variable():
76+
updated_content = dict(variable_content)
77+
updated_content["value"] = new_value
78+
79+
with responses.RequestsMock() as rsps:
80+
rsps.add(
81+
method=responses.PUT,
82+
url=variables_key_url,
83+
json=updated_content,
84+
content_type="application/json",
85+
status=200,
86+
)
87+
yield rsps
88+
89+
90+
@pytest.fixture
91+
def resp_delete_variable(no_content):
92+
with responses.RequestsMock() as rsps:
93+
rsps.add(
94+
method=responses.DELETE,
95+
url=variables_key_url,
96+
json=no_content,
97+
content_type="application/json",
98+
status=204,
99+
)
100+
yield rsps
101+
102+
103+
def test_list_instance_variables(gl, resp_list_variables):
104+
variables = gl.variables.list()
105+
assert isinstance(variables, list)
106+
assert isinstance(variables[0], Variable)
107+
assert variables[0].value == value
108+
109+
110+
def test_get_instance_variable(gl, resp_get_variable):
111+
variable = gl.variables.get(key)
112+
assert isinstance(variable, Variable)
113+
assert variable.value == value
114+
115+
116+
def test_create_instance_variable(gl, resp_create_variable):
117+
variable = gl.variables.create({"key": key, "value": value})
118+
assert isinstance(variable, Variable)
119+
assert variable.value == value
120+
121+
122+
def test_update_instance_variable(gl, resp_update_variable):
123+
variable = gl.variables.get(key, lazy=True)
124+
variable.value = new_value
125+
variable.save()
126+
assert variable.value == new_value
127+
128+
129+
def test_delete_instance_variable(gl, resp_delete_variable):
130+
variable = gl.variables.get(key, lazy=True)
131+
variable.delete()
132+
133+
134+
def test_list_project_variables(project, resp_list_variables):
135+
variables = project.variables.list()
136+
assert isinstance(variables, list)
137+
assert isinstance(variables[0], ProjectVariable)
138+
assert variables[0].value == value
139+
140+
141+
def test_get_project_variable(project, resp_get_variable):
142+
variable = project.variables.get(key)
143+
assert isinstance(variable, ProjectVariable)
144+
assert variable.value == value
145+
146+
147+
def test_create_project_variable(project, resp_create_variable):
148+
variable = project.variables.create({"key": key, "value": value})
149+
assert isinstance(variable, ProjectVariable)
150+
assert variable.value == value
151+
152+
153+
def test_update_project_variable(project, resp_update_variable):
154+
variable = project.variables.get(key, lazy=True)
155+
variable.value = new_value
156+
variable.save()
157+
assert variable.value == new_value
158+
159+
160+
def test_delete_project_variable(project, resp_delete_variable):
161+
variable = project.variables.get(key, lazy=True)
162+
variable.delete()
163+
164+
165+
def test_list_group_variables(group, resp_list_variables):
166+
variables = group.variables.list()
167+
assert isinstance(variables, list)
168+
assert isinstance(variables[0], GroupVariable)
169+
assert variables[0].value == value
170+
171+
172+
def test_get_group_variable(group, resp_get_variable):
173+
variable = group.variables.get(key)
174+
assert isinstance(variable, GroupVariable)
175+
assert variable.value == value
176+
177+
178+
def test_create_group_variable(group, resp_create_variable):
179+
variable = group.variables.create({"key": key, "value": value})
180+
assert isinstance(variable, GroupVariable)
181+
assert variable.value == value
182+
183+
184+
def test_update_group_variable(group, resp_update_variable):
185+
variable = group.variables.get(key, lazy=True)
186+
variable.value = new_value
187+
variable.save()
188+
assert variable.value == new_value
189+
190+
191+
def test_delete_group_variable(group, resp_delete_variable):
192+
variable = group.variables.get(key, lazy=True)
193+
variable.delete()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy