/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-06-11 12:46:45 UTC
  • mfrom: (7511.1.1 actions-no-fork)
  • Revision ID: breezy.the.bot@gmail.com-20200611124645-4lx66gps99i0hmzh
Avoid using fork when running the testsuite in github actions.

Merged from https://code.launchpad.net/~jelmer/brz/actions-no-fork/+merge/385565

Show diffs side-by-side

added added

removed removed

Lines of Context:
97
97
        self.error = error
98
98
 
99
99
 
100
 
class GitLabConflict(errors.BzrError):
101
 
 
102
 
    _fmt = "Conflict during operation: %(reason)s"
103
 
 
104
 
    def __init__(self, reason):
105
 
        errors.BzrError(self, reason=reason)
106
 
 
107
 
 
108
 
class ForkingDisabled(errors.BzrError):
109
 
 
110
 
    _fmt = ("Forking on project %(project)s is disabled.")
111
 
 
112
 
    def __init__(self, project):
113
 
        self.project = project
114
 
 
115
 
 
116
100
class MergeRequestExists(Exception):
117
101
    """Raised when a merge requests already exists."""
118
102
 
231
215
        return self._branch_url_from_project(
232
216
            self._mr['source_project_id'], self._mr['source_branch'])
233
217
 
234
 
    def get_source_revision(self):
235
 
        from breezy.git.mapping import default_mapping
236
 
        sha = self._mr['sha']
237
 
        if sha is None:
238
 
            return None
239
 
        return default_mapping.revision_id_foreign_to_bzr(sha.encode('ascii'))
240
 
 
241
218
    def get_target_branch_url(self):
242
219
        return self._branch_url_from_project(
243
220
            self._mr['target_project_id'], self._mr['target_branch'])
295
272
        import iso8601
296
273
        return iso8601.parse_date(merged_at)
297
274
 
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)
302
 
 
303
275
 
304
276
def gitlab_url_to_bzr_url(url, name):
305
277
    return git_url_to_bzr_url(url, branch=name)
341
313
            raise KeyError('no such user %s' % username)
342
314
        if response.status == 200:
343
315
            return json.loads(response.data)
344
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
316
        raise errors.InvalidHttpResponse(path, response.text)
345
317
 
346
318
    def _get_user_by_email(self, email):
347
319
        path = 'users?search=%s' % urlutils.quote(str(email), '')
353
325
            if len(ret) != 1:
354
326
                raise ValueError('unexpected number of results; %r' % ret)
355
327
            return ret[0]
356
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
328
        raise errors.InvalidHttpResponse(path, response.text)
357
329
 
358
330
    def _get_project(self, project_name):
359
331
        path = 'projects/%s' % urlutils.quote(str(project_name), '')
362
334
            raise NoSuchProject(project_name)
363
335
        if response.status == 200:
364
336
            return json.loads(response.data)
365
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
337
        raise errors.InvalidHttpResponse(path, response.text)
366
338
 
367
 
    def _fork_project(self, project_name, timeout=50, interval=5, owner=None):
 
339
    def _fork_project(self, project_name, timeout=50, interval=5):
368
340
        path = 'projects/%s/fork' % urlutils.quote(str(project_name), '')
369
 
        fields = {}
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'))
 
341
        response = self._api_request('POST', path)
378
342
        if response.status not in (200, 201):
379
 
            raise errors.UnexpectedHttpStatus(path, response.status)
 
343
            raise errors.InvalidHttpResponse(path, response.text)
380
344
        # The response should be valid JSON, but let's ignore it
381
345
        project = json.loads(response.data)
382
346
        # Spin and wait until import_status for new project
390
354
            project = self._get_project(project['path_with_namespace'])
391
355
        return project
392
356
 
393
 
    def get_current_user(self):
 
357
    def _get_logged_in_username(self):
394
358
        return self._current_user['username']
395
359
 
396
 
    def get_user_url(self, username):
397
 
        return urlutils.join(self.base_url, username)
398
 
 
399
360
    def _list_paged(self, path, parameters=None, per_page=None):
