101
100
def connect_gitlab(host):
102
101
from gitlab import Gitlab, GitlabGetError
103
auth = AuthenticationConfig()
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
115
except GitlabGetError:
116
raise GitLabLoginMissing()
103
for name, section in iter_tokens():
104
if section.get('url') == url:
105
return Gitlab(**section)
118
credentials['url'] = url
119
return Gitlab(**credentials)
122
def parse_gitlab_url(branch):
123
url = urlutils.split_segment_parameters(branch.user_url)[0]
109
except GitlabGetError:
110
raise GitLabLoginMissing()
113
def parse_gitlab_url(url):
124
114
(scheme, user, password, host, port, path) = urlutils.parse_url(
126
116
if scheme not in ('git+ssh', 'https', 'http'):
127
raise NotGitLabUrl(branch.user_url)
117
raise NotGitLabUrl(url)
129
raise NotGitLabUrl(branch.user_url)
119
raise NotGitLabUrl(url)
130
120
path = path.strip('/')
131
121
if path.endswith('.git'):
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
148
144
def set_description(self, description):
149
145
self._mr.description = description
151
148
def _branch_url_from_project(self, project_id, branch_name):
152
149
project = self._mr.manager.gitlab.projects.get(project_id)
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):
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)
206
203
base_project = self.gl.projects.get(base_project)
239
236
def get_derived_branch(self, base_branch, name, project=None, owner=None):
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)
244
241
base_project = self.gl.projects.get(base_project)
266
263
def iter_proposals(self, source_branch, target_branch, status):
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)
290
287
def hosts(self, branch):
292
(host, project, branch_name) = parse_gitlab_url(branch)
289
(host, project, branch_name) = parse_gitlab_branch_url(branch)
293
290
except NotGitLabUrl:
295
292
return (self.gl.url == ('https://%s' % host))
298
def probe(cls, branch):
295
def probe_from_url(cls, url):
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)
304
301
import requests.exceptions
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)
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)