100
97
self.error = error
100
class GitLabConflict(errors.BzrError):
102
_fmt = "Conflict during operation: %(reason)s"
104
def __init__(self, reason):
105
errors.BzrError(self, reason=reason)
108
class ForkingDisabled(errors.BzrError):
110
_fmt = ("Forking on project %(project)s is disabled.")
112
def __init__(self, project):
113
self.project = project
103
116
class MergeRequestExists(Exception):
104
117
"""Raised when a merge requests already exists."""
169
182
raise NotGitLabUrl(url)
170
183
path = path.strip('/')
171
184
parts = path.split('/')
186
raise NotMergeRequestUrl(host, url)
172
187
if parts[-2] != 'merge_requests':
173
188
raise NotMergeRequestUrl(host, url)
174
return host, '/'.join(parts[:-2]), int(parts[-1])
190
project_name = '/'.join(parts[:-3])
192
project_name = '/'.join(parts[:-2])
193
return host, project_name, int(parts[-1])
177
196
class GitLabMergeProposal(MergeProposal):
212
231
return self._branch_url_from_project(
213
232
self._mr['source_project_id'], self._mr['source_branch'])
234
def get_source_revision(self):
235
from breezy.git.mapping import default_mapping
236
sha = self._mr['sha']
239
return default_mapping.revision_id_foreign_to_bzr(sha.encode('ascii'))
215
241
def get_target_branch_url(self):
216
242
return self._branch_url_from_project(
217
243
self._mr['target_project_id'], self._mr['target_branch'])
248
274
elif self._mr['merge_status'] == 'can_be_merged':
276
elif self._mr['merge_status'] in (
277
'unchecked', 'cannot_be_merged_recheck'):
278
# See https://gitlab.com/gitlab-org/gitlab/-/commit/7517105303c for
279
# an explanation of the distinction between unchecked and
280
# cannot_be_merged_recheck
251
283
raise ValueError(self._mr['merge_status'])
264
296
return iso8601.parse_date(merged_at)
298
def post_comment(self, body):
299
kwargs = {'body': body}
300
self.gl._post_merge_request_note(
301
self._mr['project_id'], self._mr['iid'], kwargs)
267
304
def gitlab_url_to_bzr_url(url, name):
269
name = name.encode('utf-8')
270
305
return git_url_to_bzr_url(url, branch=name)
285
320
def base_url(self):
286
321
return self.transport.base
324
def base_hostname(self):
325
return urlutils.parse_url(self.base_url)[3]
288
327
def _api_request(self, method, path, fields=None, body=None):
289
328
return self.transport.request(
290
329
method, urlutils.join(self.base_url, 'api', 'v4', path),
302
341
raise KeyError('no such user %s' % username)
303
342
if response.status == 200:
304
343
return json.loads(response.data)
305
raise errors.InvalidHttpResponse(path, response.text)
344
raise errors.UnexpectedHttpStatus(path, response.status)
307
346
def _get_user_by_email(self, email):
308
347
path = 'users?search=%s' % urlutils.quote(str(email), '')
314
353
if len(ret) != 1:
315
354
raise ValueError('unexpected number of results; %r' % ret)
317
raise errors.InvalidHttpResponse(path, response.text)
356
raise errors.UnexpectedHttpStatus(path, response.status)
319
358
def _get_project(self, project_name):
320
359
path = 'projects/%s' % urlutils.quote(str(project_name), '')
323
362
raise NoSuchProject(project_name)
324
363
if response.status == 200:
325
364
return json.loads(response.data)
326
raise errors.InvalidHttpResponse(path, response.text)
365
raise errors.UnexpectedHttpStatus(path, response.status)
328
def _fork_project(self, project_name, timeout=50, interval=5):
367
def _fork_project(self, project_name, timeout=50, interval=5, owner=None):
329
368
path = 'projects/%s/fork' % urlutils.quote(str(project_name), '')
330
response = self._api_request('POST', path)
370
if owner is not None:
371
fields['namespace'] = owner
372
response = self._api_request('POST', path, fields=fields)
373
if response.status == 404:
374
raise ForkingDisabled(project_name)
375
if response.status == 409:
376
resp = json.loads(response.data)
377
raise GitLabConflict(resp.get('message'))
331
378
if response.status not in (200, 201):
332
raise errors.InvalidHttpResponse(path, response.text)
379
raise errors.UnexpectedHttpStatus(path, response.status)
333
380
# The response should be valid JSON, but let's ignore it
334
381
project = json.loads(response.data)
335
382
# Spin and wait until import_status for new project
343
390
project = self._get_project(project['path_with_namespace'])
346
def _get_logged_in_username(self):
393
def get_current_user(self):
347
394
return self._current_user['username']
396
def get_user_url(self, username):
397
return urlutils.join(self.base_url, username)
349
399
def _list_paged(self, path, parameters=None, per_page=None):
350
400
if parameters is None:
362
412
if response.status == 403:
363
413
raise errors.PermissionDenied(response.text)
364
414
if response.status != 200:
365
raise errors.InvalidHttpResponse(path, response.text)
415
raise errors.UnexpectedHttpStatus(path, response.status)
366
416
page = response.getheader("X-Next-Page")
367
417
for entry in json.loads(response.data):
379
429
parameters['owner_id'] = urlutils.quote(owner, '')
380
430
return self._list_paged(path, parameters, per_page=DEFAULT_PAGE_SIZE)
432
def _get_merge_request(self, project, merge_id):
433
path = 'projects/%s/merge_requests/%d' % (urlutils.quote(str(project), ''), merge_id)
434
response = self._api_request('GET', path)
435
if response.status == 403:
436
raise errors.PermissionDenied(response.text)
437
if response.status != 200:
438
raise errors.UnexpectedHttpStatus(path, response.status)
439
return json.loads(response.data)
382
441
def _list_projects(self, owner):
383
442
path = 'users/%s/projects' % urlutils.quote(str(owner), '')
390
449
response = self._api_request('PUT', path, fields=mr)
391
450
if response.status == 200:
392
451
return json.loads(response.data)
393
raise errors.InvalidHttpResponse(path, response.text)
452
raise errors.UnexpectedHttpStatus(path, response.status)
454
def _post_merge_request_note(self, project_id, iid, kwargs):
455
path = 'projects/%s/merge_requests/%s/notes' % (
456
urlutils.quote(str(project_id), ''), iid)
457
response = self._api_request('POST', path, fields=kwargs)
458
if response.status == 201:
459
json.loads(response.data)
461
raise errors.UnexpectedHttpStatus(path, response.status)
395
463
def _create_mergerequest(
396
464
self, title, source_project_id, target_project_id,
413
481
if response.status == 409:
414
482
raise MergeRequestExists()
415
483
if response.status != 201:
416
raise errors.InvalidHttpResponse(path, response.text)
484
raise errors.UnexpectedHttpStatus(path, response.status)
417
485
return json.loads(response.data)
419
487
def get_push_url(self, branch):
427
495
allow_lossy=True, tag_selector=None):
428
496
(host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
429
497
if owner is None:
430
owner = self._get_logged_in_username()
498
owner = self.get_current_user()
431
499
if project is None:
432
500
project = self._get_project(base_project)['path']
434
502
target_project = self._get_project('%s/%s' % (owner, project))
435
503
except NoSuchProject:
436
target_project = self._fork_project(base_project)
504
target_project = self._fork_project(base_project, owner=owner)
437
505
remote_repo_url = git_url_to_bzr_url(target_project['ssh_url_to_repo'])
438
506
remote_dir = controldir.ControlDir.open(remote_repo_url)
453
521
def get_derived_branch(self, base_branch, name, project=None, owner=None):
454
522
(host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
455
523
if owner is None:
456
owner = self._get_logged_in_username()
524
owner = self.get_current_user()
457
525
if project is None:
458
526
project = self._get_project(base_project)['path']
490
558
(host, project, branch_name) = parse_gitlab_branch_url(branch)
491
559
except NotGitLabUrl:
493
return (self.base_url == ('https://%s' % host))
561
return self.base_hostname == host
496
564
response = self._api_request('GET', 'user')
502
570
raise GitLabLoginMissing()
504
572
raise GitlabLoginError(response.text)
505
raise UnsupportedHoster(url)
573
raise UnsupportedHoster(self.base_url)
508
576
def probe_from_url(cls, url, possible_transports=None):
529
597
def iter_my_proposals(self, status='open'):
530
598
state = mp_status_to_status(status)
531
599
for mp in self._list_merge_requests(
532
owner=self._get_logged_in_username(), state=state):
600
owner=self.get_current_user(), state=state):
533
601
yield GitLabMergeProposal(self, mp)
535
603
def iter_my_forks(self):
536
for project in self._list_projects(owner=self._get_logged_in_username()):
604
for project in self._list_projects(owner=self.get_current_user()):
537
605
base_project = project.get('forked_from_project')
538
606
if not base_project:
545
613
except NotGitLabUrl:
546
614
raise UnsupportedHoster(url)
547
615
except NotMergeRequestUrl as e:
548
if self.base_url == ('https://%s' % e.host):
616
if self.base_hostname == e.host:
551
619
raise UnsupportedHoster(url)
552
if self.base_url != ('https://%s' % host):
620
if self.base_hostname != host:
553
621
raise UnsupportedHoster(url)
554
622
project = self._get_project(project)
555
mr = project.mergerequests.get(merge_id)
556
return GitLabMergeProposal(mr)
623
mr = self._get_merge_request(project['path_with_namespace'], merge_id)
624
return GitLabMergeProposal(self, mr)
558
626
def delete_project(self, project):
559
627
path = 'projects/%s' % urlutils.quote(str(project), '')
561
629
if response.status == 404:
562
630
raise NoSuchProject(project)
563
631
if response.status != 202:
564
raise errors.InvalidHttpResponse(path, response.text)
632
raise errors.UnexpectedHttpStatus(path, response.status)
567
635
class GitlabMergeProposalBuilder(MergeProposalBuilder):
630
698
merge_request = self.gl._create_mergerequest(**kwargs)
631
699
except MergeRequestExists:
632
raise ProposalExists(self.source_branch.user_url)
700
raise MergeProposalExists(self.source_branch.user_url)
633
701
return GitLabMergeProposal(self.gl, merge_request)