Skip to content

Commit 2e47c71

Browse files
committed
🚒 Fixes for gitlab API update
1 parent 02b95df commit 2e47c71

File tree

46 files changed

+2191
-1261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2191
-1261
lines changed

git_repo/services/ext/gitlab.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def connect(self):
4343

4444
def create(self, user, repo, add=False):
4545
try:
46-
group = self.gl.groups.search(user)
47-
data = {'name': repo, 'path': repo}
46+
group = self.gl.groups.list(search=user)
47+
data = {'name': repo}
4848
if group:
4949
data['namespace_id'] = group[0].id
5050
self.gl.projects.create(data=data)
5151
except GitlabCreateError as err:
52-
if json.loads(err.response_body.decode('utf-8'))['message']['name'][0] == 'has already been taken':
52+
if json.loads(err.response_body.decode('utf-8')).get('message', {}).get('name', [None])[0] == 'has already been taken':
5353
raise ResourceExistsError("Project already exists.") from err
5454
else:
5555
raise ResourceError("Unhandled error.") from err
@@ -60,7 +60,7 @@ def fork(self, user, repo):
6060
try:
6161
return self.gl.projects.get('{}/{}'.format(user, repo)).forks.create({}).path_with_namespace
6262
except GitlabCreateError as err:
63-
if json.loads(err.response_body.decode('utf-8'))['message']['name'][0] == 'has already been taken':
63+
if json.loads(err.response_body.decode('utf-8')).get('message', {}).get('name', [None])[0] == 'has already been taken':
6464
raise ResourceExistsError("Project already exists.") from err
6565
else:
6666
raise ResourceError("Unhandled error: {}".format(err)) from err
@@ -71,8 +71,8 @@ def delete(self, repo, user=None):
7171
try:
7272
repository = self.gl.projects.get('{}/{}'.format(user, repo))
7373
if repository:
74-
result = self.gl.delete(repository.__class__, repository.id)
75-
if not repository or not result:
74+
result = self.gl.projects.delete(repository.id)
75+
if not repository:
7676
raise ResourceNotFoundError("Cannot delete: repository {}/{} does not exists.".format(user, repo))
7777
except GitlabGetError as err:
7878
if err.response_code == 404:
@@ -197,7 +197,7 @@ def load_file(fname, path='.'):
197197

198198
data = {
199199
'title': description,
200-
'visibility_level': 0 if secret else 20
200+
'visibility': 'private' if secret else 'public'
201201
}
202202

203203
try:
@@ -211,12 +211,11 @@ def load_file(fname, path='.'):
211211
namespace = self.username
212212
gist_path = gist_pathes[1]
213213
data.update({
214-
'project_id': '/'.join([namespace, project]),
215214
'code': load_file(gist_path),
216215
'file_name': os.path.basename(gist_path),
217216
}
218217
)
219-
gist = self.gl.project_snippets.create(data)
218+
gist = self.gl.projects.get('/'.join([namespace, project])).snippets.create(data)
220219

221220
elif len(gist_pathes) == 1:
222221
gist_path = gist_pathes[0]
@@ -302,7 +301,7 @@ def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=N
302301
if not title and not description:
303302
raise ArgumentError('Missing message for request creation')
304303

305-
request = self.gl.projects.get(project.id).mergerequests.create(
304+
request = self.gl.projects.get(from_project.id).mergerequests.create(
306305
{
307306
'source_branch': from_branch,
308307
'target_branch': onto_branch,
@@ -323,14 +322,16 @@ def request_create(self, onto_user, onto_repo, from_branch, onto_branch, title=N
323322

324323
except GitlabGetError as err:
325324
raise ResourceNotFoundError(err) from err
325+
except GitlabCreateError as err:
326+
raise ResourceError("Creation error: {}".format(err)) from err
326327
except Exception as err:
327328
raise ResourceError("Unhandled error: {}".format(err)) from err
328329

329330
def request_list(self, user, repo):
330331
project = self.gl.projects.get('/'.join([user, repo]))
331332
yield "{:>3}\t{:<60}\t{:2}"
332333
yield ('id', 'title', 'URL')
333-
for mr in self.gl.projects.get(project.id).mergerequests.list() :
334+
for mr in project.mergerequests.list() :
334335
yield ( str(mr.iid),
335336
mr.title,
336337
mr.web_url

tests/helpers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@ def action_request_fetch(self, namespace, repository, request, pull=False, fail=
654654
with self.recorder.use_cassette(self._make_cassette_name()):
655655
with self.mockup_git(namespace, repository):
656656
self.set_mock_popen_commands([
657+
('git init', b'Initialized empty Git repository in /tmp/bar/.git/', b'', 0),
657658
('git remote add all {}'.format(local_slug), b'', b'', 0),
658659
('git remote add {} {}'.format(self.service.name, local_slug), b'', b'', 0),
659660
('git remote get-url --all all', local_slug.encode('utf-8'), b'', 0),
@@ -762,15 +763,15 @@ def prepare_project_for_test():
762763
if will_record:
763764
self.repository.git.push(self.service.name, 'master')
764765
# create a new branch
765-
new_branch = self.repository.create_head(source_branch, 'HEAD')
766+
new_branch = self.repository.create_head(source_branch or 'master', 'HEAD')
766767
self.repository.head.reference = new_branch
767768
self.repository.head.reset(index=True, working_tree=True)
768769
# make a modification, commit and push it to that branch
769770
with open(os.path.join(self.repository.working_dir, 'second_file'), 'w') as test:
770771
test.write('La meilleure façon de ne pas avancer est de suivre une idée fixe. J.Prévert')
771772
self.repository.git.add('second_file')
772773
self.repository.git.commit(message='Second commit')
773-
self.repository.git.push(service, source_branch)
774+
self.repository.git.push(self.service.name, source_branch or 'master')
774775
else:
775776
import git
776777
self.service._extracts_ref = lambda *a: git.Reference(

tests/integration/cassettes/test_gitlab_test_00_fork.json

Lines changed: 59 additions & 33 deletions
Large diffs are not rendered by default.

tests/integration/cassettes/test_gitlab_test_01_create__already_exists.json

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"http_interactions": [
33
{
4-
"recorded_at": "2016-10-15T20:46:51",
4+
"recorded_at": "2019-08-26T21:13:22",
55
"request": {
66
"body": {
77
"encoding": "utf-8",
@@ -11,37 +11,47 @@
1111
"Accept": "*/*",
1212
"Accept-Encoding": "identity",
1313
"Connection": "keep-alive",
14+
"Content-type": "application/json",
1415
"PRIVATE-TOKEN": "<PRIVATE_KEY_GITLAB>",
15-
"User-Agent": "python-requests/2.10.0"
16+
"User-Agent": "python-requests/2.22.0"
1617
},
1718
"method": "GET",
18-
"uri": "https://gitlab.com/api/v3/user"
19+
"uri": "https://gitlab.com/api/v4/user"
1920
},
2021
"response": {
2122
"body": {
2223
"encoding": null,
23-
"string": "{\"name\":\"Guyzmo\",\"username\":\"<GITLAB_NAMESPACE>\",\"id\":459552,\"state\":\"active\",\"avatar_url\":\"https://secure.gravatar.com/avatar/917dc55c63895af9953df7d798cdd5f8?s=80&d=identicon\",\"web_url\":\"https://gitlab.com/<GITLAB_NAMESPACE>\",\"created_at\":\"2016-03-21T12:52:59.859Z\",\"is_admin\":false,\"bio\":\": :(){ :|:& };:\",\"location\":\"Earth, Solar system, Milkyway, Universe\",\"skype\":\"\",\"linkedin\":\"\",\"twitter\":\"\",\"website_url\":\"http://i.got.nothing.to/blog\",\"organization\":null,\"last_sign_in_at\":\"2016-10-13T18:28:52.737Z\",\"confirmed_at\":\"2016-03-21T13:48:05.234Z\",\"email\":\"toto@example.org\",\"theme_id\":4,\"color_scheme_id\":4,\"projects_limit\":100000,\"current_sign_in_at\":\"2016-10-13T18:28:55.087Z\",\"identities\":[],\"can_create_group\":true,\"can_create_project\":true,\"two_factor_enabled\":false,\"external\":false}"
24+
"string": "{\"id\":459552,\"name\":\"Guyzmo\",\"username\":\"guyzmo\",\"state\":\"active\",\"avatar_url\":\"https://secure.gravatar.com/avatar/917dc55c63895af9953df7d798cdd5f8?s=80\\u0026d=identicon\",\"web_url\":\"https://gitlab.com/guyzmo\",\"created_at\":\"2016-03-21T12:52:59.859Z\",\"bio\":\": :(){ :|:\\u0026 };:\",\"location\":\"Earth, Solar system, Milkyway, Universe\",\"public_email\":\"\",\"skype\":\"\",\"linkedin\":\"\",\"twitter\":\"https://twitter.com/guyzmo\",\"website_url\":\"http://i.got.nothing.to/blog\",\"organization\":\"\",\"last_sign_in_at\":\"2019-08-19T16:34:20.318Z\",\"confirmed_at\":\"2016-03-21T13:48:05.234Z\",\"last_activity_on\":\"2019-08-26\",\"email\":\"guyzmo+gitlab@m0g.net\",\"theme_id\":2,\"color_scheme_id\":2,\"projects_limit\":100000,\"current_sign_in_at\":\"2019-08-26T12:05:47.884Z\",\"identities\":[],\"can_create_group\":true,\"can_create_project\":true,\"two_factor_enabled\":false,\"external\":false,\"private_profile\":false,\"shared_runners_minutes_limit\":2000,\"extra_shared_runners_minutes_limit\":null}"
2425
},
2526
"headers": {
2627
"Cache-Control": "max-age=0, private, must-revalidate",
27-
"Content-Length": "775",
28+
"Content-Length": "944",
2829
"Content-Type": "application/json",
29-
"Date": "Sat, 15 Oct 2016 20:46:51 GMT",
30-
"Etag": "W/\"677a046b34e34176cde8e128968cb26c\"",
30+
"Date": "Mon, 26 Aug 2019 21:13:22 GMT",
31+
"Etag": "W/\"c277655375af222ec17621ae3125a5fd\"",
32+
"RateLimit-Limit": "600",
33+
"RateLimit-Observed": "2",
34+
"RateLimit-Remaining": "598",
35+
"RateLimit-Reset": "1566854062",
36+
"RateLimit-ResetTime": "Mon, 26 Aug 2019 21:14:22 GMT",
37+
"Referrer-Policy": "strict-origin-when-cross-origin",
3138
"Server": "nginx",
39+
"Strict-Transport-Security": "max-age=31536000",
3240
"Vary": "Origin",
33-
"X-Request-Id": "0d16804f-e0d9-4841-9beb-285fd7c9817a",
34-
"X-Runtime": "0.038217"
41+
"X-Content-Type-Options": "nosniff",
42+
"X-Frame-Options": "SAMEORIGIN",
43+
"X-Request-Id": "xyVUzGHFt47",
44+
"X-Runtime": "0.026212"
3545
},
3646
"status": {
3747
"code": 200,
3848
"message": "OK"
3949
},
40-
"url": "https://gitlab.com/api/v3/user"
50+
"url": "https://gitlab.com/api/v4/user"
4151
}
4252
},
4353
{
44-
"recorded_at": "2016-10-15T20:46:52",
54+
"recorded_at": "2019-08-26T21:13:22",
4555
"request": {
4656
"body": {
4757
"encoding": "utf-8",
@@ -51,60 +61,70 @@
5161
"Accept": "*/*",
5262
"Accept-Encoding": "identity",
5363
"Connection": "keep-alive",
64+
"Content-type": "application/json",
5465
"PRIVATE-TOKEN": "<PRIVATE_KEY_GITLAB>",
55-
"User-Agent": "python-requests/2.10.0"
66+
"User-Agent": "python-requests/2.22.0"
5667
},
5768
"method": "GET",
58-
"uri": "https://gitlab.com/api/v3/groups?search=<GITLAB_NAMESPACE>"
69+
"uri": "https://gitlab.com/api/v4/groups?search=<GITLAB_NAMESPACE>"
5970
},
6071
"response": {
6172
"body": {
6273
"encoding": null,
63-
"string": "[]"
74+
"string": "[{\"id\":938543,\"web_url\":\"https://gitlab.com/groups/<GITLAB_NAMESPACE>\",\"name\":\"<GITLAB_NAMESPACE>\",\"path\":\"<GITLAB_NAMESPACE>\",\"description\":\"\",\"visibility\":\"public\",\"lfs_enabled\":true,\"avatar_url\":null,\"request_access_enabled\":true,\"full_name\":\"<GITLAB_NAMESPACE>\",\"full_path\":\"<GITLAB_NAMESPACE>\",\"parent_id\":null,\"ldap_cn\":null,\"ldap_access\":null}]"
6475
},
6576
"headers": {
6677
"Cache-Control": "max-age=0, private, must-revalidate",
67-
"Content-Length": "2",
78+
"Content-Length": "326",
6879
"Content-Type": "application/json",
69-
"Date": "Sat, 15 Oct 2016 20:46:52 GMT",
70-
"Etag": "W/\"d751713988987e9331980363e24189ce\"",
71-
"Link": "<https://gitlab.com/api/v3/groups?page=1&per_page=20&search=<GITLAB_NAMESPACE>>; rel=\"first\", <https://gitlab.com/api/v3/groups?page=0&per_page=20&search=<GITLAB_NAMESPACE>>; rel=\"last\"",
80+
"Date": "Mon, 26 Aug 2019 21:13:22 GMT",
81+
"Etag": "W/\"b808f34f76127c9de5593aaed1627c7c\"",
82+
"Link": "<https://gitlab.com/api/v4/groups?order_by=name&owned=false&page=1&per_page=20&search=<GITLAB_NAMESPACE>&sort=asc&statistics=false&with_custom_attributes=false>; rel=\"first\", <https://gitlab.com/api/v4/groups?order_by=name&owned=false&page=1&per_page=20&search=<GITLAB_NAMESPACE>&sort=asc&statistics=false&with_custom_attributes=false>; rel=\"last\"",
83+
"RateLimit-Limit": "600",
84+
"RateLimit-Observed": "3",
85+
"RateLimit-Remaining": "597",
86+
"RateLimit-Reset": "1566854062",
87+
"RateLimit-ResetTime": "Mon, 26 Aug 2019 21:14:22 GMT",
88+
"Referrer-Policy": "strict-origin-when-cross-origin",
7289
"Server": "nginx",
90+
"Strict-Transport-Security": "max-age=31536000",
7391
"Vary": "Origin",
92+
"X-Content-Type-Options": "nosniff",
93+
"X-Frame-Options": "SAMEORIGIN",
7494
"X-Next-Page": "",
7595
"X-Page": "1",
7696
"X-Per-Page": "20",
7797
"X-Prev-Page": "",
78-
"X-Request-Id": "b03eada5-dde3-47d7-973a-880905f9965b",
79-
"X-Runtime": "0.024603",
80-
"X-Total": "0",
81-
"X-Total-Pages": "0"
98+
"X-Request-Id": "mvsifxECVP7",
99+
"X-Runtime": "0.050724",
100+
"X-Total": "1",
101+
"X-Total-Pages": "1"
82102
},
83103
"status": {
84104
"code": 200,
85105
"message": "OK"
86106
},
87-
"url": "https://gitlab.com/api/v3/groups?search=<GITLAB_NAMESPACE>"
107+
"url": "https://gitlab.com/api/v4/groups?search=<GITLAB_NAMESPACE>"
88108
}
89109
},
90110
{
91-
"recorded_at": "2016-10-15T20:46:52",
111+
"recorded_at": "2019-08-26T21:13:22",
92112
"request": {
93113
"body": {
94114
"encoding": "utf-8",
95-
"string": "{\"name\": \"git-repo\"}"
115+
"string": "{\"name\": \"git-repo\", \"namespace_id\": 938543}"
96116
},
97117
"headers": {
98118
"Accept": "*/*",
99119
"Accept-Encoding": "identity",
100120
"Connection": "keep-alive",
101-
"Content-Length": "20",
121+
"Content-Length": "44",
102122
"Content-type": "application/json",
103123
"PRIVATE-TOKEN": "<PRIVATE_KEY_GITLAB>",
104-
"User-Agent": "python-requests/2.10.0"
124+
"User-Agent": "python-requests/2.22.0"
105125
},
106126
"method": "POST",
107-
"uri": "https://gitlab.com/api/v3/projects"
127+
"uri": "https://gitlab.com/api/v4/projects"
108128
},
109129
"response": {
110130
"body": {
@@ -115,17 +135,24 @@
115135
"Cache-Control": "no-cache",
116136
"Content-Length": "100",
117137
"Content-Type": "application/json",
118-
"Date": "Sat, 15 Oct 2016 20:46:52 GMT",
138+
"Date": "Mon, 26 Aug 2019 21:13:22 GMT",
139+
"RateLimit-Limit": "600",
140+
"RateLimit-Observed": "4",
141+
"RateLimit-Remaining": "596",
142+
"RateLimit-Reset": "1566854062",
143+
"RateLimit-ResetTime": "Mon, 26 Aug 2019 21:14:22 GMT",
119144
"Server": "nginx",
120145
"Vary": "Origin",
121-
"X-Request-Id": "30d60bc8-de0c-45fa-abc5-b8547bdbe283",
122-
"X-Runtime": "0.069274"
146+
"X-Content-Type-Options": "nosniff",
147+
"X-Frame-Options": "SAMEORIGIN",
148+
"X-Request-Id": "skz73zlMLW4",
149+
"X-Runtime": "0.182593"
123150
},
124151
"status": {
125152
"code": 400,
126153
"message": "Bad Request"
127154
},
128-
"url": "https://gitlab.com/api/v3/projects"
155+
"url": "https://gitlab.com/api/v4/projects"
129156
}
130157
}
131158
],

0 commit comments

Comments
 (0)