400
361
        if parameters is None:
401
362
            parameters = {}
412
373
            if response.status == 403:
413
374
                raise errors.PermissionDenied(response.text)
414
375
            if response.status != 200:
415
 
                raise errors.UnexpectedHttpStatus(path, response.status)
 
376
                raise errors.InvalidHttpResponse(path, response.text)
416
377
            page = response.getheader("X-Next-Page")
417
378
            for entry in json.loads(response.data):
418
379
                yield entry
435
396
        if response.status == 403:
436
397
            raise errors.PermissionDenied(response.text)
437
398
        if response.status != 200:
438
 
            raise errors.UnexpectedHttpStatus(path, response.status)
 
399
            raise errors.InvalidHttpResponse(path, response.text)
439
400
        return json.loads(response.data)
440
401
 
441
402
    def _list_projects(self, owner):
449
410
        response = self._api_request('PUT', path, fields=mr)
450
411
        if response.status == 200:
451
412
            return json.loads(response.data)
452
 
        raise errors.UnexpectedHttpStatus(path, response.status)
453
 
 
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)
460
 
            return
461
 
        raise errors.UnexpectedHttpStatus(path, response.status)
 
413
        raise errors.InvalidHttpResponse(path, response.text)
462
414
 
463
415
    def _create_mergerequest(
464
416
            self, title, source_project_id, target_project_id,
481
433
        if response.status == 409:
482
434
            raise MergeRequestExists()
483
435
        if response.status != 201:
484
 
            raise errors.UnexpectedHttpStatus(path, response.status)
 
436
            raise errors.InvalidHttpResponse(path, response.text)
485
437
        return json.loads(response.data)
486
438
 
487
439
    def get_push_url(self, branch):
495
447
                        allow_lossy=True, tag_selector=None):
496
448
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
497
449
        if owner is None:
498
 
            owner = self.get_current_user()
 
450
            owner = self._get_logged_in_username()
499
451
        if project is None:
500
452
            project = self._get_project(base_project)['path']
501
453
        try:
502
454
            target_project = self._get_project('%s/%s' % (owner, project))
503
455
        except NoSuchProject:
504
 
            target_project = self._fork_project(base_project, owner=owner)
 
456
            target_project = self._fork_project(base_project)
505
457
        remote_repo_url = git_url_to_bzr_url(target_project['ssh_url_to_repo'])
506
458
        remote_dir = controldir.ControlDir.open(remote_repo_url)
507
459
        try:
521
473
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
522
474
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
523
475
        if owner is None:
524
 
            owner = self.get_current_user()
 
476
            owner = self._get_logged_in_username()
525
477
        if project is None:
526
478
            project = self._get_project(base_project)['path']
527
479
        try:
570
522
                raise GitLabLoginMissing()
571
523
            else:
572
524
                raise GitlabLoginError(response.text)
573
 
        raise UnsupportedHoster(self.base_url)
 
525
        raise UnsupportedHoster(url)
574
526
 
575
527
    @classmethod
576
528
    def probe_from_url(cls, url, possible_transports=None):
597
549
    def iter_my_proposals(self, status='open'):
598
550
        state = mp_status_to_status(status)
599
551
        for mp in self._list_merge_requests(
600
 
                owner=self.get_current_user(), state=state):
 
552
                owner=self._get_logged_in_username(), state=state):
601
553
            yield GitLabMergeProposal(self, mp)
602
554
 
603
555
    def iter_my_forks(self):
604
 
        for project in self._list_projects(owner=self.get_current_user()):
 
556
        for project in self._list_projects(owner=self._get_logged_in_username()):
605
557
            base_project = project.get('forked_from_project')
606
558
            if not base_project:
607
559
                continue
629
581
        if response.status == 404:
630
582
            raise NoSuchProject(project)
631
583
        if response.status != 202:
632
 
            raise errors.UnexpectedHttpStatus(path, response.status)
 
584
            raise errors.InvalidHttpResponse(path, response.text)
633
585
 
634
586
 
635
587
class GitlabMergeProposalBuilder(MergeProposalBuilder):