Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit01de524

Browse files
author
ayoub mrini
committed
feat(api): add support for Gitlab Deploy Token API
1 parentc5904c4 commit01de524

File tree

7 files changed

+354
-8
lines changed

7 files changed

+354
-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 -v 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: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
With GitLab 12.9, 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+
Also, 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+
These issues were fixed in GitLab 12.10.
81+
82+
Remove a deploy token from the project::
83+
84+
deploy_token.delete()
85+
# or
86+
project.deploytokens.delete(deploy_token.id)
87+
88+
89+
Group deploy tokens
90+
===================
91+
92+
Reference
93+
---------
94+
95+
* v4 API:
96+
97+
+:class:`gitlab.v4.objects.GroupDeployToken`
98+
+:class:`gitlab.v4.objects.GroupDeployTokenManager`
99+
+:attr:`gitlab.v4.objects.Group.deploytokens`
100+
101+
* GitLab API: https://docs.gitlab.com/ce/api/deploy_tokens.html#group-deploy-tokens
102+
103+
Examples
104+
--------
105+
106+
List the deploy tokens for a group::
107+
108+
deploy_tokens = group.deploytokens.list()
109+
110+
Create a new deploy token to access all repositories of all projects in a group:
111+
112+
In addition to required parameters ``name`` and ``scopes``, this method accepts
113+
the following parameters:
114+
115+
* ``expires_at`` Expiration date of the deploy token. Does not expire if no value is provided.
116+
* ``username`` Username for deploy token. Default is ``gitlab+deploy-token-{n}``
117+
118+
::
119+
120+
deploy_token = group.deploytokens.create({'name': 'token1', 'scopes': ['read_repository'], 'username':'', 'expires_at':''})
121+
# show its id
122+
print(deploy_token.id)
123+
124+
..warning::
125+
126+
With GitLab 12.9, even though ``username`` and ``expires_at`` are not required, they always have to be passed to the API.
127+
You can set them to empty strings, see: https://gitlab.com/gitlab-org/gitlab/-/issues/211878.
128+
Also, the ``username``'s value is ignored by the API and will be overriden with ``gitlab+deploy-token-{n}``,
129+
see: https://gitlab.com/gitlab-org/gitlab/-/issues/211963
130+
These issues were fixed in GitLab 12.10.
131+
132+
Remove a deploy token from the group::
133+
134+
deploy_token.delete()
135+
# or
136+
group.deploytokens.delete(deploy_token.id)
137+

‎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
@@ -920,6 +920,40 @@ def resp_application_create(url, request):
920920
self.assertEqual(application.redirect_uri,"http://localhost:8080")
921921
self.assertEqual(application.scopes, ["api","email"])
922922

923+
deftest_deploy_tokens(self):
924+
@urlmatch(
925+
scheme="http",
926+
netloc="localhost",
927+
path="/api/v4/projects/1/deploy_tokens",
928+
method="post",
929+
)
930+
defresp_deploy_token_create(url,request):
931+
headers= {"content-type":"application/json"}
932+
content="""{
933+
"id": 1,
934+
"name": "test_deploy_token",
935+
"username": "custom-user",
936+
"expires_at": "2022-01-01T00:00:00.000Z",
937+
"token": "jMRvtPNxrn3crTAGukpZ",
938+
"scopes": [ "read_repository" ]}"""
939+
content=content.encode("utf-8")
940+
returnresponse(200,content,headers,None,5,request)
941+
942+
withHTTMock(resp_deploy_token_create):
943+
deploy_token=self.gl.projects.get(1,lazy=True).deploytokens.create(
944+
{
945+
"name":"test_deploy_token",
946+
"expires_at":"2022-01-01T00:00:00.000Z",
947+
"username":"custom-user",
948+
"scopes": ["read_repository"],
949+
}
950+
)
951+
self.assertIsInstance(deploy_token,ProjectDeployToken)
952+
self.assertEqual(deploy_token.id,1),
953+
self.assertEqual(deploy_token.expires_at,"2022-01-01T00:00:00.000Z"),
954+
self.assertEqual(deploy_token.username,"custom-user")
955+
self.assertEqual(deploy_token.scopes, ["read_repository"])
956+
923957
def_default_config(self):
924958
fd,temp_path=tempfile.mkstemp()
925959
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+
classDeployToken(ObjectDeleteMixin,RESTObject):
699+
pass
700+
701+
702+
classDeployTokenManager(ListMixin,RESTManager):
703+
_path="/deploy_tokens"
704+
_obj_cls=DeployToken
705+
706+
707+
classProjectDeployToken(ObjectDeleteMixin,RESTObject):
708+
pass
709+
710+
711+
classProjectDeployTokenManager(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+
classGroupDeployToken(ObjectDeleteMixin,RESTObject):
722+
pass
723+
724+
725+
classGroupDeployTokenManager(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
classNotificationSettings(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",))
@@ -4212,6 +4250,7 @@ class Project(SaveMixin, ObjectDeleteMixin, RESTObject):
42124250
("clusters","ProjectClusterManager"),
42134251
("additionalstatistics","ProjectAdditionalStatisticsManager"),
42144252
("issuesstatistics","ProjectIssuesStatisticsManager"),
4253+
("deploytokens","ProjectDeployTokenManager"),
42154254
)
42164255

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

‎tools/cli_test_v4.sh

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,6 @@ testcase "project upload" '
195195
--filename'$(basename$0)' --filepath'$0' >/dev/null 2>&1
196196
'
197197

198-
testcase"project deletion"'
199-
GITLAB project delete --id "$PROJECT_ID"
200-
'
201-
202-
testcase"group deletion"'
203-
OUTPUT=$(try GITLAB group delete --id $GROUP_ID)
204-
'
205-
206198
testcase"application settings get"'
207199
GITLAB application-settings get >/dev/null 2>&1
208200
'
@@ -222,3 +214,83 @@ testcase "values from files" '
222214
echo $OUTPUT | grep -q "Multi line"
223215
'
224216

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp