1
# Copyright (C) 2018 Breezy Developers
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Support for GitLab."""
25
branch as _mod_branch,
30
from ...git.urls import git_url_to_bzr_url
31
from ...trace import mutter
32
from ...transport import get_transport
34
from ...propose import (
41
PrerequisiteBranchUnsupported,
42
SourceNotDerivedFromTarget,
47
_DEFAULT_FILES = ['/etc/python-gitlab.cfg', '~/.python-gitlab.cfg']
48
DEFAULT_PAGE_SIZE = 50
51
def mp_status_to_status(status):
56
'closed': 'closed'}[status]
59
class NotGitLabUrl(errors.BzrError):
61
_fmt = "Not a GitLab URL: %(url)s"
63
def __init__(self, url):
64
errors.BzrError.__init__(self)
68
class NotMergeRequestUrl(errors.BzrError):
70
_fmt = "Not a merge proposal URL: %(url)s"
72
def __init__(self, host, url):
73
errors.BzrError.__init__(self)
78
class GitLabUnprocessable(errors.BzrError):
80
_fmt = "GitLab can not process request: %(error)s."
82
def __init__(self, error):
83
errors.BzrError.__init__(self, error=error)
86
class DifferentGitLabInstances(errors.BzrError):
88
_fmt = ("Can't create merge proposals across GitLab instances: "
89
"%(source_host)s and %(target_host)s")
91
def __init__(self, source_host, target_host):
92
self.source_host = source_host
93
self.target_host = target_host
96
class GitLabLoginMissing(errors.BzrError):
98
_fmt = ("Please log into GitLab")
101
class GitlabLoginError(errors.BzrError):
103
_fmt = ("Error logging in: %(error)s")
105
def __init__(self, error):
109
class GitLabConflict(errors.BzrError):
111
_fmt = "Conflict during operation: %(reason)s"
113
def __init__(self, reason):
114
errors.BzrError(self, reason=reason)
117
class ForkingDisabled(errors.BzrError):
119
_fmt = ("Forking on project %(project)s is disabled.")
121
def __init__(self, project):
122
self.project = project
125
class MergeRequestExists(Exception):
126
"""Raised when a merge requests already exists."""
129
def default_config_path():
130
return os.path.join(bedding.config_dir(), 'gitlab.conf')
133
def store_gitlab_token(name, url, private_token):
134
"""Store a GitLab token in a configuration file."""
136
config = configparser.ConfigParser()
137
path = default_config_path()
139
config.add_section(name)
140
config[name]['url'] = url
141
config[name]['private_token'] = private_token
142
with open(path, 'w') as f:
148
config = configparser.ConfigParser()
150
[os.path.expanduser(p) for p in _DEFAULT_FILES] +
151
[default_config_path()])
152
for name, section in config.items():
156
def get_credentials_by_url(url):
157
for name, credentials in iter_tokens():
158
if 'url' not in credentials:
160
if credentials['url'].rstrip('/') == url.rstrip('/'):
166
def parse_gitlab_url(url):
167
(scheme, user, password, host, port, path) = urlutils.parse_url(
169
if scheme not in ('git+ssh', 'https', 'http'):
170
raise NotGitLabUrl(url)
172
raise NotGitLabUrl(url)
173
path = path.strip('/')
174
if path.endswith('.git'):
179
def parse_gitlab_branch_url(branch):
180
url = urlutils.strip_segment_parameters(branch.user_url)
181
host, path = parse_gitlab_url(url)
182
return host, path, branch.name
185
def parse_gitlab_merge_request_url(url):
186
(scheme, user, password, host, port, path) = urlutils.parse_url(
188
if scheme not in ('git+ssh', 'https', 'http'):
189
raise NotGitLabUrl(url)
191
raise NotGitLabUrl(url)
192
path = path.strip('/')
193
parts = path.split('/')
195
raise NotMergeRequestUrl(host, url)
196
if parts[-2] != 'merge_requests':
197
raise NotMergeRequestUrl(host, url)
199
project_name = '/'.join(parts[:-3])
201
project_name = '/'.join(parts[:-2])
202
return host, project_name, int(parts[-1])
205
def _unexpected_status(path, response):
206
raise errors.UnexpectedHttpStatus(
207
path, response.status, response.data.decode('utf-8', 'replace'))
210
class GitLabMergeProposal(MergeProposal):
212
def __init__(self, gl, mr):
216
def _update(self, **kwargs):
217
self.gl._update_merge_request(self._mr['project_id'], self._mr['iid'], kwargs)
220
return "<%s at %r>" % (type(self).__name__, self._mr['web_url'])
224
return self._mr['web_url']
226
def get_description(self):
227
return self._mr['description']
229
def set_description(self, description):
230
self._update(description=description, title=determine_title(description))
232
def get_commit_message(self):
233
return self._mr.get('merge_commit_message')
235
def set_commit_message(self, message):
236
raise errors.UnsupportedOperation(self.set_commit_message, self)
238
def _branch_url_from_project(self, project_id, branch_name):
239
if project_id is None:
241
project = self.gl._get_project(project_id)
242
return gitlab_url_to_bzr_url(project['http_url_to_repo'], branch_name)
244
def get_source_branch_url(self):
245
return self._branch_url_from_project(
246
self._mr['source_project_id'], self._mr['source_branch'])
248
def get_source_revision(self):
249
from breezy.git.mapping import default_mapping
250
sha = self._mr['sha']
253
return default_mapping.revision_id_foreign_to_bzr(sha.encode('ascii'))
255
def get_target_branch_url(self):
256
return self._branch_url_from_project(
257
self._mr['target_project_id'], self._mr['target_branch'])
259
def _get_project_name(self, project_id):
260
source_project = self.gl._get_project(project_id)
261
return source_project['path_with_namespace']
263
def get_source_project(self):
264
return self._get_project_name(self._mr['source_project_id'])
266
def get_target_project(self):
267
return self._get_project_name(self._mr['target_project_id'])
270
return (self._mr['state'] == 'merged')
273
return (self._mr['state'] == 'closed')
276
return self._update(state_event='reopen')
279
self._update(state_event='close')
281
def merge(self, commit_message=None):
282
# https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr
283
self._mr.merge(merge_commit_message=commit_message)
285
def can_be_merged(self):
286
if self._mr['merge_status'] == 'cannot_be_merged':
288
elif self._mr['merge_status'] == 'can_be_merged':
290
elif self._mr['merge_status'] in (
291
'unchecked', 'cannot_be_merged_recheck'):
292
# See https://gitlab.com/gitlab-org/gitlab/-/commit/7517105303c for
293
# an explanation of the distinction between unchecked and
294
# cannot_be_merged_recheck
297
raise ValueError(self._mr['merge_status'])
299
def get_merged_by(self):
300
user = self._mr.get('merged_by')
303
return user['username']
305
def get_merged_at(self):
306
merged_at = self._mr.get('merged_at')
307
if merged_at is None:
310
return iso8601.parse_date(merged_at)
312
def post_comment(self, body):
313
kwargs = {'body': body}
314
self.gl._post_merge_request_note(
315
self._mr['project_id'], self._mr['iid'], kwargs)
318
def gitlab_url_to_bzr_url(url, name):
319
return git_url_to_bzr_url(url, branch=name)
322
class GitLab(Hoster):
323
"""GitLab hoster implementation."""
325
supports_merge_proposal_labels = True
326
supports_merge_proposal_commit_message = False
327
supports_allow_collaboration = True
328
merge_proposal_description_format = 'markdown'
331
return "<GitLab(%r)>" % self.base_url
335
return self.transport.base
338
def base_hostname(self):
339
return urlutils.parse_url(self.base_url)[3]
341
def _api_request(self, method, path, fields=None, body=None):
342
return self.transport.request(
343
method, urlutils.join(self.base_url, 'api', 'v4', path),
344
headers=self.headers, fields=fields, body=body)
346
def __init__(self, transport, private_token):
347
self.transport = transport
348
self.headers = {"Private-Token": private_token}
351
def _get_user(self, username):
352
path = 'users/%s' % urlutils.quote(str(username), '')
353
response = self._api_request('GET', path)
354
if response.status == 404:
355
raise KeyError('no such user %s' % username)
356
if response.status == 200:
357
return json.loads(response.data)
358
_unexpected_status(path, response)
360
def _get_user_by_email(self, email):
361
path = 'users?search=%s' % urlutils.quote(str(email), '')
362
response = self._api_request('GET', path)
363
if response.status == 404:
364
raise KeyError('no such user %s' % email)
365
if response.status == 200:
366
ret = json.loads(response.data)
368
raise ValueError('unexpected number of results; %r' % ret)
370
_unexpected_status(path, response)
372
def _get_project(self, project_name):
373
path = 'projects/%s' % urlutils.quote(str(project_name), '')
374
response = self._api_request('GET', path)
375
if response.status == 404:
376
raise NoSuchProject(project_name)
377
if response.status == 200:
378
return json.loads(response.data)
379
_unexpected_status(path, response)
381
def _fork_project(self, project_name, timeout=50, interval=5, owner=None):
382
path = 'projects/%s/fork' % urlutils.quote(str(project_name), '')
384
if owner is not None:
385
fields['namespace'] = owner
386
response = self._api_request('POST', path, fields=fields)
387
if response.status == 404:
388
raise ForkingDisabled(project_name)
389
if response.status == 409:
390
resp = json.loads(response.data)
391
raise GitLabConflict(resp.get('message'))
392
if response.status not in (200, 201):
393
_unexpected_status(path, response)
394
# The response should be valid JSON, but let's ignore it
395
project = json.loads(response.data)
396
# Spin and wait until import_status for new project
398
deadline = time.time() + timeout
399
while project['import_status'] not in ('finished', 'none'):
400
mutter('import status is %s', project['import_status'])
401
if time.time() > deadline:
402
raise Exception('timeout waiting for project to become available')
404
project = self._get_project(project['path_with_namespace'])
407
def get_current_user(self):
408
return self._current_user['username']
410
def get_user_url(self, username):
411
return urlutils.join(self.base_url, username)
413
def _list_paged(self, path, parameters=None, per_page=None):
414
if parameters is None:
417
parameters = dict(parameters.items())
419
parameters['per_page'] = str(per_page)
422
parameters['page'] = page
423
response = self._api_request(
425
';'.join(['%s=%s' % item for item in parameters.items()]))
426
if response.status == 403:
427
raise errors.PermissionDenied(response.text)
428
if response.status != 200:
429
_unexpected_status(path, response)
430
page = response.getheader("X-Next-Page")
431
for entry in json.loads(response.data):
434
def _list_merge_requests(self, owner=None, project=None, state=None):
435
if project is not None:
436
path = 'projects/%s/merge_requests' % urlutils.quote(str(project), '')
438
path = 'merge_requests'
441
parameters['state'] = state
443
parameters['owner_id'] = urlutils.quote(owner, '')
444
return self._list_paged(path, parameters, per_page=DEFAULT_PAGE_SIZE)
446
def _get_merge_request(self, project, merge_id):
447
path = 'projects/%s/merge_requests/%d' % (urlutils.quote(str(project), ''), merge_id)
448
response = self._api_request('GET', path)
449
if response.status == 403:
450
raise errors.PermissionDenied(response.text)
451
if response.status != 200:
452
_unexpected_status(path, response)
453
return json.loads(response.data)
455
def _list_projects(self, owner):
456
path = 'users/%s/projects' % urlutils.quote(str(owner), '')
458
return self._list_paged(path, parameters, per_page=DEFAULT_PAGE_SIZE)
460
def _update_merge_request(self, project_id, iid, mr):
461
path = 'projects/%s/merge_requests/%s' % (
462
urlutils.quote(str(project_id), ''), iid)
463
response = self._api_request('PUT', path, fields=mr)
464
if response.status == 200:
465
return json.loads(response.data)
466
if response.status == 403:
467
raise errors.PermissionDenied(response.text)
468
_unexpected_status(path, response)
470
def _post_merge_request_note(self, project_id, iid, kwargs):
471
path = 'projects/%s/merge_requests/%s/notes' % (
472
urlutils.quote(str(project_id), ''), iid)
473
response = self._api_request('POST', path, fields=kwargs)
474
if response.status == 201:
475
json.loads(response.data)
477
if response.status == 403:
478
raise errors.PermissionDenied(response.text)
479
_unexpected_status(path, response)
481
def _create_mergerequest(
482
self, title, source_project_id, target_project_id,
483
source_branch_name, target_branch_name, description,
484
labels=None, allow_collaboration=False):
485
path = 'projects/%s/merge_requests' % source_project_id
488
'source_branch': source_branch_name,
489
'target_branch': target_branch_name,
490
'target_project_id': target_project_id,
491
'description': description,
492
'allow_collaboration': allow_collaboration,
495
fields['labels'] = labels
496
response = self._api_request('POST', path, fields=fields)
497
if response.status == 403:
498
raise errors.PermissionDenied(response.text)
499
if response.status == 409:
500
raise MergeRequestExists()
501
if response.status == 422:
502
data = json.loads(response.data)
503
raise GitLabUnprocessable(data['error'])
504
if response.status != 201:
505
_unexpected_status(path, response)
506
return json.loads(response.data)
508
def get_push_url(self, branch):
509
(host, project_name, branch_name) = parse_gitlab_branch_url(branch)
510
project = self._get_project(project_name)
511
return gitlab_url_to_bzr_url(
512
project['ssh_url_to_repo'], branch_name)
514
def publish_derived(self, local_branch, base_branch, name, project=None,
515
owner=None, revision_id=None, overwrite=False,
516
allow_lossy=True, tag_selector=None):
517
(host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
519
owner = self.get_current_user()
521
project = self._get_project(base_project)['path']
523
target_project = self._get_project('%s/%s' % (owner, project))
524
except NoSuchProject:
525
target_project = self._fork_project(base_project, owner=owner)
526
remote_repo_url = git_url_to_bzr_url(target_project['ssh_url_to_repo'])
527
remote_dir = controldir.ControlDir.open(remote_repo_url)
529
push_result = remote_dir.push_branch(
530
local_branch, revision_id=revision_id, overwrite=overwrite,
531
name=name, tag_selector=tag_selector)
532
except errors.NoRoundtrippingSupport:
535
push_result = remote_dir.push_branch(
536
local_branch, revision_id=revision_id, overwrite=overwrite,
537
name=name, lossy=True, tag_selector=tag_selector)
538
public_url = gitlab_url_to_bzr_url(
539
target_project['http_url_to_repo'], name)
540
return push_result.target_branch, public_url
542
def get_derived_branch(self, base_branch, name, project=None, owner=None):
543
(host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
545
owner = self.get_current_user()
547
project = self._get_project(base_project)['path']
549
target_project = self._get_project('%s/%s' % (owner, project))
550
except NoSuchProject:
551
raise errors.NotBranchError('%s/%s/%s' % (self.base_url, owner, project))
552
return _mod_branch.Branch.open(gitlab_url_to_bzr_url(
553
target_project['ssh_url_to_repo'], name))
555
def get_proposer(self, source_branch, target_branch):
556
return GitlabMergeProposalBuilder(self, source_branch, target_branch)
558
def iter_proposals(self, source_branch, target_branch, status):
559
(source_host, source_project_name, source_branch_name) = (
560
parse_gitlab_branch_url(source_branch))
561
(target_host, target_project_name, target_branch_name) = (
562
parse_gitlab_branch_url(target_branch))
563
if source_host != target_host:
564
raise DifferentGitLabInstances(source_host, target_host)
565
source_project = self._get_project(source_project_name)
566
target_project = self._get_project(target_project_name)
567
state = mp_status_to_status(status)
568
for mr in self._list_merge_requests(
569
project=target_project['id'], state=state):
570
if (mr['source_project_id'] != source_project['id'] or
571
mr['source_branch'] != source_branch_name or
572
mr['target_project_id'] != target_project['id'] or
573
mr['target_branch'] != target_branch_name):
575
yield GitLabMergeProposal(self, mr)
577
def hosts(self, branch):
579
(host, project, branch_name) = parse_gitlab_branch_url(branch)
582
return self.base_hostname == host
585
response = self._api_request('GET', 'user')
586
if response.status == 200:
587
self._current_user = json.loads(response.data)
590
if json.loads(response.data) == {"message": "401 Unauthorized"}:
591
raise GitLabLoginMissing()
593
raise GitlabLoginError(response.text)
594
raise UnsupportedHoster(self.base_url)
597
def probe_from_url(cls, url, possible_transports=None):
599
(host, project) = parse_gitlab_url(url)
601
raise UnsupportedHoster(url)
602
transport = get_transport(
603
'https://%s' % host, possible_transports=possible_transports)
604
credentials = get_credentials_by_url(transport.base)
605
if credentials is not None:
606
return cls(transport, credentials.get('private_token'))
607
raise UnsupportedHoster(url)
610
def iter_instances(cls):
611
for name, credentials in iter_tokens():
612
if 'url' not in credentials:
615
get_transport(credentials['url']),
616
private_token=credentials.get('private_token'))
618
def iter_my_proposals(self, status='open'):
619
state = mp_status_to_status(status)
620
for mp in self._list_merge_requests(
621
owner=self.get_current_user(), state=state):
622
yield GitLabMergeProposal(self, mp)
624
def iter_my_forks(self):
625
for project in self._list_projects(owner=self.get_current_user()):
626
base_project = project.get('forked_from_project')
629
yield project['path_with_namespace']
631
def get_proposal_by_url(self, url):
633
(host, project, merge_id) = parse_gitlab_merge_request_url(url)
635
raise UnsupportedHoster(url)
636
except NotMergeRequestUrl as e:
637
if self.base_hostname == e.host:
640
raise UnsupportedHoster(url)
641
if self.base_hostname != host:
642
raise UnsupportedHoster(url)
643
project = self._get_project(project)
644
mr = self._get_merge_request(project['path_with_namespace'], merge_id)
645
return GitLabMergeProposal(self, mr)
647
def delete_project(self, project):
648
path = 'projects/%s' % urlutils.quote(str(project), '')
649
response = self._api_request('DELETE', path)
650
if response.status == 404:
651
raise NoSuchProject(project)
652
if response.status != 202:
653
_unexpected_status(path, response)
656
class GitlabMergeProposalBuilder(MergeProposalBuilder):
658
def __init__(self, gl, source_branch, target_branch):
660
self.source_branch = source_branch
661
(self.source_host, self.source_project_name, self.source_branch_name) = (
662
parse_gitlab_branch_url(source_branch))
663
self.target_branch = target_branch
664
(self.target_host, self.target_project_name, self.target_branch_name) = (
665
parse_gitlab_branch_url(target_branch))
666
if self.source_host != self.target_host:
667
raise DifferentGitLabInstances(self.source_host, self.target_host)
669
def get_infotext(self):
670
"""Determine the initial comment for the merge proposal."""
672
info.append("Gitlab instance: %s\n" % self.target_host)
673
info.append("Source: %s\n" % self.source_branch.user_url)
674
info.append("Target: %s\n" % self.target_branch.user_url)
677
def get_initial_body(self):
678
"""Get a body for the proposal for the user to modify.
680
:return: a str or None.
684
def create_proposal(self, description, reviewers=None, labels=None,
685
prerequisite_branch=None, commit_message=None,
686
work_in_progress=False, allow_collaboration=False):
687
"""Perform the submission."""
688
# https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
689
if prerequisite_branch is not None:
690
raise PrerequisiteBranchUnsupported(self)
691
# Note that commit_message is ignored, since Gitlab doesn't support it.
692
source_project = self.gl._get_project(self.source_project_name)
693
target_project = self.gl._get_project(self.target_project_name)
694
# TODO(jelmer): Allow setting title explicitly
695
title = determine_title(description)
697
title = 'WIP: %s' % title
698
# TODO(jelmer): Allow setting milestone field
699
# TODO(jelmer): Allow setting squash field
702
'source_project_id': source_project['id'],
703
'target_project_id': target_project['id'],
704
'source_branch_name': self.source_branch_name,
705
'target_branch_name': self.target_branch_name,
706
'description': description,
707
'allow_collaboration': allow_collaboration}
709
kwargs['labels'] = ','.join(labels)
711
kwargs['assignee_ids'] = []
712
for reviewer in reviewers:
714
user = self.gl._get_user_by_email(reviewer)
716
user = self.gl._get_user(reviewer)
717
kwargs['assignee_ids'].append(user['id'])
719
merge_request = self.gl._create_mergerequest(**kwargs)
720
except MergeRequestExists:
721
raise MergeProposalExists(self.source_branch.user_url)
722
except GitLabUnprocessable as e:
724
"Source project is not a fork of the target project"]:
725
raise SourceNotDerivedFromTarget(
726
self.source_branch, self.target_branch)
727
return GitLabMergeProposal(self.gl, merge_request)
730
def register_gitlab_instance(shortname, url):
731
"""Register a gitlab instance.
733
:param shortname: Short name (e.g. "gitlab")
734
:param url: URL to the gitlab instance
736
from breezy.bugtracker import (
738
ProjectIntegerBugTracker,
740
tracker_registry.register(
741
shortname, ProjectIntegerBugTracker(
742
shortname, url + '/{project}/issues/{id}'))