Skip to content

Commit 15ca555

Browse files
author
ayoub mrini
committed
feat(api): add support for Gitlab Deploy Token API
1 parent 4ffaf1d commit 15ca555

File tree

7 files changed

+348
-8
lines changed

7 files changed

+348
-8
lines changed

docs/cli.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ Get a specific user by id:
207207
208208
$ gitlab user get --id 3
209209
210+
Create a deploy token for a project:
211+
212+
.. code-block:: console
213+
214+
$ gitlab project-deploy-token create --project-id 2 \
215+
--name bar --username root --expires-at "2021-09-09" --scopes "read_repository"
216+
217+
List deploy tokens for a group:
218+
219+
.. code-block:: console
220+
221+
$ gitlab -v group-deploy-token list --group-id 3
222+
210223
Get a list of snippets for this project:
211224

212225
.. code-block:: console

docs/gl_objects/deploy_tokens.rst

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#######
2+
Deploy tokens
3+
#######
4+
5+
Deploy tokens allow read-only access to your repository and registry images
6+
without having a user and a password.
7+
8+
Deploy tokens
9+
=============
10+
11+
This endpoint requires admin access.
12+
13+
Reference
14+
---------
15+
16+
* v4 API:
17+
18+
+ :class:`gitlab.v4.objects.DeployToken`
19+
+ :class:`gitlab.v4.objects.DeployTokenManager`
20+
+ :attr:`gitlab.Gitlab.deploytokens`
21+
22+
* GitLab API: https://docs.gitlab.com/ce/api/deploy_tokens.html
23+
24+
Examples
25+
--------
26+
27+
Use the ``list()`` method to list all deploy tokens across the GitLab instance.
28+
29+
::
30+
31+
# List deploy tokens
32+
deploy_tokens = gl.deploytokens.list()
33+
34+
Project deploy tokens
35+
=====================
36+
37+
This endpoint requires project maintainer access or higher.
38+
39+
Reference
40+
---------
41+
42+
* v4 API:
43+
44+
+ :class:`gitlab.v4.objects.ProjectDeployToken`
45+
+ :class:`gitlab.v4.objects.ProjectDeployTokenManager`
46+
+ :attr:`gitlab.v4.objects.Project.deploytokens`
47+
48+
* GitLab API: https://docs.gitlab.com/ce/api/deploy_tokens.html#project-deploy-tokens
49+
50+
Examples
51+
--------
52+
53+
List the deploy tokens for a project::
54+
55+
deploy_tokens = project.deploytokens.list()
56+
57+
Create a new deploy token to access registry images of a project:
58+
59+
In addition to required parameters ``name`` and ``scopes``, this method accepts
60+
the following parameters:
61+
62+
* ``expires_at`` Expiration date of the deploy token. Does not expire if no value is provided.
63+
* ``username`` Username for deploy token. Default is ``gitlab+deploy-token-{n}``
64+
65+
66+
::
67+
68+
deploy_token = project.deploytokens.create({'name': 'token1', 'scopes': ['read_registry'], 'username':'', 'expires_at':''})
69+
# show its id
70+
print(deploy_token.id)
71+
# show the token value. Make sure you save it, you won't be able to access it again.
72+
print(deploy_token.token)
73+
74+
.. warning::
75+
76+
Even though ``username`` and ``expires_at`` are not required, they always have to be passed to the API,
77+
you can set them to empty strings, see: https://gitlab.com/gitlab-org/gitlab/-/issues/211878.
78+
The ``username``'s value is ignored by the API and will be overriden with ``gitlab+deploy-token-{n}``,
79+
see: https://gitlab.com/gitlab-org/gitlab/-/issues/211963
80+
81+
Remove a deploy token from the project::
82+
83+
deploy_token.delete()
84+
# or
85+
project.deploytokens.delete(deploy_token.id)
86+
87+
88+
Group deploy tokens
89+
===================
90+
91+
Reference
92+
---------
93+
94+
* v4 API:
95+
96+
+ :class:`gitlab.v4.objects.GroupDeployToken`
97+
+ :class:`gitlab.v4.objects.GroupDeployTokenManager`
98+
+ :attr:`gitlab.v4.objects.Group.deploytokens`
99+
100+
* GitLab API: https://docs.gitlab.com/ce/api/deploy_tokens.html#group-deploy-tokens
101+
102+
Examples
103+
--------
104+
105+
List the deploy tokens for a group::
106+
107+
deploy_tokens = group.deploytokens.list()
108+
109+
Create a new deploy token to access all repositories of all projects in a group:
110+
111+
In addition to required parameters ``name`` and ``scopes``, this method accepts
112+
the following parameters:
113+
114+
* ``expires_at`` Expiration date of the deploy token. Does not expire if no value is provided.
115+
* ``username`` Username for deploy token. Default is ``gitlab+deploy-token-{n}``
116+
117+
::
118+
119+
deploy_token = group.deploytokens.create({'name': 'token1', 'scopes': ['read_repository'], 'username':'', 'expires_at':''})
120+
# show its id
121+
print(deploy_token.id)
122+
123+
.. warning::
124+
125+
Even though ``username`` and ``expires_at`` are not required, they always have to be passed to the API,
126+
you can set them to empty strings, see: https://gitlab.com/gitlab-org/gitlab/-/issues/211878.
127+
The ``username``'s value is ignored by the API and will be overriden with ``gitlab+deploy-token-{n}``,
128+
see: https://gitlab.com/gitlab-org/gitlab/-/issues/211963
129+
130+
Remove a deploy token from the group::
131+
132+
deploy_token.delete()
133+
# or
134+
group.deploytokens.delete(deploy_token.id)
135+

