Skip to content

Commit 9f36444

Browse files
committed
feat: add support got groups_allowlist in job_token_scope
Signed-off-by: Tim Knight <tim.knight1@engineering.digital.dwp.gov.uk>
1 parent e602f40 commit 9f36444

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

docs/gl_objects/job_token_scope.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,24 @@ Remove a project from the project's inbound allowlist::
6969
Similar to above, the ID attributes you receive from the create and list
7070
APIs are not consistent. To safely retrieve the ID of the allowlisted project
7171
regardless of how the object was created, always use its ``.get_id()`` method.
72+
73+
Get a project's CI/CD job token inbound groups allowlist::
74+
75+
allowlist = scope.groups_allowlist.list()
76+
77+
Add a project to the project's inbound groups allowlist::
78+
79+
allowed_project = scope.groups_allowlist.create({"target_project_id": 42})
80+
81+
Remove a project from the project's inbound agroups llowlist::
82+
83+
allowed_project.delete()
84+
# or directly using a Group ID
85+
scope.groups_allowlist.delete(42)
86+
87+
.. warning::
88+
89+
Similar to above, the ID attributes you receive from the create and list
90+
APIs are not consistent. To safely retrieve the ID of the allowlisted group
91+
regardless of how the object was created, always use its ``.get_id()`` method.
92+

gitlab/v4/objects/job_token_scope.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ProjectJobTokenScope(RefreshMixin, SaveMixin, RESTObject):
2424
_id_attr = None
2525

2626
allowlist: "AllowlistedProjectManager"
27+
groups_allowlist: "AllowlistedGroupManager"
2728

2829

2930
class ProjectJobTokenScopeManager(GetWithoutIdMixin, UpdateMixin, RESTManager):
@@ -54,3 +55,23 @@ class AllowlistedProjectManager(ListMixin, CreateMixin, DeleteMixin, RESTManager
5455
_obj_cls = AllowlistedProject
5556
_from_parent_attrs = {"project_id": "project_id"}
5657
_create_attrs = RequiredOptional(required=("target_project_id",))
58+
59+
60+
class AllowlistedGroup(ObjectDeleteMixin, RESTObject):
61+
_id_attr = "target_group_id" # note: only true for create endpoint
62+
63+
def get_id(self) -> int:
64+
"""Returns the id of the resource. This override deals with
65+
the fact that either an `id` or a `target_project_id` attribute
66+
is returned by the server depending on the endpoint called."""
67+
try:
68+
return cast(int, getattr(self, self._id_attr))
69+
except AttributeError:
70+
return cast(int, getattr(self, "id"))
71+
72+
73+
class AllowlistedGroupManager(ListMixin, CreateMixin, DeleteMixin, RESTManager):
74+
_path = "/projects/{project_id}/job_token_scope/groups_allowlist"
75+
_obj_cls = AllowlistedProject
76+
_from_parent_attrs = {"project_id": "project_id"}
77+
_create_attrs = RequiredOptional(required=("target_group_id",))

tests/functional/api/test_project_job_token_scope.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import pytest
2-
3-
41
def test_add_project_to_job_token_scope_allowlist(gl, project):
52
project_to_add = gl.projects.create({"name": "Ci_Cd_token_add_proj"})
63

@@ -44,16 +41,30 @@ def test_remove_project_by_id_from_projects_job_token_scope_allowlist(gl, projec
4441
project_to_add.delete()
4542

4643

47-
@pytest.mark.xfail(reason="Group allowlist not yet implemented")
4844
def test_add_group_to_job_token_scope_allowlist(gl, project):
4945
group_to_add = gl.groups.create(
5046
{"name": "Ci_Cd_token_add_proj", "path": "allowlisted"}
5147
)
5248

5349
scope = project.job_token_scope.get()
54-
resp = scope.allowlist.create({"target_project_id": group_to_add.id})
50+
resp = scope.groups_allowlist.create({"target_project_id": group_to_add.id})
5551

5652
assert resp.id == group_to_add.id
5753
assert resp.name == group_to_add.name
5854

5955
group_to_add.delete()
56+
57+
58+
def test_projects_job_token_scope_groups_allowlist_contains_added_group_name(
59+
gl, project
60+
):
61+
scope = project.job_token_scope.get()
62+
group_name = "Ci_Cd_token_named_group"
63+
group_to_add = gl.groups.create({"name": group_name, "path": "allowlisted"})
64+
65+
scope.groups_allowlist.create({"target_group_id": group_to_add.id})
66+
67+
scope.refresh()
68+
assert any(allowed.name == group_name for allowed in scope.groups_allowlist.list())
69+
70+
group_to_add.delete()

0 commit comments

Comments
 (0)