summaryrefslogtreecommitdiff
path: root/gitlab
diff options
context:
space:
mode:
authorayoub mrini <amrini@wiremind.fr>2020-03-19 23:30:40 +0100
committerayoub mrini <amrini@wiremind.fr>2020-04-06 20:52:35 +0200
commit01de524ce39a67b549b3157bf4de827dd0568d6b (patch)
treee195869989b88b998ab0452ac8572c02895db396 /gitlab
parentc5904c4c2e79ec302ff0de20bcb2792be4924bbe (diff)
downloadgitlab-01de524ce39a67b549b3157bf4de827dd0568d6b.tar.gz
feat(api): add support for Gitlab Deploy Token API
Diffstat (limited to 'gitlab')
-rw-r--r--gitlab/__init__.py1
-rw-r--r--gitlab/tests/test_gitlab.py34
-rw-r--r--gitlab/v4/objects.py39
3 files changed, 74 insertions, 0 deletions
diff --git a/gitlab/__init__.py b/gitlab/__init__.py
index a12ffb9..94e80f8 100644
--- a/gitlab/__init__.py
+++ b/gitlab/__init__.py
@@ -119,6 +119,7 @@ class Gitlab(object):
self.broadcastmessages = objects.BroadcastMessageManager(self)
self.deploykeys = objects.DeployKeyManager(self)
+ self.deploytokens = objects.DeployTokenManager(self)
self.geonodes = objects.GeoNodeManager(self)
self.gitlabciymls = objects.GitlabciymlManager(self)
self.gitignores = objects.GitignoreManager(self)
diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py
index d104c7d..113093a 100644
--- a/gitlab/tests/test_gitlab.py
+++ b/gitlab/tests/test_gitlab.py
@@ -920,6 +920,40 @@ class TestGitlab(unittest.TestCase):
self.assertEqual(application.redirect_uri, "http://localhost:8080")
self.assertEqual(application.scopes, ["api", "email"])
+ def test_deploy_tokens(self):
+ @urlmatch(
+ scheme="http",
+ netloc="localhost",
+ path="/api/v4/projects/1/deploy_tokens",
+ method="post",
+ )
+ def resp_deploy_token_create(url, request):
+ headers = {"content-type": "application/json"}
+ content = """{
+ "id": 1,
+ "name": "test_deploy_token",
+ "username": "custom-user",
+ "expires_at": "2022-01-01T00:00:00.000Z",
+ "token": "jMRvtPNxrn3crTAGukpZ",
+ "scopes": [ "read_repository" ]}"""
+ content = content.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+ with HTTMock(resp_deploy_token_create):
+ deploy_token = self.gl.projects.get(1, lazy=True).deploytokens.create(
+ {
+ "name": "test_deploy_token",
+ "expires_at": "2022-01-01T00:00:00.000Z",
+ "username": "custom-user",
+ "scopes": ["read_repository"],
+ }
+ )
+ self.assertIsInstance(deploy_token, ProjectDeployToken)
+ self.assertEqual(deploy_token.id, 1),
+ self.assertEqual(deploy_token.expires_at, "2022-01-01T00:00:00.000Z"),
+ self.assertEqual(deploy_token.username, "custom-user")
+ self.assertEqual(deploy_token.scopes, ["read_repository"])
+
def _default_config(self):
fd, temp_path = tempfile.mkstemp()
os.write(fd, valid_config)
diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py
index 8852a1e..116c7ec 100644
--- a/gitlab/v4/objects.py
+++ b/gitlab/v4/objects.py
@@ -695,6 +695,43 @@ class DeployKeyManager(ListMixin, RESTManager):
_obj_cls = DeployKey
+class DeployToken(ObjectDeleteMixin, RESTObject):
+ pass
+
+
+class DeployTokenManager(ListMixin, RESTManager):
+ _path = "/deploy_tokens"
+ _obj_cls = DeployToken
+
+
+class ProjectDeployToken(ObjectDeleteMixin, RESTObject):
+ pass
+
+
+class ProjectDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager):
+ _path = "/projects/%(project_id)s/deploy_tokens"
+ _from_parent_attrs = {"project_id": "id"}
+ _obj_cls = ProjectDeployToken
+ _create_attrs = (
+ ("name", "scopes",),
+ ("expires_at", "username",),
+ )
+
+
+class GroupDeployToken(ObjectDeleteMixin, RESTObject):
+ pass
+
+
+class GroupDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager):
+ _path = "/groups/%(group_id)s/deploy_tokens"
+ _from_parent_attrs = {"group_id": "id"}
+ _obj_cls = GroupDeployToken
+ _create_attrs = (
+ ("name", "scopes",),
+ ("expires_at", "username",),
+ )
+
+
class NotificationSettings(SaveMixin, RESTObject):
_id_attr = None
@@ -1301,6 +1338,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
("subgroups", "GroupSubgroupManager"),
("variables", "GroupVariableManager"),
("clusters", "GroupClusterManager"),
+ ("deploytokens", "GroupDeployTokenManager"),
)
@cli.register_custom_action("Group", ("to_project_id",))
@@ -4212,6 +4250,7 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
("clusters", "ProjectClusterManager"),
("additionalstatistics", "ProjectAdditionalStatisticsManager"),
("issuesstatistics", "ProjectIssuesStatisticsManager"),
+ ("deploytokens", "ProjectDeployTokenManager"),
)
@cli.register_custom_action("Project", ("submodule", "branch", "commit_sha"))