diff --git a/gitlab/v4/objects/services.py b/gitlab/v4/objects/services.py index 9b8e7f3a0..424d08563 100644 --- a/gitlab/v4/objects/services.py +++ b/gitlab/v4/objects/services.py @@ -3,7 +3,7 @@ https://docs.gitlab.com/ee/api/integrations.html """ -from typing import Any, cast, Dict, List, Optional, Union +from typing import Any, cast, List, Union from gitlab import cli from gitlab.base import RESTManager, RESTObject @@ -23,7 +23,7 @@ class ProjectService(SaveMixin, ObjectDeleteMixin, RESTObject): - pass + _id_attr = "slug" class ProjectServiceManager(GetMixin, UpdateMixin, DeleteMixin, ListMixin, RESTManager): @@ -264,53 +264,7 @@ class ProjectServiceManager(GetMixin, UpdateMixin, DeleteMixin, ListMixin, RESTM def get( self, id: Union[str, int], lazy: bool = False, **kwargs: Any ) -> ProjectService: - """Retrieve a single object. - - Args: - id: ID of the object to retrieve - lazy: If True, don't request the server, but create a - shallow object giving access to the managers. This is - useful if you want to avoid useless calls to the API. - **kwargs: Extra options to send to the server (e.g. sudo) - - Returns: - The generated RESTObject. - - Raises: - GitlabAuthenticationError: If authentication is not correct - GitlabGetError: If the server cannot perform the request - """ - obj = cast( - ProjectService, - super().get(id, lazy=lazy, **kwargs), - ) - obj.id = id - return obj - - def update( - self, - id: Optional[Union[str, int]] = None, - new_data: Optional[Dict[str, Any]] = None, - **kwargs: Any - ) -> Dict[str, Any]: - """Update an object on the server. - - Args: - id: ID of the object to update (can be None if not required) - new_data: the update data for the object - **kwargs: Extra options to send to the server (e.g. sudo) - - Returns: - The new object data (*not* a RESTObject) - - Raises: - GitlabAuthenticationError: If authentication is not correct - GitlabUpdateError: If the server cannot perform the request - """ - new_data = new_data or {} - result = super().update(id, new_data, **kwargs) - self.id = id - return result + return cast(ProjectService, super().get(id=id, lazy=lazy, **kwargs)) @cli.register_custom_action("ProjectServiceManager") def available(self, **kwargs: Any) -> List[str]: diff --git a/tests/functional/api/test_services.py b/tests/functional/api/test_services.py index 100c0c9e5..51805ef37 100644 --- a/tests/functional/api/test_services.py +++ b/tests/functional/api/test_services.py @@ -6,6 +6,33 @@ import gitlab -def test_services(project): +def test_get_service_lazy(project): service = project.services.get("jira", lazy=True) assert isinstance(service, gitlab.v4.objects.ProjectService) + + +def test_update_service(project): + service_dict = project.services.update( + "emails-on-push", {"recipients": "email@example.com"} + ) + assert service_dict["active"] + + +def test_list_services(project, service): + services = project.services.list() + assert isinstance(services[0], gitlab.v4.objects.ProjectService) + assert services[0].active + + +def test_get_service(project, service): + service_object = project.services.get(service["slug"]) + assert isinstance(service_object, gitlab.v4.objects.ProjectService) + assert service_object.active + + +def test_delete_service(project, service): + service_object = project.services.get(service["slug"]) + service_object.delete() + + service_object = project.services.get(service["slug"]) + assert not service_object.active diff --git a/tests/functional/conftest.py b/tests/functional/conftest.py index d34c87e67..ca589f257 100644 --- a/tests/functional/conftest.py +++ b/tests/functional/conftest.py @@ -392,6 +392,21 @@ def release(project, project_file): return release +@pytest.fixture(scope="function") +def service(project): + """This is just a convenience fixture to make test cases slightly prettier. Project + services are not idempotent. A service cannot be retrieved until it is enabled. + After it is enabled the first time, it can never be fully deleted, only disabled.""" + service = project.services.update("asana", {"api_key": "api_key"}) + + yield service + + try: + project.services.delete("asana") + except gitlab.exceptions.GitlabDeleteError as e: + print(f"Service already disabled: {e}") + + @pytest.fixture(scope="module") def user(gl): """User fixture for user API resource tests."""