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."""
26
branch as _mod_branch,
31
from ...git.urls import git_url_to_bzr_url
32
from ...trace import mutter
33
from ...transport import get_transport
35
from ...propose import (
42
PrerequisiteBranchUnsupported,
43
SourceNotDerivedFromTarget,
48
_DEFAULT_FILES = ['/etc/python-gitlab.cfg', '~/.python-gitlab.cfg']
49
DEFAULT_PAGE_SIZE = 50
52
def mp_status_to_status(status):
57
'closed': 'closed'}[status]
60
class NotGitLabUrl(errors.BzrError):
62
_fmt = "Not a GitLab URL: %(url)s"
64
def __init__(self, url):
65
errors.BzrError.__init__(self)
69
class NotMergeRequestUrl(errors.BzrError):
71
_fmt = "Not a merge proposal URL: %(url)s"
73
def __init__(self, host, url):
74
errors.BzrError.__init__(self)
79
class GitLabUnprocessable(errors.BzrError):
81
_fmt = "GitLab can not process request: %(error)s."
83
def __init__(self, error):
84
errors.BzrError.__init__(self, error=error)
87
class DifferentGitLabInstances(errors.BzrError):
89
_fmt = ("Can't create merge proposals across GitLab instances: "
90
"%(source_host)s and %(target_host)s")
92
def __init__(self, source_host, target_host):
93
self.source_host = source_host
94
self.target_host = target_host
97
class GitLabLoginMissing(errors.BzrError):
99
_fmt = ("Please log into GitLab")
102
class GitlabLoginError(errors.BzrError):
104
_fmt = ("Error logging in: %(error)s")
106
def __init__(self, error):
110
class GitLabConflict(errors.BzrError):
112
_fmt = "Conflict during operation: %(reason)s"
114
def __init__(self, reason):
115
errors.BzrError(self, reason=reason)
118
class ForkingDisabled(errors.BzrError):
120
_fmt = ("Forking on project %(project)s is disabled.")
122
def __init__(self, project):
123
self.project = project
126
class MergeRequestConflict(Exception):
127
"""Raised when a merge requests conflicts."""
129
def __init__(self, reason):
133
class ProjectCreationTimeout(errors.BzrError):
135
_fmt = ("Timeout (%(timeout)ds) while waiting for project "
136
"%(project)s to be created.")
138
def __init__(self, project, timeout):
139
self.project = project
140
self.timeout = timeout
143
def default_config_path():
144
return os.path.join(bedding.config_dir(), 'gitlab.conf')
147
def store_gitlab_token(name, url, private_token):
148
"""Store a GitLab token in a configuration file."""
150
config = configparser.ConfigParser()
151
path = default_config_path()
153
config.add_section(name)
154
config[name]['url'] = url
155
config[name]['private_token'] = private_token
156
with open(path, 'w') as f:
162
config = configparser.ConfigParser()
164
[os.path.expanduser(p) for p in _DEFAULT_FILES] +
165
[default_config_path()])
166
for name, section in config.items():
170
def get_credentials_by_url(url):
171
for name, credentials in iter_tokens():
172
if 'url' not in credentials:
174
if credentials['url'].rstrip('/') == url.rstrip('/'):
180
def parse_gitlab_url(url):
181
(scheme, user, password, host, port, path) = urlutils.parse_url(
183
if scheme not in ('git+ssh', 'https', 'http'):
184
raise NotGitLabUrl(url)
186
raise NotGitLabUrl(url)
187
path = path.strip('/')
188
if path.endswith('.git'):
193
def parse_gitlab_branch_url(branch):
194
url = urlutils.strip_segment_parameters(branch.user_url)
195
host, path = parse_gitlab_url(url)
196
return host, path, branch.name
199
def parse_gitlab_merge_request_url(url):
200
(scheme, user, password, host, port, path) = urlutils.parse_url(
202
if scheme not in ('git+ssh', 'https', 'http'):
203
raise NotGitLabUrl(url)
205
raise NotGitLabUrl(url)
206
path = path.strip('/')
207
parts = path.split('/')
209
raise NotMergeRequestUrl(host, url)
210
if parts[-2] != 'merge_requests':
211
raise NotMergeRequestUrl(host, url)
213
project_name = '/'.join(parts[:-3])
215
project_name = '/'.join(parts[:-2])
216
return host, project_name, int(parts[-1])
219
def _unexpected_status(path, response):
220
raise errors.UnexpectedHttpStatus(
221
path, response.status, response.data.decode('utf-8', 'replace'))
224
class GitLabMergeProposal(MergeProposal):
226
def __init__(self, gl, mr):
230
def _update(self, **kwargs):
231
self.gl._update_merge_request(self._mr['project_id'], self._mr['iid'], kwargs)
234
return "<%s at %r>" % (type(self).__name__, self._mr['web_url'])
238
return self._mr['web_url']
240
def get_description(self):
241
return self._mr['description']
243
def set_description(self, description):
244
self._update(description=description, title=determine_title(description))
246
def get_commit_message(self):
247
return self._mr.get('merge_commit_message')
249
def set_commit_message(self, message):
250
raise errors.UnsupportedOperation(self.set_commit_message, self)
252
def _branch_url_from_project(self, project_id, branch_name):
253
if project_id is None:
255
project = self.gl._get_project(project_id)
256
return gitlab_url_to_bzr_url(project['http_url_to_repo'], branch_name)
258
def get_source_branch_url(self):
259
return self._branch_url_from_project(
260
self._mr['source_project_id'], self._mr['source_branch'])
262
def get_source_revision(self):
263
from breezy.git.mapping import default_mapping
264
sha = self._mr['sha']
267
return default_mapping.revision_id_foreign_to_bzr(sha.encode('ascii'))
269
def get_target_branch_url(self):
270
return self._branch_url_from_project(
271
self._mr['target_project_id'], self._mr['target_branch'])
273
def _get_project_name(self, project_id):
274
source_project = self.gl._get_project(project_id)
275
return source_project['path_with_namespace']
277
def get_source_project(self):
278
return self._get_project_name(self._mr['source_project_id'])
280
def get_target_project(self):
281
return self._get_project_name(self._mr['target_project_id'])
284
return (self._mr['state'] == 'merged')
287
return (self._mr['state'] == 'closed')
290
return self._update(state_event='reopen')
293
self._update(state_event='close')
295
def merge(self, commit_message=None):
296
# https://docs.gitlab.com/ee/api/merge_requests.html#accept-mr
297
self._mr.merge(merge_commit_message=commit_message)
299
def can_be_merged(self):
300
if self._mr['merge_status'] == 'cannot_be_merged':
302
elif self._mr['merge_status'] == 'can_be_merged':
304
elif self._mr['merge_status'] in (
305
'unchecked', 'cannot_be_merged_recheck'):
306
# See https://gitlab.com/gitlab-org/gitlab/-/commit/7517105303c for
307
# an explanation of the distinction between unchecked and
308
# cannot_be_merged_recheck
311
raise ValueError(self._mr['merge_status'])
313
def get_merged_by(self):
314
user = self._mr.get('merged_by')
317
return user['username']
319
def get_merged_at(self):
320
merged_at = self._mr.get('merged_at')
321
if merged_at is None:
324
return iso8601.parse_date(merged_at)
326
def post_comment(self, body):
327
kwargs = {'body': body}
328
self.gl._post_merge_request_note(
329
self._mr['project_id'], self._mr['iid'], kwargs)
332
def gitlab_url_to_bzr_url(url, name):
333
return git_url_to_bzr_url(url, branch=name)
336
class GitLab(Hoster):
337
"""GitLab hoster implementation."""
339
supports_merge_proposal_labels = True
340
supports_merge_proposal_commit_message = False
341
supports_allow_collaboration = True
342
merge_proposal_description_format = 'markdown'
345
return "<GitLab(%r)>" % self.base_url
349
return self.transport.base
352
def base_hostname(self):
353
return urlutils.parse_url(self.base_url)[3]
355
def _find_correct_project_name(self, path):
357
resp = self.transport.request(
358
'GET', urlutils.join(self.base_url, path),
359
headers=self.headers)
360
except errors.RedirectRequested as e:
361
return urlutils.parse_url(e.target)[-1].strip('/')
362
if resp.status != 200:
363
_unexpected_status(path, resp)
366
def _api_request(self, method, path, fields=None, body=None):
367
return self.transport.request(
368
method, urlutils.join(self.base_url, 'api', 'v4', path),
369
headers=self.headers, fields=fields, body=body)
371
def __init__(self, transport, private_token):
372
self.transport = transport
373
self.headers = {"Private-Token": private_token}
376
def _get_user(self, username):
377
path = 'users/%s' % urlutils.quote(str(username), '')
378
response = self._api_request('GET', path)
379
if response.status == 404:
380
raise KeyError('no such user %s' % username)
381
if response.status == 200:
382
return json.loads(response.data)
383
_unexpected_status(path, response)
385
def _get_user_by_email(self, email):
386
path = 'users?search=%s' % urlutils.quote(str(email), '')
387
response = self._api_request('GET', path)
388
if response.status == 404:
389
raise KeyError('no such user %s' % email)
390
if response.status == 200:
391
ret = json.loads(response.data)
393
raise ValueError('unexpected number of results; %r' % ret)
395
_unexpected_status(path, response)
397
def _get_project(self, project_name, _redirect_checked=False):
398
path = 'projects/%s' % urlutils.quote(str(project_name), '')
399
response = self._api_request('GET', path)
400
if response.status == 404:
401
if not _redirect_checked:
402
project_name = self._find_correct_project_name(project_name)
403
if project_name is not None:
404
return self._get_project(project_name, _redirect_checked=True)
405
raise NoSuchProject(project_name)
406
if response.status == 200:
407
return json.loads(response.data)
408
_unexpected_status(path, response)
410
def _fork_project(self, project_name, timeout=50, interval=5, owner=None):
411
path = 'projects/%s/fork' % urlutils.quote(str(project_name), '')
413
if owner is not None:
414
fields['namespace'] = owner
415
response = self._api_request('POST', path, fields=fields)
416
if response.status == 404:
417
raise ForkingDisabled(project_name)
418
if response.status == 409:
419
resp = json.loads(response.data)
420
raise GitLabConflict(resp.get('message'))
421
if response.status not in (200, 201):
422
_unexpected_status(path, response)
423
# The response should be valid JSON, but let's ignore it
424
project = json.loads(response.data)
425
# Spin and wait until import_status for new project
427
deadline = time.time() + timeout
428
while project['import_status'] not in ('finished', 'none'):
429
mutter('import status is %s', project['import_status'])
430
if time.time() > deadline:
431
raise ProjectCreationTimeout(
432
project['path_with_namespace'], timeout)
434
project = self._get_project(project['path_with_namespace'])
437
def get_current_user(self):
438
return self._current_user['username']
440
def get_user_url(self, username):
441
return urlutils.join(self.base_url, username)
443
def _list_paged(self, path, parameters=None, per_page=None):
444
if parameters is None:
447
parameters = dict(parameters.items())
449
parameters['per_page'] = str(per_page)
452
parameters['page'] = page
453
response = self._api_request(
455
';'.join(['%s=%s' % item for item in parameters.items()]))
456
if response.status == 403:
457
raise errors.PermissionDenied(response.text)
458
if response.status != 200:
459
_unexpected_status(path, response)
460
page = response.getheader("X-Next-Page")
461
for entry in json.loads(response.data):
464
def _list_merge_requests(self, author=None, project=None, state=None):
465
if project is not None:
466
path = 'projects/%s/merge_requests' % urlutils.quote(str(project), '')
468
path = 'merge_requests'
471
parameters['state'] = state
473
parameters['author_username'] = urlutils.quote(author, '')
474
return self._list_paged(path, parameters, per_page=DEFAULT_PAGE_SIZE)
476
def _get_merge_request(self, project, merge_id):
477
path = 'projects/%s/merge_requests/%d' % (urlutils.quote(str(project), ''), merge_id)
478
response = self._api_request('GET', path)
479
if response.status == 403:
480
raise errors.PermissionDenied(response.text)
481
if response.status != 200:
482
_unexpected_status(path, response)
483
return json.loads(response.data)
485
def _list_projects(self, owner):
486
path = 'users/%s/projects' % urlutils.quote(str(owner), '')
488
return self._list_paged(path, parameters, per_page=DEFAULT_PAGE_SIZE)
490
def _update_merge_request(self, project_id, iid, mr):
491
path = 'projects/%s/merge_requests/%s' % (
492
urlutils.quote(str(project_id), ''), iid)
493
response = self._api_request('PUT', path, fields=mr)
494
if response.status == 200:
495
return json.loads(response.data)
496
if response.status == 403:
497
raise errors.PermissionDenied(response.text)
498
_unexpected_status(path, response)
500
def _post_merge_request_note(self, project_id, iid, kwargs):
501
path = 'projects/%s/merge_requests/%s/notes' % (
502
urlutils.quote(str(project_id), ''), iid)
503
response = self._api_request('POST', path, fields=kwargs)
504
if response.status == 201:
505
json.loads(response.data)
507
if response.status == 403:
508
raise errors.PermissionDenied(response.text)
509
_unexpected_status(path, response)
511
def _create_mergerequest(
512
self, title, source_project_id, target_project_id,
513
source_branch_name, target_branch_name, description,
514
labels=None, allow_collaboration=False):
515
path = 'projects/%s/merge_requests' % source_project_id
518
'source_branch': source_branch_name,
519
'target_branch': target_branch_name,
520
'target_project_id': target_project_id,
521
'description': description,
522
'allow_collaboration': allow_collaboration,
525
fields['labels'] = labels
526
response = self._api_request('POST', path, fields=fields)
527
if response.status == 403:
528
raise errors.PermissionDenied(response.text)
529
if response.status == 409:
530
raise MergeRequestConflict(json.loads(response.data))
531
if response.status == 422:
532
data = json.loads(response.data)
533
raise GitLabUnprocessable(data['error'])
534
if response.status != 201:
535
_unexpected_status(path, response)
536
return json.loads(response.data)
538
def get_push_url(self, branch):
539
(host, project_name, branch_name) = parse_gitlab_branch_url(branch)
540
project = self._get_project(project_name)
541
return gitlab_url_to_bzr_url(
542
project['ssh_url_to_repo'], branch_name)
544
def publish_derived(self, local_branch, base_branch, name, project=None,
545
owner=None, revision_id=None, overwrite=False,
546
allow_lossy=True, tag_selector=None):
547
(host, base_project_name, base_branch_name) = parse_gitlab_branch_url(base_branch)
549
owner = base_branch.get_config_stack().get('fork-namespace')
551
owner = self.get_current_user()
552
base_project = self._get_project(base_project_name)
554
project = base_project['path']
556
target_project = self._get_project('%s/%s' % (owner, project))
557
except NoSuchProject:
558
target_project = self._fork_project(
559
base_project['path_with_namespace'], owner=owner)
560
remote_repo_url = git_url_to_bzr_url(target_project['ssh_url_to_repo'])
561
remote_dir = controldir.ControlDir.open(remote_repo_url)
563
push_result = remote_dir.push_branch(
564
local_branch, revision_id=revision_id, overwrite=overwrite,
565
name=name, tag_selector=tag_selector)
566
except errors.NoRoundtrippingSupport:
569
push_result = remote_dir.push_branch(
570
local_branch, revision_id=revision_id, overwrite=overwrite,
571
name=name, lossy=True, tag_selector=tag_selector)
572
public_url = gitlab_url_to_bzr_url(
573
target_project['http_url_to_repo'], name)
574
return push_result.target_branch, public_url
576
def get_derived_branch(self, base_branch, name, project=None, owner=None):
577
(host, base_project, base_branch_name) = parse_gitlab_branch_url(base_branch)
579
owner = self.get_current_user()
581
project = self._get_project(base_project)['path']
583
target_project = self._get_project('%s/%s' % (owner, project))
584
except NoSuchProject:
585
raise errors.NotBranchError('%s/%s/%s' % (self.base_url, owner, project))
586
return _mod_branch.Branch.open(gitlab_url_to_bzr_url(
587
target_project['ssh_url_to_repo'], name))
589
def get_proposer(self, source_branch, target_branch):
590
return GitlabMergeProposalBuilder(self, source_branch, target_branch)
592
def iter_proposals(self, source_branch, target_branch, status):
593
(source_host, source_project_name, source_branch_name) = (
594
parse_gitlab_branch_url(source_branch))
595
(target_host, target_project_name, target_branch_name) = (
596
parse_gitlab_branch_url(target_branch))
597
if source_host != target_host:
598
raise DifferentGitLabInstances(source_host, target_host)
599
source_project = self._get_project(source_project_name)
600
target_project = self._get_project(target_project_name)
601
state = mp_status_to_status(status)
602
for mr in self._list_merge_requests(
603
project=target_project['id'], state=state):
604
if (mr['source_project_id'] != source_project['id'] or
605
mr['source_branch'] != source_branch_name or
606
mr['target_project_id'] != target_project['id'] or
607
mr['target_branch'] != target_branch_name):
609
yield GitLabMergeProposal(self, mr)
611
def hosts(self, branch):
613
(host, project, branch_name) = parse_gitlab_branch_url(branch)
616
return self.base_hostname == host
619
response = self._api_request('GET', 'user')
620
if response.status == 200:
621
self._current_user = json.loads(response.data)
624
if json.loads(response.data) == {"message": "401 Unauthorized"}:
625
raise GitLabLoginMissing()
627
raise GitlabLoginError(response.text)
628
raise UnsupportedHoster(self.base_url)
631
def probe_from_url(cls, url, possible_transports=None):
633
(host, project) = parse_gitlab_url(url)
635
raise UnsupportedHoster(url)
636
transport = get_transport(
637
'https://%s' % host, possible_transports=possible_transports)
638
credentials = get_credentials_by_url(transport.base)
639
if credentials is not None:
640
return cls(transport, credentials.get('private_token'))
641
raise UnsupportedHoster(url)
644
def iter_instances(cls):
645
for name, credentials in iter_tokens():
646
if 'url' not in credentials:
649
get_transport(credentials['url']),
650
private_token=credentials.get('private_token'))
652
def iter_my_proposals(self, status='open', author=None):
654
author = self.get_current_user()
655
state = mp_status_to_status(status)
656
for mp in self._list_merge_requests(author=author, state=state):
657
yield GitLabMergeProposal(self, mp)
659
def iter_my_forks(self, owner=None):
660
if owner is not None:
661
owner = self.get_current_user()
662
for project in self._list_projects(owner=owner):
663
base_project = project.get('forked_from_project')
666
yield project['path_with_namespace']
668
def get_proposal_by_url(self, url):
670
(host, project, merge_id) = parse_gitlab_merge_request_url(url)
672
raise UnsupportedHoster(url)
673
except NotMergeRequestUrl as e:
674
if self.base_hostname == e.host:
677
raise UnsupportedHoster(url)
678
if self.base_hostname != host:
679
raise UnsupportedHoster(url)
680
project = self._get_project(project)
681
mr = self._get_merge_request(project['path_with_namespace'], merge_id)
682
return GitLabMergeProposal(self, mr)
684
def delete_project(self, project):
685
path = 'projects/%s' % urlutils.quote(str(project), '')
686
response = self._api_request('DELETE', path)
687
if response.status == 404:
688
raise NoSuchProject(project)
689
if response.status != 202:
690
_unexpected_status(path, response)
693
class GitlabMergeProposalBuilder(MergeProposalBuilder):
695
def __init__(self, gl, source_branch, target_branch):
697
self.source_branch = source_branch
698
(self.source_host, self.source_project_name, self.source_branch_name) = (
699
parse_gitlab_branch_url(source_branch))
700
self.target_branch = target_branch
701
(self.target_host, self.target_project_name, self.target_branch_name) = (
702
parse_gitlab_branch_url(target_branch))
703
if self.source_host != self.target_host:
704
raise DifferentGitLabInstances(self.source_host, self.target_host)
706
def get_infotext(self):
707
"""Determine the initial comment for the merge proposal."""
709
info.append("Gitlab instance: %s\n" % self.target_host)
710
info.append("Source: %s\n" % self.source_branch.user_url)
711
info.append("Target: %s\n" % self.target_branch.user_url)
714
def get_initial_body(self):
715
"""Get a body for the proposal for the user to modify.
717
:return: a str or None.
721
def create_proposal(self, description, reviewers=None, labels=None,
722
prerequisite_branch=None, commit_message=None,
723
work_in_progress=False, allow_collaboration=False):
724
"""Perform the submission."""
725
# https://docs.gitlab.com/ee/api/merge_requests.html#create-mr
726
if prerequisite_branch is not None:
727
raise PrerequisiteBranchUnsupported(self)
728
# Note that commit_message is ignored, since Gitlab doesn't support it.
729
source_project = self.gl._get_project(self.source_project_name)
730
target_project = self.gl._get_project(self.target_project_name)
731
# TODO(jelmer): Allow setting title explicitly
732
title = determine_title(description)
734
title = 'WIP: %s' % title
735
# TODO(jelmer): Allow setting milestone field
736
# TODO(jelmer): Allow setting squash field
739
'source_project_id': source_project['id'],
740
'target_project_id': target_project['id'],
741
'source_branch_name': self.source_branch_name,
742
'target_branch_name': self.target_branch_name,
743
'description': description,
744
'allow_collaboration': allow_collaboration}
746
kwargs['labels'] = ','.join(labels)
748
kwargs['assignee_ids'] = []
749
for reviewer in reviewers:
751
user = self.gl._get_user_by_email(reviewer)
753
user = self.gl._get_user(reviewer)
754
kwargs['assignee_ids'].append(user['id'])
756
merge_request = self.gl._create_mergerequest(**kwargs)
757
except MergeRequestConflict as e:
759
r'Another open merge request already exists for '
760
r'this source branch: \!([0-9]+)',
761
e.reason['message'][0])
763
merge_id = int(m.group(1))
764
mr = self.gl._get_merge_request(
765
target_project['path_with_namespace'], merge_id)
766
raise MergeProposalExists(
767
self.source_branch.user_url, GitLabMergeProposal(self.gl, mr))
768
raise Exception('conflict: %r' % e.reason)
769
except GitLabUnprocessable as e:
771
"Source project is not a fork of the target project"]:
772
raise SourceNotDerivedFromTarget(
773
self.source_branch, self.target_branch)
774
return GitLabMergeProposal(self.gl, merge_request)
777
def register_gitlab_instance(shortname, url):
778
"""Register a gitlab instance.
780
:param shortname: Short name (e.g. "gitlab")
781
:param url: URL to the gitlab instance
783
from breezy.bugtracker import (
785
ProjectIntegerBugTracker,
787
tracker_registry.register(
788
shortname, ProjectIntegerBugTracker(
789
shortname, url + '/{project}/issues/{id}'))