gitlab/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def __init__(
119119

120120
self.broadcastmessages = objects.BroadcastMessageManager(self)
121121
self.deploykeys = objects.DeployKeyManager(self)
122+
self.deploytokens = objects.DeployTokenManager(self)
122123
self.geonodes = objects.GeoNodeManager(self)
123124
self.gitlabciymls = objects.GitlabciymlManager(self)
124125
self.gitignores = objects.GitignoreManager(self)

gitlab/tests/test_gitlab.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,40 @@ def resp_application_create(url, request):
964964
self.assertEqual(application.redirect_uri, "http://localhost:8080")
965965
self.assertEqual(application.scopes, ["api", "email"])
966966

967+
def test_deploy_tokens(self):
968+
@urlmatch(
969+
scheme="http",
970+
netloc="localhost",
971+
path="/api/v4/projects/1/deploy_tokens",
972+
method="post",
973+
)
974+
def resp_deploy_token_create(url, request):
975+
headers = {"content-type": "application/json"}
976+
content = """{
977+
"id": 1,
978+
"name": "test_deploy_token",
979+
"username": "custom-user",
980+
"expires_at": "2022-01-01T00:00:00.000Z",
981+
"token": "jMRvtPNxrn3crTAGukpZ",
982+
"scopes": [ "read_repository" ]}"""
983+
content = content.encode("utf-8")
984+
return response(200, content, headers, None, 5, request)
985+
986+
with HTTMock(resp_deploy_token_create):
987+
deploy_token = self.gl.projects.get(1, lazy=True).deploytokens.create(
988+
{
989+
"name": "test_deploy_token",
990+
"expires_at": "2022-01-01T00:00:00.000Z",
991+
"username": "custom-user",
992+
"scopes": ["read_repository"],
993+
}
994+
)
995+
self.assertIsInstance(deploy_token, ProjectDeployToken)
996+
self.assertEqual(deploy_token.id, 1),
997+
self.assertEqual(deploy_token.expires_at, "2022-01-01T00:00:00.000Z"),
998+
self.assertEqual(deploy_token.username, "custom-user")
999+
self.assertEqual(deploy_token.scopes, ["read_repository"])
1000+
9671001
def _default_config(self):
9681002
fd, temp_path = tempfile.mkstemp()
9691003
os.write(fd, valid_config)

gitlab/v4/objects.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,43 @@ class DeployKeyManager(ListMixin, RESTManager):
695695
_obj_cls = DeployKey
696696

697697

698+
class DeployToken(ObjectDeleteMixin, RESTObject):
699+
pass
700+
701+
702+
class DeployTokenManager(ListMixin, RESTManager):
703+
_path = "/deploy_tokens"
704+
_obj_cls = DeployToken
705+
706+
707+
class ProjectDeployToken(ObjectDeleteMixin, RESTObject):
708+
pass
709+
710+
711+
class ProjectDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager):
712+
_path = "/projects/%(project_id)s/deploy_tokens"
713+
_from_parent_attrs = {"project_id": "id"}
714+
_obj_cls = ProjectDeployToken
715+
_create_attrs = (
716+
("name", "scopes",),
717+
("expires_at", "username",),
718+
)
719+
720+
721+
class GroupDeployToken(ObjectDeleteMixin, RESTObject):
722+
pass
723+
724+
725+
class GroupDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager):
726+
_path = "/groups/%(group_id)s/deploy_tokens"
727+
_from_parent_attrs = {"group_id": "id"}
728+
_obj_cls = GroupDeployToken
729+
_create_attrs = (
730+
("name", "scopes",),
731+
("expires_at", "username",),
732+
)
733+
734+
698735
class NotificationSettings(SaveMixin, RESTObject):
699736
_id_attr = None
700737

