Verified Commit d442df27 authored by Nejc Habjan's avatar Nejc Habjan
Browse files

feat(api): add support for adding instance deploy keys

parent f62dda7f
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -19,7 +19,11 @@ Reference
Examples
--------

List the deploy keys::
Add an instance-wide deploy key (requires admin access)::

    keys = gl.deploykeys.create({'title': 'instance key', 'key': INSTANCE_KEY})

List all deploy keys::

    keys = gl.deploykeys.list()

+15 −4
Original line number Diff line number Diff line
@@ -7,7 +7,13 @@ import requests
from gitlab import cli
from gitlab import exceptions as exc
from gitlab.base import RESTObject
from gitlab.mixins import CRUDMixin, ListMixin, ObjectDeleteMixin, SaveMixin
from gitlab.mixins import (
    CreateMixin,
    CRUDMixin,
    ListMixin,
    ObjectDeleteMixin,
    SaveMixin,
)
from gitlab.types import RequiredOptional

__all__ = ["DeployKey", "DeployKeyManager", "ProjectKey", "ProjectKeyManager"]
@@ -17,9 +23,12 @@ class DeployKey(RESTObject):
    pass


class DeployKeyManager(ListMixin[DeployKey]):
class DeployKeyManager(CreateMixin[DeployKey], ListMixin[DeployKey]):
    _path = "/deploy_keys"
    _obj_cls = DeployKey
    _create_attrs = RequiredOptional(
        required=("title", "key"), optional=("expires_at",)
    )


class ProjectKey(SaveMixin, ObjectDeleteMixin, RESTObject):
@@ -30,8 +39,10 @@ class ProjectKeyManager(CRUDMixin[ProjectKey]):
    _path = "/projects/{project_id}/deploy_keys"
    _obj_cls = ProjectKey
    _from_parent_attrs = {"project_id": "id"}
    _create_attrs = RequiredOptional(required=("title", "key"), optional=("can_push",))
    _update_attrs = RequiredOptional(optional=("title", "can_push"))
    _create_attrs = RequiredOptional(
        required=("title", "key"), optional=("can_push", "expires_at")
    )
    _update_attrs = RequiredOptional(optional=("title", "can_push", "expires_at"))

    @cli.register_custom_action(
        cls_names="ProjectKeyManager",
+10 −1
Original line number Diff line number Diff line
def test_project_deploy_keys(gl, project, DEPLOY_KEY):
from gitlab import Gitlab
from gitlab.v4.objects import Project


def test_deploy_keys(gl: Gitlab, DEPLOY_KEY: str) -> None:
    deploy_key = gl.deploykeys.create({"title": "foo@bar", "key": DEPLOY_KEY})
    assert deploy_key in gl.deploykeys.list(get_all=False)


def test_project_deploy_keys(gl: Gitlab, project: Project, DEPLOY_KEY: str) -> None:
    deploy_key = project.keys.create({"title": "foo@bar", "key": DEPLOY_KEY})
    assert deploy_key in project.keys.list()