/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/gitlab/hoster.py

  • Committer: Jelmer Vernooij
  • Date: 2020-07-28 00:32:38 UTC
  • mfrom: (7490.40.77 work)
  • mto: (7490.40.79 work)
  • mto: This revision was merged to the branch mainline in revision 7521.
  • Revision ID: jelmer@jelmer.uk-20200728003238-vx5u412hn72f18lr
Merge lp:brz/3.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
        self.error = error
101
101
 
102
102
 
 
103
class GitLabConflict(errors.BzrError):
 
104
 
 
105
    _fmt = "Conflict during operation: %(reason)s"
 
106
 
 
107
    def __init__(self, reason):
 
108
        errors.BzrError(self, reason=reason)
 
109
 
 
110
 
103
111
class ForkingDisabled(errors.BzrError):
104
112
 
105
113
    _fmt = ("Forking on project %(project)s is disabled.")
338
346
            raise KeyError('no such user %s' % username)
339
347
        if response.status == 200:
340
348
            return json.loads(response.data)
341
 
        raise errors.InvalidHttpResponse(path, response.text)
 
349
        raise errors.UnexpectedHttpStatus(path, response.status)
342
350
 
343
351
    def _get_user_by_email(self, email):
344
352
        path = 'users?search=%s' % urlutils.quote(str(email), '')
350
358
            if len(ret) != 1:
351
359
                raise ValueError('unexpected number of results; %r' % ret)
352
360
            return ret[0]
353
 
        raise errors.InvalidHttpResponse(path, response.text)
 
361
        raise errors.UnexpectedHttpStatus(path, response.status)
354
362
 
355
363
    def _get_project(self, project_name):
356
364
        path = 'projects/%s' % urlutils.quote(str(project_name), '')
359
367
            raise NoSuchProject(project_name)
360
368
        if response.status == 200:
361
369
            return json.loads(response.data)
362
 
        raise errors.InvalidHttpResponse(path, response.text)
 
370
        raise errors.UnexpectedHttpStatus(path, response.status)
363
371
 
364
 
    def _fork_project(self, project_name, timeout=50, interval=5):
 
372
    def _fork_project(self, project_name, timeout=50, interval=5, owner=None):
365
373
        path = 'projects/%s/fork' % urlutils.quote(str(project_name), '')
366
 
        response = self._api_request('POST', path)
 
374
        fields = {}
 
375
        if owner is not None:
 
376
            fields['namespace'] = owner
 
377
        response = self._api_request('POST', path, fields=fields)
367
378
        if response.status == 404:
368
379
            raise ForkingDisabled(project_name)
 
380
        if response.status == 409:
 
381
            resp = json.loads(response.data)
 
382
            raise GitLabConflict(resp.get('message'))
369
383
        if response.status not in (200, 201):
370
 
            raise errors.InvalidHttpResponse(path, response.text)
 
384
            raise errors.UnexpectedHttpStatus(path, response.status)
371
385
        # The response should be valid JSON, but let's ignore it
372
386
        project = json.loads(response.data)
373
387
        # Spin and wait until import_status for new project
381
395
            project = self._get_project(project['path_with_namespace'])
382
396
        return project
383
397
 
384
 
    def _get_logged_in_username(self):
 
398
    def get_current_user(self):
385
399
        return self._current_user['username']
386
400
 
 
401
    def get_user_url(self, username):
 
402
        return urlutils.join(self.base_url, username)
 
403
 
387
404
    def _list_paged(self, path, parameters=None, per_page=None):
388
405
        if parameters is None:
389
406
            parameters = {}
400
417
            if response.status == 403:
401
418
                raise errors.PermissionDenied(response.text)
402
419
            if response.status != 200:
403
 
                raise errors.InvalidHttpResponse(path, response.text)
 
420
                raise errors.UnexpectedHttpStatus(path, response.status)
404
421
            page = response.getheader("X-Next-Page")
405
422
            for entry in json.loads(response.data):
