/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
39
39
    MergeProposalExists,
40
40
    NoSuchProject,
41
41
    PrerequisiteBranchUnsupported,
 
42
    SourceNotDerivedFromTarget,
42
43
    UnsupportedHoster,
43
44
    )
44
45
 
74
75
        self.url = url
75
76
 
76
77
 
 
78
class GitLabUnprocessable(errors.BzrError):
 
79
 
 
80
    _fmt = "GitLab can not process request: %(error)s."
 
81
 
 
82
    def __init__(self, error):
 
83
        errors.BzrError.__init__(self, error=error)
 
84
 
 
85
 
77
86
class DifferentGitLabInstances(errors.BzrError):
78
87
 
79
88
    _fmt = ("Can't create merge proposals across GitLab instances: "
193
202
    return host, project_name, int(parts[-1])
194
203
 
195
204
 
 
205
def _unexpected_status(path, response):
 
206
    raise errors.UnexpectedHttpStatus(
 
207
        path, response.status, response.data.decode('utf-8', 'replace'))
 
208
 
 
209
 
196
210
class GitLabMergeProposal(MergeProposal):
197
211
 
198
212
    def __init__(self, gl, mr):
341
355
            raise KeyError('no such user %s' % username)
342
356
        if response.status == 200:
343
357
            return json.loads(response.data)
344
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
358
        _unexpected_status(path, response)
345
359
 
346
360
    def _get_user_by_email(self, email):
347
361
        path = 'users?search=%s' % urlutils.quote(str(email), '')
353
367
            if len(ret) != 1:
354
368
                raise ValueError('unexpected number of results; %r' % ret)
355
369
            return ret[0]
356
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
370
        _unexpected_status(path, response)
357
371
 
358
372
    def _get_project(self, project_name):
359
373
        path = 'projects/%s' % urlutils.quote(str(project_name), '')
362
376
            raise NoSuchProject(project_name)
363
377
        if response.status == 200:
364
378
            return json.loads(response.data)
365
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
379
        _unexpected_status(path, response)
366
380
 
367
381
    def _fork_project(self, project_name, timeout=50, interval=5, owner=None):
368
382
        path = 'projects/%s/fork' % urlutils.quote(str(project_name), '')
376
390
            resp = json.loads(response.data)
377
391
            raise GitLabConflict(resp.get('message'))
378
392
        if response.status not in (200, 201):
379
 
            raise errors.UnexpectedHttpStatus(path, response.status)
 
393
            _unexpected_status(path, response)
380
394
        # The response should be valid JSON, but let's ignore it
381
395
        project = json.loads(response.data)
382
396
        # Spin and wait until import_status for new project
412
426
            if response.status == 403:
413
427
                raise errors.PermissionDenied(response.text)
414
428
            if response.status != 200:
415
 
                raise errors.UnexpectedHttpStatus(path, response.status)
 
429
                _unexpected_status(path, response)
416
430
            page = response.getheader("X-Next-Page")
417
431
            for entry in json.loads(response.data):
418
432
                yield entry
435
449
        if response.status == 403:
436
450
            raise errors.PermissionDenied(response.text)
437
451
        if response.status != 200:
438
 
            raise errors.UnexpectedHttpStatus(path, response.status)
 
452
            _unexpected_status(path, response)
439
453
        return json.loads(response.data)
440
454
 
441
455
    def _list_projects(self, owner):
449
463
        response = self._api_request('PUT', path, fields=mr)
450
464
        if response.status == 200:
451
465
            return json.loads(response.data)
452
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
466
        if response.status == 403:
 
467
            raise errors.PermissionDenied(response.text)
 
468
        _unexpected_status(path, response)
453
469
 
454
470
    def _post_merge_request_note(self, project_id, iid, kwargs):
455
471
        path = 'projects/%s/merge_requests/%s/notes' % (
458
474
        if response.status == 201:
459
475
            json.loads(response.data)
460
476
            return
461
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
477
        if response.status == 403:
 
478
            raise errors.PermissionDenied(response.text)
 
479
        _unexpected_status(path, response)
462
480
 
463
481
    def _create_mergerequest(
464
482
            self, title, source_project_id, target_project_id,
480
498
            raise errors.PermissionDenied(response.text)
481
499
        if response.status == 409:
482
500
            raise MergeRequestExists()
 
501
        if response.status == 422:
 
502
            data = json.loads(response.data)
 
503
            raise GitLabUnprocessable(data['error'])
483
504
        if response.status != 201:
484
 
            raise errors.UnexpectedHttpStatus(path, response.status)
 
505
            _unexpected_status(path, response)
485
506
        return json.loads(response.data)
486
507
 
487
508
    def get_push_url(self, branch):
629
650
        if response.status == 404:
630
651
            raise NoSuchProject(project)
631
652
        if response.status != 202:
632
 
            raise errors.UnexpectedHttpStatus(path, response.status)
 
653
            _unexpected_status(path, response)
633
654
 
634
655
 
635
656
class GitlabMergeProposalBuilder(MergeProposalBuilder):
698
719
            merge_request = self.gl._create_mergerequest(**kwargs)
699
720
        except MergeRequestExists:
700
721
            raise MergeProposalExists(self.source_branch.user_url)
 
722
        except GitLabUnprocessable as e:
 
723
            if e.error == [
 
724
                    "Source project is not a fork of the target project"]:
 
725
                raise SourceNotDerivedFromTarget(
 
726
                    self.source_branch, self.target_branch)
701
727
        return GitLabMergeProposal(self.gl, merge_request)
702
728
 
703
729