Skip to content

Commit 5aa3701

Browse files
committed
feat: add support for groups_allowlist in job_token_scope
Signed-off-by: Tim Knight <tim.knight1@engineering.digital.dwp.gov.uk>
1 parent 6f4a2fb commit 5aa3701

File tree

4 files changed

+99
-7
lines changed

4 files changed

+99
-7
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: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ def test_add_project_to_job_token_scope_allowlist(gl, project):
1212

1313
def test_projects_job_token_scope_allowlist_contains_added_project_name(gl, project):
1414
scope = project.job_token_scope.get()
15-
assert len(scope.allowlist.list()) == 0
16-
1715
project_name = "Ci_Cd_token_named_proj"
1816
project_to_add = gl.projects.create({"name": project_name})
1917
scope.allowlist.create({"target_project_id": project_to_add.id})
@@ -26,18 +24,70 @@ def test_projects_job_token_scope_allowlist_contains_added_project_name(gl, proj
2624

2725
def test_remove_project_by_id_from_projects_job_token_scope_allowlist(gl, project):
2826
scope = project.job_token_scope.get()
29-
assert len(scope.allowlist.list()) == 0
3027

3128
project_to_add = gl.projects.create({"name": "Ci_Cd_token_remove_proj"})
3229

3330
scope.allowlist.create({"target_project_id": project_to_add.id})
3431

3532
scope.refresh()
36-
assert len(scope.allowlist.list()) != 0
3733

38-
scope.allowlist.remove(project_to_add.id)
34+
scope.allowlist.delete(project_to_add.id)
3935

4036
scope.refresh()
41-
assert len(scope.allowlist.list()) == 0
37+
assert not any(
38+
allowed.id == project_to_add.id for allowed in scope.allowlist.list()
39+
)
4240

4341
project_to_add.delete()
42+
43+
44+
def test_add_group_to_job_token_scope_allowlist(gl, project):
45+
group_to_add = gl.groups.create(
46+
{"name": "add_group", "path": "allowlisted-add-test"}
47+
)
48+
49+
scope = project.job_token_scope.get()
50+
resp = scope.groups_allowlist.create({"target_group_id": group_to_add.id})
51+
52+
assert resp.source_project_id == project.id
53+
assert resp.target_group_id == group_to_add.id
54+
55+
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 = "list_group"
63+
group_to_add = gl.groups.create(
64+
{"name": group_name, "path": "allowlisted-add-and-list-test"}
65+
)
66+
67+
scope.groups_allowlist.create({"target_group_id": group_to_add.id})
68+
69+
scope.refresh()
70+
assert any(allowed.name == group_name for allowed in scope.groups_allowlist.list())
71+
72+
group_to_add.delete()
73+
74+
75+
def test_remove_group_by_id_from_projects_job_token_scope_groups_allowlist(gl, project):
76+
scope = project.job_token_scope.get()
77+
78+
group_to_add = gl.groups.create(
79+
{"name": "delete_group", "path": "allowlisted-delete-test"}
80+
)
81+
82+
scope.groups_allowlist.create({"target_group_id": group_to_add.id})
83+
84+
scope.refresh()
85+
86+
scope.groups_allowlist.delete(group_to_add.id)
87+
88+
scope.refresh()
89+
assert not any(
90+
allowed.name == group_to_add.name for allowed in scope.groups_allowlist.list()
91+
)
92+
93+
group_to_add.delete()

tests/functional/fixtures/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
GITLAB_IMAGE=gitlab/gitlab-ee
2-
GITLAB_TAG=16.9.1-ee.0
2+
GITLAB_TAG=nightly

0 commit comments

Comments
 (0)