406
423
                yield entry
423
440
        if response.status == 403:
424
441
            raise errors.PermissionDenied(response.text)
425
442
        if response.status != 200:
426
 
            raise errors.InvalidHttpResponse(path, response.text)
 
443
            raise errors.UnexpectedHttpStatus(path, response.status)
427
444
        return json.loads(response.data)
428
445
 
429
446
    def _list_projects(self, owner):
437
454
        response = self._api_request('PUT', path, fields=mr)
438
455
        if response.status == 200:
439
456
            return json.loads(response.data)
440
 
        raise errors.InvalidHttpResponse(path, response.text)
 
457
        raise errors.UnexpectedHttpStatus(path, response.status)
441
458
 
442
459
    def _post_merge_request_note(self, project_id, iid, kwargs):
443
460
        path = 'projects/%s/merge_requests/%s/notes' % (
446
463
        if response.status == 201:
447
464
            json.loads(response.data)
448
465
            return
449
 
        raise errors.InvalidHttpResponse(path, response.text)
 
466
        raise errors.UnexpectedHttpStatus(path, response.status)
450
467
 
451
468
    def _create_mergerequest(
452
469
            self, title, source_project_id, target_project_id,
469
486
        if response.status == 409:
470
487
            raise MergeRequestExists()
471
488
        if response.status != 201:
472
 
            raise errors.InvalidHttpResponse(path, response.text)
 
489
            raise errors.UnexpectedHttpStatus(path, response.status)
473
490
        return json.loads(response.data)
474
491
 
475
492
    def get_push_url(self, branch):
483
500
                        allow_lossy=True, tag_selector=None):
484
501
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
485
502
        if owner is None:
486
 
            owner = self._get_logged_in_username()
 
503
            owner = self.get_current_user()
487
504
        if project is None:
488
505
            project = self._get_project(base_project)['path']
489
506
        try:
490
507
            target_project = self._get_project('%s/%s' % (owner, project))
491
508
        except NoSuchProject:
492
 
            target_project = self._fork_project(base_project)
 
509
            target_project = self._fork_project(base_project, owner=owner)
493
510
        remote_repo_url = git_url_to_bzr_url(target_project['ssh_url_to_repo'])
494
511
        remote_dir = controldir.ControlDir.open(remote_repo_url)
495
512
        try:
509
526
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
510
527
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
511
528
        if owner is None:
512
 
            owner = self._get_logged_in_username()
 
529
            owner = self.get_current_user()
513
530
        if project is None:
514
531
            project = self._get_project(base_project)['path']
515
532
        try:
558
575
                raise GitLabLoginMissing()
559
576
            else:
560
577
                raise GitlabLoginError(response.text)
561
 
        raise UnsupportedHoster(url)
 
578
        raise UnsupportedHoster(self.base_url)
562
579
 
563
580
    @classmethod
564
581
    def probe_from_url(cls, url, possible_transports=None):
585
602
    def iter_my_proposals(self, status='open'):
586
603
        state = mp_status_to_status(status)
587
604
        for mp in self._list_merge_requests(
588
 
                owner=self._get_logged_in_username(), state=state):
 
605
                owner=self.get_current_user(), state=state):
589
606
            yield GitLabMergeProposal(self, mp)
590
607
 
591
608
    def iter_my_forks(self):
592
 
        for project in self._list_projects(owner=self._get_logged_in_username()):
 
609
        for project in self._list_projects(owner=self.get_current_user()):
593
610
            base_project = project.get('forked_from_project')
594
611
            if not base_project:
595
612
                continue
617
634
        if response.status == 404:
618
635
            raise NoSuchProject(project)
619
636
        if response.status != 202:
620
 
            raise errors.InvalidHttpResponse(path, response.text)
 
637
            raise errors.UnexpectedHttpStatus(path, response.status)
621
638
 
622
639
 
623
640
class GitlabMergeProposalBuilder(MergeProposalBuilder):