@@ -1301,6 +1338,7 @@ class Group(SaveMixin, ObjectDeleteMixin, RESTObject):
13011338
("subgroups", "GroupSubgroupManager"),
13021339
("variables", "GroupVariableManager"),
13031340
("clusters", "GroupClusterManager"),
1341+
("deploytokens", "GroupDeployTokenManager"),
13041342
)
13051343

13061344
@cli.register_custom_action("Group", ("to_project_id",))
@@ -4194,6 +4232,7 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
41944232
("clusters", "ProjectClusterManager"),
41954233
("additionalstatistics", "ProjectAdditionalStatisticsManager"),
41964234
("issuesstatistics", "ProjectIssuesStatisticsManager"),
4235+
("deploytokens", "ProjectDeployTokenManager"),
41974236
)
41984237

41994238
@cli.register_custom_action("Project", ("submodule", "branch", "commit_sha"))

tools/cli_test_v4.sh

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,6 @@ testcase "project upload" '
188188
--filename '$(basename $0)' --filepath '$0' >/dev/null 2>&1
189189
'
190190

191-
testcase "project deletion" '
192-
GITLAB project delete --id "$PROJECT_ID"
193-
'
194-
195-
testcase "group deletion" '
196-
OUTPUT=$(try GITLAB group delete --id $GROUP_ID)
197-
'
198-
199191
testcase "application settings get" '
200192
GITLAB application-settings get >/dev/null 2>&1
201193
'
@@ -215,3 +207,82 @@ testcase "values from files" '
215207
echo $OUTPUT | grep -q "Multi line"
216208
'
217209

