Skip to content

Feat: add support for instance variables API #1163

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
merged 5 commits into from
Sep 1, 2020
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
1 change: 1 addition & 0 deletions docs/api-objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ API examples
gl_objects/templates
gl_objects/todos
gl_objects/users
gl_objects/variables
gl_objects/sidekiq
gl_objects/wikis
52 changes: 0 additions & 52 deletions docs/gl_objects/pipelines_and_jobs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,58 +184,6 @@ Delete a schedule variable::

var.delete()

Projects and groups variables
=============================

You can associate variables to projects and groups to modify the build/job
scripts behavior.

Reference
---------

* v4 API

+ :class:`gitlab.v4.objects.ProjectVariable`
+ :class:`gitlab.v4.objects.ProjectVariableManager`
+ :attr:`gitlab.v4.objects.Project.variables`
+ :class:`gitlab.v4.objects.GroupVariable`
+ :class:`gitlab.v4.objects.GroupVariableManager`
+ :attr:`gitlab.v4.objects.Group.variables`

* GitLab API

+ https://docs.gitlab.com/ce/api/project_level_variables.html
+ https://docs.gitlab.com/ce/api/group_level_variables.html

Examples
--------

List variables::

p_variables = project.variables.list()
g_variables = group.variables.list()

Get a variable::

p_var = project.variables.get('key_name')
g_var = group.variables.get('key_name')

Create a variable::

var = project.variables.create({'key': 'key1', 'value': 'value1'})
var = group.variables.create({'key': 'key1', 'value': 'value1'})

Update a variable value::

var.value = 'new_value'
var.save()

Remove a variable::

project.variables.delete('key_name')
group.variables.delete('key_name')
# or
var.delete()

Jobs
====
Expand Down
102 changes: 102 additions & 0 deletions docs/gl_objects/variables.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
###############
CI/CD Variables
###############

You can configure variables at the instance-level (admin only), or associate
variables to projects and groups, to modify pipeline/job scripts behavior.


Instance-level variables
========================

This endpoint requires admin access.

Reference
---------

* v4 API

+ :class:`gitlab.v4.objects.Variable`
+ :class:`gitlab.v4.objects.VariableManager`
+ :attr:`gitlab.Gitlab.variables`

* GitLab API

+ https://docs.gitlab.com/ce/api/instance_level_ci_variables.html

Examples
--------

List all instance variables::

variables = gl.variables.list()

Get an instance variable by key::

variable = gl.variables.get('key_name')

Create an instance variable::

variable = gl.variables.create({'key': 'key1', 'value': 'value1'})

Update a variable value::

variable.value = 'new_value'
variable.save()

Remove a variable::

gl.variables.delete('key_name')
# or
variable.delete()

Projects and groups variables
=============================

Reference
---------

* v4 API

+ :class:`gitlab.v4.objects.ProjectVariable`
+ :class:`gitlab.v4.objects.ProjectVariableManager`
+ :attr:`gitlab.v4.objects.Project.variables`
+ :class:`gitlab.v4.objects.GroupVariable`
+ :class:`gitlab.v4.objects.GroupVariableManager`
+ :attr:`gitlab.v4.objects.Group.variables`

* GitLab API

+ https://docs.gitlab.com/ce/api/instance_level_ci_variables.html
+ https://docs.gitlab.com/ce/api/project_level_variables.html
+ https://docs.gitlab.com/ce/api/group_level_variables.html

Examples
--------

List variables::

p_variables = project.variables.list()
g_variables = group.variables.list()

Get a variable::

p_var = project.variables.get('key_name')
g_var = group.variables.get('key_name')

Create a variable::

var = project.variables.create({'key': 'key1', 'value': 'value1'})
var = group.variables.create({'key': 'key1', 'value': 'value1'})

Update a variable value::

var.value = 'new_value'
var.save()

Remove a variable::

project.variables.delete('key_name')
group.variables.delete('key_name')
# or
var.delete()
1 change: 1 addition & 0 deletions gitlab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def __init__(
self.pagesdomains = objects.PagesDomainManager(self)
self.user_activities = objects.UserActivitiesManager(self)
self.applications = objects.ApplicationManager(self)
self.variables = objects.VariableManager(self)

def __enter__(self):
return self
Expand Down
193 changes: 193 additions & 0 deletions gitlab/tests/objects/test_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
"""
GitLab API:
https://docs.gitlab.com/ee/api/instance_level_ci_variables.html
https://docs.gitlab.com/ee/api/project_level_variables.html
https://docs.gitlab.com/ee/api/group_level_variables.html
"""

import re

import pytest
import responses

from gitlab.v4.objects import GroupVariable, ProjectVariable, Variable


key = "TEST_VARIABLE_1"
value = "TEST_1"
new_value = "TEST_2"

variable_content = {
"key": key,
"variable_type": "env_var",
"value": value,
"protected": False,
"masked": True,
}
variables_url = re.compile(
r"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/variables"
)
variables_key_url = re.compile(
rf"http://localhost/api/v4/(((groups|projects)/1)|(admin/ci))/variables/{key}"
)


@pytest.fixture
def resp_list_variables():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url=variables_url,
json=[variable_content],
content_type="application/json",
status=200,
)
yield rsps


@pytest.fixture
def resp_get_variable():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.GET,
url=variables_key_url,
json=variable_content,
content_type="application/json",
status=200,
)
yield rsps


@pytest.fixture
def resp_create_variable():
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.POST,
url=variables_url,
json=variable_content,
content_type="application/json",
status=200,
)
yield rsps


@pytest.fixture
def resp_update_variable():
updated_content = dict(variable_content)
updated_content["value"] = new_value

with responses.RequestsMock() as rsps:
rsps.add(
method=responses.PUT,
url=variables_key_url,
json=updated_content,
content_type="application/json",
status=200,
)
yield rsps


@pytest.fixture
def resp_delete_variable(no_content):
with responses.RequestsMock() as rsps:
rsps.add(
method=responses.DELETE,
url=variables_key_url,
json=no_content,
content_type="application/json",
status=204,
)
yield rsps


def test_list_instance_variables(gl, resp_list_variables):
variables = gl.variables.list()
assert isinstance(variables, list)
assert isinstance(variables[0], Variable)
assert variables[0].value == value


def test_get_instance_variable(gl, resp_get_variable):
variable = gl.variables.get(key)
assert isinstance(variable, Variable)
assert variable.value == value


def test_create_instance_variable(gl, resp_create_variable):
variable = gl.variables.create({"key": key, "value": value})
assert isinstance(variable, Variable)
assert variable.value == value


def test_update_instance_variable(gl, resp_update_variable):
variable = gl.variables.get(key, lazy=True)
variable.value = new_value
variable.save()
assert variable.value == new_value


def test_delete_instance_variable(gl, resp_delete_variable):
variable = gl.variables.get(key, lazy=True)
variable.delete()


def test_list_project_variables(project, resp_list_variables):
variables = project.variables.list()
assert isinstance(variables, list)
assert isinstance(variables[0], ProjectVariable)
assert variables[0].value == value


def test_get_project_variable(project, resp_get_variable):
variable = project.variables.get(key)
assert isinstance(variable, ProjectVariable)
assert variable.value == value


def test_create_project_variable(project, resp_create_variable):
variable = project.variables.create({"key": key, "value": value})
assert isinstance(variable, ProjectVariable)
assert variable.value == value


def test_update_project_variable(project, resp_update_variable):
variable = project.variables.get(key, lazy=True)
variable.value = new_value
variable.save()
assert variable.value == new_value


def test_delete_project_variable(project, resp_delete_variable):
variable = project.variables.get(key, lazy=True)
variable.delete()


def test_list_group_variables(group, resp_list_variables):
variables = group.variables.list()
assert isinstance(variables, list)
assert isinstance(variables[0], GroupVariable)
assert variables[0].value == value


def test_get_group_variable(group, resp_get_variable):
variable = group.variables.get(key)
assert isinstance(variable, GroupVariable)
assert variable.value == value


def test_create_group_variable(group, resp_create_variable):
variable = group.variables.create({"key": key, "value": value})
assert isinstance(variable, GroupVariable)
assert variable.value == value


def test_update_group_variable(group, resp_update_variable):
variable = group.variables.get(key, lazy=True)
variable.value = new_value
variable.save()
assert variable.value == new_value


def test_delete_group_variable(group, resp_delete_variable):
variable = group.variables.get(key, lazy=True)
variable.delete()
Loading