/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/propose/gitlabs.py

  • Committer: Jelmer Vernooij
  • Date: 2019-01-02 20:22:45 UTC
  • mfrom: (0.431.63 trunk)
  • mto: This revision was merged to the branch mainline in revision 7239.
  • Revision ID: jelmer@jelmer.uk-20190102202245-i317zf85xj5i94j6
Merge support for 'brz my-proposals'.

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
        config.write(f)
84
84
 
85
85
 
 
86
def iter_tokens():
 
87
    import configparser
 
88
    from gitlab.config import _DEFAULT_FILES
 
89
    config = configparser.ConfigParser()
 
90
    config.read(_DEFAULT_FILES + [default_config_path()])
 
91
    for name, section in config.items():
 
92
        yield name, section
 
93
 
 
94
 
86
95
def connect_gitlab(host):
87
 
    from gitlab import Gitlab
 
96
    from gitlab import Gitlab, GitlabGetError
88
97
    auth = AuthenticationConfig()
89
98
 
90
99
    url = 'https://%s' % host
91
100
    credentials = auth.get_credentials('https', host)
92
101
    if credentials is None:
93
 
        import gitlab
94
 
        import configparser
95
 
        from gitlab.config import _DEFAULT_FILES
96
 
        config = configparser.ConfigParser()
97
 
        config.read(_DEFAULT_FILES + [default_config_path()])
98
 
        for name, section in config.items():
 
102
        for name, section in iter_tokens():
99
103
            if section.get('url') == url:
100
104
                credentials = section
101
105
                break
102
106
        else:
103
107
            try:
104
108
                return Gitlab(url)
105
 
            except gitlab.GitlabGetError:
 
109
            except GitlabGetError:
106
110
                raise GitLabLoginMissing()
107
111
    else:
108
112
        credentials['url'] = url
234
238
        return GitlabMergeProposalBuilder(self.gl, source_branch, target_branch)
235
239
 
236
240
    def get_proposal(self, source_branch, target_branch):
 
241
        import gitlab
237
242
        (source_host, source_project_name, source_branch_name) = (
238
243
            parse_gitlab_url(source_branch))
239
244
        (target_host, target_project_name, target_branch_name) = (
254
259
                return GitLabMergeProposal(mr)
255
260
        except gitlab.GitlabListError as e:
256
261
            if e.response_code == 403:
257
 
                raise PermissionDenied(e.error_message)
 
262
                raise errors.PermissionDenied(e.error_message)
258
263
        raise NoMergeProposal()
259
264
 
260
265
    def hosts(self, branch):
287
292
                raise
288
293
        return cls(gl)
289
294
 
 
295
    @classmethod
 
296
    def iter_instances(cls):
 
297
        from gitlab import Gitlab
 
298
        for name, credentials in iter_tokens():
 
299
            if 'url' not in credentials:
 
300
                continue
 
301
            gl = Gitlab(**credentials)
 
302
            yield cls(gl)
 
303
 
 
304
    def iter_my_proposals(self):
 
305
        self.gl.auth()
 
306
        for mp in self.gl.mergerequests.list(owner=self.gl.user.username):
 
307
            yield GitLabMergeProposal(mp)
 
308
 
290
309
 
291
310
class GitlabMergeProposalBuilder(MergeProposalBuilder):
292
311
 
343
362
            merge_request = source_project.mergerequests.create(kwargs)
344
363
        except gitlab.GitlabCreateError as e:
345
364
            if e.response_code == 403:
346
 
                raise PermissionDenied(e.error_message)
 
365
                raise errors.PermissionDenied(e.error_message)
347
366
            if e.response_code == 409:
348
367
                raise MergeProposalExists(self.source_branch.user_url)
349
368
            raise
350
369
        return GitLabMergeProposal(merge_request)
 
370
 
 
371
 
 
372
def register_gitlab_instance(shortname, url):
 
373
    """Register a gitlab instance.
 
374
 
 
375
    :param shortname: Short name (e.g. "gitlab")
 
376
    :param url: URL to the gitlab instance
 
377
    """
 
378
    from breezy.bugtracker import (
 
379
        tracker_registry,
 
380
        ProjectIntegerBugTracker,
 
381
        )
 
382
    tracker_registry.register(
 
383
        shortname, ProjectIntegerBugTracker(
 
384
            shortname, url + '/{project}/issues/{id}'))