/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-03-02 22:31:28 UTC
  • mfrom: (7291 work)
  • mto: This revision was merged to the branch mainline in revision 7293.
  • Revision ID: jelmer@jelmer.uk-20190302223128-0qk1i5tozmzq5nyq
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
    errors,
25
25
    urlutils,
26
26
    )
27
 
from ...config import AuthenticationConfig
28
27
from ...git.urls import git_url_to_bzr_url
29
28
from ...sixish import PY3
30
29
 
100
99
 
101
100
def connect_gitlab(host):
102
101
    from gitlab import Gitlab, GitlabGetError
103
 
    auth = AuthenticationConfig()
104
 
 
105
102
    url = 'https://%s' % host
106
 
    credentials = auth.get_credentials('https', host)
107
 
    if credentials is None:
108
 
        for name, section in iter_tokens():
109
 
            if section.get('url') == url:
110
 
                credentials = section
111
 
                break
112
 
        else:
113
 
            try:
114
 
                return Gitlab(url)
115
 
            except GitlabGetError:
116
 
                raise GitLabLoginMissing()
 
103
    for name, section in iter_tokens():
 
104
        if section.get('url') == url:
 
105
            return Gitlab(**section)
117
106
    else:
118
 
        credentials['url'] = url
119
 
    return Gitlab(**credentials)
120
 
 
121
 
 
122
 
def parse_gitlab_url(branch):
123
 
    url = urlutils.split_segment_parameters(branch.user_url)[0]
 
107
        try:
 
108
            return Gitlab(url)
 
109
        except GitlabGetError:
 
110
            raise GitLabLoginMissing()
 
111
 
 
112
 
 
113
def parse_gitlab_url(url):
124
114
    (scheme, user, password, host, port, path) = urlutils.parse_url(
125
115
        url)
126
116
    if scheme not in ('git+ssh', 'https', 'http'):
127
 
        raise NotGitLabUrl(branch.user_url)
 
117
        raise NotGitLabUrl(url)
128
118
    if not host:
129
 
        raise NotGitLabUrl(branch.user_url)
 
119
        raise NotGitLabUrl(url)
130
120
    path = path.strip('/')
131
121
    if path.endswith('.git'):
132
122
        path = path[:-4]
 
123
    return host, path
 
124
 
 
125
 
 
126
def parse_gitlab_branch_url(branch):
 
127
    url = urlutils.split_segment_parameters(branch.user_url)[0]
 
128
    host, path = parse_gitlab_url(url)
133
129
    return host, path, branch.name
134
130
 
135
131
 
147
143
 
148
144
    def set_description(self, description):
149
145
        self._mr.description = description
 
146
        self._mr.save()
150
147
 
151
148
    def _branch_url_from_project(self, project_id, branch_name):
152
149
        project = self._mr.manager.gitlab.projects.get(project_id)
191
188
        self.gl = gl
192
189
 
193
190
    def get_push_url(self, branch):
194
 
        (host, project_name, branch_name) = parse_gitlab_url(branch)
 
191
        (host, project_name, branch_name) = parse_gitlab_branch_url(branch)
195
192
        project = self.gl.projects.get(project_name)
196
193
        return gitlab_url_to_bzr_url(
197
194
            project.ssh_url_to_repo, branch_name)
200
197
                        owner=None, revision_id=None, overwrite=False,
201
198
                        allow_lossy=True):
202
199
        import gitlab
203
 
        (host, base_project, base_branch_name) = parse_gitlab_url(base_branch)
 
200
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
204
201
        self.gl.auth()
205
202
        try:
206
203
            base_project = self.gl.projects.get(base_project)
238
235
 
239
236
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
240
237
        import gitlab
241
 
        (host, base_project, base_branch_name) = parse_gitlab_url(base_branch)
 
238
        (host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
242
239
        self.gl.auth()
243
240
        try:
244
241
            base_project = self.gl.projects.get(base_project)
266
263
    def iter_proposals(self, source_branch, target_branch, status):
267
264
        import gitlab
268
265
        (source_host, source_project_name, source_branch_name) = (
269
 
            parse_gitlab_url(source_branch))
 
266
            parse_gitlab_branch_url(source_branch))
270
267
        (target_host, target_project_name, target_branch_name) = (
271
 
            parse_gitlab_url(target_branch))
 
268
            parse_gitlab_branch_url(target_branch))
272
269
        if source_host != target_host:
273
270
            raise DifferentGitLabInstances(source_host, target_host)
274
271
        self.gl.auth()
289
286
 
290
287
    def hosts(self, branch):
291
288
        try:
292
 
            (host, project, branch_name) = parse_gitlab_url(branch)
 
289
            (host, project, branch_name) = parse_gitlab_branch_url(branch)
293
290
        except NotGitLabUrl:
294
291
            return False
295
292
        return (self.gl.url == ('https://%s' % host))
296
293
 
297
294
    @classmethod
298
 
    def probe(cls, branch):
 
295
    def probe_from_url(cls, url):
299
296
        try:
300
 
            (host, project, branch_name) = parse_gitlab_url(branch)
 
297
            (host, project) = parse_gitlab_url(url)
301
298
        except NotGitLabUrl:
302
 
            raise UnsupportedHoster(branch)
 
299
            raise UnsupportedHoster(url)
303
300
        import gitlab
304
301
        import requests.exceptions
305
302
        try:
307
304
            gl.auth()
308
305
        except requests.exceptions.SSLError:
309
306
            # Well, I guess it could be..
310
 
            raise UnsupportedHoster(branch)
 
307
            raise UnsupportedHoster(url)
311
308
        except gitlab.GitlabGetError:
312
 
            raise UnsupportedHoster(branch)
 
309
            raise UnsupportedHoster(url)
313
310
        except gitlab.GitlabHttpError as e:
314
311
            if e.response_code in (404, 405, 503):
315
 
                raise UnsupportedHoster(branch)
 
312
                raise UnsupportedHoster(url)
316
313
            else:
317
314
                raise
318
315
        return cls(gl)
340
337
        self.gl = gl
341
338
        self.source_branch = source_branch
342
339
        (self.source_host, self.source_project_name, self.source_branch_name) = (
343
 
            parse_gitlab_url(source_branch))
 
340
            parse_gitlab_branch_url(source_branch))
344
341
        self.target_branch = target_branch
345
342
        (self.target_host, self.target_project_name, self.target_branch_name) = (
346
 
            parse_gitlab_url(target_branch))
 
343
            parse_gitlab_branch_url(target_branch))
347
344
        if self.source_host != self.target_host:
348
345
            raise DifferentGitLabInstances(self.source_host, self.target_host)
349
346