| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7073a2d commit 59d6a88
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -29,6 +29,7 @@ API examples | |||
| 29 | 29 | gl_objects/invitations | |
| 30 | 30 | gl_objects/issues | |
| 31 | 31 | gl_objects/iterations | |
| 32 | + gl_objects/job_token_scope | ||
| 32 | 33 | gl_objects/keys | |
| 33 | 34 | gl_objects/boards | |
| 34 | 35 | gl_objects/labels | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,51 @@ | |||
| 1 | + ##################### | ||
| 2 | + CI/CD job token scope | ||
| 3 | + ##################### | ||
| 4 | + | ||
| 5 | + Reference | ||
| 6 | + --------- | ||
| 7 | + | ||
| 8 | + * v4 API: | ||
| 9 | + | ||
| 10 | + + :class:`gitlab.v4.objects.ProjectJobTokenScope` | ||
| 11 | + + :class:`gitlab.v4.objects.ProjectJobTokenScopeManager` | ||
| 12 | + + :attr:`gitlab.v4.objects.Project.job_token_scope` | ||
| 13 | + | ||
| 14 | + * GitLab API: https://docs.gitlab.com/ee/api/project_job_token_scopes.html | ||
| 15 | + | ||
| 16 | + Examples | ||
| 17 | + -------- | ||
| 18 | + | ||
| 19 | + .. warning:: | ||
| 20 | + | ||
| 21 | + The GitLab API does **not** return any data when saving or updating | ||
| 22 | + the job token scope settings. You need to call ``refresh()`` (or ``get()`` | ||
| 23 | + a new object) as shown below to get the latest state. | ||
| 24 | + | ||
| 25 | + Get a project's CI/CD job token access settings:: | ||
| 26 | + | ||
| 27 | + scope = project.job_token_scope.get() | ||
| 28 | + print(scope.inbound_enabled) | ||
| 29 | + # True | ||
| 30 | + | ||
| 31 | + Update the job token scope settings:: | ||
| 32 | + | ||
| 33 | + scope.enabled = False | ||
| 34 | + scope.save() | ||
| 35 | + | ||
| 36 | + .. warning:: | ||
| 37 | + | ||
| 38 | + As you can see above, the attributes you receive from and send to the GitLab API | ||
| 39 | + are not consistent. GitLab returns `inbound_enabled` and `outbound_enabled`, | ||
| 40 | + but expects `enabled`, which only refers to the inbound scope. This is important | ||
| 41 | + when accessing and updating these attributes. | ||
| 42 | + | ||
| 43 | + Or update the job token scope settings directly:: | ||
| 44 | + | ||
| 45 | + project.job_token_scope.update(new_data={"enabled": True}) | ||
| 46 | + | ||
| 47 | + Refresh the current state of job token scope:: | ||
| 48 | + | ||
| 49 | + scope.refresh() | ||
| 50 | + print(scope.inbound_enabled) | ||
| 51 | + # False | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -309,6 +309,7 @@ def create( | |||
| 309 | 309 | class UpdateMethod(enum.IntEnum): | |
| 310 | 310 | PUT = 1 | |
| 311 | 311 | POST = 2 | |
| 312 | + PATCH = 3 | ||
| 312 | 313 | ||
| 313 | 314 | ||
| 314 | 315 | class UpdateMixin(_RestManagerBase): | |
@@ -331,6 +332,9 @@ def _get_update_method( | |||
| 331 | 332 | """ | |
| 332 | 333 | if self._update_method is UpdateMethod.POST: | |
| 333 | 334 | http_method = self.gitlab.http_post | |
| 335 | + elif self._update_method is UpdateMethod.PATCH: | ||
| 336 | + # only patch uses required kwargs, so our types are a bit misaligned | ||
| 337 | + http_method = self.gitlab.http_patch # type: ignore[assignment] | ||
| 334 | 338 | else: | |
| 335 | 339 | http_method = self.gitlab.http_put | |
| 336 | 340 | return http_method | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,7 @@ | |||
| 32 | 32 | from .invitations import * | |
| 33 | 33 | from .issues import * | |
| 34 | 34 | from .iterations import * | |
| 35 | + from .job_token_scope import * | ||
| 35 | 36 | from .jobs import * | |
| 36 | 37 | from .keys import * | |
| 37 | 38 | from .labels import * | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,29 @@ | |||
| 1 | + from typing import Any, cast | ||
| 2 | + | ||
| 3 | + from gitlab.base import RESTManager, RESTObject | ||
| 4 | + from gitlab.mixins import ( | ||
| 5 | + GetWithoutIdMixin, | ||
| 6 | + RefreshMixin, | ||
| 7 | + SaveMixin, | ||
| 8 | + UpdateMethod, | ||
| 9 | + UpdateMixin, | ||
| 10 | + ) | ||
| 11 | + | ||
| 12 | + __all__ = [ | ||
| 13 | + "ProjectJobTokenScope", | ||
| 14 | + "ProjectJobTokenScopeManager", | ||
| 15 | + ] | ||
| 16 | + | ||
| 17 | + | ||
| 18 | + class ProjectJobTokenScope(RefreshMixin, SaveMixin, RESTObject): | ||
| 19 | + _id_attr = None | ||
| 20 | + | ||
| 21 | + | ||
| 22 | + class ProjectJobTokenScopeManager(GetWithoutIdMixin, UpdateMixin, RESTManager): | ||
| 23 | + _path = "/projects/{project_id}/job_token_scope" | ||
| 24 | + _obj_cls = ProjectJobTokenScope | ||
| 25 | + _from_parent_attrs = {"project_id": "id"} | ||
| 26 | + _update_method = UpdateMethod.PATCH | ||
| 27 | + | ||
| 28 | + def get(self, **kwargs: Any) -> ProjectJobTokenScope: | ||
| 29 | + return cast(ProjectJobTokenScope, super().get(**kwargs)) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -59,6 +59,7 @@ | |||
| 59 | 59 | from .invitations import ProjectInvitationManager # noqa: F401 | |
| 60 | 60 | from .issues import ProjectIssueManager # noqa: F401 | |
| 61 | 61 | from .iterations import ProjectIterationManager # noqa: F401 | |
| 62 | + from .job_token_scope import ProjectJobTokenScopeManager # noqa: F401 | ||
| 62 | 63 | from .jobs import ProjectJobManager # noqa: F401 | |
| 63 | 64 | from .labels import ProjectLabelManager # noqa: F401 | |
| 64 | 65 | from .members import ProjectMemberAllManager, ProjectMemberManager # noqa: F401 | |
@@ -191,6 +192,7 @@ class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, RESTO | |||
| 191 | 192 | issues_statistics: ProjectIssuesStatisticsManager | |
| 192 | 193 | iterations: ProjectIterationManager | |
| 193 | 194 | jobs: ProjectJobManager | |
| 195 | + job_token_scope: ProjectJobTokenScopeManager | ||
| 194 | 196 | keys: ProjectKeyManager | |
| 195 | 197 | labels: ProjectLabelManager | |
| 196 | 198 | members: ProjectMemberManager | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,63 @@ | |||
| 1 | + """ | ||
| 2 | + GitLab API: https://docs.gitlab.com/ee/api/project_job_token_scopes.html | ||
| 3 | + """ | ||
| 4 | + | ||
| 5 | + import pytest | ||
| 6 | + import responses | ||
| 7 | + | ||
| 8 | + from gitlab.v4.objects import ProjectJobTokenScope | ||
| 9 | + | ||
| 10 | + job_token_scope_content = { | ||
| 11 | + "inbound_enabled": True, | ||
| 12 | + "outbound_enabled": False, | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + @pytest.fixture | ||
| 17 | + def resp_get_job_token_scope(): | ||
| 18 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: | ||
| 19 | + rsps.add( | ||
| 20 | + method=responses.GET, | ||
| 21 | + url="http://localhost/api/v4/projects/1/job_token_scope", | ||
| 22 | + json=job_token_scope_content, | ||
| 23 | + content_type="application/json", | ||
| 24 | + status=200, | ||
| 25 | + ) | ||
| 26 | + yield rsps | ||
| 27 | + | ||
| 28 | + | ||
| 29 | + @pytest.fixture | ||
| 30 | + def resp_patch_job_token_scope(): | ||
| 31 | + with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: | ||
| 32 | + rsps.add( | ||
| 33 | + method=responses.PATCH, | ||
| 34 | + url="http://localhost/api/v4/projects/1/job_token_scope", | ||
| 35 | + status=204, | ||
| 36 | + match=[responses.matchers.json_params_matcher({"enabled": False})], | ||
| 37 | + ) | ||
| 38 | + yield rsps | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + @pytest.fixture | ||
| 42 | + def job_token_scope(project, resp_get_job_token_scope): | ||
| 43 | + return project.job_token_scope.get() | ||
| 44 | + | ||
| 45 | + | ||
| 46 | + def test_get_job_token_scope(project, resp_get_job_token_scope): | ||
| 47 | + scope = project.job_token_scope.get() | ||
| 48 | + assert isinstance(scope, ProjectJobTokenScope) | ||
| 49 | + assert scope.inbound_enabled is True | ||
| 50 | + | ||
| 51 | + | ||
| 52 | + def test_refresh_job_token_scope(job_token_scope, resp_get_job_token_scope): | ||
| 53 | + job_token_scope.refresh() | ||
| 54 | + assert job_token_scope.inbound_enabled is True | ||
| 55 | + | ||
| 56 | + | ||
| 57 | + def test_save_job_token_scope(job_token_scope, resp_patch_job_token_scope): | ||
| 58 | + job_token_scope.enabled = False | ||
| 59 | + job_token_scope.save() | ||
| 60 | + | ||
| 61 | + | ||
| 62 | + def test_update_job_token_scope(project, resp_patch_job_token_scope): | ||
| 63 | + project.job_token_scope.update(new_data={"enabled": False}) | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments