Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 3 additions & 49 deletions gitlab/v4/objects/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,7 @@


class ProjectService(SaveMixin, ObjectDeleteMixin, RESTObject):
pass
_id_attr = "slug"


class ProjectServiceManager(GetMixin, UpdateMixin, DeleteMixin, ListMixin, RESTManager):
Expand Down Expand Up @@ -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]:
Expand Down
29 changes: 28 additions & 1 deletion tests/functional/api/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 15 additions & 0 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down