1
# Copyright (C) 2010, 2011 Canonical Ltd
2
# Copyright (C) 2018 Breezy Developers
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Support for Launchpad."""
20
from __future__ import absolute_import
26
from ...propose import (
36
branch as _mod_branch,
42
from ...git.urls import git_url_to_bzr_url
43
from ...lazy_import import lazy_import
44
lazy_import(globals(), """
45
from breezy.plugins.launchpad import (
50
from launchpadlib import uris
52
from ...transport import get_transport
55
# TODO(jelmer): Make selection of launchpad staging a configuration option.
57
def status_to_lp_mp_statuses(status):
59
if status in ('open', 'all'):
64
'Code failed to merge',
66
if status in ('closed', 'all'):
67
statuses.extend(['Rejected', 'Superseded'])
68
if status in ('merged', 'all'):
69
statuses.append('Merged')
73
def plausible_launchpad_url(url):
76
if url.startswith('lp:'):
78
regex = re.compile(r'([a-z]*\+)*(bzr\+ssh|http|ssh|git|https)'
79
r'://(bazaar|git).*\.launchpad\.net')
80
return bool(regex.match(url))
83
class WebserviceFailure(Exception):
85
def __init__(self, message):
86
self.message = message
89
def _call_webservice(call, *args, **kwargs):
90
"""Make a call to the webservice, wrapping failures.
92
:param call: The call to make.
93
:param *args: *args for the call.
94
:param **kwargs: **kwargs for the call.
95
:return: The result of calling call(*args, *kwargs).
97
from lazr.restfulclient import errors as restful_errors
99
return call(*args, **kwargs)
100
except restful_errors.HTTPError as e:
102
for line in e.content.splitlines():
103
if line.startswith(b'Traceback (most recent call last):'):
105
error_lines.append(line)
106
raise WebserviceFailure(b''.join(error_lines))
109
class LaunchpadMergeProposal(MergeProposal):
111
def __init__(self, mp):
114
def get_source_branch_url(self):
115
if self._mp.source_branch:
116
return self._mp.source_branch.bzr_identity
118
return git_url_to_bzr_url(
119
self._mp.source_git_repository.git_identity,
120
ref=self._mp.source_git_path.encode('utf-8'))
122
def get_source_revision(self):
123
if self._mp.source_branch:
124
last_scanned_id = self._mp.source_branch.last_scanned_id
126
return last_scanned_id.encode('utf-8')
130
from breezy.git.mapping import default_mapping
131
git_repo = self._mp.source_git_repository
132
git_ref = git_repo.getRefByPath(path=self._mp.source_git_path)
133
sha = git_ref.commit_sha1
136
return default_mapping.revision_id_foreign_to_bzr(
139
def get_target_branch_url(self):
140
if self._mp.target_branch:
141
return self._mp.target_branch.bzr_identity
143
return git_url_to_bzr_url(
144
self._mp.target_git_repository.git_identity,
145
ref=self._mp.target_git_path.encode('utf-8'))
149
return lp_api.canonical_url(self._mp)
152
return (self._mp.queue_status == 'Merged')
155
return (self._mp.queue_status in ('Rejected', 'Superseded'))
158
self._mp.setStatus(status='Needs review')
160
def get_description(self):
161
return self._mp.description
163
def set_description(self, description):
164
self._mp.description = description
167
def get_commit_message(self):
168
return self._mp.commit_message
170
def set_commit_message(self, commit_message):
171
self._mp.commit_message = commit_message
175
self._mp.setStatus(status='Rejected')
177
def can_be_merged(self):
178
if not self._mp.preview_diff:
181
return not bool(self._mp.preview_diff.conflicts)
183
def get_merged_by(self):
184
merge_reporter = self._mp.merge_reporter
185
if merge_reporter is None:
187
return merge_reporter.name
189
def get_merged_at(self):
190
return self._mp.date_merged
192
def merge(self, commit_message=None):
193
target_branch = _mod_branch.Branch.open(
194
self.get_target_branch_url())
195
source_branch = _mod_branch.Branch.open(
196
self.get_source_branch_url())
197
# TODO(jelmer): Ideally this would use a memorytree, but merge doesn't
199
# tree = target_branch.create_memorytree()
200
tmpdir = tempfile.mkdtemp()
202
tree = target_branch.create_checkout(
203
to_location=tmpdir, lightweight=True)
204
tree.merge_from_branch(source_branch)
205
tree.commit(commit_message or self._mp.commit_message)
207
shutil.rmtree(tmpdir)
209
def post_comment(self, body):
210
self._mp.createComment(content=body)
213
class Launchpad(Hoster):
214
"""The Launchpad hosting service."""
218
# https://bugs.launchpad.net/launchpad/+bug/397676
219
supports_merge_proposal_labels = False
221
supports_merge_proposal_commit_message = True
223
supports_allow_collaboration = False
225
merge_proposal_description_format = 'plain'
227
def __init__(self, service_root):
228
self._api_base_url = service_root
229
self._launchpad = None
233
if self._api_base_url == uris.LPNET_SERVICE_ROOT:
235
return 'Launchpad at %s' % self.base_url
239
if self._launchpad is None:
240
self._launchpad = lp_api.connect_launchpad(self._api_base_url, version='devel')
241
return self._launchpad
245
return lp_api.uris.web_root_for_service_root(self._api_base_url)
248
return "Launchpad(service_root=%s)" % self._api_base_url
250
def get_current_user(self):
251
return self.launchpad.me.name
253
def get_user_url(self, username):
254
return self.launchpad.people[username].web_link
256
def hosts(self, branch):
257
# TODO(jelmer): staging vs non-staging?
258
return plausible_launchpad_url(branch.user_url)
261
def probe_from_url(cls, url, possible_transports=None):
262
if plausible_launchpad_url(url):
263
return Launchpad(uris.LPNET_SERVICE_ROOT)
264
raise UnsupportedHoster(url)
266
def _get_lp_git_ref_from_branch(self, branch):
267
url, params = urlutils.split_segment_parameters(branch.user_url)
268
(scheme, user, password, host, port, path) = urlutils.parse_url(
270
repo_lp = self.launchpad.git_repositories.getByPath(
271
path=path.strip('/'))
273
ref_path = params['ref']
275
branch_name = params.get('branch', branch.name)
277
ref_path = 'refs/heads/%s' % branch_name
279
ref_path = repo_lp.default_branch
280
ref_lp = repo_lp.getRefByPath(path=ref_path)
281
return (repo_lp, ref_lp)
283
def _get_lp_bzr_branch_from_branch(self, branch):
284
return self.launchpad.branches.getByUrl(
285
url=urlutils.unescape(branch.user_url))
287
def _get_derived_git_path(self, base_path, owner, project):
288
base_repo = self.launchpad.git_repositories.getByPath(path=base_path)
290
project = urlutils.parse_url(base_repo.git_ssh_url)[-1].strip('/')
291
if project.startswith('~'):
292
project = '/'.join(base_path.split('/')[1:])
293
# TODO(jelmer): Surely there is a better way of creating one of these
295
return "~%s/%s" % (owner, project)
297
def _publish_git(self, local_branch, base_path, name, owner, project=None,
298
revision_id=None, overwrite=False, allow_lossy=True,
300
to_path = self._get_derived_git_path(base_path, owner, project)
301
to_transport = get_transport("git+ssh://git.launchpad.net/" + to_path)
303
dir_to = controldir.ControlDir.open_from_transport(to_transport)
304
except errors.NotBranchError:
305
# Didn't find anything
310
br_to = local_branch.create_clone_on_transport(
311
to_transport, revision_id=revision_id, name=name,
312
tag_selector=tag_selector)
313
except errors.NoRoundtrippingSupport:
314
br_to = local_branch.create_clone_on_transport(
315
to_transport, revision_id=revision_id, name=name,
316
lossy=True, tag_selector=tag_selector)
319
dir_to = dir_to.push_branch(
320
local_branch, revision_id, overwrite=overwrite, name=name,
321
tag_selector=tag_selector)
322
except errors.NoRoundtrippingSupport:
325
dir_to = dir_to.push_branch(
326
local_branch, revision_id, overwrite=overwrite, name=name,
327
lossy=True, tag_selector=tag_selector)
328
br_to = dir_to.target_branch
330
"https://git.launchpad.net/%s/+ref/%s" % (to_path, name))
332
def _get_derived_bzr_path(self, base_branch, name, owner, project):
334
base_branch_lp = self._get_lp_bzr_branch_from_branch(base_branch)
335
project = '/'.join(base_branch_lp.unique_name.split('/')[1:-1])
336
# TODO(jelmer): Surely there is a better way of creating one of these
338
return "~%s/%s/%s" % (owner, project, name)
340
def get_push_url(self, branch):
341
(vcs, user, password, path, params) = self._split_url(branch.user_url)
343
branch_lp = self._get_lp_bzr_branch_from_branch(branch)
344
return branch_lp.bzr_identity
346
return urlutils.join_segment_parameters(
347
"git+ssh://git.launchpad.net/" + path, params)
351
def _publish_bzr(self, local_branch, base_branch, name, owner,
352
project=None, revision_id=None, overwrite=False,
353
allow_lossy=True, tag_selector=None):
354
to_path = self._get_derived_bzr_path(base_branch, name, owner, project)
355
to_transport = get_transport("lp:" + to_path)
357
dir_to = controldir.ControlDir.open_from_transport(to_transport)
358
except errors.NotBranchError:
359
# Didn't find anything
363
br_to = local_branch.create_clone_on_transport(
364
to_transport, revision_id=revision_id, tag_selector=tag_selector)
366
br_to = dir_to.push_branch(
367
local_branch, revision_id, overwrite=overwrite,
368
tag_selector=tag_selector).target_branch
369
return br_to, ("https://code.launchpad.net/" + to_path)
371
def _split_url(self, url):
372
url, params = urlutils.split_segment_parameters(url)
373
(scheme, user, password, host, port, path) = urlutils.parse_url(url)
374
path = path.strip('/')
375
if host.startswith('bazaar.'):
377
elif host.startswith('git.'):
380
raise ValueError("unknown host %s" % host)
381
return (vcs, user, password, path, params)
383
def publish_derived(self, local_branch, base_branch, name, project=None,
384
owner=None, revision_id=None, overwrite=False,
385
allow_lossy=True, tag_selector=None):
386
"""Publish a branch to the site, derived from base_branch.
388
:param base_branch: branch to derive the new branch from
389
:param new_branch: branch to publish
390
:param name: Name of the new branch on the remote host
391
:param project: Optional project name
392
:param owner: Optional owner
393
:return: resulting branch
396
owner = self.launchpad.me.name
397
(base_vcs, base_user, base_password, base_path,
398
base_params) = self._split_url(base_branch.user_url)
399
# TODO(jelmer): Prevent publishing to development focus
400
if base_vcs == 'bzr':
401
return self._publish_bzr(
402
local_branch, base_branch, name, project=project, owner=owner,
403
revision_id=revision_id, overwrite=overwrite,
404
allow_lossy=allow_lossy, tag_selector=tag_selector)
405
elif base_vcs == 'git':
406
return self._publish_git(
407
local_branch, base_path, name, project=project, owner=owner,
408
revision_id=revision_id, overwrite=overwrite,
409
allow_lossy=allow_lossy, tag_selector=tag_selector)
411
raise AssertionError('not a valid Launchpad URL')
413
def get_derived_branch(self, base_branch, name, project=None, owner=None):
415
owner = self.launchpad.me.name
416
(base_vcs, base_user, base_password, base_path,
417
base_params) = self._split_url(base_branch.user_url)
418
if base_vcs == 'bzr':
419
to_path = self._get_derived_bzr_path(
420
base_branch, name, owner, project)
421
return _mod_branch.Branch.open("lp:" + to_path)
422
elif base_vcs == 'git':
423
to_path = self._get_derived_git_path(
424
base_path.strip('/'), owner, project)
425
to_url = urlutils.join_segment_parameters(
426
"git+ssh://git.launchpad.net/" + to_path,
428
return _mod_branch.Branch.open(to_url)
430
raise AssertionError('not a valid Launchpad URL')
432
def iter_proposals(self, source_branch, target_branch, status='open'):
433
(base_vcs, base_user, base_password, base_path,
434
base_params) = self._split_url(target_branch.user_url)
435
statuses = status_to_lp_mp_statuses(status)
436
if base_vcs == 'bzr':
437
target_branch_lp = self.launchpad.branches.getByUrl(
438
url=target_branch.user_url)
439
source_branch_lp = self.launchpad.branches.getByUrl(
440
url=source_branch.user_url)
441
for mp in target_branch_lp.getMergeProposals(status=statuses):
442
if mp.source_branch_link != source_branch_lp.self_link:
444
yield LaunchpadMergeProposal(mp)
445
elif base_vcs == 'git':
446
(source_repo_lp, source_branch_lp) = (
447
self._get_lp_git_ref_from_branch(source_branch))
448
(target_repo_lp, target_branch_lp) = (
449
self._get_lp_git_ref_from_branch(target_branch))
450
for mp in target_branch_lp.getMergeProposals(status=statuses):
451
if (target_branch_lp.path != mp.target_git_path or
452
target_repo_lp != mp.target_git_repository or
453
source_branch_lp.path != mp.source_git_path or
454
source_repo_lp != mp.source_git_repository):
456
yield LaunchpadMergeProposal(mp)
458
raise AssertionError('not a valid Launchpad URL')
460
def get_proposer(self, source_branch, target_branch):
461
(base_vcs, base_user, base_password, base_path,
462
base_params) = self._split_url(target_branch.user_url)
463
if base_vcs == 'bzr':
464
return LaunchpadBazaarMergeProposalBuilder(
465
self, source_branch, target_branch)
466
elif base_vcs == 'git':
467
return LaunchpadGitMergeProposalBuilder(
468
self, source_branch, target_branch)
470
raise AssertionError('not a valid Launchpad URL')
473
def iter_instances(cls):
474
credential_store = lp_api.get_credential_store()
475
for service_root in set(uris.service_roots.values()):
476
auth_engine = lp_api.get_auth_engine(service_root)
477
creds = credential_store.load(auth_engine.unique_consumer_id)
478
if creds is not None:
479
yield cls(service_root)
481
def iter_my_proposals(self, status='open'):
482
statuses = status_to_lp_mp_statuses(status)
483
for mp in self.launchpad.me.getMergeProposals(status=statuses):
484
yield LaunchpadMergeProposal(mp)
486
def iter_my_forks(self):
487
# Launchpad doesn't really have the concept of "forks"
490
def get_proposal_by_url(self, url):
491
# Launchpad doesn't have a way to find a merge proposal by URL.
492
(scheme, user, password, host, port, path) = urlutils.parse_url(
494
LAUNCHPAD_CODE_DOMAINS = [
495
('code.%s' % domain) for domain in lp_uris.LAUNCHPAD_DOMAINS.values()]
496
if host not in LAUNCHPAD_CODE_DOMAINS:
497
raise UnsupportedHoster(url)
498
# TODO(jelmer): Check if this is a launchpad URL. Otherwise, raise
500
# See https://api.launchpad.net/devel/#branch_merge_proposal
502
# https://api.launchpad.net/devel/~<author.name>/<project.name>/<branch.name>/+merge/<id>
503
api_url = str(self.launchpad._root_uri) + path
504
mp = self.launchpad.load(api_url)
505
return LaunchpadMergeProposal(mp)
508
class LaunchpadBazaarMergeProposalBuilder(MergeProposalBuilder):
510
def __init__(self, lp_host, source_branch, target_branch,
511
staging=None, approve=None, fixes=None):
514
:param source_branch: The branch to propose for merging.
515
:param target_branch: The branch to merge into.
516
:param staging: If True, propose the merge against staging instead of
518
:param approve: If True, mark the new proposal as approved immediately.
519
This is useful when a project permits some things to be approved
520
by the submitter (e.g. merges between release and deployment
523
self.lp_host = lp_host
524
self.launchpad = lp_host.launchpad
525
self.source_branch = source_branch
526
self.source_branch_lp = self.launchpad.branches.getByUrl(
527
url=source_branch.user_url)
528
if target_branch is None:
529
self.target_branch_lp = self.source_branch_lp.get_target()
530
self.target_branch = _mod_branch.Branch.open(
531
self.target_branch_lp.bzr_identity)
533
self.target_branch = target_branch
534
self.target_branch_lp = self.launchpad.branches.getByUrl(
535
url=target_branch.user_url)
536
self.approve = approve
539
def get_infotext(self):
540
"""Determine the initial comment for the merge proposal."""
541
info = ["Source: %s\n" % self.source_branch_lp.bzr_identity]
542
info.append("Target: %s\n" % self.target_branch_lp.bzr_identity)
545
def get_initial_body(self):
546
"""Get a body for the proposal for the user to modify.
548
:return: a str or None.
550
if not self.hooks['merge_proposal_body']:
553
def list_modified_files():
554
lca_tree = self.source_branch_lp.find_lca_tree(
555
self.target_branch_lp)
556
source_tree = self.source_branch.basis_tree()
557
files = modified_files(lca_tree, source_tree)
559
with self.target_branch.lock_read(), \
560
self.source_branch.lock_read():
562
for hook in self.hooks['merge_proposal_body']:
564
'target_branch': self.target_branch_lp.bzr_identity,
565
'modified_files_callback': list_modified_files,
570
def check_proposal(self):
571
"""Check that the submission is sensible."""
572
if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
573
raise errors.CommandError(
574
'Source and target branches must be different.')
575
for mp in self.source_branch_lp.landing_targets:
576
if mp.queue_status in ('Merged', 'Rejected'):
578
if mp.target_branch.self_link == self.target_branch_lp.self_link:
579
raise MergeProposalExists(lp_api.canonical_url(mp))
581
def approve_proposal(self, mp):
582
with self.source_branch.lock_read():
586
subject='', # Use the default subject.
587
content=u"Rubberstamp! Proposer approves of own proposal.")
588
_call_webservice(mp.setStatus, status=u'Approved',
589
revid=self.source_branch.last_revision())
591
def create_proposal(self, description, reviewers=None, labels=None,
592
prerequisite_branch=None, commit_message=None,
593
work_in_progress=False, allow_collaboration=False):
594
"""Perform the submission."""
596
raise LabelsUnsupported(self)
597
if prerequisite_branch is not None:
598
prereq = self.launchpad.branches.getByUrl(
599
url=prerequisite_branch.user_url)
602
if reviewers is None:
606
for reviewer in reviewers:
608
reviewer_obj = self.launchpad.people.getByEmail(email=reviewer)
610
reviewer_obj = self.launchpad.people[reviewer]
611
reviewer_objs.append(reviewer_obj)
613
mp = _call_webservice(
614
self.source_branch_lp.createMergeProposal,
615
target_branch=self.target_branch_lp,
616
prerequisite_branch=prereq,
617
initial_comment=description.strip(),
618
commit_message=commit_message,
619
needs_review=(not work_in_progress),
620
reviewers=[reviewer.self_link for reviewer in reviewer_objs],
621
review_types=['' for reviewer in reviewer_objs])
622
except WebserviceFailure as e:
624
if (b'There is already a branch merge proposal '
625
b'registered for branch ') in e.message:
626
raise MergeProposalExists(self.source_branch.user_url)
630
self.approve_proposal(mp)
632
if self.fixes.startswith('lp:'):
633
self.fixes = self.fixes[3:]
636
bug=self.launchpad.bugs[int(self.fixes)])
637
return LaunchpadMergeProposal(mp)
640
class LaunchpadGitMergeProposalBuilder(MergeProposalBuilder):
642
def __init__(self, lp_host, source_branch, target_branch,
643
staging=None, approve=None, fixes=None):
646
:param source_branch: The branch to propose for merging.
647
:param target_branch: The branch to merge into.
648
:param staging: If True, propose the merge against staging instead of
650
:param approve: If True, mark the new proposal as approved immediately.
651
This is useful when a project permits some things to be approved
652
by the submitter (e.g. merges between release and deployment
655
self.lp_host = lp_host
656
self.launchpad = lp_host.launchpad
657
self.source_branch = source_branch
658
(self.source_repo_lp,
659
self.source_branch_lp) = self.lp_host._get_lp_git_ref_from_branch(
661
if target_branch is None:
662
self.target_branch_lp = self.source_branch.get_target()
663
self.target_branch = _mod_branch.Branch.open(
664
self.target_branch_lp.git_https_url)
666
self.target_branch = target_branch
667
(self.target_repo_lp, self.target_branch_lp) = (
668
self.lp_host._get_lp_git_ref_from_branch(target_branch))
669
self.approve = approve
672
def get_infotext(self):
673
"""Determine the initial comment for the merge proposal."""
674
info = ["Source: %s\n" % self.source_branch.user_url]
675
info.append("Target: %s\n" % self.target_branch.user_url)
678
def get_initial_body(self):
679
"""Get a body for the proposal for the user to modify.
681
:return: a str or None.
683
if not self.hooks['merge_proposal_body']:
686
def list_modified_files():
687
lca_tree = self.source_branch_lp.find_lca_tree(
688
self.target_branch_lp)
689
source_tree = self.source_branch.basis_tree()
690
files = modified_files(lca_tree, source_tree)
692
with self.target_branch.lock_read(), \
693
self.source_branch.lock_read():
695
for hook in self.hooks['merge_proposal_body']:
697
'target_branch': self.target_branch,
698
'modified_files_callback': list_modified_files,
703
def check_proposal(self):
704
"""Check that the submission is sensible."""
705
if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
706
raise errors.CommandError(
707
'Source and target branches must be different.')
708
for mp in self.source_branch_lp.landing_targets:
709
if mp.queue_status in ('Merged', 'Rejected'):
711
if mp.target_branch.self_link == self.target_branch_lp.self_link:
712
raise MergeProposalExists(lp_api.canonical_url(mp))
714
def approve_proposal(self, mp):
715
with self.source_branch.lock_read():
719
subject='', # Use the default subject.
720
content=u"Rubberstamp! Proposer approves of own proposal.")
722
mp.setStatus, status=u'Approved',
723
revid=self.source_branch.last_revision())
725
def create_proposal(self, description, reviewers=None, labels=None,
726
prerequisite_branch=None, commit_message=None,
727
work_in_progress=False, allow_collaboration=False):
728
"""Perform the submission."""
730
raise LabelsUnsupported(self)
731
if prerequisite_branch is not None:
732
(prereq_repo_lp, prereq_branch_lp) = (
733
self.lp_host._get_lp_git_ref_from_branch(prerequisite_branch))
735
prereq_branch_lp = None
736
if reviewers is None:
739
mp = _call_webservice(
740
self.source_branch_lp.createMergeProposal,
741
merge_target=self.target_branch_lp,
742
merge_prerequisite=prereq_branch_lp,
743
initial_comment=description.strip(),
744
commit_message=commit_message,
745
needs_review=(not work_in_progress),
746
reviewers=[self.launchpad.people[reviewer].self_link
747
for reviewer in reviewers],
748
review_types=[None for reviewer in reviewers])
749
except WebserviceFailure as e:
751
if ('There is already a branch merge proposal '
752
'registered for branch ') in e.message:
753
raise MergeProposalExists(self.source_branch.user_url)
756
self.approve_proposal(mp)
758
if self.fixes.startswith('lp:'):
759
self.fixes = self.fixes[3:]
762
bug=self.launchpad.bugs[int(self.fixes)])
763
return LaunchpadMergeProposal(mp)
766
def modified_files(old_tree, new_tree):
767
"""Return a list of paths in the new tree with modified contents."""
768
for change in new_tree.iter_changes(old_tree):
769
if change.changed_content and change.kind[1] == 'file':