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."""
19
from __future__ import absolute_import
27
branch as _mod_branch,
32
from ...git.urls import git_url_to_bzr_url
33
from ...sixish import PY3
34
from ...trace import mutter
35
from ...transport import get_transport
37
from ...propose import (
44
PrerequisiteBranchUnsupported,
45
SourceNotDerivedFromTarget,
50
_DEFAULT_FILES = ['/etc/python-gitlab.cfg', '~/.python-gitlab.cfg']
51
DEFAULT_PAGE_SIZE = 50
54
def mp_status_to_status(status):
59
'closed': 'closed'}[status]
62
class NotGitLabUrl(errors.BzrError):
64
_fmt = "Not a GitLab URL: %(url)s"
66
def __init__(self, url):
67
errors.BzrError.__init__(self)
71
class NotMergeRequestUrl(errors.BzrError):
73
_fmt = "Not a merge proposal URL: %(url)s"
75
def __init__(self, host, url):
76
errors.BzrError.__init__(self)
81
class GitLabUnprocessable(errors.BzrError):
83
_fmt = "GitLab can not process request: %(error)s."
85
def __init__(self, error):
86
errors.BzrError.__init__(self, error=error)
89
class DifferentGitLabInstances(errors.BzrError):
91
_fmt = ("Can't create merge proposals across GitLab instances: "
92
"%(source_host)s and %(target_host)s")
94
def __init__(self, source_host, target_host):
95
self.source_host = source_host
96
self.target_host = target_host
99
class GitLabLoginMissing(errors.BzrError):
101
_fmt = ("Please log into GitLab")
104
class GitlabLoginError(errors.BzrError):
106
_fmt = ("Error logging in: %(error)s")
108
def __init__(self, error):
112
class GitLabConflict(errors.BzrError):
114
_fmt = "Conflict during operation: %(reason)s"
116
def __init__(self, reason):
117
errors.BzrError(self, reason=reason)
120
class ForkingDisabled(errors.BzrError):
122
_fmt = ("Forking on project %(project)s is disabled.")
124
def __init__(self, project):
125
self.project = project
128
class MergeRequestExists(Exception):
129
"""Raised when a merge requests already exists."""
132
def default_config_path():
133
return os.path.join(bedding.config_dir(), 'gitlab.conf')
136
def store_gitlab_token(name, url, private_token):
137
"""Store a GitLab token in a configuration file."""
139
config = configparser.ConfigParser()
140
path = default_config_path()
142
config.add_section(name)
143
config[name]['url'] = url
144
config[name]['private_token'] = private_token
145
with open(path, 'w') as f:
151
config = configparser.ConfigParser()
153
[os.path.expanduser(p) for p in _DEFAULT_FILES] +
154
[default_config_path()])
155
for name, section in config.items():
159
def get_credentials_by_url(url):
160
for name, credentials in iter_tokens():
161
if 'url' not in credentials:
163
if credentials['url'].rstrip('/') == url.rstrip('/'):
169
def parse_gitlab_url(url):
170
(scheme, user, password, host, port, path) = urlutils.parse_url(
172
if scheme not in ('git+ssh', 'https', 'http'):
173
raise NotGitLabUrl(url)
175
raise NotGitLabUrl(url)
176
path = path.strip('/')
177
if path.endswith('.git'):
182
def parse_gitlab_branch_url(branch):
183
url = urlutils.strip_segment_parameters(branch.user_url)
184
host, path = parse_gitlab_url(url)
185
return host, path, branch.name
188
def parse_gitlab_merge_request_url(url):
189
(scheme, user, password, host, port, path) = urlutils.parse_url(
191
if scheme not in ('git+ssh', 'https', 'http'):
192
raise NotGitLabUrl(url)
194
raise NotGitLabUrl(url)
195
path = path.strip('/')
196
parts = path.split('/')
198
raise NotMergeRequestUrl(host, url)
199
if parts[-2] != 'merge_requests':
200
raise NotMergeRequestUrl(host, url)
202
project_name = '/'.join(parts[:-3])
204
project_name = '/'.join(parts[:-2])
205
return host, project_name, int(parts[-1])
208
def _unexpected_status(path, response):
209
raise errors.UnexpectedHttpStatus(
210
path, response.status, response.data.decode('utf-8', 'replace'))
213
class GitLabMergeProposal(MergeProposal):
215
def __init__(self, gl, mr):
219
def _update(self, **kwargs):
220
self.gl._update_merge_request(self._mr['project_id'], self._mr['iid'], kwargs)
223
return "<%s at %r>" % (type(self).__name__, self._mr['web_url'])
227
return self._mr['web_url']
229
def get_description(self):
230
return self._mr['description']
232
def set_description(self, description):
233
self._update(description=description, title=determine_title(description))
235
def get_commit_message(self):
236
return self._mr.get('merge_commit_message')
238
def set_commit_message(self, message):
239
raise errors.UnsupportedOperation(self.set_commit_message, self)
241
def _branch_url_from_project(self, project_id, branch_name):
242
if project_id is None:
244
project = self.gl._get_project(project_id)
245
return gitlab_url_to_bzr_url(project['http_url_to_repo'], branch_name)
247
def get_source_branch_url(self):
248
return self._branch_url_from_project(
249
self._mr['source_project_id'], self._mr['source_branch'])
251
def get_source_revision(self):
252
from breezy.git.mapping import default_mapping
253
sha = self._mr['sha']
256
return default_mapping.revision_id_foreign_to_bzr(sha.encode('ascii'))
258
def get_target_branch_url(self):
259
return self._branch_url_from_project(
260
self._mr['target_project_id'], self._mr['target_branch'])
262
def _get_project_name(self, project_id):
263
source_project = self.gl._get_project(project_id)
264
return source_project['path_with_namespace']
266
def get_source_project(self):
267
return self._get_project_name(self._mr['source_project_id'])
269
def get_target_project(self):
270
return self._get_project_name(self._mr['target_project_id'])
273
return (self._mr['state'] == 'merged')
276
return (self._mr['state'] == 'closed')
279
return self._update(state_event='reopen')
282
self._update(state_event='close')
284
def merge(self, commit_message=None):
285
# https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr
286
self._mr.merge(merge_commit_message=commit_message)
288
def can_be_merged(self):
289
if self._mr['merge_status'] == 'cannot_be_merged':
291
elif self._mr['merge_status'] == 'can_be_merged':
293
elif self._mr['merge_status'] in (
294
'unchecked', 'cannot_be_merged_recheck'):
295
# See https://gitlab.com/gitlab-org/gitlab/-/commit/7517105303c for
296
# an explanation of the distinction between unchecked and
297
# cannot_be_merged_recheck
300
raise ValueError(self._mr['merge_status'])
302
def get_merged_by(self):
303
user = self._mr.get('merged_by')
306
return user['username']
308
def get_merged_at(self):
309
merged_at = self._mr.get('merged_at')
310
if merged_at is None:
313
return iso8601.parse_date(merged_at)
315
def post_comment(self, body):
316
kwargs = {'body': body}
317
self.gl._post_merge_request_note(
318
self._mr['project_id'], self._mr['iid'], kwargs)
321
def gitlab_url_to_bzr_url(url, name):
323
name = name.encode('utf-8')
324
return git_url_to_bzr_url(url, branch=name)
327
class GitLab(Hoster):
328
"""GitLab hoster implementation."""
330
supports_merge_proposal_labels = True
331
supports_merge_proposal_commit_message = False
332
supports_allow_collaboration = True
333
merge_proposal_description_format = 'markdown'
336
return "<GitLab(%r)>" % self.base_url
340
return self.transport.base
343
def base_hostname(self):
344
return urlutils.parse_url(self.base_url)[3]
346
def _find_correct_project_name(self, path):
348
resp = self.transport.request(
349
'GET', urlutils.join(self.base_url, path),
350
headers=self.headers)
351
except errors.RedirectRequested as e:
352
return urlutils.parse_url(e.target)[-1].strip('/')
353
if resp.status != 200:
354
_unexpected_status(path, resp)
357
def _api_request(self, method, path, fields=None, body=None):
358
return self.transport.request(
359
method, urlutils.join(self.base_url, 'api', 'v4', path),
360
headers=self.headers, fields=fields, body=body)
362
def __init__(self, transport, private_token):
363
self.transport = transport
364
self.headers = {"Private-Token": private_token}
367
def _get_user(self, username):
368
path = 'users/%s' % urlutils.quote(str(username), '')
369
response = self._api_request('GET', path)
370
if response.status == 404:
371
raise KeyError('no such user %s' % username)
372
if response.status == 200:
373
return json.loads(response.data)
374
_unexpected_status(path, response)
376
def _get_user_by_email(self, email):
377
path = 'users?search=%s' % urlutils.quote(str(email), '')
378
response = self._api_request('GET', path)
379
if response.status == 404:
380
raise KeyError('no such user %s' % email)
381
if response.status == 200:
382
ret = json.loads(response.data)
384
raise ValueError('unexpected number of results; %r' % ret)
386
_unexpected_status(path, response)
388
def _get_project(self, project_name, _redirect_checked=False):
389
path = 'projects/%s' % urlutils.quote(str(project_name), '')
390
response = self._api_request('GET', path)
391
if response.status == 404:
392
if not _redirect_checked:
393
project_name = self._find_correct_project_name(project_name)
394
if project_name is not None:
395
return self._get_project(project_name, _redirect_checked=True)
396
raise NoSuchProject(project_name)
397
if response.status == 200:
398
return json.loads(response.data)
399
_unexpected_status(path, response)
401
def _fork_project(self, project_name, timeout=50, interval=5, owner=None):
402
path = 'projects/%s/fork' % urlutils.quote(str(project_name), '')
404
if owner is not None:
405
fields['namespace'] = owner
406
response = self._api_request('POST', path, fields=fields)
407
if response.status == 404:
408
raise ForkingDisabled(project_name)
409
if response.status == 409:
410
resp = json.loads(response.data)
411
raise GitLabConflict(resp.get('message'))
412
if response.status not in (200, 201):
413
_unexpected_status(path, response)
414
# The response should be valid JSON, but let's ignore it
415
project = json.loads(response.data)
416
# Spin and wait until import_status for new project
418
deadline = time.time() + timeout
419
while project['import_status'] not in ('finished', 'none'):
420
mutter('import status is %s', project['import_status'])
421
if time.time() > deadline:
422
raise Exception('timeout waiting for project to become available')
424
project = self._get_project(project['path_with_namespace'])
427
def get_current_user(self):
428
return self._current_user['username']
430
def get_user_url(self, username):
431
return urlutils.join(self.base_url, username)
433
def _list_paged(self, path, parameters=None, per_page=None):
434
if parameters is None:
437
parameters = dict(parameters.items())
439
parameters['per_page'] = str(per_page)
442
parameters['page'] = page
443
response = self._api_request(
445
';'.join(['%s=%s' % item for item in parameters.items()]))
446
if response.status == 403:
447
raise errors.PermissionDenied(response.text)
448
if response.status != 200:
449
_unexpected_status(path, response)
450
page = response.getheader("X-Next-Page")
451
for entry in json.loads(response.data):
454
def _list_merge_requests(self, owner=None, project=None, state=None):
455
if project is not None:
456
path = 'projects/%s/merge_requests' % urlutils.quote(str(project), '')
458
path = 'merge_requests'
461
parameters['state'] = state
463
parameters['owner_id'] = urlutils.quote(owner, '')
464
return self._list_paged(path, parameters, per_page=DEFAULT_PAGE_SIZE)
466
def _get_merge_request(self, project, merge_id):
467
path = 'projects/%s/merge_requests/%d' % (urlutils.quote(str(project), ''), merge_id)
468
response = self._api_request('GET', path)
469
if response.status == 403:
470
raise errors.PermissionDenied(response.text)
471
if response.status != 200:
472
_unexpected_status(path, response)
473
return json.loads(response.data)
475
def _list_projects(self, owner):
476
path = 'users/%s/projects' % urlutils.quote(str(owner), '')
478
return self._list_paged(path, parameters, per_page=DEFAULT_PAGE_SIZE)
480
def _update_merge_request(self, project_id, iid, mr):
481
path = 'projects/%s/merge_requests/%s' % (
482
urlutils.quote(str(project_id), ''), iid)
483
response = self._api_request('PUT', path, fields=mr)
484
if response.status == 200:
485
return json.loads(response.data)
486
if response.status == 403:
487
raise errors.PermissionDenied(response.text)
488
_unexpected_status(path, response)
490
def _post_merge_request_note(self, project_id, iid, kwargs):
491
path = 'projects/%s/merge_requests/%s/notes' % (
492
urlutils.quote(str(project_id), ''), iid)
493
response = self._api_request('POST', path, fields=kwargs)
494
if response.status == 201:
495
json.loads(response.data)
497
if response.status == 403:
498
raise errors.PermissionDenied(response.text)
499
_unexpected_status(path, response)
501
def _create_mergerequest(
502
self, title, source_project_id, target_project_id,
503
source_branch_name, target_branch_name, description,
504
labels=None, allow_collaboration=False):
505
path = 'projects/%s/merge_requests' % source_project_id
508
'source_branch': source_branch_name,
509
'target_branch': target_branch_name,
510
'target_project_id': target_project_id,
511
'description': description,
512
'allow_collaboration': allow_collaboration,
515
fields['labels'] = labels
516
response = self._api_request('POST', path, fields=fields)
517
if response.status == 403:
518
raise errors.PermissionDenied(response.text)
519
if response.status == 409:
520
raise MergeRequestExists()
521
if response.status == 422:
522
data = json.loads(response.data)
523
raise GitLabUnprocessable(data['error'])
524
if response.status != 201:
525
_unexpected_status(path, response)
526
return json.loads(response.data)
528
def get_push_url(self, branch):
529
(host, project_name, branch_name) = parse_gitlab_branch_url(branch)
530
project = self._get_project(project_name)
531
return gitlab_url_to_bzr_url(
532
project['ssh_url_to_repo'], branch_name)
534
def publish_derived(self, local_branch, base_branch, name, project=None,
535
owner=None, revision_id=None, overwrite=False,
536
allow_lossy=True, tag_selector=None):
537
(host, base_project_name, base_branch_name) = parse_gitlab_branch_url(base_branch)
539
owner = self.get_current_user()
540
base_project = self._get_project(base_project_name)
542
project = base_project['path']
544
target_project = self._get_project('%s/%s' % (owner, project))
545
except NoSuchProject:
546
target_project = self._fork_project(
547
base_project['path_with_namespace'], owner=owner)
548
remote_repo_url = git_url_to_bzr_url(target_project['ssh_url_to_repo'])
549
remote_dir = controldir.ControlDir.open(remote_repo_url)
551
push_result = remote_dir.push_branch(
552
local_branch, revision_id=revision_id, overwrite=overwrite,
553
name=name, tag_selector=tag_selector)
554
except errors.NoRoundtrippingSupport:
557
push_result = remote_dir.push_branch(
558
local_branch, revision_id=revision_id, overwrite=overwrite,
559
name=name, lossy=True, tag_selector=tag_selector)
560
public_url = gitlab_url_to_bzr_url(
561
target_project['http_url_to_repo'], name)
562
return push_result.target_branch, public_url
564
def get_derived_branch(self, base_branch, name, project=None, owner=None):
565
(host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
567
owner = self.get_current_user()
569
project = self._get_project(base_project)['path']
571
target_project = self._get_project('%s/%s' % (owner, project))
572
except NoSuchProject:
573
raise errors.NotBranchError('%s/%s/%s' % (self.base_url, owner, project))
574
return _mod_branch.Branch.open(gitlab_url_to_bzr_url(
575
target_project['ssh_url_to_repo'], name))
577
def get_proposer(self, source_branch, target_branch):
578
return GitlabMergeProposalBuilder(self, source_branch, target_branch)
580
def iter_proposals(self, source_branch, target_branch, status):
581
(source_host, source_project_name, source_branch_name) = (
582
parse_gitlab_branch_url(source_branch))
583
(target_host, target_project_name, target_branch_name) = (
584
parse_gitlab_branch_url(target_branch))
585
if source_host != target_host:
586
raise DifferentGitLabInstances(source_host, target_host)
587
source_project = self._get_project(source_project_name)
588
target_project = self._get_project(target_project_name)
589
state = mp_status_to_status(status)
590
for mr in self._list_merge_requests(
591
project=target_project['id'], state=state):
592
if (mr['source_project_id'] != source_project['id'] or
593
mr['source_branch'] != source_branch_name or
594
mr['target_project_id'] != target_project['id'] or
595
mr['target_branch'] != target_branch_name):
597
yield GitLabMergeProposal(self, mr)
599
def hosts(self, branch):
601
(host, project, branch_name) = parse_gitlab_branch_url(branch)
604
return self.base_hostname == host
607
response = self._api_request('GET', 'user')
608
if response.status == 200:
609
self._current_user = json.loads(response.data)
612
if json.loads(response.data) == {"message": "401 Unauthorized"}:
613
raise GitLabLoginMissing()
615
raise GitlabLoginError(response.text)
616
raise UnsupportedHoster(self.base_url)
619
def probe_from_url(cls, url, possible_transports=None):
621
(host, project) = parse_gitlab_url(url)
623
raise UnsupportedHoster(url)
624
transport = get_transport(
625
'https://%s' % host, possible_transports=possible_transports)
626
credentials = get_credentials_by_url(transport.base)
627
if credentials is not None:
628
return cls(transport, credentials.get('private_token'))
629
raise UnsupportedHoster(url)
632
def iter_instances(cls):
633
for name, credentials in iter_tokens():
634
if 'url' not in credentials:
637
get_transport(credentials['url']),
638
private_token=credentials.get('private_token'))
640
def iter_my_proposals(self, status='open'):
641
state = mp_status_to_status(status)
642
for mp in self._list_merge_requests(
643
owner=self.get_current_user(), state=state):
644
yield GitLabMergeProposal(self, mp)
646
def iter_my_forks(self):
647
for project in self._list_projects(owner=self.get_current_user()):
648
base_project = project.get('forked_from_project')
651
yield project['path_with_namespace']
653
def get_proposal_by_url(self, url):
655
(host, project, merge_id) = parse_gitlab_merge_request_url(url)
657
raise UnsupportedHoster(url)
658
except NotMergeRequestUrl as e:
659
if self.base_hostname == e.host:
662
raise UnsupportedHoster(url)
663
if self.base_hostname != host:
664
raise UnsupportedHoster(url)
665
project = self._get_project(project)
666
mr = self._get_merge_request(project['path_with_namespace'], merge_id)
667
return GitLabMergeProposal(self, mr)
669
def delete_project(self, project):
670
path = 'projects/%s' % urlutils.quote(str(project), '')
671
response = self._api_request('DELETE', path)
672
if response.status == 404:
673
raise NoSuchProject(project)
674
if response.status != 202:
675
_unexpected_status(path, response)
678
class GitlabMergeProposalBuilder(MergeProposalBuilder):
680
def __init__(self, gl, source_branch, target_branch):
682
self.source_branch = source_branch
683
(self.source_host, self.source_project_name, self.source_branch_name) = (
684
parse_gitlab_branch_url(source_branch))
685
self.target_branch = target_branch
686
(self.target_host, self.target_project_name, self.target_branch_name) = (
687
parse_gitlab_branch_url(target_branch))
688
if self.source_host != self.target_host:
689
raise DifferentGitLabInstances(self.source_host, self.target_host)
691
def get_infotext(self):
692
"""Determine the initial comment for the merge proposal."""
694
info.append("Gitlab instance: %s\n" % self.target_host)
695
info.append("Source: %s\n" % self.source_branch.user_url)
696
info.append("Target: %s\n" % self.target_branch.user_url)
699
def get_initial_body(self):
700
"""Get a body for the proposal for the user to modify.
702
:return: a str or None.
706
def create_proposal(self, description, reviewers=None, labels=None,
707
prerequisite_branch=None, commit_message=None,
708
work_in_progress=False, allow_collaboration=False):
709
"""Perform the submission."""
710
# https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
711
if prerequisite_branch is not None:
712
raise PrerequisiteBranchUnsupported(self)
713
# Note that commit_message is ignored, since Gitlab doesn't support it.
714
source_project = self.gl._get_project(self.source_project_name)
715
target_project = self.gl._get_project(self.target_project_name)
716
# TODO(jelmer): Allow setting title explicitly
717
title = determine_title(description)
719
title = 'WIP: %s' % title
720
# TODO(jelmer): Allow setting milestone field
721
# TODO(jelmer): Allow setting squash field
724
'source_project_id': source_project['id'],
725
'target_project_id': target_project['id'],
726
'source_branch_name': self.source_branch_name,
727
'target_branch_name': self.target_branch_name,
728
'description': description,
729
'allow_collaboration': allow_collaboration}
731
kwargs['labels'] = ','.join(labels)
733
kwargs['assignee_ids'] = []
734
for reviewer in reviewers:
736
user = self.gl._get_user_by_email(reviewer)
738
user = self.gl._get_user(reviewer)
739
kwargs['assignee_ids'].append(user['id'])
741
merge_request = self.gl._create_mergerequest(**kwargs)
742
except MergeRequestExists:
743
raise MergeProposalExists(self.source_branch.user_url)
744
except GitLabUnprocessable as e:
746
"Source project is not a fork of the target project"]:
747
raise SourceNotDerivedFromTarget(
748
self.source_branch, self.target_branch)
749
return GitLabMergeProposal(self.gl, merge_request)
752
def register_gitlab_instance(shortname, url):
753
"""Register a gitlab instance.
755
:param shortname: Short name (e.g. "gitlab")
756
:param url: URL to the gitlab instance
758
from breezy.bugtracker import (
760
ProjectIntegerBugTracker,
762
tracker_registry.register(
763
shortname, ProjectIntegerBugTracker(
764
shortname, url + '/{project}/issues/{id}'))