210+
# Test deploy tokens
211+
CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v project-deploy-token create --project-id $PROJECT_ID \
212+
--name foo --username root --expires-at "2021-09-09" --scopes "read_registry")
213+
CREATED_DEPLOY_TOKEN_ID=$(echo "$CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT" | grep ^id: | cut -d" " -f2)
214+
testcase "create project deploy token" '
215+
echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep -q "name: foo"
216+
'
217+
testcase "create project deploy token" '
218+
echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep -q "expires-at: 2021-09-09T00:00:00.000Z"
219+
'
220+
testcase "create project deploy token" '
221+
echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep "scopes: " | grep -q "read_registry"
222+
'
223+
# Uncomment once https://gitlab.com/gitlab-org/gitlab/-/issues/211963 is fixed
224+
#testcase "create project deploy token" '
225+
# echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep "username: root"
226+
#'
227+
228+
# Remove once https://gitlab.com/gitlab-org/gitlab/-/issues/211963 is fixed
229+
testcase "create project deploy token" '
230+
echo $CREATE_PROJECT_DEPLOY_TOKEN_OUTPUT | grep "gitlab+deploy-token"
231+
'
232+
233+
LIST_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v deploy-token list)
234+
testcase "list all deploy tokens" '
235+
echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -q "name: foo"
236+
'
237+
testcase "list all deploy tokens" '
238+
echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -q "id: $CREATED_DEPLOY_TOKEN_ID"
239+
'
240+
testcase "list all deploy tokens" '
241+
echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -q "expires-at: 2021-09-09T00:00:00.000Z"
242+
'
243+
testcase "list all deploy tokens" '
244+
echo $LIST_DEPLOY_TOKEN_OUTPUT | grep "scopes: " | grep -q "read_registry"
245+
'
246+
247+
testcase "list project deploy tokens" '
248+
OUTPUT=$(GITLAB -v project-deploy-token list --project-id $PROJECT_ID)
249+
echo $OUTPUT | grep -q "id: $CREATED_DEPLOY_TOKEN_ID"
250+
'
251+
testcase "delete project deploy token" '
252+
GITLAB -v project-deploy-token delete --project-id $PROJECT_ID --id $CREATED_DEPLOY_TOKEN_ID
253+
LIST_PROJECT_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v project-deploy-token list --project-id $PROJECT_ID)
254+
echo $LIST_PROJECT_DEPLOY_TOKEN_OUTPUT | grep -qv "id: $CREATED_DEPLOY_TOKEN_ID"
255+
'
256+
GITLAB -v deploy-token list
257+
testcase "delete project deploy token" '
258+
LIST_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v deploy-token list)
259+
echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -qv "id: $CREATED_DEPLOY_TOKEN_ID"
260+
'
261+
262+
CREATE_GROUP_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v group-deploy-token create --group-id $GROUP_ID \
263+
--name bar --username root --expires-at "2021-09-09" --scopes "read_repository")
264+
CREATED_DEPLOY_TOKEN_ID=$(echo "$CREATE_GROUP_DEPLOY_TOKEN_OUTPUT" | grep ^id: | cut -d" " -f2)
265+
testcase "create group deploy token" '
266+
echo $CREATE_GROUP_DEPLOY_TOKEN_OUTPUT | grep -q "name: bar"
267+
'
268+
testcase "list group deploy tokens" '
269+
OUTPUT=$(GITLAB -v group-deploy-token list --group-id $GROUP_ID)
270+
echo $OUTPUT | grep -q "id: $CREATED_DEPLOY_TOKEN_ID"
271+
'
272+
testcase "delete group deploy token" '
273+
GITLAB -v group-deploy-token delete --group-id $GROUP_ID --id $CREATED_DEPLOY_TOKEN_ID
274+
LIST_GROUP_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v group-deploy-token list --group-id $GROUP_ID)
275+
echo $LIST_GROUP_DEPLOY_TOKEN_OUTPUT | grep -qv "id: $CREATED_DEPLOY_TOKEN_ID"
276+
'
277+
testcase "delete group deploy token" '
278+
LIST_DEPLOY_TOKEN_OUTPUT=$(GITLAB -v deploy-token list)
279+
echo $LIST_DEPLOY_TOKEN_OUTPUT | grep -qv "id: $CREATED_DEPLOY_TOKEN_ID"
280+
'
281+
282+
testcase "project deletion" '
283+
GITLAB project delete --id "$PROJECT_ID"
284+
'
285+
286+
testcase "group deletion" '
287+
OUTPUT=$(try GITLAB group delete --id $GROUP_ID)
288+
'

0 commit comments

Comments
 (0)