GeezerVison is showing github.com — feat(api): add support for resource groups · mkarthiatgithub/python-gitlab@5f8b8f5
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: docs/api-objects.rst +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -51,6 +51,7 @@ API examples | 51 | 51 | gl_objects/remote_mirrors | 52 | 52 | gl_objects/repositories | 53 | 53 | gl_objects/repository_tags | | 54 | + gl_objects/resource_groups | 54 | 55 | gl_objects/search | 55 | 56 | gl_objects/secure_files | 56 | 57 | gl_objects/settings | |
Collapse file Copy file name to clipboard +38Lines changed: 38 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +############### | | 2 | +Resource Groups | | 3 | +############### | | 4 | + | | 5 | +Reference | | 6 | +--------- | | 7 | + | | 8 | +* v4 API: | | 9 | + | | 10 | + + :class:`gitlab.v4.objects.ProjectResourceGroup` | | 11 | + + :class:`gitlab.v4.objects.ProjectResourceGroupManager` | | 12 | + + :attr:`gitlab.v4.objects.Project.resource_groups` | | 13 | + + :class:`gitlab.v4.objects.ProjectResourceGroupUpcomingJob` | | 14 | + + :class:`gitlab.v4.objects.ProjectResourceGroupUpcomingJobManager` | | 15 | + + :attr:`gitlab.v4.objects.ProjectResourceGroup.upcoming_jobs` | | 16 | + | | 17 | +* Gitlab API: https://docs.gitlab.com/ee/api/resource_groups.html | | 18 | + | | 19 | +Examples | | 20 | +-------- | | 21 | + | | 22 | +List resource groups for a project:: | | 23 | + | | 24 | + project = gl.projects.get(project_id, lazy=True) | | 25 | + resource_group = project.resource_groups.list() | | 26 | + | | 27 | +Get a single resource group:: | | 28 | + | | 29 | + resource_group = project.resource_groups.get("production") | | 30 | + | | 31 | +Edit a resource group:: | | 32 | + | | 33 | + resource_group.process_mode = "oldest_first" | | 34 | + resource_group.save() | | 35 | + | | 36 | +List upcoming jobs for a resource group:: | | 37 | + | | 38 | + upcoming_jobs = resource_group.upcoming_jobs.list() |
Collapse file Copy file name to clipboardExpand all lines: gitlab/v4/objects/__init__.py +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 52 | 52 | from .push_rules import * | 53 | 53 | from .releases import * | 54 | 54 | from .repositories import * | | 55 | +from .resource_groups import * | 55 | 56 | from .runners import * | 56 | 57 | from .secure_files import * | 57 | 58 | from .settings import * | |
Collapse file Copy file name to clipboardExpand all lines: gitlab/v4/objects/projects.py +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 81 | 81 | from .push_rules import ProjectPushRulesManager # noqa: F401 | 82 | 82 | from .releases import ProjectReleaseManager # noqa: F401 | 83 | 83 | from .repositories import RepositoryMixin | | 84 | +from .resource_groups import ProjectResourceGroupManager | 84 | 85 | from .runners import ProjectRunnerManager # noqa: F401 | 85 | 86 | from .secure_files import ProjectSecureFileManager # noqa: F401 | 86 | 87 | from .snippets import ProjectSnippetManager # noqa: F401 | @@ -207,6 +208,7 @@ class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, RESTO | 207 | 208 | protectedtags: ProjectProtectedTagManager | 208 | 209 | pushrules: ProjectPushRulesManager | 209 | 210 | releases: ProjectReleaseManager | | 211 | + resource_groups: ProjectResourceGroupManager | 210 | 212 | remote_mirrors: "ProjectRemoteMirrorManager" | 211 | 213 | repositories: ProjectRegistryRepositoryManager | 212 | 214 | runners: ProjectRunnerManager | |
Collapse file Copy file name to clipboard +45Lines changed: 45 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +from typing import Any, cast, Union | | 2 | + | | 3 | +from gitlab.base import RESTManager, RESTObject | | 4 | +from gitlab.mixins import ListMixin, RetrieveMixin, SaveMixin, UpdateMixin | | 5 | +from gitlab.types import RequiredOptional | | 6 | + | | 7 | +__all__ = [ | | 8 | + "ProjectResourceGroup", | | 9 | + "ProjectResourceGroupManager", | | 10 | + "ProjectResourceGroupUpcomingJob", | | 11 | + "ProjectResourceGroupUpcomingJobManager", | | 12 | +] | | 13 | + | | 14 | + | | 15 | +class ProjectResourceGroup(SaveMixin, RESTObject): | | 16 | + _id_attr = "key" | | 17 | + | | 18 | + upcoming_jobs: "ProjectResourceGroupUpcomingJobManager" | | 19 | + | | 20 | + | | 21 | +class ProjectResourceGroupManager(RetrieveMixin, UpdateMixin, RESTManager): | | 22 | + _path = "/projects/{project_id}/resource_groups" | | 23 | + _obj_cls = ProjectResourceGroup | | 24 | + _from_parent_attrs = {"project_id": "id"} | | 25 | + _list_filters = ( | | 26 | + "order_by", | | 27 | + "sort", | | 28 | + "include_html_description", | | 29 | + ) | | 30 | + _update_attrs = RequiredOptional(optional=("process_mode",)) | | 31 | + | | 32 | + def get( | | 33 | + self, id: Union[str, int], lazy: bool = False, **kwargs: Any | | 34 | + ) -> ProjectResourceGroup: | | 35 | + return cast(ProjectResourceGroup, super().get(id=id, lazy=lazy, **kwargs)) | | 36 | + | | 37 | + | | 38 | +class ProjectResourceGroupUpcomingJob(RESTObject): | | 39 | + pass | | 40 | + | | 41 | + | | 42 | +class ProjectResourceGroupUpcomingJobManager(ListMixin, RESTManager): | | 43 | + _path = "/projects/{project_id}/resource_groups/{resource_group_key}/upcoming_jobs" | | 44 | + _obj_cls = ProjectResourceGroupUpcomingJob | | 45 | + _from_parent_attrs = {"project_id": "project_id", "resource_group_key": "key"} |
Collapse file Copy file name to clipboard +79Lines changed: 79 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +""" | | 2 | +GitLab API: | | 3 | +https://docs.gitlab.com/ee/api/resource_groups.html | | 4 | +""" | | 5 | +import pytest | | 6 | +import responses | | 7 | + | | 8 | +from gitlab.v4.objects import ProjectResourceGroup, ProjectResourceGroupUpcomingJob | | 9 | + | | 10 | +from .test_jobs import job_content | | 11 | + | | 12 | +resource_group_content = { | | 13 | + "id": 3, | | 14 | + "key": "production", | | 15 | + "process_mode": "unordered", | | 16 | + "created_at": "2021-09-01T08:04:59.650Z", | | 17 | + "updated_at": "2021-09-01T08:04:59.650Z", | | 18 | +} | | 19 | + | | 20 | + | | 21 | +@pytest.fixture | | 22 | +def resp_list_resource_groups(): | | 23 | + with responses.RequestsMock() as rsps: | | 24 | + rsps.add( | | 25 | + method=responses.GET, | | 26 | + url="http://localhost/api/v4/projects/1/resource_groups", | | 27 | + json=[resource_group_content], | | 28 | + content_type="application/json", | | 29 | + status=200, | | 30 | + ) | | 31 | + yield rsps | | 32 | + | | 33 | + | | 34 | +@pytest.fixture | | 35 | +def resp_get_resource_group(): | | 36 | + with responses.RequestsMock() as rsps: | | 37 | + rsps.add( | | 38 | + method=responses.GET, | | 39 | + url="http://localhost/api/v4/projects/1/resource_groups/production", | | 40 | + json=resource_group_content, | | 41 | + content_type="application/json", | | 42 | + status=200, | | 43 | + ) | | 44 | + yield rsps | | 45 | + | | 46 | + | | 47 | +@pytest.fixture | | 48 | +def resp_list_upcoming_jobs(): | | 49 | + with responses.RequestsMock() as rsps: | | 50 | + rsps.add( | | 51 | + method=responses.GET, | | 52 | + url="http://localhost/api/v4/projects/1/resource_groups/production/upcoming_jobs", | | 53 | + json=[job_content], | | 54 | + content_type="application/json", | | 55 | + status=200, | | 56 | + ) | | 57 | + yield rsps | | 58 | + | | 59 | + | | 60 | +def test_list_project_resource_groups(project, resp_list_resource_groups): | | 61 | + resource_groups = project.resource_groups.list() | | 62 | + assert isinstance(resource_groups, list) | | 63 | + assert isinstance(resource_groups[0], ProjectResourceGroup) | | 64 | + assert resource_groups[0].process_mode == "unordered" | | 65 | + | | 66 | + | | 67 | +def test_get_project_resource_group(project, resp_get_resource_group): | | 68 | + resource_group = project.resource_groups.get("production") | | 69 | + assert isinstance(resource_group, ProjectResourceGroup) | | 70 | + assert resource_group.process_mode == "unordered" | | 71 | + | | 72 | + | | 73 | +def test_list_resource_group_upcoming_jobs(project, resp_list_upcoming_jobs): | | 74 | + resource_group = project.resource_groups.get("production", lazy=True) | | 75 | + upcoming_jobs = resource_group.upcoming_jobs.list() | | 76 | + | | 77 | + assert isinstance(upcoming_jobs, list) | | 78 | + assert isinstance(upcoming_jobs[0], ProjectResourceGroupUpcomingJob) | | 79 | + assert upcoming_jobs[0].ref == "main" |
|