Skip to content

Commit 28a689c

Browse files
committed
feat: Add support to list Protected Environments
- https://docs.gitlab.com/ee/api/protected_environments.html - #1130 no write operation are implemented yet as I have no use case right now and am not sure how it should be done
1 parent 8342f53 commit 28a689c

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

gitlab/v4/objects/environments.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
__all__ = [
1919
"ProjectEnvironment",
2020
"ProjectEnvironmentManager",
21+
"ProjectProtectedEnvironment",
22+
"ProjectProtectedEnvironmentManager",
2123
]
2224

2325

@@ -55,3 +57,26 @@ def get(
5557
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
5658
) -> ProjectEnvironment:
5759
return cast(ProjectEnvironment, super().get(id=id, lazy=lazy, **kwargs))
60+
61+
62+
class ProjectProtectedEnvironment(ObjectDeleteMixin, RESTObject):
63+
_id_attr = "name"
64+
_repr_attr = "name"
65+
66+
67+
class ProjectProtectedEnvironmentManager(
68+
RetrieveMixin, CreateMixin, UpdateMixin, DeleteMixin, RESTManager
69+
):
70+
_path = "/projects/{project_id}/protected_environments"
71+
_obj_cls = ProjectProtectedEnvironment
72+
_from_parent_attrs = {"project_id": "id"}
73+
_create_attrs = RequiredOptional(required=("name",), optional=("external_url",))
74+
_update_attrs = RequiredOptional(optional=("name", "external_url"))
75+
_list_filters = ("name", "search", "states")
76+
77+
def get(
78+
self, id: Union[str, int], lazy: bool = False, **kwargs: Any
79+
) -> ProjectProtectedEnvironment:
80+
return cast(
81+
ProjectProtectedEnvironment, super().get(id=id, lazy=lazy, **kwargs)
82+
)

gitlab/v4/objects/projects.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
from .deploy_keys import ProjectKeyManager # noqa: F401
3232
from .deploy_tokens import ProjectDeployTokenManager # noqa: F401
3333
from .deployments import ProjectDeploymentManager # noqa: F401
34-
from .environments import ProjectEnvironmentManager # noqa: F401
34+
from .environments import ( # noqa: F401
35+
ProjectEnvironmentManager,
36+
ProjectProtectedEnvironmentManager,
37+
)
3538
from .events import ProjectEventManager # noqa: F401
3639
from .export_import import ProjectExportManager, ProjectImportManager # noqa: F401
3740
from .files import ProjectFileManager # noqa: F401
@@ -175,6 +178,7 @@ class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, RESTO
175178
pagesdomains: ProjectPagesDomainManager
176179
pipelines: ProjectPipelineManager
177180
pipelineschedules: ProjectPipelineScheduleManager
181+
protected_environments: ProjectProtectedEnvironmentManager
178182
protectedbranches: ProjectProtectedBranchManager
179183
protectedtags: ProjectProtectedTagManager
180184
pushrules: ProjectPushRulesManager

tests/unit/objects/test_environments.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
import responses
66

7-
from gitlab.v4.objects import ProjectEnvironment
7+
from gitlab.v4.objects import ProjectEnvironment, ProjectProtectedEnvironment
88

99

1010
@pytest.fixture
@@ -22,9 +22,36 @@ def resp_get_environment():
2222
yield rsps
2323

2424

25+
@pytest.fixture
26+
def resp_get_protected_environment():
27+
content = {
28+
"name": "protected_environment_name",
29+
"id": 2,
30+
"last_deployment": "my birthday",
31+
}
32+
33+
with responses.RequestsMock() as rsps:
34+
rsps.add(
35+
method=responses.GET,
36+
url="http://localhost/api/v4/projects/1/protected_environments/2",
37+
json=content,
38+
content_type="application/json",
39+
status=200,
40+
)
41+
yield rsps
42+
43+
2544
def test_project_environments(project, resp_get_environment):
2645
environment = project.environments.get(1)
2746
assert isinstance(environment, ProjectEnvironment)
2847
assert environment.id == 1
2948
assert environment.last_deployment == "sometime"
3049
assert environment.name == "environment_name"
50+
51+
52+
def test_project_protected_environments(project, resp_get_protected_environment):
53+
protected_environment = project.protected_environments.get(2)
54+
assert isinstance(protected_environment, ProjectProtectedEnvironment)
55+
assert protected_environment.id == 2
56+
assert protected_environment.last_deployment == "my birthday"
57+
assert protected_environment.name == "protected_environment_name"

0 commit comments

Comments
 (0)